Skip to content

security: sanitize DOM content in LLM prompts to mitigate indirect prompt injection#2063

Open
johnpippett wants to merge 1 commit intobrowserbase:mainfrom
johnpippett:security/critical-fixes-2026-04-28
Open

security: sanitize DOM content in LLM prompts to mitigate indirect prompt injection#2063
johnpippett wants to merge 1 commit intobrowserbase:mainfrom
johnpippett:security/critical-fixes-2026-04-28

Conversation

@johnpippett
Copy link
Copy Markdown

@johnpippett johnpippett commented Apr 28, 2026

Summary

Fixes a Critical security finding: indirect prompt injection via unsanitized DOM content being fed directly into LLM prompts.

Changes

  • Adds sanitizeDomForPrompt() helper in packages/core/lib/prompt.ts that wraps raw DOM/webpage content in clear <<<<STAGEHAND_DOM_BEGIN>>>> / <<<<STAGEHAND_DOM_END>>>> boundaries and escapes any embedded end-marker sequences to prevent boundary breakout.
  • Applies sanitization at all DOM-to-prompt injection points:
    • buildExtractUserPrompt (extract inference)
    • buildObserveUserMessage (observe / act inference)
    • ariaTree tool toModelOutput (agent tool output)
  • Adds unit tests covering boundary wrapping, marker escaping, and prompt builder integration.

Test Results

  • New tests: 5 passed (prompt-sanitize-dom.test.ts)
  • Existing related tests: 55 passed (prompt-observe-variables, snapshot-a11y-tree-utils, snapshot-capture-orchestration, agent-execution-model, llm-middleware)

Audit Reference

  • Source: 31.08 AI Tooling Security Audit Findingsbrowserbase/stagehand
  • Finding: [Critical / Trivial / Single-user] — Indirect prompt injection via unsanitized DOM content (packages/core/lib/prompt.ts, packages/core/lib/v3/agent/tools/ariaTree.ts, packages/core/lib/v3/handlers/actHandler.ts)

Files Changed

  • packages/core/lib/prompt.ts
  • packages/core/lib/v3/agent/tools/ariaTree.ts
  • packages/core/tests/unit/prompt-sanitize-dom.test.ts

Summary by cubic

Fixes a critical security issue: sanitizes DOM content before it enters LLM prompts to prevent indirect prompt injection. Adds clear start/end boundaries and escapes embedded markers; applied across extract, observe, and ariaTree tool flows.

  • Bug Fixes
    • Added sanitizeDomForPrompt() in packages/core/lib/prompt.ts to wrap DOM with <<<<STAGEHAND_DOM_BEGIN>>>> / <<<<STAGEHAND_DOM_END>>>> and escape nested end markers.
    • Applied sanitization in buildExtractUserPrompt, buildObserveUserMessage, and packages/core/lib/v3/agent/tools/ariaTree.ts output.
    • Added unit tests for boundary wrapping, marker escaping, and prompt builder integration.

Written for commit 26a5b7d. Summary will update on new commits. Review in cubic

…injection

Adds sanitizeDomForPrompt() to wrap raw DOM/webpage content in clear
start/end boundaries and escape any boundary-breaking sequences inside
the content. This addresses the critical finding of unsanitized DOM
content being injected directly into LLM prompts.

Affected paths:
- buildExtractUserPrompt (extract inference)
- buildObserveUserMessage (observe / act inference)
- ariaTree tool toModelOutput (agent tool output)

Audit ref: 31.08 AI Tooling Security Audit Findings — browserbase/stagehand
Critical: DOM prompt injection via unsanitized DOM content.
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 28, 2026

⚠️ No Changeset found

Latest commit: 26a5b7d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a 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 Apr 28, 2026
Copy link
Copy Markdown
Contributor

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

Choose a reason for hiding this comment

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

No issues found across 3 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.
Architecture diagram
sequenceDiagram
    participant Agent as Agent / Orchestrator
    participant Browser as Browser Instance
    participant PromptLib as Prompt Library
    participant AriaTool as AriaTree Tool
    participant LLM as LLM Provider

    Note over Agent,LLM: DOM Extraction & Sanitization Flow

    rect rgb(240, 248, 255)
    Note right of Agent: Extraction / Observation Flow
    Agent->>Browser: Get Page DOM / Accessibility Tree
    Browser-->>Agent: Raw DOM String
    
    Agent->>PromptLib: buildExtractUserPrompt(instruction, rawDom)
    
    PromptLib->>PromptLib: NEW: sanitizeDomForPrompt(rawDom)
    Note right of PromptLib: Escapes end-markers &<br/>wraps in <<<<STAGEHAND_DOM_BEGIN>>>>
    
    PromptLib-->>Agent: ChatMessage (Sanitized)
    Agent->>LLM: POST /chat/completions
    end

    rect rgb(255, 240, 245)
    Note right of Agent: Tool Execution Flow (ariaTree)
    LLM-->>Agent: Tool Call: ariaTree()
    Agent->>AriaTool: execute()
    
    AriaTool->>Browser: Capture Accessibility Tree
    Browser-->>AriaTool: Tree Content
    
    AriaTool->>PromptLib: NEW: sanitizeDomForPrompt(content)
    PromptLib-->>AriaTool: Sanitized Content
    
    AriaTool-->>Agent: Tool Output (Sanitized)
    Agent->>LLM: Submit Tool Results
    end

    Note over PromptLib: Security Detail: sanitizeDomForPrompt()<br/>1. Replace "<<<<STAGEHAND_DOM_END>>>>" with escaped version<br/>2. Wrap content in STAGEHAND_DOM boundaries
Loading

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.

1 participant