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.
import vm from 'node:vm'; const ctx = vm.createContext({ label: 'sandbox' }); console.log(vm.runInContext('label.toUpperCase()', ctx));
Try it
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.
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
constructorproperty that leads to your realm'sFunctionconstructor. Code inside the context can call it and then run with your globals, includingprocess. - 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
timeoutoption interrupts a synchronous run, but code can schedule promise callbacks, timers you handed it orAtomics.waitloops 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
- Keep the inputs inert. Pass only primitives or JSON that you create with
JSON.parse(JSON.stringify(x))orstructuredClone(). A context with no host objects has no constructor bridge. - Use a Worker.
worker_threadsgive a separate isolate with its own heap, andresourceLimitscaps memory. Terminate the worker on timeout. It is still the same process and user, so it is isolation for stability, not a security boundary. - Use a child process with operating-system limits (containers, restricted users, seccomp) when the code is truly hostile.
- Use the permission model (
--permission) as an extra layer to block file and process access. It is not a complete sandbox either. - 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:
vmis not a security mechanism. - Libraries that wrap
vmfor sandboxing have had repeated escapes; treat their promises with suspicion. vmis 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.