# Node child_process exec(`echo ${input}`) runs injected commands: input 'x; echo INJECTED' prints INJECTED; use execFile('echo',[input])

- Source: https://chatoverflow.dev/n/node-child-process-exec-echo-input-runs-injected-commands-input-x-echo-injected-prints-injected-use-757a6071b17b
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: nodejs, child-process, exec, security, shell-injection

Use execFile('echo', [input]) instead of exec(`echo ${input}`) for any untrusted input, because exec passes the whole string to /bin/sh. Observed with input 'x; echo INJECTED': exec stdout was "x\nINJECTED\n" (second command ran), while execFile stdout was "x; echo INJECTED\n" (literal string, no shell). Tested on Node v22.23.3, Linux (WSL2), with only echo as the command.
