The import alias that slipped past my shell-injection scanner
A few weeks ago I added a rule to my own static scanner and it immediately flagged three files in my own project. All three were safe. I wrote about that embarrassment already. This is the opposite story: the case my sca
A few weeks ago I added a rule to my own static scanner and it immediately flagged three files in my own project. All three were safe. I wrote about that embarrassment already. This is the opposite story: the case my scanner missed, and why it took a bug shaped like production to find it.
I maintain a small Python library called shellguard. It exists for one reason: I once watched a build script die because a user prompt contained (. The command was built with an f-string and handed to shell=True, and bash tried to parse (17*23) as a subshell. The scary version is the same line with backticks in the prompt โ that is execution of attacker-chosen commands wearing a "build failed" costume.
So the rule in my code is simple: build an argv list, never a shell string. The scanner part, sg audit, walks Python source and flags the dangerous forms โ shell=True, os.system, os.popen, asyncio.create_subprocess_shell, the usual list.
Then someone reported something I had not considered. Their code looked like this:
import subprocess as sp
sp.run(f"echo {user_input}", shell=True)
My scanner matched the literal token subprocess.run. It did not match sp.run. Same bug, different name. The import was aliased and my rule was matching text, not bindings.
The fix is not a bigger regex. It is an AST pass that collects import X as Y and from X import run as r first, then resolves call nodes against those bindings. That handles from subprocess import run as r too, which is the version I actually see most often in small scripts.
There is a second miss that is harder to explain and worse in practice:
sp.run(["sh", "-c", user_text])
That is an argv list. It passes the "are you using shell=True?" check with flying colors. It still hands user_text to a shell parser. I added a rule for that: shell interpreters (sh, bash, dash, zsh, ksh, fish, cmd, powershell, pwsh, absolute paths and .exe variants) invoked with -c or -lc on a non-literal command. A literal ["sh", "-c", "true"] stays quiet โ flagging that would just teach people to ignore the scanner.
Two things I learned from this.
Matching source text is not the same as understanding code. A scanner that greps for subprocess.run is a grep with extra steps. Resolve the binding, or do not claim you found anything.
A clean scan is a statement about what the scanner can see, not about safety. My reports now name the rule catalogue by count and SHA-256, and list what was skipped and why. If a file could not be parsed, you get a skipped line, not silence. Silence is how people get hurt.
The scanner ships inside shellguard, alongside the argv runner and 142 tests that run real subprocesses. The MIT source is free; I sell a commercial license with an offline key for EUR49 because embedding it without attribution is a real business need. The write-up and source are here: https://derbaroser-eng.github.io/shellguard/
If you have found a bug your own security tool missed, I would genuinely like to hear what it was.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes โ full credit and traffic to the original publisher.