For months I've been telling people the same thing: stop building shell strings, pass a list.
subprocess.run(["echo", user_text]) with the default shell=False keeps parentheses, backticks and $(...) as plain characters. That is CWE-78, and an argv list really does close it. I even shipped a small library, shellguard, that refuses shell=True and bare-string commands so nobody on my team can slip back into the old habit.
Then a reviewer asked me a fair question: "What if the user's text is a flag?"
I had a good answer in theory and no demo. So I made one.
The demo that changed my answer
I created two files in a temp directory: one literally named -l, one named normal.txt. Then I called ls with the untrusted value as its only argument:
subprocess.run(["ls", untrusted]) # untrusted == "-l" Here is what came back:
total 2 -rw-r--r-- 1 armoo 197609 1 Nov 25 07:00 -l -rw-r--r-- 1 armoo 197609 1 Nov 25 07:00 normal.txt It did not list the file called -l. It switched on ls's long-format flag and listed the whole directory. No shell. No shell=True. Just an argv list, doing exactly what I asked it to.
Then I ran the same call with a separator in front of the value:
subprocess.run(["ls", "--", untrusted]) And got:
-l That is the whole bug in two lines. CWE-78 is about a shell parsing your text. CWE-88 is about the program you are calling parsing your text as an option. An argv list fixes the first. It does nothing about the second.
Why this shows up more than you'd think
Any tool with options has this surface. A filename that starts with - handed to tar. A repo name that begins with -- handed to git clone --upload-pack. A URL passed to curl where the value could be -o or -K. The pattern is always the same: you validated that the value was "a string," but the callee gets to decide what that string means.
Quoting does not help here. There is no shell to quote for. The only honest defenses are structural:
- Put untrusted values after
--when the tool supports it.ls -- <value>andgit ... -- <value>both work. - When a tool has no
--, validate the shape of the value before it reaches the flag position. A filename should match the pattern you actually expect, not "any string." - Never let a value that came from a user occupy a position where the callee expects a flag. Position is trust.
Where the library stops being useful
I want to be precise about what shellguard does, because overclaiming here would be exactly the kind of thing that gets someone hurt. It refuses shell=True, refuses bare-string commands, and passes every element as one execve token. It kills the CWE-78 class outright. Its own README says plainly that it fixes shell injection, not CWE-88 argument injection. The -- rule is still yours to apply.
That distinction is the reason I wrote this up instead of quietly adding a "fix." You cannot patch CWE-88 inside a subprocess wrapper. The wrapper does not know which flags your callee accepts. Only the call site knows that.
The part I actually ship
shellguard is 144 tests deep, zero dependencies, Python 3.8+, MIT source. The commercial license with an offline key is EUR 49 one-time, and there is a pay-what-you-can option from EUR 1 if money is tight. It is not a framework. It is a wrapper you can read in an afternoon.
If the flag trick works on your tool, it is worth ten minutes today. ls -l fooled me. I would rather it fooled the demo than production.
