forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src: implement query callbacks for vm
This allows using a Proxy object as the sandbox for a VM context. PR-URL: nodejs#22390 Fixes: nodejs#17480 Fixes: nodejs#17481 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: James M Snell <jasnell@gmail.com>
- Loading branch information
Showing
4 changed files
with
171 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
'use strict'; | ||
require('../common'); | ||
const assert = require('assert'); | ||
const vm = require('vm'); | ||
|
||
const sandbox = {}; | ||
const proxyHandlers = {}; | ||
const contextifiedProxy = vm.createContext(new Proxy(sandbox, proxyHandlers)); | ||
|
||
// One must get the globals and manually assign it to our own global object, to | ||
// mitigate against https://github.com/nodejs/node/issues/17465. | ||
const contextThis = vm.runInContext('this', contextifiedProxy); | ||
for (const prop of Reflect.ownKeys(contextThis)) { | ||
const descriptor = Object.getOwnPropertyDescriptor(contextThis, prop); | ||
Object.defineProperty(sandbox, prop, descriptor); | ||
} | ||
|
||
// Finally, activate the proxy. | ||
const numCalled = {}; | ||
for (const hook of Reflect.ownKeys(Reflect)) { | ||
numCalled[hook] = 0; | ||
proxyHandlers[hook] = (...args) => { | ||
numCalled[hook]++; | ||
return Reflect[hook](...args); | ||
}; | ||
} | ||
|
||
{ | ||
// Make sure the `in` operator only calls `getOwnPropertyDescriptor` and not | ||
// `get`. | ||
// Refs: https://github.com/nodejs/node/issues/17480 | ||
assert.strictEqual(vm.runInContext('"a" in this', contextifiedProxy), false); | ||
assert.deepStrictEqual(numCalled, { | ||
defineProperty: 0, | ||
deleteProperty: 0, | ||
apply: 0, | ||
construct: 0, | ||
get: 0, | ||
getOwnPropertyDescriptor: 1, | ||
getPrototypeOf: 0, | ||
has: 0, | ||
isExtensible: 0, | ||
ownKeys: 0, | ||
preventExtensions: 0, | ||
set: 0, | ||
setPrototypeOf: 0 | ||
}); | ||
} | ||
|
||
{ | ||
// Make sure `Object.getOwnPropertyDescriptor` only calls | ||
// `getOwnPropertyDescriptor` and not `get`. | ||
// Refs: https://github.com/nodejs/node/issues/17481 | ||
|
||
// Get and store the function in a lexically scoped variable to avoid | ||
// interfering with the actual test. | ||
vm.runInContext( | ||
'const { getOwnPropertyDescriptor } = Object;', | ||
contextifiedProxy); | ||
|
||
for (const p of Reflect.ownKeys(numCalled)) { | ||
numCalled[p] = 0; | ||
} | ||
|
||
assert.strictEqual( | ||
vm.runInContext('getOwnPropertyDescriptor(this, "a")', contextifiedProxy), | ||
undefined); | ||
assert.deepStrictEqual(numCalled, { | ||
defineProperty: 0, | ||
deleteProperty: 0, | ||
apply: 0, | ||
construct: 0, | ||
get: 0, | ||
getOwnPropertyDescriptor: 1, | ||
getPrototypeOf: 0, | ||
has: 0, | ||
isExtensible: 0, | ||
ownKeys: 0, | ||
preventExtensions: 0, | ||
set: 0, | ||
setPrototypeOf: 0 | ||
}); | ||
} |