node icon indicating copy to clipboard operation
node copied to clipboard

`Reflect.has` triggers getters on global object contextified by `vm`

Open lachrist opened this issue 10 months ago • 3 comments

Version

v20.12.2

Platform

Darwin Laurents-MacBook-Air.local 23.2.0 Darwin Kernel Version 23.2.0: Wed Nov 15 21:59:33 PST 2023; root:xnu-10002.61.3~2/RELEASE_ARM64_T8112 arm64

Subsystem

vm

What steps will reproduce the bug?

import { createContext, runInContext } from "vm";
runInContext(
  `
    "use strict";
    Reflect.defineProperty(globalThis, "FOO", {
      get: () => {
        throw new Error("This should not get triggered");
      },
    });
    Reflect.has(globalThis, "FOO");
  `,
  createContext(),
);

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

All the time.

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

Reflect.has should never trigger getter.

What do you see instead?

Reflect.has triggers getters for contextified global object. This is somehow similar to https://github.com/nodejs/node/issues/52720 in that it makes contextified global objects break some invariants of the language.

In case anyone is wondering, I use vm to avoid having to spawn a new node process for each test cases of the test262 suite. These invariant breakages make some of them fail.

Maybe vm's contextified object are meant to be "magical" and I should stop reporting these issues. But then I feel this information would be a worthwhile addition to the doc.

Additional information

No response

lachrist avatar Apr 28 '24 17:04 lachrist

I'm not able to reproduce on the latest main. It looks like it's already been fixed, but the fix has not landed on 20.x.

aduh95 avatar Apr 28 '24 19:04 aduh95

Can confirm this repros on v20.12.2 but not on v22.0, I recall a fix (by @fhinkel ?) for something in that space a few months ago.

benjamingr avatar Apr 28 '24 20:04 benjamingr

Same here, this issue does not show on my setup on 22.0.0. I wrongly assumed it would have because of https://github.com/nodejs/node/issues/52720

lachrist avatar Apr 29 '24 08:04 lachrist

The error is swallowed on v22.0.0 with https://github.com/nodejs/node/commit/d9c47e9b5f1f4e8fbcd178ae8c8341321404e725. The getter is still getting invoked and side effects are observable.

This was fixed in https://github.com/nodejs/node/pull/53517 since v22.5.0.

legendecas avatar Sep 03 '24 10:09 legendecas