Critical remote code execution in vm2, a widely used Node.js sandbox library
To search repositories and projects, login to gitlab.com . Suggestions GitLab Duo Agent Platform Code Suggestions (AI) CI/CD GitLab on AWS GitLab on Google Cloud Why GitLab? Published on: September 2, 2026 11 min read Critical remote code execution in vm2, a widely used Node.js sandbox library GitLab's Threat Research Group found a critical sandbox escape in vm2 that runs attacker code using the library's own documented configuration.
Abisheik Magesh Daniel Abeles security GitLab's Threat Research Group found a critical sandbox escape vulnerability in vm2, one of the most widely adopted Node.js sandboxing libraries. The vulnerability uses a configuration copied straight from vm2's own README. We found the flaw, rated CVSS 3.1: 10.0, critical, using our own AI automated tools. Anyone running vm2 Version 3.11.6 or earlier with require.external turned on should treat this as directly exploitable.
Once we found the vulnerability, we verified GitLab does not use vm2. We also reported it privately to vm2 and the maintainer fixed it fast, in vm2 Version 3.11.7. When we tested that fix again, it blocked the exact attack we reported. For anyone relying on vm2, it’s worth flagging that there's a broader configuration risk here that goes beyond this one patch, based on the maintainer's own description of the fix. TL;DR Critical vulnerability: GitLab's Threat Research Group discovered a critical sandbox escape (CVSS 3.1: 10.0) in vm2, a widely used Node.js sandboxing library, which allows for remote code execution.
The root cause: The vulnerability stems from default configurations found in the library's own "Quick Examples" README, where the sandbox fails to properly isolate itself from the host system, allowing malicious code to gain unrestricted access. Fix limitations: While updating to Version 3.11.7 blocks the specific attack reported, it does not fully resolve the underlying configuration risk; developers remain vulnerable if they continue to use require.external with overly broad require.root settings.
Immediate recommendations: Users should update to Version 3.11.7, but must also manually harden their configurations by restricting require.root to only necessary files and setting context: 'sandbox' instead of relying on the default 'host' setting. Long-term advice: Due to vm2's history of recurring sandbox escape bugs, it is recommended to avoid using it for isolating truly untrusted code and instead opt for more robust methods like containers or separate processes.
Why sandboxing libraries are a high-value target Apps that run code they don't fully trust (plugin systems, coding playgrounds, CI test runners, and now tools that let an AI run its own generated code) can't just hand that code to Node's normal require() and eval() functions. If they did, the code would have the same power as the app itself. vm2 is built to close that gap. It gives the untrusted code a fake version of a normal JavaScript environment, and blocks anything that tries to reach the real system underneath.
But if an attacker can break out of that sandbox, none of that protection is real, even though it still looks fine to the developer who installed vm2. And because so many apps use vm2, one hole in it becomes a hole in every app built on it. How the vulnerability works vm2's NodeVM has a require option that controls whether the sandboxed code can load modules outside JavaScript's built-in ones. Here's the first example from vm2's own README, under "Quick Examples": const vm = new NodeVM ({ require: { external: true , root: './' }, }); external: true lets the sandboxed code call require() on outside files. root is supposed to limit that to one safe folder.
But in a normal npm project, ./node_modules , which includes vm2's own installed copy of itself, sits inside ./ . So root: './' looks like a safety rule, but it doesn't actually keep vm2 out. Two default settings in lib/resolver-compat.js turn that gap into full code execution: const { external : externalOpt , root : rootPaths , context = 'host' , // defaults to 'host' ... } = options; isPathAllowed (filename) { if ( this .rootPaths === undefined ) return true ; // otherwise: is filename inside rootPaths? } require('./node_modules/vm2') passes the check, because that path is inside the root.
And because context defaults to 'host' , the file loads through Node's real, unsandboxed require() , instead of vm2's own loader: loadJS (vm, mod, filename) { if ( this . pathContext (filename, 'js' ) !== 'host' ) return super . loadJS (vm, mod, filename); const m = this . hostRequire (filename); // real Node.js require() mod.exports = vm. readonly (m); } At that point the sandboxed code isn't holding a limited, wrapped copy of vm2.
It's holding the real thing, with full power. It uses that real copy to start a second NodeVM , with rules it sets itself: const real = require ( './node_modules/vm2' ); const inner = new real. NodeVM ({ require: { builtin: [ 'child_process' ], external: false } }); module . exports = inner. run ( "module.exports = require('child_process').execSync('whoami').toString().trim()" , 'inner.js' ); The outer sandbox's rules don't apply to the inner, because it's a brand new sandbox that the attacker built themselves, with child_process switched on.
If the attacker tries to reach fs or child_process straight from the outer sandbox, that still fails like it should ( require('fs') and require('child_process') both throw "Cannot find module" ). Every normal block still works. The one gap is requiring a file that happens to be vm2's own package. At that point the attacker isn't holding a blocked, fake copy anymore. They're holding the real one. vm2 has had two earlier sandbox escape bugs with the same end result: a nesting: true bypass (GHSA-8hg8-63c5-gwmx) and a require.root symlink bypass (GHSA-cp6g-6699-wx9c).
Neither fix touches this code. A developer who fixed both of those is still fully open to this one. Here's the full chain, step by step: The setup. A developer creates the sandbox exactly as shown in vm2's README, under "Quick Examples": new NodeVM({ require: { external: true, root: './' } }). . The untrusted code runs. vm.run(untrustedCode) starts executing whatever code the attacker controls. The code requires vm2 itself.
It calls require('./node_modules/vm2') . Two things go wrong here, both in resolver-compat.js : isPathAllowed() returns true , because that path sits inside root . context defaults to 'host' , so hostRequire() runs instead of vm2's own loader. The result: The attacker's code now holds the real, unwrapped vm2 module, not a sandboxed copy. The code builds a second, unrestricted sandbox. Using that real vm2 module, it creates a new NodeVM with its own settings: new real.NodeVM({ require: { builtin: ['child_process'] } }). .
This second sandbox is entirely under the attacker's control. The code runs, unrestricted. inner.run("require('child_process').execSync(...)") executes, and the attacker has arbitrary command execution on the host. The fix, and what it actually covers The maintainer confirmed how it works, and folded in a duplicate report we had also filed (GHSA-w9c4-gw9x-53mq), closing it in favor of this one. The fix they shipped, in lib/resolver-compat.js , blocks a sandboxed require() of vm2's own lib/ folder and main file.
That closes the nested-VM trick no matter what require.root is set to. It leaves nesting: true alone, since that's a different bug entirely. That said, the fix is narrower than it might first appear. If you turn on require.external: true with no require.root set at all, vm2 now just prints one warning instead of blocking it, and the code still runs. But point that same README setup to any other file on disk that exposes child_process , not vm2's own package, and you still get full remote code execution on 3.11.7.
No error, no warning: // evil-helper/index.js, unrelated to vm2 module . exports = require ( 'child_process' ); const cp = require ( './evil-helper/index.js'
The rest of this page is on the live site.