Repository navigation
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Own service-worker CDP registrations and retry failed teardown. Admit redaction and network metadata against remaining capture capacity, retain explicit omission reasons, and preserve source URL, redirect ancestry, caller-selected content, and historical capture values. Forward CLI cancellation through provider cleanup. Reviewed fixes: #2263, #2637, #2767, #2845, #2859, #2863, #2872, #2874, #2876, #2879, #2955, #2957, #2961, #3033. Validation: 25 current event/network source tests passed; earlier focused browser lanes passed 201 source tests and 176 boundary tests with two skips, plus 35 Python tests. Real browser/Windows lanes were not rerun. Playwright allocation before admission remains unbounded by these retention limits.
|
CI-fix track: this change is already on main via f690571, which lists #2957 among its reviewed fixes. After merging current main and resolving the two conflicts, every code, script and test file matches main: the attached-browser service-worker |
|
Thanks for tracing this through main and checking the cleanup behavior. I verified that f6905719c incorporates this fix, including the owned registration policy, live-frame installation checks, bounded and cancellable connection setup, and the service-worker verifier modules. The session still releases its owned policy before disconnecting; main also includes the additional event-finalization handling. That covers the outcome of #2956: attached I'm closing this PR as superseded by the implementation on main and marking #2956 as completed. Thanks for incorporating the fix and its regression coverage. |
Summary
An attached browser scenario could accept
service_workers: "block"while collecting a page controlled by an existing worker. The admission check ran against the old document before navigation; starting fromabout:blanktherefore bypassed registrations belonging to the actual destination. The same policy also left a nonconfigurable register replacement in the caller's live document after disconnect.This change validates the current frames before accepting their capture and gives the temporary registration wrapper an explicit owner. Worker-influenced captures are rejected, caller-owned registrations survive, and cleanup restores the property state the scenario replaced. Cleanup removes the owned future-document scripts before restoring live frames, and cancelled navigation is stopped even when the scenario has no storage seeds.
Fixes #2956
Problem and expected behavior
The smallest trigger is an external Chrome profile with a worker registered for a local origin, whose selected tab has moved to
about:blank. A block scenario reconnects to that tab and navigates back to the origin. Previously,scenario_startand the following wait step completed even thoughnavigator.serviceWorker.controllerwas non-null and a probe request returned a worker-generated response.The existing policy already treats caller-owned registrations as a reason to reject attachment rather than something REA may remove. That decision must apply to the documents actually observed, including destinations reached by later actions and child frames. A block request should not become silently permissive after the first navigation.
Policy cleanup has the corresponding ownership invariant: a scenario may temporarily replace registration behavior, but must release its own replacement when it disconnects. The original descriptor, a deliberately absent property, and a subsequent caller replacement are distinct states. Cleanup must preserve that distinction rather than always assigning the browser's native method.
Change and scope
PlaywrightScenarioServiceWorkersowns the existing attached registration policy. It checks controller and registrations in every current frame before installation. Each wrapper keeps the original descriptor inside its closure; its public symbol marker contains only an ownership boolean. A restoration function requires a separate, randomly generated session token and does not return the original descriptor. Enumerating the wrapper's symbols therefore does not expose the native registration method. The wrapper remains configurable so its owner can restore it.The owner reuses the existing CDP endpoint and target-session helpers to attach to the caller-selected page. It installs a tracked
Page.addScriptToEvaluateOnNewDocumentscript in that page and its out-of-process iframe targets. Iframe-only auto-attachment preserves pre-script registration blocking across renderer boundaries; workers and popups are excluded. Each owned script has its own protocol identifier. A frame is resumed after installation, including when installation fails, and repeated installation in the same realm recognizes the existing wrapper. The owner also verifies the actual registration wrapper in the current realm before accepting initialization or capture. A CDP script identifier alone is insufficient: if the page prevents property replacement, such as through a non-configurable descriptor, attached block observation is rejected instead of silently accepting an uninstalled policy.PlaywrightScenarioSessioncreates the owner only for attached block scenarios. It validates immediately after initial navigation and at the shared capture boundary used after each action. This catches ordinary goto, click navigation, and embedded-frame cases without distributing URL-specific guards through the action implementation. An explicit allow request does not create this owner. Launch continues using the existing Playwright-owned context policy.The capture check deliberately rejects an existing registration even if its controller is currently null. Registration existence and current control are separate browser facts; either is already incompatible with the attached block admission rule. No worker is unregistered to make the check pass.
Cleanup restores a property only when its current function is still the wrapper saved in that realm's closure. If the caller replaced register afterward, that replacement wins. If the original property did not exist, restoration deletes the temporary property rather than inventing a descriptor. The ownership marker is used to detect unresolved restoration, not to store the original method. A wrong restoration token leaves the block in place.
Policy setup has its own abort controller and uses the existing 10-second CDP discovery deadline across the initial read-only frame check, endpoint discovery, WebSocket handshake, and target attachment. Closing the owner aborts that setup lifetime before awaiting initialization. A peer that never sends the WebSocket upgrade response therefore cannot hold initialization open indefinitely, and a stalled frame preflight cannot start a late attachment after cleanup.
That setup signal is deliberately not passed to
Page.addScriptToEvaluateOnNewDocument: once a page mutation has been sent, the owner must receive its script identifier and remove that exact script before releasing ownership. Cleanup first closes admission for new policy installation and waits for already-sent installation requests to settle. For cancellation or failed navigation, this policy owner sendsPage.stopLoadingindependently of storage seeding. Restoration does not wait for the page or a child frame to emitload, so an image or child response that never finishes cannot strand cleanup behind that event.The owner then removes every tracked future-document script before restoring any live frame. A main-frame navigation during child-frame restoration consequently creates a document without the wrapper. Caller-owned init scripts remain installed. Finally, cleanup detaches the owned target session and closes its socket before the scenario releases its transport and target lease. It uses the existing execution boundary and retains the owner when protocol or live-document restoration cannot be confirmed. Retry preserves completed cleanup stages rather than attempting to detach a session that was already released. Initialization-failure cleanup also awaits the primary browser owner using its independent cleanup lifetime, rather than the already-aborted scenario signal. Confirmed release then makes the same selected target available to a subsequent scenario.
The seven files have specific responsibilities:
src/browser/PlaywrightScenarioServiceWorkers.tsowns bounded and cancellable setup, frame checks, private registration state, page/iframe script identifiers, stopped navigation, exact property restoration, and cleanup retry state.src/browser/PlaywrightScenarioSession.tsconnects that owner to initialization, initial navigation, per-step capture, and cleanup.src/browser/PlaywrightScenarioSession.test.tscovers restoration failures, cancellation during pending installation or a stalled preflight, suppression of late setup, stopped navigation without storage seeds, installation-before-restoration ordering, and retry after protocol cleanup failure.scripts/lib/browser-scenario-service-workers-e2e.mjsexercises the policy against actual Chromium, including caller ownership and future-document cleanup.scripts/lib/browser-scenario-service-worker-setup-e2e.mjsuses a real loopback proxy to withhold policy discovery or a WebSocket upgrade, then checks connection release and reuse of the selected target.scripts/verify-real-browser.mjsincludes that verifier in the existing browser lane.docs/browser-scenario-contract.mdstates the attached rejection rule and its navigation boundary.Contribution scope follows the existing browser scenario workflow. A separate maintainer scope decision is not applicable: this is a correction to existing policy enforcement and cleanup, with the same selected target, navigation authority, and temporary registration injection. It adds no general script-execution operation or worker-removal capability.
Contract and boundary impact
target_not_allowedfailure.Expected effects reflect the existing workflow and its verification:
The checked mutation effect is the temporary JavaScript registration replacement already present in this workflow, plus the existing disposable profile behavior in verification. Ordinary browsing contacts the requested application's services. No protected-code recovery or new execution authority is introduced.
Evidence and regression coverage
timeout, closes the withheld socket, and successfully reuses the target. The direct production policy owner additionally passes a stalled browser-WebSocket handshake case; this is owner-level coverage and adds no endpoint variant to the public scenario schema.ebaaa79ca02bde4a73013a2a9a781d0b07a57ee4, with unchanged supporting production modules and a real local worker. Block returned completed steps while the controller remained active; the wrapper remained nonconfigurable after disconnect. This establishes the affected Session behavior rather than claiming a complete baseline qualification run.browser.cleanup: "disconnected-external". For the worker-controlled destination, the production provider now returnsBrowserObservationErrorwith reasontarget_not_allowed, retaining cleanup and partial-observation semantics instead of returning completed capture steps. Shared-provider behavior applies beneath CLI/MCP; direct MCP execution is not claimed for this new regression.For evidence-bearing changes:
Validation performed
The final focused checks used Node.js 24.18.0 and Chrome 154.0.8037.98 on Windows x64:
npm run test:focused -- src/browser/PlaywrightScenarioSession.test.ts src/browser/PlaywrightScenarioBrowser.test.ts src/browser/PlaywrightBrowserScenarioProvider.test.ts— 41 tests passed.npm run docs:check— passed, including 5 README consistency tests.npm run typecheck,npm run lint, andnpm run knip— passed.npm run check— passed on upstream base8fef2aa64ec1ee742e8b70b825938d30a0ea7319with this independent fix applied.npm run verify:test-discoveryandnpm run verify:architecture-guards— passed.oxfmt --checkandgit diff --check— passed.The executed focused real-browser harness invoked the new service-worker verifier module with the built CLI and production provider. It passed the observed policy and ownership cases listed above using self-owned loopback fixtures and an isolated disposable Chrome profile. Caller registrations and the external target survived scenario cleanup. The complete
verify:browserlane is not reported as passing qualification.Compatibility, safety, and release
Before merge
type(scope): outcome.