Repository navigation
[feat]: add domain allowlist - #2283
Merged
Merged
Conversation
🦋 Changeset detectedLatest commit: 60bafc3 The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
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
force-pushed
the
add-domain-allowlist
branch
from
June 27, 2026 00:16
0e3ce3a to
206e42c
Compare
seanmcguire12
marked this pull request as ready for review
June 27, 2026 00:24
Contributor
There was a problem hiding this comment.
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
Reply with feedback, questions, or to request a fix.
Fix all with cubic | Re-trigger cubic
Member
Author
Contributor
|
@seanmcguire12 I have started the AI code review. It will take a few minutes to complete. |
Contributor
There was a problem hiding this comment.
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)
This was referenced Jun 29, 2026
Merged
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]
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
users need a way to restrict browser sessions to known domains. addresses #2272
what changed
stagehand.context.setDomainPolicy({ allowedDomains })to block HTTP(S) requests that do not match the allowed domainsDomainPolicynow supports bothallowedDomains&blockedDomainsFetchrequest patterns because allowlists require checking every HTTP(S) requestallowedDomainsblocks fromblockedDomainsblocksbehavioural notes
setDomainPolicy({ allowedDomains: [...] })applies to active context sessions & future pages/targetsexample.comallow only that hostname*.example.comallow subdomains, but not the apex domainallowedDomainsare failed withBlockedByClientblockedDomains, it is blocked even if it also matchesallowedDomainssetDomainPolicy()replaces the current policy; callers should pass bothallowedDomains&blockedDomainsin the same call when they want bothsetDomainPolicy(null),{ allowedDomains: [] }, or an otherwise empty policy disables the policy when no blocked domains are presenttest plan
packages/core/tests/unit/domain-policy.test.tsvalidates allowlist normalization, exact/wildcard matching, invalid allowed domain rejection, block-over-allow precedence, non-HTTP(S) continuation & broad HTTP(S)Fetch.RequestPatterngenerationpackages/core/tests/unit/context-domain-policy.test.tsvalidatescontext.setDomainPolicy()enables broad HTTP(S) interception for allowlists, preserves precise blocklist-only interception, returns normalized policy state, continues allowed paused requests & fails disallowed paused requestspackages/core/tests/integration/context-domain-policy.spec.tsvalidates allowed-domain requests are not blocked, non-matching requests fail withERR_BLOCKED_BY_CLIENT, blocked domains take precedence over allowed domains & allowlist policies apply to pages created after the policy is setSummary 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.
context.setDomainPolicy({ allowedDomains?: string[], blockedDomains?: string[] })andcontext.getDomainPolicy(). Domain-only patterns; exact or*.example.com(wildcards do not match the apex). HTTP(S) only. Applies to current and future pages. Clear withnullor empty arrays;getDomainPolicy()returns only set fields.blockedDomainstake precedence overallowedDomains. Allowlists use broad HTTP(S)Fetchinterception; blocklist-only policies keep precise patterns. Logs include the block reason (allowedDomainsvsblockedDomains).StagehandSetDomainPolicyErrorwhen enforcement can’t be guaranteed during target attach. Added runtime validation; malformed inputs (omitted policy, non-array fields, non-string or non-domain patterns) throwStagehandInvalidArgumentError.Written for commit 60bafc3. Summary will update on new commits.