Repository navigation
fix(web): return plain objects made in a same-origin iframe from evaluate - #2102
samanyudas wants to merge 3 commits into
Conversation
…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>
There was a problem hiding this comment.
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
|
/ready |
okwasniewski
left a comment
There was a problem hiding this comment.
[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-isObjectPrototypenow runs page-defined Proxy traps (getPrototypeOf,getOwnPropertyDescriptor) on the prototype. Example:Object.create(new Proxy({}, { getPrototypeOf() { throw new Error('x'); } }))gaveINVALID_ARGUMENT: evaluate result must be JSON-safeon main. Here it givesEVALUATE_FAILED: evaluate result cannot be read. That breaks the PR's stated invariant that the check cannot turn the error intoEVALUATE_FAILED. Rare, but the fix is cheap: wrap the cross-realm branch intry { ... } catch { return false; }. Optionally add the Proxy case to theit.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 --noEmitand 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/toHaveJSPropertysharepageValueEncoder, so they pick up the fix). Also clean into #1503, where the change auto-lands inin-page/evaluation.ts. #1503's version ofevaluate.test.tshas no Playwrightpage, though, so the two new integration tests will need porting tosession.setContentthere (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>
There was a problem hiding this comment.
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
|
Note 🤖 Claude Opus 5.5 responding on behalf of samanyudas Both items from the review at
Verified at |
|
/ready |
michal-e2e
left a comment
There was a problem hiding this comment.
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
Objectthat extendsnullnow encodes as{}. On the merge base it was rejected withevaluate result must be JSON-safe. The class's prototype has a null parent and an ownconstructornamedObjectthat points back to it, soisObjectPrototypereads it as another frame'sObject.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.toStringgivesfunction Object() { [native code] }for any realm'sObjectand the class body for this one. That check is one more line in thetry. Otherwise a sentence in the docs that this shape is accepted would do. -
The comment on
ownsays it never calls a getter.getOwnPropertyDescriptor(...)?.valuereadsvaluefrom the descriptor object, which inherits from the page'sObject.prototype. If the page defines avaluegetter 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 accessorconstructor. Both commits still reject that value with the same error. Reading through'value' in descriptororhasOwn(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
srcdociframe and returnscontentWindow.eval('({ mode: "dark", nested: { data: [1, 2] } })')throughbrowser.evaluate. On the merge base it fails withINVALID_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'snew 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 (
vmrealms). 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 testhad 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 neitherpackages/mobilenor 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 existingencodes a null-prototype objecttest 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.evaluateof 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 prototyperejected rejected, same code and message
Not run
- Android mobile benchmark: I ran iOS only.
pnpm test:mobile-benchmark --target android-emulatorwould 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-simulatorat970b74ed4.
For the maintainer
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>
|
Note 🤖 Claude Opus 5.5 responding on behalf of samanyudas
Verified at |
There was a problem hiding this comment.
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
|
/ready |
michal-e2e
left a comment
There was a problem hiding this comment.
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_ARGUMENTon 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
Objectthat extendsnullstill 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
left a comment
There was a problem hiding this comment.
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)
| if (getPrototypeOf(prototype) !== null) return false; | ||
| const constructor = own(prototype, 'constructor'); | ||
| return typeof constructor === 'function' | ||
| && own(constructor, 'name') === 'Object' | ||
| && own(constructor, 'prototype') === prototype; |
There was a problem hiding this comment.
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} |
|
Note This text was drafted with AI and posted after human approval.
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 |
browser.evaluatefailed withINVALID_ARGUMENT: evaluate result must be JSON-safewhen 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.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.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.Object.prototypestill matches by identity first, so a page that renamedObjectkeeps working.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.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.evaluateOnandtoHaveJSPropertyfrom #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
@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.main'sevaluation.tsand pass on 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-safeTests 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