Can a filename run commands on your server? Closing command injection
OS command injection happens when your code builds a shell command from user input, so a character like ; adds a second command. OWASP says the first fix is to not call OS commands at all and use your language's library.

A page uploads an image and shrinks it with one command on the server. Everything works until someone names their file photo.jpg; cat /etc/passwd. In this article you will see why that name runs a second command, and how to write the code so it can't.
What is OS command injection?
OS command injection is a flaw that happens when your code builds a system command from outside input and doesn't neutralize the special characters in it. Those characters change the command you meant to run.
According to the OWASP cheat sheet on OS command injection defense, characters such as &, |, ;, $, >, < and the backtick all mean something to the shell, and the second command comes in through them.
Why does a filename become a command?
A filename becomes a command because the shell reads the whole text as one command, and it can't tell where your code ends and the user's input begins.
The mistake is simple and human: we glue the value into the command text because it's the quickest way, and we think of it as "just a filename". But when the shell sees ;, it ends the first command and runs the next one with your application's privileges.
// Wrong: the value is glued into the command text
shell_exec("convert " . $_GET['file'] . " thumb.png");How do you close it?
You close it by keeping the command apart from the input. OWASP gives three options in order, and the first is the best one: don't call OS commands at all.
Use your language's library instead of the command. The cheat sheet says built-in library functions are a very good alternative to OS commands, and if a library or API exists for the job in your language, that is the preferred method. To resize an image, for example, look for an image library in your language instead of the
convertcommand.If you must run the command, escape the value for the OS. In PHP the cheat sheet prefers
escapeshellarg()overescapeshellcmd()for user input, because it wraps the value in single quotes, so only one argument reaches the command.Separate the command from its arguments, and validate both. The cheat sheet asks for a mechanism that separates data from the command by itself, like Java's
ProcessBuilderwhere the command is kept apart from its arguments, then a check of the command against a list of allowed commands, and a check of the arguments against an allowlist or a regex that only accepts the right shape.
// Better: one argument, and its shape is checked first
if (!preg_match('/^[a-z0-9_-]+\.jpg$/', $name)) { exit; }
shell_exec('convert ' . escapeshellarg($name) . ' thumb.png');The same split between code and data is what protects you from SQL injection with parameterized queries, and it's close to checking file paths when the user picks the filename.
What limits should you know?
Escaping alone isn't enough, because an attacker can still send one argument that changes what the command does. The cheat sheet says it plainly: even with escapeshellarg(), an attacker can still pass a single argument to the command.
So a value like --help, or another option of the tool, can get through the escaping. That's why the third option pairs the split with an allowlist, and the cheat sheet calls the first option, no OS commands, the primary defense.
Where do you start now?
The lesson for us as developers: an OS command is text that another program reads, so any value glued into it becomes part of the command. Search your code today for exec, system, shell_exec and their relatives, and replace each one with a library, or separate its value and check it.
If your app accepts uploads, also review file upload validation. And if you want someone to look for this kind of flaw in your platform before anyone else does, there's Muhassan. Where in your code does a value from the user reach an OS command?
FAQ
If I escape the input, is the problem solved?
Not all of it. OWASP says escapeshellarg prevents command injection, but an attacker can still pass a single argument to the command, such as an option that changes what it does. So check the value against an allowlist too.
If the input only comes from admins, should I care?
Yes. An admin account can be stolen, and the value can arrive from a file or another API. Treat any value you did not type yourself as outside input.
How do I find this in existing code?
Search the code for every place that calls a shell or an OS command, such as exec, system and shell_exec, and check whether a value from the request or the database is glued into the command text.


