Summary
A critical sandbox escape in vm2 exposes the host realm's global object when sandboxed code invokes an exposed non-strict host function without a receiver. This can allow untrusted JavaScript to access host capabilities, including process-level functionality, and potentially execute arbitrary commands on the host.
Affected context
The vulnerability occurs in vm2 deployments that expose at least one non-strict, host-defined JavaScript function to sandboxed code. It is triggered when that function is called with a nullish receiver, allowing the JavaScript engine to substitute the host realm's global object before vm2 wraps the result for the sandbox. Strict-mode and ES module host functions are not affected by this specific behavior.
Exploit preconditions
- Attack surface: The vm2 JavaScript sandbox and its bridge for host-provided functions
- Required access: Ability to execute untrusted JavaScript within the affected vm2 sandbox
- Required exposure: At least one non-strict host function must be made available to sandboxed code
- Trigger point: Invocation of the exposed function without a receiver
- Why exploitation is possible: The bridge forwards a nullish receiver to the host call, while the JavaScript runtime substitutes the host global object and vm2 returns a live wrapped reference to the sandbox
Why this happens
The underlying bug is an incorrect handling of a nullish this receiver in the bridge's apply trap. For non-strict functions, the JavaScript runtime may replace an undefined receiver with the host realm's global object. By passing that object back through the sandbox boundary, vm2 violates the isolation assumption that host globals and their capabilities cannot be reached by untrusted code. This behavior maps to CWE-94: Improper Control of Generation of Code, because the resulting boundary escape can expose host execution capabilities to attacker-controlled script.
Potential impact
- Sandboxed code could escape its intended isolation boundary
- An attacker may access host process and runtime capabilities
- Arbitrary code or operating-system commands could be executed on the host
- Application credentials, environment data, or local files could be exposed
- The host application and other workloads sharing the runtime could be compromised
General mitigation guidance
Mitigation generally focuses on preserving a strict capability boundary between sandboxed code and the host runtime.
Common defensive patterns include:
- Avoiding exposure of non-strict host functions to untrusted sandboxes
- Prefering strict-mode or narrowly scoped wrapper functions when host functionality is required
- Explicitly validating and normalizing receivers at the bridge boundary
- Exposing minimal capabilities through purpose-built interfaces rather than host objects or globals
- Running sandboxed workloads with separate process, container, or operating-system isolation where practical
- Applying least privilege to the host process and restricting access to sensitive files, credentials, and runtime services
- Treating sandbox escape findings as boundary failures requiring review of all exposed host APIs
How teams detect issues like this
Detection can combine static analysis, code review, and runtime policy checks:
- Search for vm2 configurations that expose host-defined functions to untrusted code
- Review whether exposed callbacks are non-strict and whether receiver handling is explicit
- Identify bridge or proxy traps that forward nullish receivers without normalization
- Enforce policies limiting access to process, module-loading, filesystem, and command-execution capabilities
- Add tests covering detached calls and other receiver-less invocation forms at the sandbox boundary
- Use automated security review tools to flag dynamic host capability exposure and unsafe sandbox assumptions

