Skip to content

Add Domain whitelisting for a stagehand session #2272

Closed

Description

@garrettgruss-realpage

I want to enforce a guardrail to terminate a session if an agent clicks on a link that is outside of an allowed list of domains. See claude proposal:

When running an autonomous agent.execute() loop, the agent can freely navigate to any URL. The goto tool calls page.goto(url) with no validation (goto.ts:25), as does the CUA handler (v3CuaAgentHandler.ts:573). There is currently no built-in way to constrain an agent to a set of approved domains, or to terminate a session when it "wanders" off-domain (via a tool call, an HTTP redirect, or JS-driven window.location changes).

For regulated / security-sensitive automation, letting an LLM-driven agent navigate to arbitrary domains is a real risk (data exfiltration to an unexpected host, following a malicious link/redirect, credential entry on a look-alike domain). Today the only mitigation is to roll your own check.

Describe the solution you'd like

A first-class config option to declare allowed domains and a policy for what happens on violation. Proposed shape:

const stagehand = new Stagehand({
// ...
navigationGuardrail: {
allowedDomains: ["example.com", "*.example.com"], // exact + wildcard subdomains
onViolation: "abort", // "abort" | "block" | "warn"
},
});
"abort" — terminate the run, surfacing the existing AgentAbortError with a descriptive reason.
"block" — cancel the navigation/keep the agent on the current page and feed an error back to the model.
"warn" — log only.
The most robust enforcement seam is Page.onFrameNavigated (page.ts:456), since it catches all navigation — agent goto calls, redirects, and client-side location changes — not just tool invocations. CDP request interception (Fetch.enable / Network.setBlockedURLs, not currently used in the codebase) would enable true preventive "block" behavior.

Describe alternatives you've considered

The hooks exist to approximate this today, but each has gaps:

signal + onStepFinish (agent.ts:305, agent.ts:126): check page.url() after each step and call controller.abort() if off-domain. Works, but is reactive — the off-domain page has already loaded, and the check only runs at step boundaries, so one off-domain action can slip through before the abort. It also misses mid-step redirects.

const controller = new AbortController();
const ALLOWED = new Set(["example.com"]);
await agent.execute({
instruction: "...",
signal: controller.signal,
callbacks: {
onStepFinish: async () => {
const host = new URL((await stagehand.page).url()).hostname;
if (![...ALLOWED].some(d => host === d || host.endsWith("." + d)))
controller.abort(Off-domain navigation blocked: ${host});
},
},
});
Validate inside the goto tool / CUA goto case — preventive for tool-driven navigation, but a fork is required and it still misses redirects and JS navigation.

A built-in guardrail at the navigation layer would cover all of these uniformly and be safe-by-default for security-conscious users.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions