Dev.to Security 🔐 Cybersecurity 👁 0 📖 2 min read

shell=False is not the end of argument injection

shell=False is not the end of argument injection I spent most of a Friday chasing a bug that should not have existed. The call was textbook: subprocess.run(["grep", user_value, logfile], shell=False, check=True)

shell=False is not the end of argument injection

I spent most of a Friday chasing a bug that should not have existed. The call was textbook:

subprocess.run(["grep", user_value, logfile], shell=False, check=True)

No shell string. No os.system. A list, with shell=False. The kind of call you write in a security review and move on from.

It went wrong in two different ways, on two different machines.

The first was option injection. user_value came from a form field. Someone sent a value that began with a dash, and grep read it as a flag instead of a pattern. Nothing executed, there was no shell involved, and every line of the call was "safe." The program simply did something I never intended, because an argument that starts with - is an option, not a value. That is CWE-88, and shell=False does nothing to stop it.

The second only appeared on a Windows machine running Git Bash. Same call, this time with a value like *.log, and grep reported that the file did not exist. It did not: the MSYS compatibility layer had already expanded the wildcard into a list of filenames before grep saw it. On Linux the * reaches the program as a literal character. Under MSYS2 or Git Bash the POSIX layer expands wildcards in argv before handing them to a native Windows program.

Both bugs live in the arguments, not the shell. shell=False fixes command injection. It is not a defense against argument injection.

What I do now

  1. Treat any untrusted value that lands in argv as a possible option. If the program supports --, put it before user values so everything after it is positional.
  2. Strip or escape glob characters (*, ?, [) in values that reach a native program on Windows, or set MSYS2_ARG_CONV_EXCL for that call.
  3. Re-review calls after refactors. The bug above arrived when a hardcoded pattern became a form value. The shape of the call never changed.

I wrote a small scanner, sg audit, that checks a repo for exactly these two cases: a tainted argv token with a provable leading dash (PY-ARGV-001), and a tainted token with provable wildcard characters (PY-MSYS-ARGV-001). It runs on Python and shell, exits nonzero on findings so it can sit in CI, and reports what it could not see instead of claiming it proved safety. It ships inside shellguard, a zero-dependency library I started after the grep incident. There is a commercial licence if you would rather have the signed per-repo audit run for you.

The takeaway is narrower than "use my tool." If a subprocess call takes user input, shell=False is the start of the review, not the end of it. The arguments are still yours to validate.

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.