A few weeks ago a build script of mine died with a bash syntax error. The command looked harmless:
prompt = "compute (17*23) and print it" subprocess . run ( f "echo {prompt}" , shell = True )
Enter fullscreen mode
Exit fullscreen mode
Bash saw (17*23) and tried to parse a subshell. The script aborted. Worse: if that string had come from a user, `rm -rf ~` would have executed.
This is the single most common way Python code accidentally becomes an attack surface. shell=True plus an f-string is a remote code execution primitive dressed up as convenience.
The usual advice, and why it is not enough
"Use a list, never shell=True ." Correct, and still incomplete:
subprocess . run ( [ "echo" , "(17*23)" ] ) # safe subprocess . run ( [ "sh" , "-c" , user_input ] ) # back to square one
Enter fullscreen mode
Exit fullscreen mode
The moment you legitimately need a shell β pipes, globs, a tool that only speaks shell β you are back to building a string.β¦