Skip to main content

no-node-vm-run-in-context

Disallow node:vm dynamic code execution APIs that are commonly mistaken for a security sandbox.

Targeted pattern scopeโ€‹

This rule targets node:vm and vm imports or require(...) bindings when code is executed through:

  • runInNewContext(...)
  • runInContext(...)
  • runInThisContext(...)
  • compileFunction(...)
  • new Script(...)

What this rule reportsโ€‹

This rule reports direct use of the vm module's code-execution APIs because those APIs compile or execute JavaScript source text.

Why this rule existsโ€‹

Node's own documentation warns that the vm module is not a security mechanism. Teams sometimes treat it like a safe sandbox for untrusted code, but that assumption is fragile and can lead to code execution or sandbox-escape exposure.

โŒ Incorrectโ€‹

import vm from "node:vm";

vm.runInNewContext(userCode, sandbox);
const { Script } = require("vm");

new Script(untrustedSource);

โœ… Correctโ€‹

import vm from "node:vm";

vm.measureMemory();

Behavior and migration notesโ€‹

This rule intentionally focuses on the vm module's code-execution entry points. It does not attempt to determine whether a specific source string is trusted.

ESLint flat config exampleโ€‹

import sdl from "eslint-plugin-sdl-2";

export default [
{
plugins: { sdl },
rules: {
"sdl/no-node-vm-run-in-context": "error",
},
},
];

When not to use itโ€‹

Disable this rule only if your project has a reviewed and documented reason to use node:vm code-execution APIs and that risk is accepted explicitly.

Package documentationโ€‹

Further readingโ€‹

Rule catalog ID: R064