Summary
math-codegen prior to version 0.4.3 constructs JavaScript functions using user-supplied string literals that are injected directly into a new Function() body.
When untrusted input is passed to cg.parse(), an attacker can break out of the intended expression context and execute arbitrary system commands in applications that expose math evaluation endpoints.
Affected context
This vulnerability affects applications that:
- Use the math-codegen library (pre-0.4.3) to parse mathematical expressions, and
- Pass user-controlled strings into
cg.parse()or equivalent parsing flows, and - Execute the resulting generated function in a context with access to sensitive APIs (e.g., server-side JavaScript with filesystem or OS command access).
It is especially relevant for math or formula evaluation endpoints, calculators, or rule engines that evaluate expressions supplied by remote users.
Exploit preconditions
- Attack surface: Any endpoint or feature that accepts user-provided expressions and passes them to
cg.parse(). - Required access: Ability to submit or influence the expression string given to the parser; no prior authentication may be required if the endpoint is public.
- Trigger point: Invocation of
cg.parse()on untrusted input, followed by execution of the generated function. - Why exploitation is possible: The library injects raw string content directly into a
new Function()body without sanitization, escaping, or restriction, enabling arbitrary JavaScript code execution instead of limiting behavior to mathematical operations.
Why this happens
The core issue is an unsafe code generation pattern where untrusted data is treated as executable code.
Specifically, math-codegen uses new Function() and concatenates or interpolates user-controlled string literals into the function body without any validation or escaping.
This maps to CWE-94: Improper Control of Generation of Code ('Code Injection'), where data intended to represent simple expressions is allowed to introduce arbitrary statements or function calls.
Instead of being constrained to a safe expression grammar, the parser effectively grants users the ability to inject full JavaScript code that runs with the application's privileges.
Potential impact
- Remote execution of arbitrary JavaScript code on the server.
- Execution of arbitrary system commands via accessible OS or process APIs.
- Reading, modifying, or deleting server-side files and environment variables.
- Exfiltration of sensitive configuration data, credentials, or API keys.
- Pivoting to other internal services reachable from the compromised server.
- Complete compromise of the affected application and potentially its hosting environment.
General mitigation guidance
Effective mitigation focuses on removing direct code execution from untrusted input and constraining expression evaluation:
- Use a strict, well-defined expression grammar and parse input into an abstract syntax tree (AST) rather than building code strings.
- Avoid
new Function(),eval(), or equivalent dynamic code construction for handling user expressions; instead, interpret the AST directly or map it to a restricted set of operations. - Apply whitelisting of allowed operators and functions so only numeric and safe math operations are permitted.
- Run any expression evaluation in a restricted execution environment with minimal privileges and no direct access to OS command execution or sensitive APIs.
- Enforce input validation to reject unexpected tokens, control characters, or constructs that extend beyond the supported math syntax.
- Apply least privilege and defense-in-depth controls (e.g., process isolation, containerization, limited filesystem access) so that, even if expression handling is abused, impact is constrained.
How teams detect issues like this
Detection typically combines code-level analysis with runtime monitoring:
- Static analysis: Search for uses of
new Function(),eval(), or similar dynamic evaluation APIs in combination with user-controlled input; flag math-codegen usage where raw strings are passed directly tocg.parse(). - Code review: Examine expression or formula evaluation features for:
- Direct string concatenation into function bodies.
- Lack of tokenization or AST-based parsing.
- Absence of explicit whitelisting of operators and functions.
- Security testing: Perform targeted fuzzing of math or expression endpoints to detect unexpected behavior when injecting non-mathematical tokens or JavaScript syntax.
- Policy enforcement: Establish secure coding guidelines that prohibit dynamic code generation from untrusted input and integrate automated checks into CI pipelines to catch violations early.

