Repository navigation
feat(python): headers for local_browser.connect - #3224
Closed
sydney-runkle wants to merge 1 commit into
Closed
sydney-runkle wants to merge 1 commit into
sydney-runkle wants to merge 1 commit into
Conversation
local_browser.connect(cdp_url=..., headers=...) sends headers with the /json/version request and the CDP WebSocket handshake, so a gateway's credentials need not ride in the URL. Headers follow the advertised WebSocket only when it is on the same host as cdp_url, are never passed to the extension, are validated as RFC 9110 field names and values, and are never echoed in validation errors. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
🦋 Changeset detectedLatest commit: 9448aad The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Contributor
|
This PR is from an external contributor and must be approved by a stagehand team member with write access before CI can run. |
sydney-runkle
marked this pull request as ready for review
October 19, 2026 15:35
seanmcguire12
changed the base branch from
main
to
evals/sydney-runkle-connect-headers
October 19, 2026 20:03
seanmcguire12
changed the base branch from
evals/sydney-runkle-connect-headers
to
main
October 19, 2026 20:09
seanmcguire12
added a commit
that referenced
this pull request
Oct 19, 2026
) changes pulled in from #3224 , thanks @sydney-runkle for the contrib! ## Why `local_browser.connect` takes only a URL. To reach a CDP endpoint behind an authenticated gateway, the credentials have to go in the query string. URLs leak easily: into exception messages, tracebacks and proxy logs. So callers end up redacting them by hand. Playwright solves this with `connect_over_cdp(..., headers=...)`. ## What ```python browser = await local_browser.connect( cdp_url="https://gateway/.../cdp", headers={"X-Api-Key": "..."}, ) ``` - `headers` are sent with the `/json/version` request and with the CDP WebSocket handshake (`websockets`' `additional_headers`). - They follow the advertised `webSocketDebuggerUrl` only when it's on the same host and port as `cdp_url`, so a gateway can't redirect credentials elsewhere. They're never passed to the extension. - Names must be RFC 9110 tokens, and values may only contain visible characters, spaces and tabs, so CR/LF injection is rejected. `LocalBrowserConnectOptions` sets `hide_input_in_errors` so a rejected credential isn't echoed in the `ValidationError`. - When `headers` is unset, nothing changes: `CDPClient.connect` isn't even passed the argument. - Python SDK only, with a minor changeset. Independent of #3223; the two compose. ## Tests - New tests cover headers on `/json/version`, resolve passing them through, the same-host rule (6 cases), connect forwarding them only when given, and rejection of unsafe names and values without echoing them. - The existing `test_cdp_client` fakes now accept the new keyword argument. - `ruff format --check`, `ruff check`, `ty check` and `generate.py --check` pass. - `pytest`: 798 passed. `test_official_api_routes_through_and_never_closes_a_caller_http_client` fails with and without this change, because it needs the built extension in `packages/extension/dist`. - **Live:** against a LangSmith browser sandbox's CDP gateway, `/json/version` with an `X-Api-Key` header returned a credential-free WebSocket URL, the header-authenticated WebSocket reached Chrome (`Browser.getVersion`), and the same WebSocket without the header was refused. 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Adds optional `headers` to `local_browser.connect` in the Python SDK so callers can authenticate against CDP gateways without putting credentials in the URL. - Headers go to the `/json/version` request and the CDP WebSocket handshake, and only to endpoints on the same host and port as `cdp_url`; redirects off-host or to insecure endpoints are refused rather than followed. Headers are never passed to the extension. - Header names and values are validated against RFC 9110 rules, and validation errors never echo header values since they may be credentials. - Discovery that returns 401 or 403 now fails immediately with a message pointing at gateway credentials instead of retrying. - When `headers` is unset, nothing changes: the keyword argument isn't passed to `CDPClient.connect`, and redirect behavior is unchanged. <sup>Written for commit 564690c. Summary will update on new commits.</sup> <a href="https://cubic.dev/pr/browserbase/stagehand/pull/3260?utm_source=github" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="View guided diff" src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a> <a href="https://www.cubic.dev/action/auto-fix/pr/browserbase/stagehand/3260?returnTo=https%3A%2F%2Fgithub.com%2Fbrowserbase%2Fstagehand%2Fpull%2F3260&source=description" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/turn-on-auto-fix-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg"><img alt="Turn on auto-fix" src="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg"></picture></a> <!-- End of auto-generated description by cubic. --> --------- Co-authored-by: Sydney Runkle <[email protected]> Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
local_browser.connecttakes only a URL. To reach a CDP endpoint behind an authenticated gateway, the credentials have to go in the query string. URLs leak easily: into exception messages, tracebacks and proxy logs. So callers end up redacting them by hand. Playwright solves this withconnect_over_cdp(..., headers=...).What
headersare sent with the/json/versionrequest and with the CDP WebSocket handshake (websockets'additional_headers).webSocketDebuggerUrlonly when it's on the same host and port ascdp_url, so a gateway can't redirect credentials elsewhere. They're never passed to the extension.LocalBrowserConnectOptionssetshide_input_in_errorsso a rejected credential isn't echoed in theValidationError.headersis unset, nothing changes:CDPClient.connectisn't even passed the argument.Tests
/json/version, resolve passing them through, the same-host rule (6 cases), connect forwarding them only when given, and rejection of unsafe names and values without echoing them.test_cdp_clientfakes now accept the new keyword argument.ruff format --check,ruff check,ty checkandgenerate.py --checkpass.pytest: 798 passed.test_official_api_routes_through_and_never_closes_a_caller_http_clientfails with and without this change, because it needs the built extension inpackages/extension/dist./json/versionwith anX-Api-Keyheader returned a credential-free WebSocket URL, the header-authenticated WebSocket reached Chrome (Browser.getVersion), and the same WebSocket without the header was refused.🤖 Generated with Claude Code
Summary by cubic
Adds optional
headerstolocal_browser.connectin the Python SDK so callers can authenticate against CDP gateways without putting credentials in the URL./json/versionrequest and the CDP WebSocket handshake, and follow the advertisedwebSocketDebuggerUrlonly when it's on the same host and port ascdp_url; they're never passed to another host or to the extension.headersis unset, nothing changes: no new keyword argument is passed toCDPClient.connect.Written for commit 9448aad. Summary will update on new commits.