Repository navigation
feat(python): extension_cdp_url for local_browser.connect - #3223
Merged
seanmcguire12 merged 2 commits intoOct 19, 2026
Conversation
local_browser.connect(cdp_url=..., extension_cdp_url=...) sends the extension a browser-local CDP WebSocket instead of the URL the client connected with. That URL may be an authenticated proxy the browser cannot reach, or one whose query carries credentials that should not be handed to the browser. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
🦋 Changeset detectedLatest commit: b340f47 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-extension-cdp-url
October 19, 2026 17:38
seanmcguire12
merged commit Oct 19, 2026
e4eae67
into
browserbase:evals/sydney-runkle-extension-cdp-url
5 checks passed
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
Stagehand's extension opens its own CDP connection to the browser, and it is handed the URL the Python client connected with. That only works when the client and the browser can use the same address.
Sometimes they can't. Our case: the client runs in a server process and reaches a sandboxed Chrome through an authenticated gateway,
wss://<gateway>/.../cdp?api_key=.... The extension, running inside that Chrome, should dial the browser's local endpoint (ws://127.0.0.1:<port>/devtools/browser/<id>). Using the gateway URL would hand the gateway's credentials to the browser, which loads arbitrary pages, and send every extension command out and back in through the gateway. Today the only workaround is overriding the privateStagehand._worker_init_params.What
local_browser.connecttakes an optionalextension_cdp_url, which must be aws://orwss://URL with a host.Stagehand.createsends it as the init parambrowser_cdp_urlin place of the client's URL.Tests
ruff format --check,ruff check,ty checkandgenerate.py --checkpass.pytest: 793 passed.test_official_api_routes_through_and_never_closes_a_caller_http_clientfails with and without this change in my checkout, because it needs the extension built inpackages/extension/dist.🤖 Generated with Claude Code
Summary by cubic
Adds an optional
extension_cdp_urltolocal_browser.connectso Stagehand's extension can dial a different CDP WebSocket than the client's, for cases where the client reaches Chrome through a proxy the extension cannot use.browser_cdp_urlin place of the client's URL; when unset, behavior is unchanged.ws://orwss://with a host; anything else raises a validation error.pytestreports 793 passed;test_official_api_routes_through_and_never_closes_a_caller_http_clientfails with and without this change because it needs the extension built inpackages/extension/dist.Written for commit b340f47. Summary will update on new commits.