Summary
binary-parser has a code injection vulnerability when parser definitions contain attacker-controlled string options that are used during parser compilation. If an attacker can influence those definitions, crafted values may cause arbitrary JavaScript to execute in the Node.js process.
Affected context
The issue affects binary-parser versions before 2.3.1 when parser definitions are compiled using string-typed options that are passed through Context.generateOption() without adequate validation. Relevant options include length, lengthInBytes, key, tag, offset, and aliases created with namely(). Risk depends on whether an untrusted party can supply or modify parser definitions.
Exploit preconditions
- Attack surface: Applications that use binary-parser to compile parser definitions.
- Required access: The ability to control or influence one or more string-typed parser option values; the description does not specify a particular access level.
- Trigger point: Compilation of a parser containing an attacker-controlled option or alias.
- Why exploitation is possible: Option values are passed into code-generation logic without sufficient validation, allowing injected JavaScript to become executable code.
Why this happens
The underlying issue is improper neutralization of input used in dynamically generated code. Context.generateOption() handles string-typed values without adequately restricting how they affect generated JavaScript. This maps to CWE-94: Improper Control of Generation of Code ('Code Injection').
Potential impact
- An attacker may execute arbitrary JavaScript in the Node.js process.
- The process could be used to access data or resources available to the application.
- Application integrity or availability could be affected.
- The consequences could extend to connected systems, depending on the process's permissions and environment.
General mitigation guidance
Defensive approaches focus on preventing untrusted values from becoming executable code:
- Prefer parser construction patterns that do not generate code from input strings.
- Treat parser definitions and aliases from external or user-controlled sources as untrusted.
- Validate option types and values against strict schemas and allowlists before compilation.
- Where dynamic generation is necessary, use safe structured representations and context-appropriate escaping rather than direct string composition.
- Run the Node.js process with only the permissions and access it needs.
How teams detect issues like this
- Use static analysis to identify dynamic code generation and string interpolation involving parser options.
- Review paths from external inputs or configuration into
Context.generateOption()and parser compilation. - Check for insufficient validation of
length,lengthInBytes,key,tag,offset, andnamely()alias values. - Apply code-review and CI policies that restrict unsafe dynamic code generation and require validation of parser definitions.

