vm2 Sandbox Escape (Node.js)
vm2 is the library many Node applications use to run third-party code "in a sandbox": online editors, template engines, platforms that let a user write a function. Node's built-in vm module isolates nothing (its own documentation says so), and vm2 was born to plug that gap. The catch is that the sandbox is built on the same runtime as the code inside it, so escaping is not breaking a memory barrier: it is reaching an object from outside.
The giveaway is the error message itself. The first thing anyone tries in a server-side JavaScript interpreter is the module that spawns processes:
require('child_process').execSync('id').toString()And the answer matters far more than it looks:
Module "child_process" is not allowed→ there is a blacklist, meaning a permission mechanism exists and the module sits behind it. Where there is a blacklist there is something nobody listed.require is not defined→ the context has norequireat all, and one has to be built.
A blacklist is a very different decision from an allow-list: an allow-list forbids everything but what is enumerated, a blacklist permits everything but what is enumerated. It is enough to reach the same place by a route nobody enumerated.
The vm2 escape family (CVE-2023-29017, CVE-2023-30547, CVE-2023-37466, among others) always uses the same idea: get a reference to a host object and climb its prototype chain up to the outer Function, which is no longer in the box. The classic vehicle is an exception, because the Error object the sandbox hands back is constructed outside:
const err = new Error(); err.name = {toString(){ return process.mainModule.require('child_process').execSync('<cmd>').toString(); }}; try { err.stack } catch(e) {}
node -e "console.log(require('vm2/package.json').version)"Checking the version is the real first step: vm2 was discontinued by its own authors in 2023 precisely because the isolation model was not sustainable, and the official advice is to move to isolated-vm.
Requirements. That the application evaluates code we write, and that it does so with an affected version of vm2. The blocked-module error message identifies the library without ever seeing package.json. Execution lands as the Node process's user, usually a low-privilege service account in these applications: escalation follows from there.
How to detect it. The node process spawning sh, bash or cmd.exe as a child is the signal, and it has no legitimate use in an application that only evaluates expressions. In the application log, the failed attempts leave a trail before the good one: a burst of evaluations mentioning child_process, process.mainModule, constructor.constructor or __proto__ is exactly a blacklist being swept. And at the network layer, an outbound connection from the Node process to an address that is none of its known dependencies.
Defence. Do not use vm2: it is discontinued. If third-party code must run, isolation has to be at the runtime level or below (isolated-vm, a container per execution, or a separate process with no network and no filesystem), not a list of forbidden modules inside the same interpreter. And in any case, an allow-list, never a blacklist.
It is a relative of command injection by its outcome, but the mechanism is different: here the application already lets us run code on purpose, and what breaks is the boundary of what that code may touch.
Machines where it's used
- CodifyHackTheBox