Summary
shell-quote's quote() function accepted object tokens with operator fields (.op) that were not validated against the parser's control-operator model, allowing newline characters to pass through unescaped.
Because POSIX shells treat a literal newline as a command separator, attacker-controlled input reaching .op or related shapes could inject additional shell commands, leading to command execution under specific usage patterns.
Affected context
The vulnerability appears in JavaScript/Node.js environments that use the shell-quote library to safely construct shell command strings.
It becomes relevant when applications:
- Use quote() with object tokens constructed from external or semi-trusted input, and
- Rely on parse(cmd, envFn) with envFn returning object tokens whose .op or related fields are influenced by user-controlled values.
In these contexts, developers may assume shell-quote enforces a safe operator model and escapes all dangerous characters, including line terminators, which was not the case.
Exploit preconditions
- Attack surface: Code paths that:
- Call quote() with object tokens like
{ op: ... },{ op: 'glob', pattern }, or{ comment }derived from user or environment input, or - Use parse(cmd, envFn) where envFn returns object tokens including attacker-influenced .op values.
- Required access: Ability to supply or influence data that is transformed into these object tokens, either directly or through envFn logic.
- Trigger point: The vulnerable behavior is triggered when quote() processes an object token whose .op (or related fields) contains a line terminator (e.g.,
\n,\r, U+2028, U+2029), which is passed into a shell command line. - Why exploitation is possible: The per-character backslash escaping used
/(.)/g, which does not match line terminators in JavaScript regexes. As a result, newline characters in .op were not escaped, and POSIX shells interpret literal newlines as command separators, effectively allowing concatenation of a second command segment under attacker influence.
Why this happens
The core bug lies in inconsistent validation between parsing and quoting logic:
- parse() implements a specific operator model and expects operators to come from a restricted set of tokens representing shell control operators.
- quote() assumed that object tokens already adhere to this operator model and attempted to escape .op by backslash-escaping each character via a regex (
/(.)/g).
In JavaScript, /(.)/g does not match line terminators, meaning newline and similar characters bypassed escaping.
Because object tokens could be constructed directly from external input or via envFn in parse(), unvalidated .op values could contain line terminators that were preserved in the resulting shell string.
This maps to:
- CWE-77: Improper Neutralization of Special Elements used in a Command ('Command Injection'), where shell metacharacters (here, newlines acting as command separators) are not properly neutralized.
- CWE-78: Improper Neutralization of Special Elements used in an OS Command, reflecting that attacker-controlled shell fragments can influence OS-level command execution when the resulting string is passed to a shell.
Potential impact
- Application-level shell commands may be extended with attacker-controlled second commands separated by newlines.
- Scripts that invoke system utilities via child_process and shell-quote could run unintended shell pipelines or sequences.
- Environment-dependent data processing (e.g., building commands from env or configuration) can be subverted, resulting in execution of arbitrary shell commands within the privileges of the calling process.
- Logging or debugging helpers that later route constructed commands into a shell may inadvertently enable command injection paths.
General mitigation guidance
Effective mitigation focuses on aligning token validation with the parser's operator model and minimizing trust in complex token shapes:
- Validate object tokens passed to quote() against a strict schema, ensuring .op values are drawn from a known, fixed set of control operators and disallowing line terminators in any operator-like field.
- Treat specialized object tokens such as
{ op: 'glob', pattern }or{ comment }as strongly typed structures, validating both their shape and contents (including forbidding line terminators and other shell control characters where not explicitly required). - Prefer simple string tokens for untrusted input, letting shell-quote or similar libraries escape them as data rather than operators, and avoid allowing untrusted input to decide whether something is an operator, glob, or comment.
- Reduce reliance on shells for command execution when possible by using APIs that pass arguments as arrays (e.g., execFile-like patterns), ensuring the shell is not involved in interpretation of constructed strings.
- Apply least-privilege principles so that processes constructing and executing shell commands run with minimal permissions, limiting the blast radius if misquoting occurs.
How teams detect issues like this
To detect similar issues, organizations can:
- Use static analysis or linters to flag:
- Calls to quote() with object literals or dynamic object tokens derived from untrusted input.
- Custom envFn functions passed to parse() that construct object tokens from request parameters, environment variables, or configuration without explicit validation.
- Incorporate code review checklists that require:
- Verification that any use of operator-like fields (.op) is limited to known constants.
- Inspection of any custom tokenization or wrapping logic mixing untrusted data with shell control operators.
- Configure security testing (SAST/DAST) to:
- Exercise command-building code paths with inputs containing newlines and other control characters, observing whether resulting commands are split into multiple segments.
- Use automated dependency and configuration scanning tools to:
- Highlight modules or internal wrappers that implement custom "shell quoting" logic, prompting focused security review on their handling of line terminators and operators.

