-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
src: fix vm enumerator callbacks #22836
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,45 @@ | ||
'use strict'; | ||
require('../common'); | ||
const assert = require('assert'); | ||
const vm = require('vm'); | ||
|
||
// Regression test for https://github.com/nodejs/node/issues/22723. | ||
|
||
const kFoo = Symbol('kFoo'); | ||
|
||
const test = { | ||
'not': 'empty', | ||
'0': 0, | ||
'0.5': 0.5, | ||
'1': 1, | ||
'-1': -1, | ||
[kFoo]: kFoo | ||
}; | ||
vm.createContext(test); | ||
const proxied = vm.runInContext('this', test); | ||
|
||
// TODO(addaleax): The .sort() calls should not be necessary; the property | ||
// order should be indices, then other properties, then symbols. | ||
assert.deepStrictEqual( | ||
Object.keys(proxied).sort(), | ||
['0', '1', 'not', '0.5', '-1'].sort()); | ||
assert.deepStrictEqual( | ||
// Filter out names shared by all global objects, i.e. JS builtins. | ||
Object.getOwnPropertyNames(proxied) | ||
.filter((name) => !(name in global)) | ||
.sort(), | ||
['0', '1', 'not', '0.5', '-1'].sort()); | ||
assert.deepStrictEqual(Object.getOwnPropertySymbols(proxied), []); | ||
|
||
Object.setPrototypeOf(test, { inherited: 'true', 17: 42 }); | ||
|
||
assert.deepStrictEqual( | ||
Object.keys(proxied).sort(), | ||
['0', '1', 'not', '0.5', '-1'].sort()); | ||
assert.deepStrictEqual( | ||
// Filter out names shared by all global objects, i.e. JS builtins. | ||
Object.getOwnPropertyNames(proxied) | ||
.filter((name) => !(name in global)) | ||
.sort(), | ||
['0', '1', 'not', '0.5', '-1'].sort()); | ||
assert.deepStrictEqual(Object.getOwnPropertySymbols(proxied), []); |
File renamed without changes.
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 | ||
}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the sorting necessary?
Object.keys()
andObject.getOwnPropertyNames()
should always return the properties in the order you already expect them to be. Everything else is likely a bug.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It fails if I don’t do this, even though this is the order in which they were declared. I’m not sure on the “why” of that, though. (i.e. this is also a question for @nodejs/vm)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By spec the return value is:
So if something else is returned, we should probably look into that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you be so kind and add a comment that this is against the spec and should be fixed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BridgeAR does b9ec6b825844 sound okay?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes :)