Skip to content

feat(python): extension_cdp_url for local_browser.connect - #3223

Merged
seanmcguire12 merged 2 commits into
browserbase:evals/sydney-runkle-extension-cdp-urlfrom
sydney-runkle:sydney/extension-cdp-url
Oct 19, 2026
Merged

seanmcguire12 merged 2 commits into
browserbase:evals/sydney-runkle-extension-cdp-urlfrom
sydney-runkle:sydney/extension-cdp-url

Conversation

@sydney-runkle

@sydney-runkle sydney-runkle commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

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 private Stagehand._worker_init_params.

What

browser = await local_browser.connect(
    cdp_url="wss://gateway/cdp?api_key=...",
    extension_cdp_url="ws://127.0.0.1:9300/devtools/browser/<id>",
)
stagehand = await Stagehand.create(browser=browser)
  • local_browser.connect takes an optional extension_cdp_url, which must be a ws:// or wss:// URL with a host.
  • It is carried on the claimed browser's worker metadata, and Stagehand.create sends it as the init param browser_cdp_url in place of the client's URL.
  • When it is unset, behaviour is unchanged.
  • Python SDK only, with a minor changeset. The TypeScript SDK has the same assumption and could take the same option; happy to follow up.

Tests

  • New tests cover the init param, connect carrying the value through, the default, and rejection of non-WebSocket URLs.
  • ruff format --check, ruff check, ty check and generate.py --check pass.
  • pytest: 793 passed. test_official_api_routes_through_and_never_closes_a_caller_http_client fails with and without this change in my checkout, because it needs the extension built in packages/extension/dist.

🤖 Generated with Claude Code


Summary by cubic

Adds an optional extension_cdp_url to local_browser.connect so 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.

  • When set, Stagehand sends it as the init param browser_cdp_url in place of the client's URL; when unset, behavior is unchanged.
  • The URL must be ws:// or wss:// with a host; anything else raises a validation error.
  • Python SDK only; the TypeScript SDK has the same assumption and could take the same option in a follow-up.
  • New tests cover the init param, carrying the value through, the default, and rejection of invalid URLs. pytest reports 793 passed; test_official_api_routes_through_and_never_closes_a_caller_http_client fails with and without this change because it needs the extension built in packages/extension/dist.

Written for commit b340f47. Summary will update on new commits.

View guided diff

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-bot

changeset-bot Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: b340f47

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@browserbasehq/stagehand-python Minor

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

@github-actions

Copy link
Copy Markdown
Contributor

This PR is from an external contributor and must be approved by a stagehand team member with write access before CI can run.
Approving the latest commit mirrors it into an internal PR owned by the approver.
If new commits are pushed later, the internal PR stays open but is marked stale until someone approves the latest external commit and refreshes it.

@github-actions github-actions Bot added external-contributor Tracks PRs mirrored from external contributor forks. external-contributor:awaiting-approval Waiting for a stagehand team member to approve the latest external commit. labels Oct 16, 2026
@sydney-runkle
sydney-runkle marked this pull request as ready for review October 19, 2026 15:35
@seanmcguire12
seanmcguire12 changed the base branch from main to evals/sydney-runkle-extension-cdp-url October 19, 2026 17:38
@seanmcguire12
seanmcguire12 merged commit e4eae67 into browserbase:evals/sydney-runkle-extension-cdp-url Oct 19, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external-contributor:awaiting-approval Waiting for a stagehand team member to approve the latest external commit. external-contributor Tracks PRs mirrored from external contributor forks.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants