# node:vm, vm2 and Python's exec are not sandboxes: in-process isolation and why it fails

> Running model-written code inside your own process with node:vm, vm2 or exec with empty builtins looks like isolation. The docs say it is not, and two short escapes show why. What to use instead.

Running model-written code inside your own process with node:vm, vm2 or exec with empty builtins looks like isolation. The docs say it is not, and two short escapes show why. What to use instead.

November 23, 2026 · 4 min read · The Sandscale team

The first version of many agent tools runs model-written code the quick way: inside the same process, with a language feature that sounds like isolation. In Node that is the `vm` module. In Python it is `exec` with a stripped-down `__builtins__`. It works in the demo, it is easy, and it does not isolate anything.

This post shows why, using the languages’ own documentation and two escapes short enough to fit in a tweet, then covers what to use instead.

## What the docs say

Both runtimes are explicit about this, which makes it unusual among security pitfalls: you do not need to find a researcher’s blog post, the warning is in the manual.

The [Node.js `vm` documentation](https://nodejs.org/api/vm.html) opens with: “The `node:vm` module is not a security mechanism. Do not use it to run untrusted code.”

The [Python documentation for `eval` and `exec`](https://docs.python.org/3/library/functions.html) warns that calling them “with untrusted user-supplied input will lead to security vulnerabilities”, and on the `__builtins__` trick says that overriding it “can be used to restrict or change the available names, but this is not a security mechanism: the executed code can still access all builtins.”

Code written by a model from a prompt that may contain text from the internet is untrusted input. That settles it, but it helps to see why.

## The Node escape

`vm.runInNewContext` runs code with a different global object. It does not run it in a different heap, process or permission set. Objects you pass into the context, including the context object itself, come from the outer world, and every object in JavaScript can reach its constructor:

```js
const vm = require('node:vm');

const result = vm.runInNewContext(
  'this.constructor.constructor("return process")().version',
  {},
);
```

`this` inside the context is the object you passed in, created outside. Its `constructor` is the outer `Object`, and that function’s `constructor` is the outer `Function`. Calling it compiles a function in the host’s realm, which returns the host’s `process`. We ran this on Node 24, and `result` equals the host’s `process.version`. Replace `.version` with `.env` or `.mainModule.require('child_process')` and the “sandboxed” code reads your secrets or runs commands.

Freezing the context object or passing `Object.create(null)` closes this particular path, and then someone finds another through an error object, a callback or a promise. That is the history of the next section.

## vm2: a library that tried harder

[vm2](https://github.com/patriksimek/vm2) wrapped `vm` with proxies that intercepted every object crossing the boundary, and for years it was the popular answer to “how do I run untrusted JavaScript in Node?”. It also accumulated a series of sandbox escapes. One example is [CVE-2023-37466](https://nvd.nist.gov/vuln/detail/CVE-2023-37466): in vm2 up to 3.9.19, Promise handler sanitization could be bypassed with the `@@species` accessor property, allowing attackers to escape the sandbox and run arbitrary code.

The project’s README now says it is discontinued, that it “contains critical security issues and should not be used for production”, and points users to isolated-vm. The lesson is not that vm2’s authors were careless. It is that intercepting every path between two worlds that share a heap is a game the defender has to win every time.

## The Python escape

Python’s version of the trick is to pass `exec` or `eval` a globals dictionary with `__builtins__` set to an empty dict, so `open`, `__import__` and friends are gone. But the code can still write literals, and every literal is an object with a class, and every class leads back to `object`, which knows all its subclasses:

```python
g = {"__builtins__": {}}
code = (
    '[c for c in ().__class__.__base__.__subclasses__()'
    ' if c.__name__ == "_wrap_close"][0].__init__.__globals__["system"]'
)
system = eval(code, g)
```

`()` is a tuple; its base class is `object`; `object.__subclasses__()` lists every class loaded in the interpreter. One of them, `os._wrap_close`, is defined in the `os` module, so its `__init__` function’s globals are the `os` module’s globals, which include `system`. On Python 3.13, `system` is the real `os.system`. No builtins needed.

Blocklisting `__subclasses__`, double underscores or particular names in the source string is the usual next step, and it fails for the same reason vm2 did: there are too many paths, and encodings, `getattr` with computed strings and format tricks make source filtering a losing game.

## Why in-process isolation keeps failing

The common thread: the untrusted code and your code share one process. They share a memory space, a set of loaded modules, file descriptors, credentials and the operating system’s view of who is running. Language-level tricks try to hide those things. Any object that leaks across, any bug in the interception layer, or any bug in the runtime itself puts the untrusted code on your side.

Even a stronger in-process tool, such as a separate V8 isolate, still runs inside your process: it narrows what the code can reach through the language, but a bug in the engine is still a bug in the process holding your credentials.

## What to use instead

Put a boundary the operating system or hardware enforces between the code and anything you care about:

- **A separate process with reduced privileges**, at minimum: a different user, no secrets in its environment, a seccomp filter, resource limits and a timeout. Better than in-process, though it shares your kernel.
- **A container with a hardened profile**, for code you mostly trust. It also shares your kernel, and our post on [container escape CVEs](https://sandscale.dev/blog/container-escape-cves-lessons-for-agent-sandboxes) shows what that has meant in practice.
- **A microVM or a user-space kernel** such as gVisor, for code nobody has read. See our comparison of [containers, gVisor and Firecracker](https://sandscale.dev/blog/isolating-ai-agent-code-containers-gvisor-firecracker).

Whatever you pick, keep the habits that matter at every layer: give the code no secrets it does not need, no network it does not need, and a time limit.

## Where Sandscale fits

Sandscale gives each run its own Firecracker microVM with its own kernel, which is the third option above as an API. You send the model’s code with `writeFile`, run it with `run`, and read the exit code and output back, instead of calling `eval` in your server. Our [quickstart](https://sandscale.dev/blog/run-agent-code-in-a-microvm-quickstart) shows the whole loop.

Whether you use us or not, delete the `vm.runInNewContext` call. The docs told us all not to use it this way, and they were right.

**On this page**

1. What the docs say
2. The Node escape
3. vm2: a library that tried harder
4. The Python escape
5. Why in-process isolation keeps failing
6. What to use instead
7. Where Sandscale fits

**Run your agents in a box of their own**

Each sandbox is a Firecracker microVM. Free includes 100 awake vCPU-hours a month.

[Start free](https://cloud.sandscale.dev/auth/signup)

## Keep reading

- [November 23, 2026 · **A checklist for reviewing a pull request an AI agent wrote** · Agent-written PRs fail in different places than human ones. A practical review checklist: what to read first, the lines that deserve the most suspicion, and what to run before you approve.](https://sandscale.dev/blog/reviewing-an-agent-diff-checklist)
- [November 23, 2026 · **A threat model for AI coding agents: what can go wrong, and where to put the controls** · A practical threat model for coding agents: the assets at risk, the ways untrusted text gets in, the attacker goals, and which control belongs at which layer.](https://sandscale.dev/blog/threat-model-for-ai-coding-agents)

---
Source: https://sandscale.dev/blog/node-vm-module-is-not-a-sandbox
