Summary
A flaw in vm2's NodeVM builtin allowlist allows sandboxed code to bypass builtin restrictions when the module builtin is permitted, even indirectly via the '*' wildcard.
By accessing Module._load() from within the sandbox, an attacker can load otherwise excluded core modules such as child_process in the host context and ultimately achieve remote code execution.
Affected context
This issue affects applications that:
- Embed vm2 as a sandbox for executing untrusted or partially trusted JavaScript, and
- Use NodeVM with a builtin allowlist that includes
moduleor uses the'*'wildcard to allow all builtins, and - Rely on vm2's builtin restrictions as a primary defense against access to sensitive Node.js APIs.
In these configurations, the sandbox is intended to prevent direct use of dangerous builtins, but the exposed module builtin reintroduces a direct bridge into the host's module loading system.
Exploit preconditions
- Attack surface: Code executed inside a vm2 NodeVM sandbox (e.g., user-supplied scripts, plugins, or extensions).
- Required access: Ability to provide or influence JavaScript code that runs within a NodeVM instance configured with
moduleor'*'in the builtin allowlist. - Trigger point: Execution of attacker-controlled code that calls into the
modulebuiltin and then uses Node'sModule._load()functionality. - Why exploitation is possible: The sandbox grants access to the
modulebuiltin, which in turn exposes the host'sModule._load()function. This function loads modules directly in the host context, ignoring vm2's builtin restrictions and enabling loading of excluded modules such aschild_process.
Why this happens
The underlying issue is a logical authorization bypass in vm2's sandbox boundary design.
NodeVM is designed to enforce a builtin allowlist that restricts which Node.js core modules and APIs can be used by sandboxed code. However, when the module builtin is permitted, it exposes Node's internal Module loader, including methods like Module._load(), which operate in the host context. This effectively creates an unintended privileged capability inside the sandbox.
Because the allowlist was applied to direct builtin usage but not to the indirect loading behavior of Module._load(), sandboxed code can circumvent the intended policy and load any builtin module, including ones that should remain inaccessible. This maps to CWE-863: Incorrect Authorization, where the component responsible for enforcing access control fails to properly restrict access to sensitive functionality reachable through a permitted interface.
Potential impact
- Sandboxed code can load sensitive core modules (e.g.,
child_process,fs,net) that were meant to be excluded. - Execution of arbitrary system commands via modules like
child_process, leading to remote code execution in the host Node.js process. - Reading, modifying, or deleting files accessible to the host application.
- Establishing outbound network connections or acting as a pivot point for further attacks inside the hosting environment.
- Complete compromise of the security guarantees expected from vm2-based sandboxing.
General mitigation guidance
Mitigation centers on tightening sandbox boundaries and minimizing indirect capability exposure:
- Avoid granting the
modulebuiltin to untrusted or semi-trusted code; prefer a minimal, explicit builtin allowlist that only includes strictly necessary APIs. - Refrain from using wildcard allowlists (e.g.,
'*') for builtins when handling untrusted input; start from a deny-by-default configuration and add only carefully vetted capabilities. - Conceptually separate "safe scripting" from general-purpose module loading; treat any API that can dynamically resolve or load modules as a privileged interface.
- Apply the principle of least privilege to sandbox configurations: restrict filesystem, network, and process-related capabilities at both the Node.js and OS levels (e.g., container isolation, reduced process privileges).
- Consider layering additional isolation mechanisms (such as OS-level sandboxes or containers) so that a failure in vm2's logical isolation does not directly expose the underlying system.
How teams detect issues like this
Organizations can approach detection of this pattern using:
- Configuration reviews: Examine vm2 usage (especially NodeVM) for builtin allowlists that include
moduleor the'*'wildcard where untrusted code may run. - Static analysis and code scanning: Search for constructors or helper functions that create NodeVM instances and flag those with permissive
requireor builtin configurations, especially where inputs originate from users, plugins, or external services. - Code review guidelines: Treat sandbox boundary code as security-critical; reviewers should verify that no APIs exposed into the sandbox can indirectly reach unrestricted module loaders or other privileged host capabilities.
- Runtime monitoring: Instrument and log sandboxed module loading behavior (where feasible) and alert on unexpected access to sensitive modules (e.g.,
child_process,fs,net) originating from sandboxed contexts. - Automated security testing tools may assist in discovering sandbox escape vectors by fuzzing sandboxed environments and testing known bypass patterns.

