Advertisement
Advanced Node.js, Core Node.js Modules

What is the node:vm module, and what are the security implications of using it to execute untrusted code?

Short answer

vm compiles and runs JavaScript within a separate V8 context, often for sandboxing -- but its isolation is not a genuine hardened security boundary, since code running inside can, through known techniques, escape into the surrounding process in certain configurations, so it…

Save Node.js mock interview
Advertisement
AModel answer
vm compiles and runs JavaScript within a separate V8 context, often for sandboxing -- but its isolation is not a genuine hardened security boundary, since code running inside can, through known techniques, escape into the surrounding process in certain configurations, so it shouldn't be the sole defense for truly untrusted code.
vm.createContext(context);
vm.runInContext('result = 2 + 2;', context);
Real-world example
A low-code platform initially ran user formula scripts via vm alone, but moved to a separate resource-limited child process after a security review found vm's isolation alone insufficient.
Likely follow-ups
  • What are documented ways code can escape a vm context?
  • What are more robust alternatives like isolated-vm or a separate process/container?
Related topics