fix(web): return plain objects made in a same-origin iframe from evaluate
Closed
SA
2d ago+104+104+012345678901234567890123456789-4-4-0123456789
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'sObject.prototype. An iframe has its own realm, so itsObject.prototypeis a different object.Array.isArrayworks across realms, which is why arrays passed.- The encoder now also accepts another realm's
Object.prototype: a prototype with anullprototype whose ownconstructoris a function with ownname'Object'and ownprototypepointing 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
getPrototypeOforgetOwnPropertyDescriptortrap does run, so the check treats a throw as "not anObject.prototype". Either way the result staysINVALID_ARGUMENT, as onmain, and never becomesEVALUATE_FAILED. - The evaluating frame's own
Object.prototypestill matches by identity first, so a page that renamedObjectkeeps working. - A
Date, aMap, an element, a class instance (a class namedObjectincluded),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 (anode:vmrealm) that fails when the guard is removed. The integration test uses a real Chromiumsrcdociframe. - A page could still forge its own
function Objectwhoseprototypepoints at a null-rooted object. Such an object is accepted and encodes as JSON of its own data, which is harmless. Rejecting it would needFunction.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/webbuilt from each branch,e2e runon a scratch test that appends asrcdociframe and returnscontentWindow.eval('({ mode: "dark", items: [1, 2] })')throughbrowser.evaluate. The test also checks that[1, 2]passes and thatnew Date(0)still fails withINVALID_ARGUMENT. - The new unit and integration tests fail on
main'sevaluation.tsand pass on this branch.
| main | this branch |
|---|---|
× tests/zz-cross-realm.e2e.ts:14 > evaluate returns a plain object made in a same-origin iframeINVALID_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