fix(web): return plain objects made in a same-origin iframe from evaluate

Closed
SA
2d ago
+104-4

browser.evaluate failed with INVALID_ARGUMENT: evaluate result must be JSON-safe when the result was a plain object made in a same-origin iframe, while an array from the same iframe passed.

  • pageValueEncoder (packages/web/src/evaluation.ts) compared each object's prototype by identity with the evaluating frame's Object.prototype. An iframe has its own realm, so its Object.prototype is a different object. Array.isArray works across realms, which is why arrays passed.
  • The encoder now also accepts another realm's Object.prototype: a prototype with a null prototype whose own constructor is a function with own name 'Object' and own prototype pointing back to it. This is the check suggested in the issue.
  • It reads those three properties as own data descriptors, so a getter on them never runs. A Proxy prototype's getPrototypeOf or getOwnPropertyDescriptor trap does run, so the check treats a throw as "not an Object.prototype". Either way the result stays INVALID_ARGUMENT, as on main, and never becomes EVALUATE_FAILED.
  • The evaluating frame's own Object.prototype still matches by identity first, so a page that renamed Object keeps working.
  • A Date, a Map, an element, a class instance (a class named Object included), Object.create(Object.create(null)), and an object on a throwing Proxy prototype from the iframe still fail. Each guard in the check has a unit test (a node:vm realm) that fails when the guard is removed. The integration test uses a real Chromium srcdoc iframe.
  • A page could still forge its own function Object whose prototype points at a null-rooted object. Such an object is accepted and encodes as JSON of its own data, which is harmless. Rejecting it would need Function.prototype.toString, which the page can also replace.

evaluateOn and toHaveJSProperty from #1822 use the same encoder, so they get the fix once #1822 rebases. This PR does not depend on #1822.

Issue

Fixes #2086

Verified

Ran it locally: yes

  • Testbed, with @e2e-dev/web built from each branch, e2e run on a scratch test that appends a srcdoc iframe and returns contentWindow.eval('({ mode: "dark", items: [1, 2] })') through browser.evaluate. The test also checks that [1, 2] passes and that new Date(0) still fails with INVALID_ARGUMENT.
  • The new unit and integration tests fail on main's evaluation.ts and pass on this branch.
mainthis branch
× tests/zz-cross-realm.e2e.ts:14 > evaluate returns a plain object made in a same-origin iframe
INVALID_ARGUMENT: evaluate result must be JSON-safe
Tests 1 passed (1)

Reviewed with sol-loop: 2 rounds with gpt-6.2-sol. Sol verdict: approved

The second commit handles the Proxy trap case from the maintainer review. It was not re-reviewed by Sol.

Created with Claude Opus 5.5 in Claude Code, with review from GPT-6.2 Sol.

🤖 Generated with Claude Code