Summary
A sandbox escape vulnerability in vm2 allows untrusted code to reach BaseHandler.getPrototypeOf, exposing arbitrary JavaScript prototypes inside the host environment.
Under certain conditions, an attacker running code inside a vm2 sandbox can abuse this prototype access to break isolation and potentially execute arbitrary code outside the intended sandbox boundary.
Affected context
This issue affects applications that:
- Use vm2 as a security boundary to execute untrusted or semi-trusted JavaScript,
- Rely on vm2's VM/sandbox abstraction to prevent access to host objects and prototypes, and
- Expose vm2-powered execution as part of an API, plugin system, rules engine, or user-supplied scripting layer.
The vulnerability is relevant when sandboxed code can indirectly reach BaseHandler.getPrototypeOf, which undermines the assumption that prototype chains and core objects are safely isolated from untrusted scripts.
Exploit preconditions
- Attack surface: Any feature where users can supply JavaScript executed inside a vm2 sandbox (e.g., custom scripts, expressions, extensions, in-app automation).
- Required access: Ability to run code in the vm2 VM (even without direct access to Node.js APIs).
- Trigger point: Invocation of APIs or language constructs that, due to vm2's internal logic, end up calling
BaseHandler.getPrototypeOf. - Why exploitation is possible: The internal handler permits access to arbitrary prototypes, allowing the attacker to tamper with or traverse prototype chains and potentially craft objects that escape the sandbox's intended constraints, leading to code injection or access to host capabilities.
Why this happens
The root cause lies in vm2's handling of object introspection and prototype access inside the sandbox. BaseHandler.getPrototypeOf is part of vm2's machinery for mediating object interactions between the sandbox and host environment.
By allowing untrusted code to reach this handler, the sandbox leaks a powerful primitive: direct control over which prototypes can be observed or interacted with. This can enable construction of objects with privileged or unexpected prototypes, bypassing isolation assumptions and enabling execution paths that were not intended to be exposed. Conceptually, this maps to CWE-94: Improper Control of Generation of Code, because it breaks the controls around what code and behavior can be instantiated from untrusted input in a supposedly restricted execution environment.
Potential impact
- Sandbox escape from vm2 into the surrounding Node.js process.
- Execution of arbitrary JavaScript or OS commands with the privileges of the hosting application.
- Access to sensitive process resources (environment variables, filesystem, network, secrets in memory).
- Lateral movement within the application's infrastructure if the compromised process has further network or service access.
- Tampering with application logic, data, and security checks that rely on the sandbox boundary.
General mitigation guidance
Effective mitigation focuses on hardening sandbox boundaries and minimizing trust in untrusted code execution:
- Reassess whether untrusted JavaScript execution is strictly necessary; prefer declarative configuration or constrained DSLs when possible.
- Treat the sandbox as a defense-in-depth control rather than a sole security boundary, and avoid granting it high-privilege process environments.
- Apply strict input and feature restrictions to what untrusted scripts can access (e.g., avoid exposing powerful host objects or bridging helpers into the VM).
- Run sandboxed execution in isolated processes or containers with least-privilege OS-level permissions, separate from core application services.
- Enforce timeouts, memory limits, and resource quotas around untrusted script execution to constrain blast radius.
- Regularly review and update sandbox libraries and incorporate them into a broader secure design instead of relying purely on library-level isolation promises.
How teams detect issues like this
Detection typically involves a mix of code-level analysis and runtime safeguards:
- Static analysis / review: Look for code paths that:
- Instantiate vm2 and execute arbitrary user-controlled code,
- Treat vm2 as a primary security boundary (e.g., "this is safe because it runs in vm2"),
- Expose powerful host objects, functions, or bridges into the sandbox context.
- Security code review: Flag any use of vm2 in authentication, authorization, or multi-tenant isolation boundaries for deeper manual inspection.
- Policy enforcement: Incorporate rules in CI/CD to:
- Identify and gate the use of sandbox libraries for untrusted code,
- Require security review when changing sandbox configuration or upgrading sandbox-related dependencies.
- Runtime monitoring: Instrument sandboxed execution to track unexpected access patterns, errors, or escape attempts, and correlate them with upstream requests or tenants.

