The break is that sandboxing assumptions collapse under escape conditions. Node.js vm is not a security boundary, and vm2 has had fundamental bypasses that let attackers reach outside objects and execute code with host privileges. If untrusted input can also reach template literal evaluation, the attack surface grows further because code may run outside the intended sandbox entirely.
Why This Matters for Security Teams
Using Node.js NIST Cybersecurity Framework 2.0 language, the issue is not just whether code can be executed, but whether the environment truly enforces a security boundary. With vm and vm2, teams often assume isolation where only partial containment exists. That assumption fails fast when attacker-controlled data can influence evaluation paths, object access, or helper functions that sit outside the sandbox.
This matters because the security model changes from "restricted execution" to "host compromise is possible if the boundary is bypassed." In practice, that means template rendering, plugin systems, expression evaluators, and AI-adjacent orchestration layers can turn a convenience feature into a privilege escalation path. The risk is higher when developers treat sandboxing as a primary defense instead of one layer in a broader control stack.
Security teams also need to separate runtime containment from application trust. A sandbox that blocks obvious globals but still exposes enough object graph behavior to escape is not equivalent to process isolation, container hardening, or a zero trust design. In practice, many security teams encounter vm and vm2 failures only after untrusted input has already been used in production templating or plugin execution, rather than through intentional boundary testing.
How It Works in Practice
Node.js vm creates a separate context, but it still runs inside the same process and depends on the same underlying runtime. That means memory, event loop behavior, and host-controlled objects can become part of the attack surface. vm2 attempted to add stronger containment by filtering access to dangerous APIs, but repeated bypasses showed that blacklisting specific objects and methods is not a durable isolation strategy.
In practical deployments, the problem usually appears in one of three forms:
- Untrusted code can reach host objects through prototype chains, constructors, or exception handling paths.
- Application code passes rich helper objects into the sandbox, unintentionally exposing capabilities.
- Template or expression evaluation gives attackers a path to execute logic outside the intended trust zone.
For security architecture, the safer pattern is to treat vm and vm2 as convenience wrappers, not boundaries. Use a separate process, container, or service boundary for genuinely untrusted execution. Limit input to a narrow declarative format where possible, and validate output before it is consumed by downstream systems. Where dynamic evaluation is unavoidable, reduce the available surface area to the smallest possible set of functions and data types.
Operationally, monitoring should focus on unusual evaluation patterns, unexpected access to constructors or function wrappers, and any workflow where user input changes the code path rather than the data. Current guidance suggests that untrusted code should be isolated with OS-level controls or dedicated execution services, because in-process sandboxes inherit too much from the host runtime. These controls tend to break down when developers pass complex objects into the sandbox because object graph traversal can reintroduce host capabilities.
Common Variations and Edge Cases
Tighter isolation often increases engineering overhead, requiring organisations to balance developer convenience against the cost of stronger execution boundaries. That tradeoff becomes sharper in systems that rely on rapid plugin loading, user-defined rules, or AI-generated code fragments.
There is no universal standard for this yet, but best practice is evolving toward "assume sandbox escape is possible until proven otherwise." That means avoiding security claims based solely on vm or vm2, especially in multi-tenant products, SaaS automation layers, and applications that let customers upload scripts or templates. If the code must be isolated, a separate process with restricted OS permissions is a more defensible baseline than an in-process context wrapper.
The edge case that often gets missed is helper reuse. A sandbox may look safe in unit tests, then fail in production because a seemingly harmless utility exposes filesystem, network, or module-loading behavior. Another common issue is template engines that allow expression execution but are only reviewed as "presentation logic." In those cases, the security boundary is not the sandbox library itself, but the trust placed in every object and callback that enters it.
For teams building AI-enabled tooling, the intersection matters when generated code, prompts, or tool calls are evaluated dynamically. That is a governance problem as much as a runtime problem, because the system is now making execution decisions on untrusted or semi-trusted content.
Standards & Framework Alignment
This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.
MITRE ATT&CK and OWASP Agentic AI Top 10 address the attack and risk surface, while NIST CSF 2.0 set the governance and control requirements practitioners need to meet.
| Framework | Control / Reference | Relevance |
|---|---|---|
| NIST CSF 2.0 | PR.AC-3 | Sandbox escapes undermine access restrictions and host boundary assumptions. |
| MITRE ATT&CK | T1059 | Dynamic code execution is the core abuse path when sandboxing fails. |
| OWASP Agentic AI Top 10 | Agentic and tool-using systems can turn prompt or tool input into execution. |
Treat in-process sandboxes as non-boundaries and enforce separation with stronger runtime controls.
Related resources from NHI Mgmt Group
- What breaks when developers rely on string concatenation for SQL queries in Node.js?
- What breaks when organisations rely on container isolation alone for AI agent security?
- What breaks when developers rely on filename or extension checks alone for upload security?
- How should security teams choose authentication for Node.js apps that may become B2B products?
Deepen Your Knowledge
Reviewed and updated by the NHIMG editorial team on September 1, 2026.
NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org