Menu

Your subprocess call is a shell injection waiting to happen
πŸ“°

Your subprocess call is a shell injection waiting to happen

DEV Community: securityΒ·Aurum RevenueΒ·15 days ago
#LZwsobQv
#devtosecurity#devto#dev#shell#echo
Long article (444 words) β€” no AI summary yet.
Reading 0:00
15s threshold

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.…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.