Summary
vm2's Promise callback sanitization can be bypassed by abusing async function return values, which are implemented as global Promise objects whose callbacks are not correctly sanitized.
Under these conditions, an attacker running code inside a vm2 sandbox may escape isolation and execute arbitrary code in the host Node.js environment.
Affected context
This vulnerability affects applications that rely on vm2 as a security boundary to sandbox untrusted or semi-trusted JavaScript.
Specifically, it arises when:
- vm2 is used in versions prior to 3.10.2
- Untrusted code can define or invoke async functions inside the sandbox
- The application assumes vm2 reliably prevents access to the host Node.js environment
In these setups, differences between local Promise and global Promise handling create an unintended escape hatch.
Exploit preconditions
- Attack surface: Code paths that execute untrusted or user-supplied JavaScript inside a vm2 sandbox.
- Required access: Ability to supply or influence JavaScript executed within vm2 (e.g., plugins, scripts, user-defined rules, online code runners).
- Trigger point: Execution of async functions whose return values are
globalPromiseobjects, enabling use of unsanitizedthen/catchcallbacks. - Why exploitation is possible: vm2 sanitizes callbacks on
localPromise.prototype.then, but not onglobalPromise.prototype.then; async functions return global Promise instances, so attacker-controlled callbacks can interact with host objects and escape the sandbox.
Why this happens
The core issue is an incomplete enforcement of the sandbox boundary for asynchronous promise handling.
vm2 wraps and sanitizes certain Promise APIs (such as localPromise.prototype.then) to ensure callbacks execute in a restricted context, but leaves the corresponding globalPromise callbacks unsanitized.
Because async functions in JavaScript always return a Promise implemented as a globalPromise, attacker-controlled async code can attach then or catch callbacks that are not passed through vm2's security wrappers.
This design gap maps to:
- CWE-94: Improper Control of Generation of Code, since untrusted code gains a pathway to execute arbitrary operations in the host environment.
- It also relates to broken enforcement of a defined security boundary, where not all asynchronous primitives are treated consistently.
Potential impact
- Execution of arbitrary JavaScript code in the host Node.js process outside the intended sandbox.
- Complete compromise of the sandboxing guarantees provided by vm2.
- Access to sensitive host resources (filesystem, network, environment variables, credentials) depending on host application permissions.
- Ability to manipulate application logic, exfiltrate data, or pivot to other systems from the compromised host.
- Potential use of the host process as a stepping stone for broader infrastructure attacks.
General mitigation guidance
Effective mitigation focuses on strengthening the sandbox boundary and reducing trust in sandboxed code:
- Ensure all asynchronous primitives (Promises, async/await, microtasks) are consistently wrapped so that callbacks are always executed in a constrained context.
- Treat sandboxed JavaScript as untrusted even when vm2 or similar tools are in use; apply additional isolation layers such as OS-level process separation, containers, or restricted system users.
- Design sandbox policies following a strict allowlist model, exposing only minimal, well-reviewed APIs to sandboxed code and avoiding direct access to powerful host objects.
- Adopt a defense-in-depth approach: even if the sandbox fails, limit the privileges of the hosting process (e.g., restricted filesystem access, minimal environment variables, limited network connectivity).
- Regularly review sandbox configuration and usage patterns for assumptions that untrusted code is "fully contained" and adjust architecture to avoid relying on a single mechanism for isolation.
How teams detect issues like this
To identify and monitor for similar weaknesses:
- Perform code reviews focusing on sandbox implementation details, ensuring that both synchronous and asynchronous execution paths are wrapped and sanitized consistently.
- Use static analysis or custom linters to flag direct use of global Promise objects or unwrapped async behavior in sandboxed environments.
- Introduce security-focused tests that intentionally attempt sandbox escape via async/await, Promises, and other advanced language features to validate isolation guarantees.
- Apply dynamic monitoring and logging around sandbox boundaries to detect anomalous behavior, such as unexpected access to host APIs or modules from ostensibly sandboxed code.
- Include automated security checks in CI/CD pipelines that validate the integrity of sandbox libraries and highlight changes affecting security-related code paths.

