Skip to content

[feat]: add domain allowlist - #2283

Merged
seanmcguire12 merged 5 commits into
mainfrom
add-domain-allowlist
Jun 29, 2026
Merged

seanmcguire12 merged 5 commits into
mainfrom
add-domain-allowlist

Conversation

@seanmcguire12

@seanmcguire12 seanmcguire12 commented Jun 27, 2026 •

Copy link
Copy Markdown
Member

why

users need a way to restrict browser sessions to known domains. addresses #2272

what changed

  • users can now call stagehand.context.setDomainPolicy({ allowedDomains }) to block HTTP(S) requests that do not match the allowed domains
  • DomainPolicy now supports both allowedDomains & blockedDomains
  • blocked domains take precedence over allowed domains when both are provided
  • allowlist policies use broad HTTP(S) Fetch request patterns because allowlists require checking every HTTP(S) request
  • blocklist-only policies keep the existing optimized behavior of only pausing requests that match generated blocklist patterns
  • domain policy matching also returns a decision with the block reason, so logs can distinguish allowedDomains blocks from blockedDomains blocks

behavioural notes

  • setDomainPolicy({ allowedDomains: [...] }) applies to active context sessions & future pages/targets
  • exact domains like example.com allow only that hostname
  • wildcard domains like *.example.com allow subdomains, but not the apex domain
  • requests to domains outside allowedDomains are failed with BlockedByClient
  • if a request matches blockedDomains, it is blocked even if it also matches allowedDomains
  • non-HTTP(S) urls continue instead of being blocked by the domain policy
  • setDomainPolicy() replaces the current policy; callers should pass both allowedDomains & blockedDomains in the same call when they want both
  • clearing with setDomainPolicy(null), { allowedDomains: [] }, or an otherwise empty policy disables the policy when no blocked domains are present

test plan

  • packages/core/tests/unit/domain-policy.test.ts validates allowlist normalization, exact/wildcard matching, invalid allowed domain rejection, block-over-allow precedence, non-HTTP(S) continuation & broad HTTP(S) Fetch.RequestPattern generation
  • packages/core/tests/unit/context-domain-policy.test.ts validates context.setDomainPolicy() enables broad HTTP(S) interception for allowlists, preserves precise blocklist-only interception, returns normalized policy state, continues allowed paused requests & fails disallowed paused requests
  • packages/core/tests/integration/context-domain-policy.spec.ts validates allowed-domain requests are not blocked, non-matching requests fail with ERR_BLOCKED_BY_CLIENT, blocked domains take precedence over allowed domains & allowlist policies apply to pages created after the policy is set

Summary by cubic

Add a context-wide domain policy to control which HTTP(S) requests Stagehand can access. You can allow specific domains or block known hosts, with strict enforcement across existing and new pages.

  • New Features
    • Added context.setDomainPolicy({ allowedDomains?: string[], blockedDomains?: string[] }) and context.getDomainPolicy(). Domain-only patterns; exact or *.example.com (wildcards do not match the apex). HTTP(S) only. Applies to current and future pages. Clear with null or empty arrays; getDomainPolicy() returns only set fields.
    • Enforcement: blockedDomains take precedence over allowedDomains. Allowlists use broad HTTP(S) Fetch interception; blocklist-only policies keep precise patterns. Logs include the block reason (allowedDomains vs blockedDomains).
    • Errors & validation: Added StagehandSetDomainPolicyError when enforcement can’t be guaranteed during target attach. Added runtime validation; malformed inputs (omitted policy, non-array fields, non-string or non-domain patterns) throw StagehandInvalidArgumentError.

Written for commit 60bafc3. Summary will update on new commits.

Review in cubic

@changeset-bot

changeset-bot Bot commented Jun 27, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 60bafc3

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

This PR includes changesets to release 3 packages
Name Type
@browserbasehq/stagehand Minor
@browserbasehq/stagehand-evals Patch
@browserbasehq/stagehand-server-v3 Patch

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 force-pushed the add-domain-allowlist branch from 0e3ce3a to 206e42c Compare June 27, 2026 00:16
@seanmcguire12
seanmcguire12 marked this pull request as ready for review June 27, 2026 00:24

@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.

1 issue found across 7 files

Confidence score: 3/5

  • In packages/core/lib/v3/understudy/domainPolicy.ts, malformed runtime input that isn’t validated as an array can cause domain policy checks to be skipped, weakening a security control silently rather than failing fast; this creates a concrete risk of unintended access if bad input reaches this path — add explicit array-type validation (and fail closed/log clearly) before merging.
Architecture diagram
sequenceDiagram
    participant User as User Code
    participant Context as V3Context
    participant Policy as Domain Policy Logic
    participant CDP as CDP Session
    participant Browser as Browser Engine
    participant Web as External Web

    Note over User,Web: NEW: Domain Allowlist Flow

    User->>Context: setDomainPolicy({ allowedDomains, blockedDomains })
    Context->>Policy: normalizeDomainPolicy()
    Policy->>Policy: Validate & dedupe patterns
    alt Both allowedDomains and blockedDomains present
        Policy->>Policy: Generate broad HTTP(S) fetch patterns
    else Only blockedDomains present
        Policy->>Policy: Generate precise blocklist fetch patterns
    else Only allowedDomains present
        Policy->>Policy: Generate broad HTTP(S) fetch patterns
    else Empty or null policy
        Policy-->>Context: return null
        Context->>CDP: Fetch.disable (if previously enabled)
        CDP-->>Context: Disabled
        Context-->>User: Policy cleared
    end
    Policy-->>Context: NormalizedDomainPolicy
    Context->>CDP: Fetch.enable({ patterns })
    alt Fetch.enable succeeds
        CDP-->>Context: Enabled
        Context-->>User: Policy applied
    else Fetch.enable fails (e.g., during new page attach)
        Context->>CDP: Close target
        Context-->>User: StagehandSetDomainPolicyError
    end

    Note over User,Browser: Runtime Request Handling

    Browser->>CDP: Emit Fetch.requestPaused
    CDP->>Context: onRequestPaused(event)
    Context->>Policy: getDomainPolicyDecision(url, policy)
    Policy->>Policy: Extract hostname from URL
    alt Non-HTTP(S) URL
        Policy-->>Context: { action: "continue" }
        Context->>CDP: Fetch.continueRequest()
        CDP->>Browser: Continue request
        Browser->>Web: Normal request flow
    else Hostname matches blockedDomains
        Policy-->>Context: { action: "block", reason: "blockedDomains" }
        Context->>Context: Log block event with reason
        Context->>CDP: Fetch.failRequest({ errorReason: "BlockedByClient" })
        CDP->>Browser: Block request
        Browser-->>Web: Request failed
    else allowedDomains exists AND hostname NOT in allowedDomains
        Policy-->>Context: { action: "block", reason: "allowedDomains" }
        Context->>Context: Log block event with reason
        Context->>CDP: Fetch.failRequest({ errorReason: "BlockedByClient" })
        CDP->>Browser: Block request
        Browser-->>Web: Request failed
    else Hostname allowed
        Policy-->>Context: { action: "continue" }
        Context->>CDP: Fetch.continueRequest()
        CDP->>Browser: Continue request
        Browser->>Web: Normal request flow
    end
Loading

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

Fix all with cubic | Re-trigger cubic

@seanmcguire12

Copy link
Copy Markdown
Member Author

@cubic-dev-ai

@cubic-dev-ai

cubic-dev-ai Bot commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai

@seanmcguire12 I have started the AI code review. It will take a few minutes to complete.

@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.

No issues found across 7 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.
Architecture diagram
sequenceDiagram
    participant Client as User Code
    participant Context as V3Context
    participant Policy as normalizeDomainPolicy()
    participant Decision as getDomainPolicyDecision()
    participant Session as CDPSession
    participant Target as Browser Target

    Note over Client,Target: NEW: Domain Allowlist Flow

    Client->>Context: setDomainPolicy({ allowedDomains: ["example.com", "*.example.com"], blockedDomains: ["ads.example.com"] })
    
    Context->>Policy: normalizeDomainPolicy(policy)
    
    alt Valid policy with allowedDomains
        Policy->>Policy: Validate and deduplicate domain patterns
        Policy->>Policy: Generate broad HTTP(S) Fetch patterns
        Policy-->>Context: NormalizedDomainPolicy { allowedDomainRules, blockedDomainRules, fetchPatterns }
    else Valid policy with only blockedDomains
        Policy->>Policy: Generate precise blocklist patterns
        Policy-->>Context: NormalizedDomainPolicy { blockedDomainRules, fetchPatterns }
    else Null/empty policy
        Policy-->>Context: null
    end

    alt Policy is active (not null)
        Context->>Session: Fetch.enable({ patterns: broad ["http://*/*", "https://*/*"] })
        Context->>Target: Attach Fetch.requestPaused listener
        
        Note over Context,Session: For each existing page/target
        
        opt New target attaches
            Context->>Target: Enable Fetch with same patterns
        end
    else Policy is cleared
        Context->>Session: Fetch.disable()
        Context->>Target: Remove Fetch.requestPaused listener
    end

    Note over Client,Session: Runtime Request Handling

    Session->>Context: Fetch.requestPaused event
    
    Context->>Decision: getDomainPolicyDecision(url, policy)
    
    alt Blocked domain matches first (blockedDomains takes precedence)
        Decision-->>Context: { action: "block", reason: "blockedDomains" }
        Context->>Session: Fetch.failRequest({ errorReason: "BlockedByClient" })
        Context->>Context: Log block reason as "blockedDomains"
    else Allowed domains set but URL doesn't match any allowed rule
        Decision-->>Context: { action: "block", reason: "allowedDomains" }
        Context->>Session: Fetch.failRequest({ errorReason: "BlockedByClient" })
        Context->>Context: Log block reason as "allowedDomains"
    else URL is allowed or policy doesn't apply
        Decision-->>Context: { action: "continue" }
        Context->>Session: Fetch.continueRequest({ requestId })
    end

    Note over Client,Context: Policy Inspection

    Client->>Context: getDomainPolicy()
    Context-->>Client: { allowedDomains: string[], blockedDomains: string[] } (only non-empty arrays returned)
Loading

Re-trigger cubic

@akeimach akeimach 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.

fantastic

@seanmcguire12
seanmcguire12 merged commit 871ca7e into main Jun 29, 2026
238 checks passed
seanmcguire12 pushed a commit that referenced this pull request Jul 13, 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

- [#2283](#2283)
[`871ca7e`](871ca7e)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - add
`context.setDomainPolicy({ allowedDomains: ["allowed.domain"] })` which
allows users to define a set of domains that are accessible to stagehand

- [#2274](#2274)
[`f31980f`](f31980f)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - add
`context.setDomainPolicy({blockedDomains: ["some.domain"]})` which
allows users to define a list of domains that will be blocked by
stagehand

### Patch Changes

- [#2305](#2305)
[`cd1daad`](cd1daad)
Thanks [@shrey150](https://github.com/shrey150)! - Remove the noisy AI
SDK "system message in messages" warning logged on every hybrid/DOM
`agent.execute()` call.

- [#2328](#2328)
[`d287ff4`](d287ff4)
Thanks [@miguelg719](https://github.com/miguelg719)! - Allow modelName
"auto" in the constructor and per-primitive model overrides when running
through the Stagehand API

- [#2294](#2294)
[`3938590`](3938590)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! -
automatically close popups that violate user defined domain policy

- [#2298](#2298)
[`892701a`](892701a)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Fix CUA
`keypress` actions to press key combinations as a single chord.

- [#2345](#2345)
[`21826c7`](21826c7)
Thanks [@monadoid](https://github.com/monadoid)! - Repair malformed
UTF-16 snapshot text before it reaches model prompts.

- [#2306](#2306)
[`8dcef1b`](8dcef1b)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Use the
screenshot provider's declared media type when sending CUA image
payloads. The `setScreenshotProvider` callback now returns
`ScreenshotProviderResult` (`{ base64, mediaType }`) instead of a bare
base64 string.

- [#2273](#2273)
[`93a23d3`](93a23d3)
Thanks [@miguelg719](https://github.com/miguelg719)! - Add support for
the new `google/gemini-3.5-flash` computer-use tools model

- [#2278](#2278)
[`022d68f`](022d68f)
Thanks [@shrey150](https://github.com/shrey150)! - Fix `TypeError:
Converting circular structure to JSON` when creating an agent with MCP
`integrations` that include a `Client` instance (e.g. a local/stdio
server from `connectToMCPServer`). The agent-creation log serialized the
raw `integrations` array, and a live MCP `Client` is circular. It now
logs a safe descriptor (URL strings kept, client instances summarized)
so `agent({ integrations: [client] })` works.

- [#2288](#2288)
[`bb5ffa6`](bb5ffa6)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - clean up
cdp session event handlers on target detach

## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies
\[[`cd1daad`](cd1daad),
[`d287ff4`](d287ff4),
[`3938590`](3938590),
[`892701a`](892701a),
[`21826c7`](21826c7),
[`8dcef1b`](8dcef1b),
[`93a23d3`](93a23d3),
[`871ca7e`](871ca7e),
[`022d68f`](022d68f),
[`bb5ffa6`](bb5ffa6),
[`f31980f`](f31980f)]:
    -   @browserbasehq/[email protected]

## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies
\[[`cd1daad`](cd1daad),
[`d287ff4`](d287ff4),
[`3938590`](3938590),
[`892701a`](892701a),
[`21826c7`](21826c7),
[`8dcef1b`](8dcef1b),
[`93a23d3`](93a23d3),
[`871ca7e`](871ca7e),
[`022d68f`](022d68f),
[`bb5ffa6`](bb5ffa6),
[`f31980f`](f31980f)]:
    -   @browserbasehq/[email protected]

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
felipeofdev-ai pushed a commit to felipeofdev-ai/stagehand that referenced this pull request Aug 4, 2026
felipeofdev-ai pushed a commit to felipeofdev-ai/stagehand that referenced this pull request Aug 4, 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

- [browserbase#2283](browserbase#2283)
[`871ca7e`](browserbase@871ca7e)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - add
`context.setDomainPolicy({ allowedDomains: ["allowed.domain"] })` which
allows users to define a set of domains that are accessible to stagehand

- [browserbase#2274](browserbase#2274)
[`f31980f`](browserbase@f31980f)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - add
`context.setDomainPolicy({blockedDomains: ["some.domain"]})` which
allows users to define a list of domains that will be blocked by
stagehand

### Patch Changes

- [browserbase#2305](browserbase#2305)
[`cd1daad`](browserbase@cd1daad)
Thanks [@shrey150](https://github.com/shrey150)! - Remove the noisy AI
SDK "system message in messages" warning logged on every hybrid/DOM
`agent.execute()` call.

- [browserbase#2328](browserbase#2328)
[`d287ff4`](browserbase@d287ff4)
Thanks [@miguelg719](https://github.com/miguelg719)! - Allow modelName
"auto" in the constructor and per-primitive model overrides when running
through the Stagehand API

- [browserbase#2294](browserbase#2294)
[`3938590`](browserbase@3938590)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! -
automatically close popups that violate user defined domain policy

- [browserbase#2298](browserbase#2298)
[`892701a`](browserbase@892701a)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Fix CUA
`keypress` actions to press key combinations as a single chord.

- [browserbase#2345](browserbase#2345)
[`21826c7`](browserbase@21826c7)
Thanks [@monadoid](https://github.com/monadoid)! - Repair malformed
UTF-16 snapshot text before it reaches model prompts.

- [browserbase#2306](browserbase#2306)
[`8dcef1b`](browserbase@8dcef1b)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Use the
screenshot provider's declared media type when sending CUA image
payloads. The `setScreenshotProvider` callback now returns
`ScreenshotProviderResult` (`{ base64, mediaType }`) instead of a bare
base64 string.

- [browserbase#2273](browserbase#2273)
[`93a23d3`](browserbase@93a23d3)
Thanks [@miguelg719](https://github.com/miguelg719)! - Add support for
the new `google/gemini-3.5-flash` computer-use tools model

- [browserbase#2278](browserbase#2278)
[`022d68f`](browserbase@022d68f)
Thanks [@shrey150](https://github.com/shrey150)! - Fix `TypeError:
Converting circular structure to JSON` when creating an agent with MCP
`integrations` that include a `Client` instance (e.g. a local/stdio
server from `connectToMCPServer`). The agent-creation log serialized the
raw `integrations` array, and a live MCP `Client` is circular. It now
logs a safe descriptor (URL strings kept, client instances summarized)
so `agent({ integrations: [client] })` works.

- [browserbase#2288](browserbase#2288)
[`bb5ffa6`](browserbase@bb5ffa6)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - clean up
cdp session event handlers on target detach

## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies
\[[`cd1daad`](browserbase@cd1daad),
[`d287ff4`](browserbase@d287ff4),
[`3938590`](browserbase@3938590),
[`892701a`](browserbase@892701a),
[`21826c7`](browserbase@21826c7),
[`8dcef1b`](browserbase@8dcef1b),
[`93a23d3`](browserbase@93a23d3),
[`871ca7e`](browserbase@871ca7e),
[`022d68f`](browserbase@022d68f),
[`bb5ffa6`](browserbase@bb5ffa6),
[`f31980f`](browserbase@f31980f)]:
    -   @browserbasehq/[email protected]

## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies
\[[`cd1daad`](browserbase@cd1daad),
[`d287ff4`](browserbase@d287ff4),
[`3938590`](browserbase@3938590),
[`892701a`](browserbase@892701a),
[`21826c7`](browserbase@21826c7),
[`8dcef1b`](browserbase@8dcef1b),
[`93a23d3`](browserbase@93a23d3),
[`871ca7e`](browserbase@871ca7e),
[`022d68f`](browserbase@022d68f),
[`bb5ffa6`](browserbase@bb5ffa6),
[`f31980f`](browserbase@f31980f)]:
    -   @browserbasehq/[email protected]
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