Summary
The Locutus library's create_function(args, code) helper forwards both parameters directly into JavaScript's Function constructor without validation or restriction.
If an attacker can influence either the args or code parameters, they can inject and execute arbitrary JavaScript within the host environment, leading to full remote code execution in contexts such as Node.js or embedded runtimes.
Affected context
This vulnerability affects applications that depend on Locutus v3.x prior to 3.0.14 and use the create_function(args, code) helper.
The risk materializes when user-controlled or otherwise untrusted input is passed directly or indirectly into either the args or code parameters, often as part of dynamic callbacks, expression builders, or educational examples.
Because new Function() executes in a global scope, any code it runs can typically access the same privileges as the surrounding JavaScript environment.
Exploit preconditions
- Attack surface: Any code path that calls
create_function()with data that can be influenced by an attacker (HTTP parameters, JSON payloads, query strings, form fields, WebSocket messages, etc.). - Required access: Ability to supply or modify the
argsorcodearguments, either directly in application code or via a data flow that reachescreate_function(). - Trigger point: Invocation of
create_function(args, code)at runtime; exploitation occurs when the returned function is constructed, regardless of whether it is subsequently invoked. - Why exploitation is possible:
create_function()passes both parameters directly tonew Function(args, code)without sanitization, escaping, or structural validation, effectively treating attacker-controlled strings as executable JavaScript.
Why this happens
The core issue is the use of JavaScript's Function constructor as a dynamic code execution mechanism with untrusted input.
This is a textbook case of CWE-94: Improper Control of Generation of Code, where application logic constructs executable code from strings derived from external sources.
Because create_function() does not enforce any constraints on the args or code arguments, it transforms data into code at runtime, allowing attackers to inject arbitrary statements, access sensitive APIs, and escape intended control flow.
The problem is inherent to the pattern of "build a string of code and execute it" and is not limited to a particular payload or framework.
Potential impact
- Execution of arbitrary JavaScript with the privileges of the hosting environment (e.g., Node.js process, browser extension, serverless runtime).
- Access to sensitive data in memory, configuration values, environment variables, and secrets available to the runtime.
- File system reads and writes, process spawning, or network calls in server-side environments, potentially leading to full system compromise.
- Manipulation of application logic, including bypassing authorization checks, tampering with business rules, or disabling security controls.
- Use of the compromised application as a pivot point for lateral movement within the surrounding infrastructure.
General mitigation guidance
Effective mitigation centers on removing or strictly limiting dynamic code generation:
- Replace
create_function()and directnew Function()usage with static function definitions or controlled callback registries that map allowed names to vetted implementations. - Avoid any pattern where application behavior is defined by concatenating strings into executable code; instead, use structured representations (e.g., configuration objects, ASTs, or whitelisted operations).
- If expression evaluation is genuinely required, adopt sandboxed interpreters or DSLs that do not expose host primitives such as
require,process, or the global object. - Constrain input to well-defined, validated formats using allowlists, strict schemas, and type checks, ensuring untrusted data cannot influence code structure.
- Run JavaScript with least-privilege configurations (e.g., restricted containers, minimal filesystem and network access) to limit blast radius if any code injection occurs.
How teams detect issues like this
Organizations can detect this class of vulnerability through a combination of code analysis and review practices:
- Use static application security testing (SAST) rules to flag calls to
new Function(),eval(), and helpers likecreate_function()when arguments are not constant or originate from external input. - During code review, treat any dynamic code construction as a high-risk pattern requiring strong justification and robust isolation; scrutinize call sites where user data reaches
Functionconstructors. - Enforce linting and coding standards that prohibit or severely restrict the use of dynamic evaluation primitives except in explicitly approved modules.
- Incorporate dependency review into CI pipelines to highlight libraries known to use
eval/new Functionand subject their integration points to additional security assessment.

