Summary
Math.js versions 13.1.0 through 15.1.x contain an issue in the expression parser that allows arbitrary JavaScript execution when evaluating crafted expressions.
Applications that expose mathjs expression evaluation to users without strict sandboxing or validation may inadvertently grant attackers code execution within the server or client runtime.
Affected context
This vulnerability appears in projects that integrate Math.js and allow dynamic expression evaluation using its parser, especially in Node.js backends or privileged browser contexts.
Risk increases when user-controlled or externally sourced strings are passed directly to the parser without filtering, sandboxing, or constraining the available functions and symbols.
Environments where the Math.js evaluation context has access to sensitive data, system APIs, or other application components are more likely to see impactful exploitation.
Exploit preconditions
- Attack surface: Any API, UI component, or service that evaluates Math.js expressions derived from untrusted or semi-trusted input.
- Required access: Ability to submit or influence expressions processed by the Math.js parser (e.g., form fields, query parameters, stored configuration, workflow rules).
- Trigger point: Invocation of the Math.js evaluation mechanisms (such as
math.evaluateor parser-based evaluation) on attacker-crafted expressions. - Why exploitation is possible: The parser can be driven to execute arbitrary JavaScript rather than being strictly limited to mathematical operations and safe expression constructs.
Why this happens
The underlying issue is a failure to strictly separate data from executable logic in the expression parsing and evaluation pipeline.
Instead of constraining evaluation to a safe subset of math operations, the parser path permits constructs that resolve to general-purpose JavaScript execution.
This behavior corresponds to CWE-94: Improper Control of Generation of Code ('Code Injection') and is closely related to CWE-915: Improperly Controlled Modification of Dynamically-Determined Object Attributes, where dynamic evaluation and attribute access are insufficiently restricted.
Potential impact
- Attackers may execute arbitrary JavaScript within the application runtime context.
- Sensitive data accessible to the Math.js environment could be read, modified, or exfiltrated.
- Server-side usage may lead to compromise of application integrity, data stores, or connected services.
- Client-side usage can allow hostile scripts to pivot into broader cross-site scripting or account takeover scenarios.
- Application-specific business logic and security controls implemented around expression evaluation may be bypassed or subverted.
General mitigation guidance
Effective mitigation centers on tightening the evaluation model and reducing trust in dynamically provided expressions.
Recommended defensive patterns include:
- Treat all user-provided expressions as untrusted data and apply strict allowlists of supported operations, functions, and symbols.
- Prefer safe expression evaluators that do not expose general-purpose JavaScript execution and avoid generic
eval-like semantics entirely. - Constrain the execution context so that evaluated expressions cannot access sensitive APIs, global objects, or mutable shared state.
- Apply least-privilege principles to the environment where expressions are evaluated, including process isolation or sandboxing where feasible.
- Integrate robust input validation and schema-based checking for expression formats before they reach the evaluation path.
How teams detect issues like this
Detection typically focuses on identifying dangerous expression evaluation patterns and contexts where untrusted input reaches Math.js.
Useful approaches include:
- Static analysis rules to flag calls to Math.js evaluation APIs when their arguments originate from request data, form inputs, or other external sources.
- Code review checklists that require explicit justification and constraints for any dynamic expression evaluation, including reviews of the permitted function set and evaluation context.
- Logging and monitoring around expression evaluation endpoints to detect anomalous or complex expressions indicative of probing or exploitation attempts.
- Policy-based enforcement in CI/CD pipelines that disallow generic code-generation or evaluation patterns and highlight risky parser configurations.

