Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท node:sqlite and Runtime Services

The vm module is not a sandbox

Why Node.js vm contexts cannot safely run untrusted code: constructor escapes, infinite loops, shared prototypes, and what to use instead.

The node:vm module runs code in a separate global object, called a context. That makes it good for evaluating templates, configuration scripts or user formulas that you wrote the rules for. It does not make the code safe. A script inside a vm context lives in the same process, on the same V8 heap, with the same privileges as the rest of your program.

The short version: if you would not eval() a string, do not run it with vm either. This page shows the main ways a context leaks and what to use when you really need isolation.

NODE
import vm from 'node:vm';

const ctx = vm.createContext({ label: 'sandbox' });
console.log(vm.runInContext('label.toUpperCase()', ctx));

Try it

Node.js A new global is not a new world

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Limits that vm does not enforce

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

How contexts leak

  • Constructor chain. Any object that comes from your side (an argument, a returned value, a function) has a constructor property that leads to your realm's Function constructor. Code inside the context can call it and then run with your globals, including process.
  • Promises and errors. Objects created by your own APIs, such as error objects thrown from host callbacks, are the same kind of bridge.
  • Timeouts are partial. The timeout option interrupts a synchronous run, but code can schedule promise callbacks, timers you handed it or Atomics.wait loops that outlive it.
  • No memory or CPU quota. A script can allocate until the whole process dies.
  • Shared engine. Bugs in V8 affect contexts exactly as they affect everything else.
  • Prototype tricks. Passing arrays and plain objects in lets the context modify the prototypes of your realm.

What to do instead

  1. Keep the inputs inert. Pass only primitives or JSON that you create with JSON.parse(JSON.stringify(x)) or structuredClone(). A context with no host objects has no constructor bridge.
  2. Use a Worker. worker_threads give a separate isolate with its own heap, and resourceLimits caps memory. Terminate the worker on timeout. It is still the same process and user, so it is isolation for stability, not a security boundary.
  3. Use a child process with operating-system limits (containers, restricted users, seccomp) when the code is truly hostile.
  4. Use the permission model (--permission) as an extra layer to block file and process access. It is not a complete sandbox either.
  5. Avoid executing code at all. A small expression parser or a data-driven rule engine is almost always safer than evaluating JavaScript.

Usage notes

  • The Node.js documentation says it directly: vm is not a security mechanism.
  • Libraries that wrap vm for sandboxing have had repeated escapes; treat their promises with suspicion.
  • vm is still fine for trusted code that needs its own globals: REPLs, test runners, template engines, build tools.
  • microtaskMode: 'afterEvaluate' makes timeouts also cover microtasks, which closes one gap but not the others.