Q50: What is the `vm` module, and why is it unsuitable as a security sandbox for untrusted code?

The vm core module allows compiling and running JavaScript code within specific V8 context scopes. While useful for creating isolated execution contexts, it is not a security sandbox. Untrusted code running in a vm context can easily escape by accessing constructors of objects passed into the context (e.g., this.constructor.constructor('return process')()), granting full access to the host Node.js process and system environment. Isolated execution of untrusted code requires dedicated process-level or container-level isolation (e.g., Docker, WebAssembly sandboxes, or isolated-vm).

Key Takeaways
  • The vm module isolates scope contexts, not process permissions or system security boundaries.
  • Untrusted code can escape vm contexts via object prototypes and constructor chains to execute arbitrary host code.
Knowledge Check

How can untrusted code escape a basic Node.js `vm` context?