Skip to main content

11. Security & Ethics

Prompt Injection Attacks (Direct and Indirect)

Prompt injection is when untrusted text in the model context steers the agent away from system policy — toward leaking data, calling the wrong tools, or following attacker goals. Direct injection comes from the user channel; indirect injection hides instructions in retrieved web pages, emails, docs, OCR, or tool output. Defenses assume content can lie: isolate untrusted channels, keep privileges outside the model, detect anomalies, and HITL risky actions. This lesson explains the threat class for builders — not how to weaponize it.

What you will learn

  • Define prompt injection and why agents are exposed
  • Distinguish direct vs indirect injection paths
  • List high-impact abuse goals (exfil, tool misuse)
  • Apply isolation, privilege, detection, and HITL defenses
  • Answer interview questions with short, solid answers
System policy model untrusted text competing in context
Interview staple: the model cannot reliably tell policy from hostile content in the same window.

Definition

Prompt injection is a class of attacks where instructions embedded in untrusted input cause an LLM-powered system to ignore or override intended policy. In agents, success often means unauthorized tool use, data exfiltration through allowed channels, or policy bypass — not merely a cheeky chat reply.

Treat this as an application-security problem with untrusted content in the control plane. Defenses live mainly outside the model (allowlists, isolation, HITL), because prompt-only mitigations are incomplete.

Introduction

This lesson (#2 in Security & Ethics) follows AI Agent Security. You have the threat map; injection is the most common way untrusted content steers the planner.

Next: Data Privacy for Agents — what must not leak even when the model is cooperative.

Why agents are exposed

  • Context mixes system rules with user and retrieved text
  • Models follow fluent instructions even from the wrong source
  • Tools amplify a steered plan into real side effects
  • RAG, email, browsers, and OCR widen the untrusted surface
  • Memory can persist injected instructions across turns

Direct vs indirect

Classify by where the hostile instructions enter.

Direct user injection versus indirect retrieved content
Indirect injection often fools honest users who paste or fetch poisoned content.
  • Direct - adversary controls the user/API message channel
  • Indirect - instructions embedded in third-party content the agent reads
  • Common indirect sources - web pages, tickets, PDFs, images/OCR, tool results
  • Both can aim at tool misuse or data exfiltration
  • Indirect is harder to spot because the human did not type the attack

Abuse goals (defensive view)

  • Override policy (“ignore previous rules” class of attempts)
  • Coerce sensitive tool calls the user did not intend
  • Exfiltrate secrets via allowed channels (email, URL fetch, logs)
  • Poison memory so later sessions stay compromised
  • Social-engineer the agent into revealing system prompts or keys

Defenses

Assume any retrieved string may contain instructions; enforce outside the LLM.

Isolate privilege detect HITL defense pillars
Detection helps; privilege separation and HITL stop damage when detection fails.
  • Isolate - label and segment untrusted content; never merge as system role
  • Privilege - deny-by-default tools; policy not writable by content
  • Detect - scanners/critics for instruction-like retrieved text; alert on odd tool patterns
  • HITL - require humans for send, pay, delete, deploy, secret access
  • Minimize - strip or summarize untrusted docs before tools see raw text
  • Monitor - log tool args and destinations; blocklist exfil patterns

Design patterns that help

  • Two-model pattern - planner sees data; separate executor only runs allowlisted tools
  • Structured outputs - tool args must pass schema validation
  • Capability tokens - short-lived, scoped credentials per tool
  • Content security policy for browsers/fetch tools
  • Clear UX when actions are about to run on the user’s behalf

Failure modes

  • Believing a stronger system prompt alone fixes injection
  • Feeding raw web HTML straight into a tool-calling agent
  • Letting tool output become new “system” instructions
  • No human gate on irreversible actions
  • Shared high-privilege credentials for every tool

Evaluation

  • Red-team suites of direct and indirect cases (internal, controlled)
  • Measure tool-call refusal and HITL catch rates
  • Track false positives so product still works
  • Regression-test after every prompt or tool change

Program / implementation notes

Tag every context block as system, user, or untrusted_retrieved. Never promote retrieved text to system. Schema-validate tool args. Require HITL for write tools. Add logging when retrieved content contains instruction-like phrases — for analysts, not as the only control.

  • Practice defenses on staged fixtures, never on live customer data
  • Next: Data Privacy for Agents

Advantages and limitations

  • Advantage: clear direct/indirect model guides design reviews
  • Advantage: privilege + HITL cut blast radius dramatically
  • Advantage: eval suites catch regressions early
  • Limitation: no detector catches every injection
  • Limitation: heavy filtering can hurt legitimate docs
  • Limitation: new modalities (images, audio) keep opening channels

Key points

  • Prompt injection: untrusted text steers the model against policy.
  • Direct = user channel; indirect = retrieved/tool content.
  • Agents raise impact because tools and memory amplify steering.
  • Defend with isolation, allowlists, detection, and HITL — not prompts alone.
  • Never promote retrieved content to system privileges.
  • Eval and monitor tool patterns continuously.

FAQs

Is prompt injection the same as jailbreaking?

Related but not identical. Jailbreaking often targets model safety refusals in chat. Prompt injection targets application policy and tool control, especially via untrusted context.

Why is indirect injection dangerous for agents?

Honest users fetch poisoned pages or tickets; the agent may follow hidden instructions and call tools without the user typing an attack.

Can we filter all bad instructions with regex?

No. Filters help as a signal but are incomplete. Enforce tool allowlists, schemas, and HITL so missed detections cannot cause full damage.

Where should policy live?

In host code and authZ systems — not only in natural-language system prompts that untrusted content can contradict.

Interview questions

  1. What is prompt injection?

    Answer When untrusted text in the model context causes the system to deviate from intended policy, often toward tool misuse or data exposure.

  2. Direct vs indirect prompt injection?

    Answer Direct comes from the attacker-controlled user/API input. Indirect hides instructions in content the agent retrieves or receives from tools.

  3. Why are agents especially vulnerable?

    Answer They mix untrusted content with policy and can execute tools, so successful steering becomes real side effects, not just text.

  4. Name primary defenses.

    Answer Isolate untrusted content, deny-by-default tool privileges, schema validation, detection/monitoring, and HITL on high-risk actions.

  5. Why are system-prompt-only mitigations insufficient?

    Answer Models can still follow conflicting instructions in the same context; enforcement must happen in code, authZ, and tool gateways.

  6. How do you evaluate injection resilience?

    Answer Run controlled direct/indirect test suites, measure blocked tool calls and HITL catches, and regress after prompt or tool changes.

Related topics

Practice task

Complete each numbered practice step in a notebook or doc before opening the next lesson. Focus on defenses — do not practice attacks on live systems.

  1. List five untrusted content sources your agent reads (web, email, PDF, OCR, tools).
  2. For each source, note whether it is tagged/isolated before entering the model.
  3. Mark which tools an injection must never be able to trigger without HITL.
  4. Design a logging field set for suspicious retrieved-instruction alerts.
  5. Answer interview questions 1, 2, and 4 in two sentences each.