Skip to content

structuredClone() uses wrong context in vm.runInContext() #55554

Open
@kanongil

Description

@kanongil

Version

v22.10.0

Platform

Darwin Silmaril.home 23.6.0 Darwin Kernel Version 23.6.0: Wed Jul 31 20:49:46 PDT 2024; root:xnu-10063.141.1.700.5~1/RELEASE_ARM64_T8103 arm64

Subsystem

vm

What steps will reproduce the bug?

const vm = require('node:vm');

const context = vm.createContext({ structuredClone });
const result = vm.runInContext('structuredClone(new Error()) instanceof Error', context);

console.log('match:', result);

How often does it reproduce? Is there a required condition?

100%

What is the expected behavior? Why is that the expected behavior?

match: true because structuredClone should use the context of the VM for the prototype of the produced Error 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. Even Object fails instanceof:

structuredClone({}) instanceof Object       // <= equals false inside VM

FYI, this causes problems when running tests in jest, which use the VM to run tests.

Metadata

Metadata

Assignees

No one assigned

    Labels

    vmIssues and PRs related to the vm subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions