-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
structuredClone() uses wrong context in vm.runInContext() #55554
Comments
This behavior (to me) makes sense based on what does on behind-the-scenes. Behind the scenes, the const vm = require('node:vm');
const context = vm.createContext({ structuredClone, Error });
const result = vm.runInContext('structuredClone(new Error()) instanceof Error', context);
console.log('match:', result); You'll see that it is Take the following example, const vm = require('node:vm');
const context = vm.createContext({ func: (input) => input instanceof Object });
const result = vm.runInContext('func({})', context);
console.log('match:', result); The callback ( |
Thanks, but I'm already aware of how it happens. Using the parent prototypes in the VM doesn't help, as it allows the code easy access to modify the prototypes of the parent. The question is why? And is it intentional? Maybe an API to bind a function to the context of the VM is needed? As it is, it would be cumbersome to work around to restore the correct prototypes. |
Reading the spec, the serialization and deserialization steps for an error object do not include sending the prototype – which makes sense IMO, if you call IMO the current behavior is correct per spec. |
Looks like a duplicate of #46558 - the vm contexts do not have their own Node.js builtins. They only have V8 ones. The |
#46558 relates to exposing node APIs in the vm context, so it is only tangentially related to this issue, which concerns a web API exposed on the global object. Though I guess it is possible that it will be needed to be exposed to support the node APIs. |
Version
v22.10.0
Platform
Subsystem
vm
What steps will reproduce the bug?
How often does it reproduce? Is there a required condition?
100%
What is the expected behavior? Why is that the expected behavior?
match: true
becausestructuredClone
should use the context of the VM for the prototype of the producedError
instance.What do you see instead?
match: false
Additional investigation shows that the produced
Error
uses the prototype of the main runtime. Ie. a context escape, which would be a security error, if not for the disclaimer not to run untrusted code.Additional information
This issue also applies to all other cloneable types like
Map
. EvenObject
failsinstanceof
:FYI, this causes problems when running tests in jest, which use the VM to run tests.
The text was updated successfully, but these errors were encountered: