Blog

Python scripts: now checked

Shaun Hardneck · · 3 min read

Dark ScriptPreflight cover showing the top lines of a report: result, reach and undo.

ScriptPreflight now checks Python scripts. Paste or upload a .py file, or choose Python in the language menu, and you get the same report as for a PowerShell or Bash script: what the script changes, how far it reaches, whether it can be undone, and every finding with its rule ID and line.

Python is where a lot of admin automation lives: a cleanup job that clears old build folders, a Boto3 script that tidies up instances, a Kubernetes helper that deletes a namespace, a one-off fix that runs a DELETE against a database. These scripts are often written fast, run from a laptop or a cron job, and reviewed by nobody.

What is checked

The checks are for admin, automation, deployment and maintenance scripts, grouped by what the script does:

  • Files: shutil.rmtree, os.remove and pathlib deletes, overwrites with mode "w", moves and copies onto an existing file, permission and ownership changes, rated by what the path is and whether it is only known at run time
  • Commands: text handed to subprocess, os.system or Paramiko is checked by the Bash rules, and the finding points at the Python line
  • Cloud SDKs: Boto3, Azure SDK and Google Cloud deletes and terminations, bucket and blob deletes, IAM and role changes, and network rules opened to everyone
  • Kubernetes and Docker: namespace, workload and volume deletes, cluster role bindings and prunes
  • Databases: DROP, TRUNCATE, and DELETE or UPDATE with no WHERE through DB-API drivers, SQL built from strings, ORM bulk deletes and commit placement
  • HTTP changes, eval and exec, unsafe deserialization, secrets written in the script, and TLS verification turned off

A delete inside a loop over a listing or a list call is rated as a change to every object of that kind. A path built from sys.argv or an environment variable is rated at its worst plausible value, never assumed harmless.

How we decided it was ready

A new language is switched on only after it passes a sealed evaluation: scripts written separately from the rules, kept closed while the rules were built, and scored once. Python took several rounds and more than one sealed set: the earlier sets that did not pass stay on record. The set that passed held 50 dangerous scripts, 50 benign scripts and 6 files that should be refused:

  • 0 dangerous scripts called clean
  • Critical and High precision 97.8%, recall 93.6%
  • a useful result on 82.0% of the benign scripts
  • all 6 out-of-scope files refused

Three dangerous scripts came back below High. None of them was called clean. One benign script was rated High or above, and nine benign scripts were reported as not assessed rather than given a result.

The coverage page lists all 36 categories, the parser and what a report cannot see.

What it does not do

ScriptPreflight reads your script as text. It never runs, imports or compiles it, never runs pip, and never connects to your hosts, cloud account, cluster or database. It can't know how many objects a call will touch or what permissions the script will run with, and the report says so.

Python decides a lot at run time. Anything a script changes that no rule covers, such as a file written to a path known only at run time, an HTTP call other than GET, or code run through eval, exec or a dynamic import, is reported as not assessed and lowers coverage to Partial. It is never reported as clean.

A clean report means no known issue in what we could check, not a promise that nothing can go wrong. Keep your review, your test environment and your change window.

Try it on the next Python script you're about to run. The free trial includes three preflights, no card required.