Skip to content
Esc

noctcore-security/no-shell-interpolation

A dynamically-built command string must not flow into a shell runner. Pass the program and its arguments separately.

A dynamically-interpolated command string handed to a shell is the classic command-injection sink:

Incorrect
// a crafted `branch` runs arbitrary commands
exec(`git checkout ${branch}`);

If branch is main; rm -rf /, the shell happily runs both. The fix is to pass the program and its arguments separately, so the OS never re-parses a shell string:

Correct
// no shell: arguments cannot inject
execFile('git', ['checkout', branch]);

Precision is the whole point — this rule fires only on the genuinely dangerous shape:

  • exec / execSync (always run a shell): a first argument that is a template literal with expressions, or a + concatenation with a dynamic part.
  • spawn / spawnSync / execFile / execFileSync: the same dynamic first argument, but only when an options object literal passes a shell value other than false: true, a shell path string, or any value decided at run time.
Incorrect
// shell:true re-parses the interpolated command
spawn(`cmd ${x}`, { shell: true });

A shell chosen at run time is a shell on some machine. This call is an injection sink on Windows only, which is exactly where a macOS developer never sees it fail:

Incorrect
spawn(`git ${branch}`, { shell: process.platform === 'win32' });

A binding made with promisify from a child_process runner is followed through scope and checked like the runner it wraps (util.promisify(cp.exec) too):

Incorrect
import { exec } from 'node:child_process';
import { promisify } from 'node:util';
const execAsync = promisify(exec);
export async function checkout(branch: string) {
await execAsync(`git checkout ${branch}`);
}
Correct
import { execFile } from 'node:child_process';
import { promisify } from 'node:util';
const execFileAsync = promisify(execFile);
export async function checkout(branch: string) {
await execFileAsync('git', ['checkout', branch]);
}

There is no autofix: the safe rewrite changes the call shape (string → program + args array).

The array-args, no-shell forms are safe and left alone, even with a dynamic argument (and with a statically off shell: false, null, undefined or ''):

Correct
// left alone: no shell
spawn('ls', [dir]);
execFile('git', ['log', branch]);

exec called as a member on a non-child_process object — notably regex.exec(...) — is excluded so RegExp.prototype.exec never false-positives. The child_process member form is recognised via the conventional object names cp / childProcess / child_process, or the bare imported exec(...).

An options object passed by name (spawn(cmd, options)) is not followed, so its shell value is not seen. A runner wrapped some other way than promisify (a hand-written new Promise around exec) is not followed either; name the wrapper in extraCallees.

A fully static command is not flagged either: exec('ls -la'), a template literal with no expressions, or a + of literals only ('ls ' + '-la').

Option Type Default Meaning
extraCallees string[] [] Extra callee names to treat as always-shell command runners (checked like exec).
'noctcore-security/no-shell-interpolation': ['error', { extraCallees: ['runShell', 'sh'] }]

If a call site is provably safe (a hard-coded allowlist of commands, no user input in the string), refactor to execFile anyway — it is clearer — or disable the rule for that line.