Security rules
NGB078: OS command injection
Traces untrusted data (Express / Node request query, params, body, cookies and headers, postMessage data, URLs) into Node.js child_process calls that run a shell: the command string of exec() / execSync(), and the program or arguments of execFile() / spawn() (and their sync variants) when shell: true is set.
| Property | Value |
|---|---|
| Rule ID | NGB078 |
| Category | Angular SSR security |
| Default severity | critical |
| CWE | CWE-78, CWE-88 |
| Built-in presets | recommended: error, strict: error |
Confidence is reported per finding (high, medium, low) and is independent of severity. See Reading findings.
What it checks
Traces untrusted data (Express / Node request query, params, body, cookies and headers, postMessage data, URLs) into Node.js child_process calls that run a shell: the command string of exec() / execSync(), and the program or arguments of execFile() / spawn() (and their sync variants) when shell: true is set. Also reports a program name chosen by untrusted data for spawn() / execFile() without a shell, and shelljs exec().
Why it matters
SSR servers, API routes and build helpers sometimes shell out to tools (image converters, PDF renderers, git). When request data becomes part of a shell command line, ;, &&, $(…) or backticks run arbitrary commands with the server's privileges — remote code execution on the machine that holds your secrets and session keys.
Detection behavior and supported patterns
exec(cmd)/execSync(cmd)fromchild_process/node:child_process(named import, namespace / default import,require()), andshell.exec(cmd)fromshelljs, wherecmdcontains traced untrusted data (critical)execFile(file, args, { shell: true }),spawn(cmd, args, { shell: true })and sync variants with untrusted program or arguments (critical)spawn(file, …)/execFile(file, …)without a shell where the program itself is untrusted (high)- Recognized mitigation: values passed through a shell-quoting helper (
quote()from shell-quote,shellEscape(),shescape), numeric conversion
Example
exec(`convert ${req.query.file} out.png`);Safer alternative
execFile('convert', ['--', path.basename(String(req.query.file)), 'out.png']);Remediation
Do not build shell command lines from request data. Use execFile() / spawn() with a fixed program and an argument array (no shell: true), validate each argument against an allowlist or strict pattern, and pass -- before user-supplied operands.
False-positive considerations
- Values validated against a strict allowlist before reaching the command are reported; prefer
execFile()with an argument array so the check is not needed.
Known limitations
- Arguments passed to
spawn()/execFile()without a shell are not reported, although option injection (--upload-pack=…,-o) can still be possible for some programs. - Process helpers other than
child_processandshelljs(execa, zx$) are not modeled. - Allowlist checks are not recognized; validated values are still reported (document an exception).
References
Run it
ngbastion explain NGB078