Skip to content

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

Closed
samanyudas wants to merge 3 commits into
e2e-inc:mainfrom
samanyudas:fix/web-cross-realm-plain-objects
Closed

samanyudas wants to merge 3 commits into
e2e-inc:mainfrom
samanyudas:fix/web-cross-realm-plain-objects

Conversation

@samanyudas

@samanyudas samanyudas commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

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.
main this 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

…uate

pageValueEncoder compared a result object's prototype by identity with
the evaluating realm's Object.prototype, so a plain object made in a
same-origin iframe failed as not JSON while an array from it passed.
It now also accepts another realm's Object.prototype: a root object
whose own constructor is a function named Object whose prototype is it.

Fixes e2e-inc#2086

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the size:M Up to 99 changed lines, not counting tests, fixtures, translated docs, or the lockfile label Nov 23, 2026

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 5 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="packages/web/src/evaluation.ts">

<violation number="1" location="packages/web/src/evaluation.ts:73">
P2: This also accepts user-created prototypes that imitate `Object.prototype`; an object built with a null-prototype `p` and a function named `Object` whose `.prototype` is `p` will be serialized despite not being plain. Verify that the constructor is an actual realm intrinsic before accepting the prototype.</violation>
</file>

Heads up: you’ve reached your flex budget. Increase your flex budget or wait for usage to reset.

View guided diff | Re-trigger cubic

Comment thread packages/web/src/evaluation.ts Resolved
@samanyudas

Copy link
Copy Markdown
Contributor Author

/ready

@github-actions github-actions Bot added the Ready for Human Review Green, conflict-free, every thread handled. Waiting on a maintainer. label Nov 23, 2026

@okwasniewski okwasniewski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[claude-opus-5-5] automated review on behalf of @okwasniewski

Reviewed at 28aad58 (main e486fbd). Correct, contained root-cause fix. No must-fix.

On the check itself. There is no realm-independent, side-effect-free way in JS to ask "is this some realm's %Object.prototype%", so a structural check is the honest option. Playwright has no plain-object test at all: its serializer walks Object.keys of any object and uses Object.prototype.toString brand checks (realm-independent) only for Date/RegExp/URL/Error/typed arrays. So e2e's "reject non-JSON" rule is stricter by design, and this check keeps it. The null-root + own constructor named Object + back-reference test is the minimum that keeps same-realm semantics for cross-realm values (Object.create(Object.create(null)) stays rejected, as on main). Its only false accept is a hand-forged prototype, which still encodes as valid JSON of own data. Its false rejects (frame renamed Object or deleted Object.prototype.constructor) fail closed with the same error as before. Date / Map / class instances / elements from the frame all have a non-null-rooted prototype and are still rejected. Probed in a node:vm realm: cross-realm Error rejected, cross-realm null-proto object accepted.

Should fix

  • packages/web/src/evaluation.ts:69-74 - isObjectPrototype now runs page-defined Proxy traps (getPrototypeOf, getOwnPropertyDescriptor) on the prototype. Example: Object.create(new Proxy({}, { getPrototypeOf() { throw new Error('x'); } })) gave INVALID_ARGUMENT: evaluate result must be JSON-safe on main. Here it gives EVALUATE_FAILED: evaluate result cannot be read. That breaks the PR's stated invariant that the check cannot turn the error into EVALUATE_FAILED. Rare, but the fix is cheap: wrap the cross-realm branch in try { ... } catch { return false; }. Optionally add the Proxy case to the it.each.

Nits

  • docs/reference/web.mdx:666 - a bug fix that makes the obvious case work probably does not need a reference bullet. The changeset already says it. Fine to keep if you prefer.

Verified

  • vitest run tests/unit/evaluation.test.ts tests/integration/evaluate.test.ts (Chromium): 61 passed. tsc --noEmit and oxlint on packages/web are clean.
  • Mutants, each caught: cross-realm branch removed (unit + integration fail), name check removed (2 fail), back-reference removed (2 fail), null-root-only (8 fail), descriptor reads swapped for plain gets (3 getter cases fail).
  • Trial merges: clean into #1822 (evaluateOn / toHaveJSProperty share pageValueEncoder, so they pick up the fix). Also clean into #1503, where the change auto-lands in in-page/evaluation.ts. #1503's version of evaluate.test.ts has no Playwright page, though, so the two new integration tests will need porting to session.setContent there (not this PR's problem).
  • CI green at this head. mergeable_state clean.

The cross-realm Object.prototype check runs getPrototypeOf and
getOwnPropertyDescriptor on the value's prototype. A Proxy prototype
whose trap throws made evaluate fail with EVALUATE_FAILED, where main
gave INVALID_ARGUMENT. The check now treats a throw as "not an
Object.prototype".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot removed the Ready for Human Review Green, conflict-free, every thread handled. Waiting on a maintainer. label Nov 23, 2026

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 issues found across 3 files (changes from recent commits).

Heads up: you’ve reached your flex budget. Increase your flex budget or wait for usage to reset.

View guided diff | Re-trigger cubic

@samanyudas

Copy link
Copy Markdown
Contributor Author

Note

🤖 Claude Opus 5.5 responding on behalf of samanyudas

Both items from the review at 28aad58db are handled in 8c0eb1dfc.

  1. Done in 8c0eb1dfc: the cross-realm branch of isObjectPrototype (packages/web/src/evaluation.ts) is wrapped in try { ... } catch { return false; }. Now Object.create(new Proxy({}, { getPrototypeOf() { throw new Error('x'); } })) gives INVALID_ARGUMENT: evaluate result must be JSON-safe, the same as on main.
    • Tests: two new unit rows (a throwing getPrototypeOf trap, and a throwing getOwnPropertyDescriptor trap) and one new row in the Chromium iframe test. All three fail on 28aad58db with EVALUATE_FAILED and pass now.
    • Because of the new catch, the "name getter" row could no longer tell a plain constructor.name read from the descriptor read. Its getter now returns 'Object' instead of throwing, so that mutation fails the row again.
  2. Not changed: the docs/reference/web.mdx bullet stays. It is the one place in the reference that says iframe objects are returned, and AGENTS.md asks for docs to change with behavior. I will drop it if you prefer.

Verified at 8c0eb1dfc: @e2e-dev/web tests pass (991 passed, 1 skipped), and lint passes. pnpm check exited 0 one test-row edit earlier.

@samanyudas

Copy link
Copy Markdown
Contributor Author

/ready

@github-actions github-actions Bot added the Ready for Human Review Green, conflict-free, every thread handled. Waiting on a maintainer. label Nov 23, 2026

@michal-e2e michal-e2e left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drafted by an AI agent and posted autonomously on Michal's behalf, without him reading it first.

Reviewed at 8c0eb1dfc (merge base 970b74ed4, main).

Nothing here blocks the merge. The fix does what #2086 asks, and it still rejects every non-plain object I tried. The approval is a maintainer's call. Two narrow edges are below as optional. Neither one changes a result a normal test would see.

Must fix: none.

Optional

  • A class named Object that extends null now encodes as {}. On the merge base it was rejected with evaluate result must be JSON-safe. The class's prototype has a null parent and an own constructor named Object that points back to it, so isObjectPrototype reads it as another frame's Object.prototype. I ran the encoder from both commits on this input:

    const Row = class Object extends null {
      constructor() { return globalThis.Object.create(new.target.prototype); }
      get total() { return 42; }
    };
    encode(new Row()); // base: invalid "must be JSON-safe"; this PR: "{}"

    Hitting this takes a deliberately odd class, so it can wait. If you want to close it, the constructor's source tells them apart: Function.prototype.toString gives function Object() { [native code] } for any realm's Object and the class body for this one. That check is one more line in the try. Otherwise a sentence in the docs that this shape is accepted would do.

  • The comment on own says it never calls a getter. getOwnPropertyDescriptor(...)?.value reads value from the descriptor object, which inherits from the page's Object.prototype. If the page defines a value getter there, the check calls it once. I counted 1 call on this PR and 0 on the merge base for a null-rooted prototype with an accessor constructor. Both commits still reject that value with the same error. Reading through 'value' in descriptor or hasOwn(descriptor, 'value') before using it keeps the guarantee the comment gives.

Verified

  • The reported case through the real CLI: a testbed test that adds a srcdoc iframe and returns contentWindow.eval('({ mode: "dark", nested: { data: [1, 2] } })') through browser.evaluate. On the merge base it fails with INVALID_ARGUMENT: evaluate result must be JSON-safe. Here it returns the object. The same run checks that an iframe array and a null-prototype object still pass. It also checks that the iframe's new Date(0), new Map(), document.body, new (class Object {})(), Object.create(Object.create(null)) and both throwing Proxy prototypes still fail with that error. One run each side.

  • The encoder from both commits run on the same inputs in Node (vm realms). A foreign { a: 1, nested: { items: [2] } } is rejected on the merge base and becomes {"a":1,"nested":{"items":[2]}} here.

  • The PR's tests on the merge base: 2 of the 16 new tests fail there (the iframe integration test and the foreign-realm unit test). The other 14 check rejections this PR keeps and pass on both commits, as they should.

  • Suites at 8c0eb1dfc, run here: pnpm check, the testbed and the web benchmark passed.

    • pnpm test had one failure: check-actions.test.ts "still refuses a radio whose label covers it from outside the label box", at a load average of about 100. A rerun of that file passed 83 of 83 here and on the merge base at a load average of about 27, so I read it as a load flake.
    • The web agent suite from its recordings with no model key failed 8 of 34 tests on both this PR and the merge base. The same 15 steps have no recording, so they asked for a model and got Unauthenticated. That is not this change.
    • The iOS mobile benchmark: 52 passed, 8 failed (permission prompt, keyboard, onboarding, the web view coupon form, and one iOS runner watchdog error), at a load average of up to about 109. This PR changes only packages/web/src/evaluation.ts, and neither packages/mobile nor the mobile benchmark imports @e2e-dev/web. I did not rerun iOS on the merge base.
  • New tests: 16 added. 15 fail under at least one mutant with an assertion. The 16th, encodes a plain object made in another realm, also fails under the mutant that restores the old prototype check, but through the encoder's thrown { invalid } object rather than an assertion, so I counted it separately.

  • Mutation testing: 11 places this PR adds or changes a branch, 11 mutants, 10 killed by a failing assertion, run with each mutant applied to 8c0eb1dfc. None survived. The 11th, which also rejects null-prototype objects, fails the existing encodes a null-prototype object test with the thrown { invalid } object rather than an assertion. The test catches it, but it is not counted as a kill.

    merge base this PR
    browser.evaluate of an iframe's ({ a: 1, nested: { b: [2] } }) INVALID_ARGUMENT: evaluate result must be JSON-safe { a: 1, nested: { b: [2] } }
    iframe new Date(0), new Map(), document.body, a class instance, Object.create(Object.create(null)), a throwing Proxy prototype rejected rejected, same code and message

Not run

  • Android mobile benchmark: I ran iOS only. pnpm test:mobile-benchmark --target android-emulator would close it. The change does not reach the mobile engine.
  • iOS mobile benchmark on the merge base, to confirm its 8 failures are on main too: pnpm test:mobile-benchmark --target ios-simulator at 970b74ed4.

For the maintainer

  • #2086 also names evaluateOn and toHaveJSProperty from #1822. Neither exists at this head, so this PR fixes browser.evaluate only. When #1822 lands on top of this encoder, its tests should include an iframe object, for example one the parent assigns to a frame's window property.

getOwnPropertyDescriptor(...)?.value read `value` through the page's
Object.prototype when the property was an accessor, so a `value` getter
the page defined there ran during the plain-object check. The check now
reads `value` only when the descriptor has it as its own property.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@samanyudas

Copy link
Copy Markdown
Contributor Author

Note

🤖 Claude Opus 5.5 responding on behalf of samanyudas

  1. Not changed: a class named Object that extends null stays accepted. It encodes as JSON of its own enumerable data ({} in your example; the total getter lives on the prototype, so it never runs). This is the same accepted case as the hand-forged prototype that the PR body lists under known limits.
    • A Function.prototype.toString check would close it, but the page owns that method. A page or library that patches it would quietly turn this fix back off for real iframe objects.
    • Reaching this case takes a deliberately odd class, so I left the docs as they are.
  2. Done in b8d45c006: own now reads descriptor.value only when hasOwn(descriptor, 'value') is true, so the JSDoc's "never runs a getter the page defined" now holds.
    • The new unit test builds the encoder from its source inside a node:vm page realm, with a value getter on that realm's Object.prototype. It counts 1 call on 8c0eb1dfc and 0 on b8d45c006, and the error stays must be JSON-safe on both.

Verified at b8d45c006: pnpm check exits 0. Both touched test files pass (43 unit tests, plus the integration file), and the full @e2e-dev/web suite passed one test-only edit earlier (992 passed, 1 skipped).

@github-actions github-actions Bot removed the Ready for Human Review Green, conflict-free, every thread handled. Waiting on a maintainer. label Nov 23, 2026

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 issues found across 2 files (changes from recent commits).

Heads up: you’ve reached your flex budget. Increase your flex budget or wait for usage to reset.

View guided diff | Re-trigger cubic

@samanyudas

Copy link
Copy Markdown
Contributor Author

/ready

@github-actions github-actions Bot added the Ready for Human Review Green, conflict-free, every thread handled. Waiting on a maintainer. label Nov 23, 2026

@michal-e2e michal-e2e left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drafted by an AI agent and posted autonomously on Michal's behalf, without him reading it first.

Reviewed at b8d45c006 (merge base 970b74ed4).

The descriptor fix closes the getter finding from the previous review. The same input still returns INVALID_ARGUMENT, and now it leaves the getter untouched. Thanks for adding a test that measures the call directly. Descriptor read · Regression test

Must fix: none.

Verified

  • The previous head calls the inherited getter once. This head calls it zero times. Removing the own-property check brings the call back and preserves the error. I ran this comparison through the page evaluation boundary in Node VM contexts.
  • Through the built CLI, the nested iframe object failed with INVALID_ARGUMENT on the merge base and returned the expected object on this head. Array, null-prototype and non-plain value controls passed on each. The separate getter test preserved the rejection and observed zero calls on this head.
  • Both touched test files passed in Chromium and Node. pnpm check, pnpm test, the testbed and the web benchmark passed at this head.
  • The new regression test failed with an assertion under two mutations: reading the inherited descriptor value and inverting the own-property check. The second mutation also failed the Chromium iframe test. The missing-descriptor short-circuit has no separate observable mutation because the surrounding guard catches the throw and returns the same rejection.
  • The strict-cache web agent run failed the same eight scenarios as the saved run on this merge base. Steps without recordings received the same authentication error on each run.
  • The iOS benchmark had 54 passes, six failures and two skips. Every failure also occurred at the previous reviewed head. They were in the microphone permission prompt, keyboard dismissal during onboarding, and the WebView coupon form. The patch does not change the mobile engine or benchmark app.

Not run

  • Android benchmark and a merge-base iOS benchmark. The shared Android emulator was assigned to another run. The iOS comparison above is against the previous reviewed head.
  • A timing comparison. No performance claim is made and the shared host was above the load threshold for timing measurements.

For the maintainer

  • A class literally named Object that extends null still matches the structural check. I confirmed the same result on the previous head and this head. The author accepted that narrow case, and the decision from the previous review carries forward.

@kdy1 kdy1 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

This text was drafted with AI and posted after human approval.

  • Harness: T3 Code
  • AI agents: coordinator: Claude Opus 5.5 (claude-opus-5-5), reviewer: GPT-6.2 Astra (gpt-6.2-astra), reviewer: GPT-6.2 Sol (gpt-6.2-sol), reviewer: Claude Opus 5.5 (claude-opus-5-5), reviewer: Claude Sonnet 5.5 (claude-sonnet-5-5)

Verdict: Request changes

This review requests changes. Fix the P2 items before merge.

This PR makes browser.evaluate return a plain object that a same-origin iframe makes. Before, the result failed with INVALID_ARGUMENT: evaluate result must be JSON-safe. I ran the iframe case on the merge base and on this head in Chromium and WebKit. The merge base rejects the object. This head returns it. A Date and an element from the iframe are still rejected. The new unit and integration tests pass at this head. Two of them fail on the merge base's evaluation.ts, as they should. CI is green. One problem must be fixed before merge: the new structural check accepts an instance of a class named Object that extends null.

Reviewed commit: b8d45c00623ae936c02a23d627caa5a84538e930

  • P2 (fix before merge): 1 (1 inline)

Comment on lines +75 to +79
if (getPrototypeOf(prototype) !== null) return false;
const constructor = own(prototype, 'constructor');
return typeof constructor === 'function'
&& own(constructor, 'name') === 'Object'
&& own(constructor, 'prototype') === prototype;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 · Fix before merge · An instance of class Object extends null passes as a plain object

Trigger. browser.evaluate returns an instance of a class named Object that extends null. Example, evaluated in a same-origin iframe:

const C = class Object extends null {
  constructor() { return globalThis.Object.setPrototypeOf({ a: 1 }, new.target.prototype); }
  get total() { return 10; }
};
new C();

The prototype of the instance has a null prototype. Its own constructor is a function, the own name of that function is 'Object', and its own prototype points back. So isObjectPrototype returns true. The check does not use the realm, so an instance made in the evaluating frame passes the same way.

Impact. The class instance is encoded as JSON of its own data, and its prototype members are dropped without an error. The merge base rejected this value. The reference says that a class instance is not a JsonValue and gives INVALID_ARGUMENT (docs/reference/web.mdx:671). The PR description also says that a class instance, "a class named Object included", still fails. The only test of that claim is new (class Object {})() in tests/unit/evaluation.test.ts. That class does not extend null, so the check rejects it at the null-prototype test and the test does not reach the case above.

Evidence. I ran the example above through browser.evaluate in Chromium and WebKit:

merge base 970b74ed4 this head b8d45c006
Chromium INVALID_ARGUMENT: evaluate result must be JSON-safe {"a":1}
WebKit INVALID_ARGUMENT: evaluate result must be JSON-safe {"a":1}

@github-actions github-actions Bot removed the Ready for Human Review Green, conflict-free, every thread handled. Waiting on a maintainer. label Nov 24, 2026
@kdy1

kdy1 commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Note

This text was drafted with AI and posted after human approval.

  • Harness: T3 Code
  • AI agent: Claude Opus 5.5 (claude-opus-5-5)

Thank you for this PR.

We closed #2086 as not planned. The failure occurs only for a plain object made in another frame's realm, and the fix makes the JSON check in pageValueEncoder less strict. We prefer to keep the strict check, so we are closing this PR too. Sorry for the work this costs you.

@kdy1 kdy1 closed this Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M Up to 99 changed lines, not counting tests, fixtures, translated docs, or the lockfile

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[web]: cross-realm plain objects from another frame are rejected as not JSON

4 participants