Summary
The expr-eval library allows crafted user expressions to be converted into executable JavaScript via the toJSFunction() API, enabling arbitrary code execution.
When applications pass untrusted expressions into this compilation path, attackers can escape the intended sandbox and run arbitrary code in the application's execution context.
Affected context
This vulnerability arises in applications that embed expr-eval for dynamic expression parsing and then use the toJSFunction() API on values influenced by users or external systems. It is most relevant where expression strings are treated as data but are transformed directly into JavaScript source and executed with new Function(), especially in server-side environments or privileged browser contexts. The risk increases when the generated functions have access to sensitive objects, application state, or elevated privileges.
Exploit preconditions
- Attack surface: Any endpoint, API, or UI path where expressions are accepted and evaluated via expr-eval.
- Required access: Ability to supply or modify expression strings passed into toJSFunction(), either directly or through indirectly controlled configuration or data fields.
- Trigger point: Invocation of toJSFunction() (or wrappers around it) that compile and execute the attacker-supplied expression using new Function().
- Why exploitation is possible: User-controlled expressions are interpolated into JavaScript source and executed without robust sanitization, validation, or isolation, allowing injection of arbitrary code beyond the intended expression semantics.
Why this happens
The underlying bug class is code injection as described by CWE-94, where text-based expressions are treated as executable code in a general-purpose language. The expr-eval library's toJSFunction() API constructs JavaScript source strings incorporating user-controlled expression content and then compiles them using new Function(). This pattern turns untrusted data into active code, bypassing any logical sandbox provided by the expression evaluator. Without strict separation between data and code or constraints on what expressions can contain, attackers can supply payloads that extend beyond simple arithmetic or logical operations and instead execute arbitrary JavaScript in the host environment.
Potential impact
- Arbitrary JavaScript execution in the application's process context.
- Access to sensitive in-memory data, including tokens, configuration, and user records.
- Unauthorized invocation of internal APIs and business logic.
- Data tampering or exfiltration from server-side or privileged browser environments.
- Escalation to full system compromise where the application has extensive OS or network privileges.
General mitigation guidance
Effective mitigation focuses on preventing untrusted expressions from being converted into executable code and limiting what evaluated expressions can do. Developers can favor evaluation paths that do not rely on new Function() or similar dynamic code generation mechanisms, instead using interpreters that treat expressions purely as data. Where expression-based logic is necessary, restrict the allowable expression grammar and operators to a minimal, declarative subset and deny any capability to reference arbitrary identifiers or functions. Apply strong input validation and reject expressions that deviate from these constraints. Additionally, isolate any evaluation logic in a low-privilege context, minimizing access to sensitive objects and external resources, and ensure the surrounding application architecture maintains least privilege and clear trust boundaries.
How teams detect issues like this
Security teams can detect this class of issue by scanning codebases for uses of expr-eval's toJSFunction() API, as well as generic patterns involving new Function(), eval(), or similar dynamic code execution primitives. Static analysis rules can flag any combination of user-controlled input flowing into expression evaluation or code generation functions. During code review, engineers should treat expression evaluation code as high-risk, looking for clear separation between trusted and untrusted data, and verifying that only constrained grammars are accepted. Policy-based checks in CI/CD pipelines can enforce bans or guarded usage of dynamic evaluation APIs, while automated security testing tools and fuzzers can probe expression endpoints for signs of code execution beyond allowed operations.

