Skip to content

feat(python): headers for local_browser.connect - #3224

Closed
sydney-runkle wants to merge 1 commit into
browserbase:mainfrom
sydney-runkle:sydney/connect-headers
Closed

sydney-runkle wants to merge 1 commit into
browserbase:mainfrom
sydney-runkle:sydney/connect-headers

Conversation

@sydney-runkle

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

Copy link
Copy Markdown
Contributor

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

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 feat(python): extension_cdp_url for local_browser.connect #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


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 are sent with the /json/version request and the CDP WebSocket handshake, and follow the advertised webSocketDebuggerUrl only when it's on the same host and port as cdp_url; they're never passed to another host or 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.
  • When headers is unset, nothing changes: no new keyword argument is passed to CDPClient.connect.

Written for commit 9448aad. Summary will update on new commits.

View guided diff

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

changeset-bot Bot commented Oct 16, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 9448aad

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-connect-headers October 19, 2026 20:03
@seanmcguire12
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]>
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