Skip to content

vm module regression in v18: setting properties on global proxy breaks jsdom #42962

Closed
@domenic

Description

@domenic

Version

v18.1.0

Platform

Microsoft Windows NT 10.0.22000.0 x64

Subsystem

vm

What steps will reproduce the bug?

Put this code in vm-test.js:

"use strict";
const vm = require("vm");

const window = createWindow();

console.log(Object.getOwnPropertyDescriptor(window.globalProxy, "onhashchange"));
window.globalProxy.onhashchange = () => {};

console.log("success");

function createWindow() {
  const obj = {};
  vm.createContext(obj);
  Object.defineProperty(obj, "onhashchange", {
    get() { },
    set() {},
    configurable: true
  });

  obj.globalProxy = vm.runInContext("this", obj);

  return obj;
}

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

Always reproduces

What is the expected behavior?

The v17 behavior was relied upon by jsdom extensively. Using Node v17.9.0 you get:

$ node vm-test.js
{
  get: [Function: get],
  set: [Function: set],
  enumerable: false,
  configurable: true
}
success

What do you see instead?

Using Node v18.0.0 or v18.1.0 you get:

$ node vm-test.js
{
  get: [Function: get],
  set: [Function: set],
  enumerable: false,
  configurable: true
}
C:\Users\Domenic\Dropbox\GitHub\jsdom\jsdom\vm-test.js:7
window.globalProxy.onhashchange = () => {};
                                ^

TypeError: Cannot redefine property: onhashchange
    at Object.<anonymous> (C:\Users\Domenic\Dropbox\GitHub\jsdom\jsdom\vm-test.js:7:33)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Module._load (node:internal/modules/cjs/loader:827:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

Node.js v18.1.0

Additional information

No response

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