← All posts

Prompt injection in LLM apps starts below the prompt

Prompt injection is moving from chat demos into retrieval pipelines and browser agents. For teams shipping LLM systems, the key work is trust boundaries, typed tool contracts, and adversarial tests across the full data flow.

Practitioners who ship LLM features often focus on prompts, model choice, and latency. The quieter risk sits in the retrieval layer and the tool layer. If your system reads untrusted text, browses the web, or calls internal tools, your attack surface is larger than the chat box.

Simon Willison’s November sponsors-only newsletter points to fresh prompt-injection material, including work on browser agents and model behavior under adversarial instructions. The useful takeaway is simple. Treat every token from outside your trust boundary as hostile until you have constrained where it flows.

This matters in production systems because prompt injection is rarely a single dramatic failure. It is often a chain. A crawler reads a page. A summarizer repeats an embedded instruction. An agent calls a tool with arguments derived from hostile text. A support bot exposes hidden data because retrieval pulled in a poisoned document. You need inspection points across the chain, not a single filter at the front.

Map the trust boundaries first

Start by writing down where untrusted text enters your system. Most teams list user chat input and stop there. That misses the common paths.

Inspect these sources:

  • Web pages fetched by browsing agents
  • PDFs and office files in retrieval pipelines
  • Issue trackers, tickets, and CRM notes
  • Email content and attachments
  • OCR output from screenshots and scans
  • Tool outputs from third-party APIs
  • Transcripts from voice systems

For each source, record four facts:

  1. Who controls the content.
  2. Whether the content is stored for later retrieval.
  3. Which model sees it.
  4. Which tools or data sources the model can reach after reading it.

This exercise often exposes hidden escalation paths. A document ingestion pipeline looks harmless until you notice the same index feeds an agent with write access to a ticketing system. Or a browsing agent looks isolated until you see it shares credentials with an internal knowledge tool.

The core design rule is separation. Untrusted content belongs in a lower-trust lane. Sensitive tools belong in a higher-trust lane. Crossing between them needs explicit policy checks, narrow schemas, and logging.

A useful pattern is to split your architecture into three roles:

  • Reader, which ingests and summarizes hostile content
  • Planner, which proposes actions but has no side effects
  • Executor, which runs a small set of approved actions from validated structured input

When teams skip this split, the same model both reads attacker-controlled text and decides which tools to call. That is where instruction hierarchy becomes fragile.

Constrain tools like public APIs

Most prompt-injection writeups focus on the model. Production failures often come from weak tool contracts.

Treat every tool exposed to a model as if it were a public internet API. Give it the smallest scope possible. Validate every field. Reject free-form arguments where a closed set works.

Look for these weak spots:

  • A file-read tool with broad path access
  • A browser tool that carries authenticated session cookies across domains
  • A search tool that returns raw page text and lets the model decide what to trust
  • An internal admin tool exposed with natural-language parameters
  • A send-email or post-message tool without recipient allowlists
  • A SQL tool that accepts free-form query text

Good tool design looks boring. Inputs are typed. Allowed actions are enumerated. Object identifiers come from prior trusted steps, not from model-generated guesses. Side effects require a second gate.

For example, do not expose a tool like this:

{"tool":"refund_order","args":{"order_ref":"<free text>","reason":"<free text>","amount":"<free text>"}}

Prefer a shape like this:

{
  "tool": "request_refund_review",
  "args": {
    "order_id": "ord_12345",
    "reason_code": "duplicate_charge"
  }
}

Then enforce server-side checks:

  • order_id exists and belongs to the active tenant
  • reason_code is in a fixed enum
  • refund amount is derived server-side
  • final execution requires a separate approval path

This reduces the damage from a poisoned context window. The model no longer controls the dangerous parts.

Test for indirect injection, not only direct attacks

Teams often test with obvious payloads pasted into chat. Real incidents lean toward indirect injection. The attacker places instructions in a page, document, code comment, hidden HTML element, or support transcript, then waits for your system to ingest it.

Build tests around content your pipeline already handles:

  • A web page with hidden text telling the agent to reveal system prompts
  • A PDF with an instruction block after the visible report content
  • A markdown document with malicious links and role-play text
  • A support ticket thread containing credential-harvest prompts
  • A spreadsheet cell with command-like natural language for downstream summarizers

What to verify:

  • Does the model repeat or obey the hostile instruction.
  • Does the agent call a tool after reading it.
  • Does retrieval rank the poisoned chunk highly.
  • Do logs preserve the source document and chunk IDs.
  • Does your policy layer block the action.

This is where a dedicated red-team harness helps. The Prompt-Injection & System-Prompt-Leak Tester fits this use case for systems you control. The goal is not a single score. You want repeatable cases tied to your real tool graph.

Measure outcomes with concrete counters:

  • unsafe tool call attempts per 1,000 tasks
  • blocked policy violations per tool
  • retrieval hits on seeded poisoned documents
  • prompt leakage rate under hostile browsing tasks
  • percentage of actions requiring human approval

If you do not track these, you will not know whether a model upgrade or prompt tweak made your system safer or weaker.

Defend the retrieval pipeline, not only the agent prompt

Retrieval-augmented generation creates a broad injection surface because it turns stored text into model context. Many teams filter user input but trust their own index. That trust is misplaced when outside parties influence source documents.

Inspect your retrieval stack in layers.

At ingestion:

  • tag each document with source, owner, and trust level
  • strip active content and normalize formats
  • keep the raw original for forensics, but feed a sanitized derivative to models
  • detect hidden text, oversized repeated strings, and unusual instruction phrases

At chunking and ranking:

  • keep trust metadata on every chunk
  • down-rank low-trust chunks for tasks with sensitive tool access
  • prevent one document from dominating top-k results
  • separate policy text from user content so retrieval does not blend them

At answer generation:

  • pass citations and trust labels with each chunk
  • forbid the model from treating retrieved instructions as policy
  • require a second model or rule layer to classify requested actions before execution

A common failure mode is policy collision. The system prompt says one thing. A retrieved document says another. The model blends both and follows the hostile one because it is recent, specific, or repeated. You reduce this risk by carrying trust metadata outside the natural-language context window, then using code to enforce it.

Simple example. Instead of sending only text passages, send a structured bundle:

{
  "chunks": [
    {"id":"c1","source":"kb_internal","trust":"high","text":"..."},
    {"id":"c2","source":"web","trust":"low","text":"..."}
  ],
  "policy": {
    "web_chunks_are_non_authoritative": true,
    "tool_use_requires_trusted_evidence": true
  }
}

Then let application logic, not the model alone, decide whether low-trust evidence is enough for a tool call.

Watch browser agents and cross-origin data flow

Browser agents deserve special attention because they mix hostile content, credentials, and action-taking in one loop. A page does not need remote code execution to influence an agent. It only needs text the model reads and obeys.

Inspect these browser-agent controls:

  • Cookie and local-storage isolation per domain
  • No credential reuse between unrelated origins
  • Allowlists for navigable domains in sensitive tasks
  • Download restrictions and file-type controls
  • Confirmation gates before form submission or outbound messaging
  • Screenshots and DOM snapshots tied to every action step

One common mistake is letting the agent browse the open web while signed into internal systems in the same session. Another is allowing hidden DOM content, alt text, or off-screen elements to enter the model context without any trust marking.

Logging matters here. Keep a step log with:

  • visited URL
  • final URL after redirects
  • extracted text length
  • whether the page was trusted or untrusted
  • tools invoked after the page was read
  • user-visible confirmation, if any

These records help you trace whether a failure started in retrieval, browsing, prompt policy, or tool validation.

What to watch next

Expect more work on indirect injection against agents, especially browser-based systems and enterprise retrieval stacks. Watch for benchmarks tied to real tool use, not only prompt leakage demos. In your own systems, focus on trust boundaries, typed tools, retrieval metadata, and repeatable adversarial tests. Those controls age better than prompt wording.