Shell Commands and Argument Injection
Follow a saved caption into a worker and compare a shell command string with distinct arguments to a fixed executable.
You’re reading a preview. Premium includes the full lesson and specialist practice. View the full curriculum.
A user saves a caption for an image preview. A worker later loads that caption and builds a command. The request handler only saw text; the worker may give that text to a shell.
Compare these two process shapes:
Shell: /bin/sh, -c, "a completed command string"
Direct: a fixed executable, argument 1, argument 2, the caption value
A shell reads its command string as instructions. A direct argument still needs the called tool's own option rules, but it does not become shell source merely because it contains a quote.
We'll follow the caption from the request through the queue to the actual process call, then separate shell injection from tool-specific argument injection.
Find the actual interpreter
Search process creation calls, wrappers, worker scripts, shell commands, dynamic execution functions, and tool-specific argument construction. Follow values across the queue boundary. A request handler may appear harmless because the dangerous call runs later in another language or process.
// Unsafe original preview-worker outline.
String caption = job.getCaption();
String command = "/usr/local/bin/proof-image --caption "" + caption +
"" --input " + job.getServerInputPath();
Process process = new ProcessBuilder("/bin/sh", "-c", command).start();
The array separates the shell program's arguments. One of those arguments is a complete program for the shell to parse. The caption is still inside that program. Double quotes do not suppress every shell expansion; command substitution can remain active. Single quotes have different rules and become fragile when the supplied value contains their delimiter. Do not build a universal shell sanitizer from a few replacements.
Select each file boundary and retain the original writer.
Controller. Type and length checks preserve a string, not shell safety.
job.setCaption(body.caption().trim());
Queue. The worker reloads the same externally controlled caption.
String caption = jobs.load(jobId).getCaption();
Execute. The explicit shell parses the completed string at start().
new ProcessBuilder("/bin/sh", "-c", command).start();
Review question: The decisive sink is the interpreter that runs, not the outer container used to launch it.