Skip to content

[feat]: expose headers param on LocalBrowserConnectOptions - #3265

Merged
seanmcguire12 merged 5 commits into
local-connect-headers-tsfrom
local-connect-headers-go
Oct 19, 2026
Merged

seanmcguire12 merged 5 commits into
local-connect-headers-tsfrom
local-connect-headers-go

Conversation

@seanmcguire12

@seanmcguire12 seanmcguire12 commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

why

port the Python support from #3260 to Go so callers can authenticate against CDP gateways without putting credentials in the URL

what

browser, err := stagehand.ConnectLocalBrowser(ctx, stagehand.LocalBrowserConnectOptions{
    CDPURL:  "https://gateway/.../cdp",
    Headers: map[string]string{"X-Api-Key": "..."},
})

Headers are sent with HTTP discovery and the client’s CDP WebSocket handshake. They only reach endpoints on the same host and effective port, without downgrading TLS, and are never passed to the extension. Both HTTP and WebSocket redirects are guarded using a copy of the HTTP client, preserving the caller’s settings.

Header validation rejects unsafe names, non-ASCII values, and leading or trailing whitespace without exposing credentials in errors. Discovery fails immediately on HTTP 401/403, and debug logging explains when headers are withheld. Includes a minor changeset.

test plan

  • Network tests cover discovery and WebSocket header forwarding, allowed and blocked redirects, redirect limits, and connections with nil or empty headers.
  • Regression tests cover validation, error redaction, host/port/TLS boundaries, safe diagnostics, immediate 401/403 failures, and retries for transient HTTP failures.
  • Factory tests verify headers are copied from the caller’s map and kept out of extension initialization metadata. Redirect tests verify the caller’s HTTP client and redirect policy are preserved.
  • Targeted tests pass with the race detector, and go vet . passes. The broader go test ./... command hits duplicate declarations in the standalone examples directory.

Summary by cubic

Adds Headers to LocalBrowserConnectOptions so Go callers can authenticate against CDP gateways without putting credentials in the URL.

  • Ports the Python support from feat(python): send HTTP headers when connecting to a CDP endpoint #3260 to Go, sending headers with HTTP discovery and the client's CDP WebSocket handshake.
  • Headers only reach endpoints on the same host and effective port, never downgrading TLS, and are not passed to the extension.
  • Redirects are guarded using a copy of the HTTP client, preserving caller settings, and URLs are re-validated even if the caller's redirect callback rewrites them; duplicate header names ignoring case are rejected.
  • 401/403 discovery failures fail immediately, and validation rejects unsafe names, non-ASCII values, and whitespace trim issues without exposing credentials in errors.

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

View guided diff Turn on auto-fix

@changeset-bot

changeset-bot Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: b1b9938

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

@seanmcguire12
seanmcguire12 added this pull request to stack #3264 October 19, 2026 21:44

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 5 files

Confidence score: 3/5

  • In cdp_headers.go, a custom CheckRedirect can change the URL after the safety check, sending CDP headers to another host. Revalidate the final URL after the callback returns nil.
  • In browser_factories.go, case variants of the same header can pass validation and produce a nondeterministic winning value. Reject duplicate names case-insensitively before setting headers.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="packages/sdk-go/cdp_headers.go">

<violation number="1" location="packages/sdk-go/cdp_headers.go:71">
P1: A custom `CheckRedirect` can rewrite `request.URL` to another host after the safety check, causing `net/http` to send the CDP headers there. Revalidate the final URL after the caller callback returns nil.</violation>
</file>

<file name="packages/sdk-go/browser_factories.go">

<violation number="1" location="packages/sdk-go/browser_factories.go:227">
P2: Case variants of the same header pass validation, but `http.Header.Set` collapses them and Go map iteration makes the winning value nondeterministic. Reject duplicate names case-insensitively before setting the header.</violation>
</file>
Architecture diagram
sequenceDiagram
    participant Client as Go SDK Caller
    participant Factory as browser_factories.go
    participant Validator as cdp_headers.go
    participant Discovery as resolveBrowserWebSocketURL
    participant Gateway as CDP Gateway
    participant WSClient as dialCDPWebSocket
    participant Extension as Browser Extension

    Note over Client,Extension: Local Browser Connect with Auth Headers

    Client->>Factory: ConnectLocalBrowser(CDPURL, Headers)
    Factory->>Validator: validateCDPHeader(name, value)
    Validator-->>Factory: Validation result
    alt Valid headers
        Factory->>Factory: Copy headers map
        Factory->>WSClient: connect() with headers
    else Invalid header
        Factory-->>Client: Error (redacted)
    end

    Note over WSClient,Gateway: Discovery Phase
    WSClient->>Discovery: resolveBrowserWebSocketURL(cdpURL, headers)
    Discovery->>Discovery: Clone HTTP client with CheckRedirect guard
    Discovery->>Gateway: GET /json/version with headers
    alt Discovery rejected (401/403)
        Gateway-->>Discovery: HTTP 401/403
        Discovery-->>Client: Immediate failure (no retry)
    else Transient failure (429/500/503)
        Gateway-->>Discovery: HTTP 429/500/503
        Discovery->>Discovery: Retry with polling
        Discovery->>Gateway: GET /json/version (retry)
    end

    Gateway-->>Discovery: webSocketDebuggerUrl
    Discovery-->>WSClient: Return WebSocket URL

    Note over WSClient,Gateway: WebSocket Handshake Phase
    WSClient->>WSClient: Check canForwardCDPHeaders(original, advertised)
    alt Headers safe to forward (same host/port, no TLS downgrade)
        WSClient->>Gateway: WebSocket handshake with headers
    else Headers withheld (different host/port or TLS downgrade)
        WSClient->>WSClient: Debug log about withholding
        WSClient->>Gateway: WebSocket handshake without headers
    end

    Note over WSClient,Extension: Redirect Guard
    WSClient->>WSClient: Check redirect target (host, port, TLS)
    alt Safe redirect (same host/port, TLS preserved)
        WSClient->>Gateway: Follow redirect with headers
    else Unsafe redirect
        WSClient-->>Client: errUnsafeCDPRedirect
    end

    WSClient-->>Client: Connected browser session

    Note over Client,Extension: Extension Connection (no headers)
    WSClient->>Extension: Connect via browser-level CDP URL
    Extension-->>WSClient: Extension session established
Loading

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread packages/sdk-go/cdp_headers.go Resolved
Comment thread packages/sdk-go/browser_factories.go Resolved

@miguelg719 miguelg719 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — two nits below, non-blocking.

if response != nil {
return nil, fmt.Errorf("open CDP WebSocket: HTTP %d", response.StatusCode)
}
return nil, errors.New("failed to open authenticated CDP WebSocket")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this drops the underlying cause, so TLS/DNS/connection failures become opaque. The dial error carries the URL, not the headers, so wrapping it (%w) should be safe. Same for the discovery timeout at L1194, which now drops lastError.

}
canonicalName := http.CanonicalHeaderKey(name)
if _, exists := headers[canonicalName]; exists {
return nil, errors.New("headers must not contain duplicate names ignoring case")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Go rejects case-insensitive duplicates, but Python and TS accept {"x-a": …, "X-A": …}, and then the two requests disagree (Python: urllib keeps the last value, websockets sends both; TS: fetch merges into "1, 2"). Worth rejecting in Python/TS too so the three SDKs match.

@seanmcguire12
seanmcguire12 merged commit a31e2da into main Oct 19, 2026
26 checks passed
@seanmcguire12
seanmcguire12 deleted the local-connect-headers-go branch October 19, 2026 22:49
miguelg719 pushed a commit that referenced this pull request Oct 20, 2026
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## @browserbasehq/[email protected]

### Minor Changes

- [#3267](#3267)
[`cb9a1d1`](cb9a1d1)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - add
extension archive getter

- [#3253](#3253)
[`6bf4146`](6bf4146)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - add
optional `extensionCdpUrl` to `localBrowser.connect` to allow
configuring the in-browser CDP url

- [#3263](#3263)
[`ecb2a6b`](ecb2a6b)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - add
`headers` to `localBrowser.connect` so CDP gateways can be authenticated
without credentials in the URL

- [#3183](#3183)
[`901eb27`](901eb27)
Thanks [@supremeboxlogos](https://github.com/supremeboxlogos)! - Add
`page.waitForFunction()` to wait until a predicate in the page returns a
truthy value.

### Patch Changes

- [#3229](#3229)
[`c6b6f27`](c6b6f27)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - skip
caching for request bodies larger than 25 MB.

- [#3198](#3198)
[`10418f9`](10418f9)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - enable
WebMCP by default for local chromium versions newer than 157

- [#3210](#3210)
[`1c40ba7`](1c40ba7)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Require
client transport for Browserbase Model Gateway and cache requests.

- [#3209](#3209)
[`17de6ea`](17de6ea)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Route
Browserbase Model Gateway and cache requests through the connected SDK.
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies
[[`c6b6f27`](c6b6f27),
[`cb9a1d1`](cb9a1d1),
[`10418f9`](10418f9),
[`1c40ba7`](1c40ba7),
[`17de6ea`](17de6ea),
[`6bf4146`](6bf4146),
[`ecb2a6b`](ecb2a6b),
[`901eb27`](901eb27)]:
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Major Changes

- [#3210](#3210)
[`1c40ba7`](1c40ba7)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Require
client transport for Browserbase Model Gateway and cache requests.

### Minor Changes

- [#3208](#3208)
[`4a7c637`](4a7c637)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Support
routing Browserbase Model Gateway and cache requests through the
connected SDK.

- [#3183](#3183)
[`901eb27`](901eb27)
Thanks [@supremeboxlogos](https://github.com/supremeboxlogos)! - Add the
`page.wait_for_function` method.
## @browserbasehq/[email protected]

### Minor Changes

- [#3208](#3208)
[`4a7c637`](4a7c637)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Support
routing Browserbase Model Gateway and cache requests through the
connected SDK.

- [#3183](#3183)
[`901eb27`](901eb27)
Thanks [@supremeboxlogos](https://github.com/supremeboxlogos)! - Add the
`page.wait_for_function` method.

### Patch Changes

- [#3229](#3229)
[`c6b6f27`](c6b6f27)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - skip
caching for request bodies larger than 25 MB.

- [#3257](#3257)
[`113ad81`](113ad81)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - honour a
changed extension CDP WS URL when creating a new Stagehand instance
after closing the previous one

- [#3210](#3210)
[`1c40ba7`](1c40ba7)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Require
client transport for Browserbase Model Gateway and cache requests.
## @browserbasehq/[email protected]

### Minor Changes

- [#3267](#3267)
[`cb9a1d1`](cb9a1d1)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - add
extension archive getter

- [#3265](#3265)
[`a31e2da`](a31e2da)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - add
`Headers` to `LocalBrowserConnectOptions` so CDP gateways can be
authenticated without credentials in the URL

- [#3254](#3254)
[`9eac212`](9eac212)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - add
optional `ExtensionCDPURL` to `LocalBrowserConnectOptions` to allow
configuring the in-browser CDP url.

- [#3183](#3183)
[`901eb27`](901eb27)
Thanks [@supremeboxlogos](https://github.com/supremeboxlogos)! - Add
`Page.WaitForFunction` to wait until a predicate in the page returns a
truthy value.

### Patch Changes

- [#3229](#3229)
[`c6b6f27`](c6b6f27)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - skip
caching for request bodies larger than 25 MB.

- [#3198](#3198)
[`10418f9`](10418f9)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - enable
WebMCP by default for local chromium versions newer than 157

- [#3210](#3210)
[`1c40ba7`](1c40ba7)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Require
client transport for Browserbase Model Gateway and cache requests.

- [#3209](#3209)
[`17de6ea`](17de6ea)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Route
Browserbase Model Gateway and cache requests through the connected SDK.
## @browserbasehq/[email protected]

### Minor Changes

- [#3260](#3260)
[`2859fd5`](2859fd5)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - add
`headers` to `local_browser.connect` so CDP gateways can be
authenticated without credentials in the URL

- [#3267](#3267)
[`cb9a1d1`](cb9a1d1)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - add
extension archive getter

- [#3252](#3252)
[`21e0363`](21e0363)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - add
`extension_cdp_url` to `local_browser.connect` for clients that reach
Chrome through a proxy the extension cannot use

- [#3183](#3183)
[`901eb27`](901eb27)
Thanks [@supremeboxlogos](https://github.com/supremeboxlogos)! - Add
`page.wait_for_function()` to wait until a predicate in the page returns
a truthy value.

### Patch Changes

- [#3229](#3229)
[`c6b6f27`](c6b6f27)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - skip
caching for request bodies larger than 25 MB.

- [#3198](#3198)
[`10418f9`](10418f9)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - enable
WebMCP by default for local chromium versions newer than 157

- [#3210](#3210)
[`1c40ba7`](1c40ba7)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Require
client transport for Browserbase Model Gateway and cache requests.

- [#3209](#3209)
[`17de6ea`](17de6ea)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Route
Browserbase Model Gateway and cache requests through the connected SDK.
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies
[[`c6b6f27`](c6b6f27),
[`cb9a1d1`](cb9a1d1),
[`10418f9`](10418f9),
[`1c40ba7`](1c40ba7),
[`17de6ea`](17de6ea),
[`6bf4146`](6bf4146),
[`ecb2a6b`](ecb2a6b),
[`901eb27`](901eb27)]:
  - @browserbasehq/[email protected]
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
##
@browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies
[[`c6b6f27`](c6b6f27),
[`cb9a1d1`](cb9a1d1),
[`10418f9`](10418f9),
[`1c40ba7`](1c40ba7),
[`17de6ea`](17de6ea),
[`6bf4146`](6bf4146),
[`ecb2a6b`](ecb2a6b),
[`901eb27`](901eb27)]:
  - @browserbasehq/[email protected]
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies
[[`c6b6f27`](c6b6f27),
[`cb9a1d1`](cb9a1d1),
[`10418f9`](10418f9),
[`1c40ba7`](1c40ba7),
[`17de6ea`](17de6ea),
[`6bf4146`](6bf4146),
[`ecb2a6b`](ecb2a6b),
[`901eb27`](901eb27)]:
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies
[[`c6b6f27`](c6b6f27),
[`cb9a1d1`](cb9a1d1),
[`10418f9`](10418f9),
[`1c40ba7`](1c40ba7),
[`17de6ea`](17de6ea),
[`6bf4146`](6bf4146),
[`ecb2a6b`](ecb2a6b),
[`901eb27`](901eb27)]:
  - @browserbasehq/[email protected]
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies
[[`c6b6f27`](c6b6f27),
[`cb9a1d1`](cb9a1d1),
[`10418f9`](10418f9),
[`1c40ba7`](1c40ba7),
[`17de6ea`](17de6ea),
[`6bf4146`](6bf4146),
[`ecb2a6b`](ecb2a6b),
[`901eb27`](901eb27)]:
  - @browserbasehq/[email protected]
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants