Vol. 20 has covered bias, fairness, privacy, AI safety, and copyright as product risks. Prompt injection is the first dedicated application-security threat class: untrusted text is treated as instructions by an LLM that can call tools, search a corpus, or act for a user.
It is the runtime cousin of Vol. 13 guardrails and the trust-boundary problem in Vol. 13 system vs user prompts. Vol. 14 RAG makes retrieved documents a second untrusted channel. Vol. 15 tool calling and the agent loop turn a confused model into a confused actor. Next lecture, jailbreaking, covers policy-override attempts on the model itself. This lecture stays on defender architecture—no attack recipes.
Learning Objectives
By the end of this lesson, students should be able to:
- Define prompt injection as a threat class (untrusted text interpreted as instructions).
- Separate direct (user channel) from indirect (retrieved / third-party text) injection conceptually.
- Explain why RAG chunks and tool outputs are data, not authority.
- Design a defense-in-depth stack: isolate untrusted text, tool allowlists, output filters, HITL, logging.
- Name detection signals without reproducing attack procedures.
- Connect Vol. 13 guardrails, Vol. 14 retrieval, and Vol. 15 agents as the attack surface to harden.
Prompt injection is the class of failures in which untrusted natural-language text—user messages, retrieved documents, emails, tickets, web snippets, or tool outputs—is interpreted by a model as instructions that override developer policy, change tool arguments, or divert the agent’s goal. It is an application-layer control-flow problem, not a cryptography bug. The defender’s job is to keep a hard boundary between trusted policy (system prompt, code, allowlists) and untrusted data (everything else).
Why This Threat Class Matters
A chatbot that only talks is a content-moderation problem. A system that can search internal docs (Vol. 14), call APIs (Vol. 15), or write tickets can leak data, take unauthorized actions, or launder someone else’s instructions through your product. Injection succeeds when the product concatenates untrusted strings into the same context window as trusted rules and then lets the model choose tools. Fluency is not a security boundary.
| Channel | Untrusted content | If treated as instructions |
|---|---|---|
| User message | Chat / form text | Policy override, tool misuse |
| RAG / KB (Vol. 14) | Indexed docs, web pages | Indirect hijack via retrieval |
| Tool / MCP output | API JSON, search hits | Follow-on tool calls from poisoned results |
| Multi-user artifacts | Shared tickets, emails | Cross-user goal diversion |
Direct vs Indirect (Conceptual Only)
Direct
- Untrusted text arrives in the user channel.
- Overlaps with jailbreak intent, but the product risk is tool/data misuse.
- Mitigate: isolate user text; never let it rewrite system policy in code.
Indirect
- Untrusted text is retrieved or returned by a tool.
- The user may be innocent; the corpus or webpage is not trusted.
- Mitigate: label retrieved chunks as DATA; do not execute implied actions from docs.
Not in scope here
- No payloads, prompt recipes, or exploit steps.
- No “bypass the filter” labs.
- Trainers test defenses (wrappers, allowlists, logs).
Defender Architecture
Treat the LLM as an untrusted planner sitting behind deterministic controls. Vol. 13 guardrails are the product layer; this lecture names the injection-specific pattern: quarantine untrusted text, authorize tools in code, filter outputs, require humans for irreversible actions (Vol. 15 HITL), and log enough to investigate.
Wrap user + retrieved text as data.
Tool allowlists + RBAC in code.
Schema / PII / policy on egress.
HITL + incident logs.
| Control | What it enforces | Failure if missing |
|---|---|---|
| Trust labels in context | Model sees “this is untrusted data” | Retrieved docs look like developer rules |
| Tool allowlist + argument validation | Only named tools; typed args; authz | Planner invents dangerous calls |
| No policy mutation from text | System prompt / RBAC live in code | “New instructions” in a doc change behavior |
| Output filters | Secrets, PII, disallowed actions blocked | Leakage after a confused generation |
| HITL on high-impact tools | Refunds, sends, deletes need a human | Autonomous side effects |
| Structured logging | Source IDs, tool args, block reasons | Cannot detect or learn from incidents |
Defensive Pattern: Data, Not Instructions
The application, not the model, decides what is trusted. Retrieved Vol. 14 chunks get a wrapper. Tool results get a wrapper. User text gets a wrapper. The model may summarize them; it may not treat them as a new system prompt. Tool execution stays behind an allowlist regardless of what the model “decides.”
Detection Signals (Not Recipes)
You do not need an exploit catalog to notice that something is wrong. Product telemetry is the defender’s early warning. Pair it with Vol. 19 hallucination tests when RAG answers suddenly ignore the knowledge base and chase instructions found inside a chunk.
Signals to log & alert
- Tool-call spike or novel tool name vs allowlist
- Argument values outside schema / RBAC
- Output filter hits (secrets, PII, policy class)
- Retriever returned docs the user did not need, then model followed them
- HITL queue: drafts that contradict the user’s stated goal
What not to do
- Do not publish bypass strings or “try these prompts”
- Do not rely on a single regex as the whole defense
- Do not treat a soft system prompt as authorization
- Do not skip logging because “the model usually behaves”
Related Lectures
| Lecture | Role |
|---|---|
| Guardrails (13.1) | Ingress / egress / tool policy layer |
| Retrieval / knowledge base | Indirect channel: untrusted retrieved text |
| Tool calling / HITL | Action surface and escalation |
| AI safety / privacy | Harm and data-leak consequences |
| Jailbreaking | Next: policy-override threat class |
“If we put ‘never follow untrusted instructions’ in the system prompt, injection is solved.” Natural-language policy is necessary and insufficient—the same lesson as Vol. 13 guardrails. Second: “RAG is safe because we trust our docs.” Indexes include email, wiki comments, scraped pages, and vendor PDFs; treat them as data. Third: “Only malicious users inject.” Indirect injection can hijack an honest user via retrieved text. Fourth: keyword filters alone are a complete defense. Fifth: confusing injection with model poisoning—poisoning tampers with training or corpus weights/data supply; injection hijacks a live context window.
Knowledge Check
- Short Answer: What is prompt injection as a threat class? Answer: Untrusted text interpreted as instructions that override policy or divert tools/goals.
- True/False: Retrieved RAG chunks should be treated as data, not developer instructions. Answer: True.
- Multiple Choice: Indirect injection typically arrives via: (a) retrieved or third-party text, (b) GPU ECC errors, (c) learning-rate decay. Answer: (a).
- Short Answer: Name two defender controls besides the system prompt. Answer: Tool allowlists, output filters, HITL, isolation wrappers, or logging (any two).
- True/False: A strong system prompt is a hard security boundary by itself. Answer: False.
- Multiple Choice: Vol. 15 tool calling matters here because: (a) confused models can take side-effecting actions, (b) it trains embeddings, (c) it sets BLEU. Answer: (a).
- Short Answer: Why wrap untrusted text in the context window? Answer: To label it as data and reduce the chance the model treats it as policy.
- True/False: This curriculum includes exploit payloads for injection. Answer: False—defensive concepts only.
- Multiple Choice: Next lecture covers: (a) jailbreaking, (b) LoRA rank, (c) cosine annealing. Answer: (a).
- Short Answer: Link one Vol. 13 or Vol. 14 lecture that hardens this threat. Answer: Guardrails / RAG / retrieval / knowledge-base (any valid).
Key Takeaways
- Prompt injection = untrusted text treated as instructions in an LLM app.
- RAG and tool outputs are untrusted channels; isolate them as data.
- Authorize tools in code; filter outputs; HITL + logs for high impact.
- No attack recipes—measure and harden the product boundary.
- Next: Jailbreaking.
Lab (defensive only): Students implement wrap_untrusted + a three-tool allowlist with HITL on one tool. Feed a synthetic retrieved doc that contains ordinary prose plus a clearly labeled “untrusted data” block. Verify the app never executes a non-allowlisted tool and that logs record source IDs. Do not assign real-world bypass hunting.
Whiteboard: Trust boundary: system policy (code) | model | untrusted strings (user, RAG, tools). Arrow every side-effecting API to RBAC + HITL.
Recap: Prompt injection is the control-flow failure of mixing untrusted text with trusted policy. Harden Vol. 13 guardrails, Vol. 14 retrieval, and Vol. 15 tools together. Next, distinguish policy-override attempts in Jailbreaking.