/

CVE-2026-41507

Severity:

CRITICAL RISK

(9.8)

Code Injection in math-codegen

CVE-2026-41507

Analyzed

Vulnerability type

Category:

Code Injection

CWE:

CWE-94

Impact:

Remote Code Execution

Severity:

CRITICAL RISK

(9.8)

CVSS version:

3.1

Catch this CVE while code is being written

Detect vulnerable code directly in pull requests, so CVEs are fixed before merge.

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 to cg.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.

Sources

SourceLink
github.comgithub.com/mauriciopoppe/math-codegen/commit/4bb52d3030683362b3559ee8dd91350555a05f6b
github.comgithub.com/mauriciopoppe/math-codegen/pull/11
github.comgithub.com/mauriciopoppe/math-codegen/security/advisories/GHSA-p6x5-p4xf-cc4r

Frequently asked

questions

Yes. Any remotely accessible endpoint that passes user input into cg.parse() and executes the generated code is potentially vulnerable to remote code execution.

Not necessarily. If the math or expression evaluation feature is available to unauthenticated users, then no authentication is required. If it is behind login or API keys, an attacker must first obtain that level of access.

Replace dynamic code generation (new Function(), eval(), etc.) with an AST-based or interpreter-style evaluation model that only supports a constrained set of mathematical operations, and ensure that user input cannot introduce arbitrary JavaScript or system-level calls.

Design expression handling components under the assumption that all input is untrusted, forbid dynamic evaluation of user data, enforce secure coding policies around code generation APIs, and use review or automated tooling to detect when user-controlled strings are passed into dynamic execution constructs.

Find out what's already
exploitable in your codebase.