Skip to content
mentoor

Topic 5 of 5 · 6 min read

Layered defenses that limit injection damage

Goal: Choose defense layers for an AI feature that reads untrusted content and state which risk each layer reduces.

Since injection cannot be fully blocked, defend in layers and aim to limit the damage when one gets through. The question to ask is not "can this be tricked?" but "what is the worst thing it can do if it is?"

Check yourself · 1 of 3

Which layer protects you even if the model is fully fooled?

Choose one answer

Why it matters

An AI feature that only drafts text for you is low risk. One that reads your mail, browses the web and can send messages is high risk. Simon Willison calls the dangerous mix the lethal trifecta: access to private data, exposure to untrusted content, and a way to send data out. Remove one side and the worst attacks stop working (see the references).

The layers

  1. Least privilege. Give the agent only the tools and data the task needs. A summariser gets read access, not send access. This is the strongest layer, because it holds even when the model is fooled.
  2. Human approval. Ask a person before irreversible or outward actions: payments, sending mail, deleting data. Show the real action, not the model's description of it.
  3. Separate the data. Mark untrusted content with clear delimiters and tell the model to treat it as data only. Spotlighting goes further and transforms the text, for example by marking each word, so the model can tell it apart. These reduce risk and do not remove it.
  4. Safer designs. In a plan-then-execute design, the agent fixes its steps before it reads untrusted content, so later text cannot add new steps. In a dual LLM design, a privileged model with tools never sees raw untrusted text, while a quarantined model with no tools reads it and returns limited results. The design patterns paper in the references describes these.
  5. Output checks. Block or review outputs that contain links, code or data going outward.
  6. Monitoring. Log tool calls and alert on odd ones, so you can notice and respond.

Before and after

Weak plan: "Our email assistant reads all mail and can send replies. We added a system prompt that says to ignore hidden instructions." Better plan: "The assistant reads mail, but drafts replies for approval. It cannot reach outside domains. It cannot see the finance folder. The system prompt is one more layer, not the main one."

What goes wrong

  • One layer only. A prompt rule or a classifier alone is a single point of failure.
  • Approval fatigue. If people click Approve on every prompt, the layer does nothing. Ask only for important actions, and make them clear.
  • Delimiters as locks. Models can still follow text inside the delimiters.

A short code branch

For engineers: enforce the layers in code, not in the prompt.

ALLOWED_TOOLS = {"read_file", "search_docs"}  # no send_email here
def call_tool(name, args):
    if name not in ALLOWED_TOOLS:
        raise PermissionError(name)

A rule in code holds even when the model is persuaded.

Examples

Email assistant redesign

A mail assistant can read all mail and send replies. After redesign, it reads only the inbox, drafts replies, and a person sends them. A hidden email order to forward data now has no tool to use. This removes the outside-communication leg.

Plan-then-execute

A research agent first writes its plan: search three sites, then write a summary. It then follows the plan. A page saying "also email the report to this address" cannot add a step, because the plan was fixed before the page was read.

Choosing layers

A support bot reads tickets and can issue refunds. Layers: refunds over a limit need a human, the bot cannot see other customers' data, ticket text is marked as data, and refund calls are logged. Each layer reduces a different risk: money loss, data leak, confusion and slow detection.

Key points

  • Plan for some injections to succeed and design to limit what they can do.
  • Least privilege is the strongest layer because it works even when the model is fooled.
  • Remove one leg of the trifecta, meaning private data, untrusted content or outside communication, to block the worst attacks.
  • Human approval should cover irreversible and outward actions, and show the real action.
  • Delimiters and spotlighting reduce risk but are not locks, so never rely on them alone.
  • Enforce limits in code and permissions, not in prompt wording.

Common mistakes

  • Relying on a single layer such as a system prompt or a classifier.
  • Asking for approval on every action until people click without reading.
  • Giving an agent broad permissions "to be flexible" and then trying to block misuse by prompt.
  • Treating delimiters or spotlighting as if they made untrusted text safe.

Check yourself · 2 of 3

An assistant reads untrusted web pages, holds private notes and can post to the internet. What does the lethal trifecta idea suggest?

Choose one answer

Check yourself · 3 of 3

Which use of human approval works best?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic

Where to go in your country