Stop Prompt Injection in RAG: 12 Steps, 90 Min A tutorial published on Shattered.io lays out a 12-step, roughly 90-minute Python pipeline for defending retrieval-augmented generation (RAG) applications against indirect prompt injection, arguing that model providers offer no built-in protection even as Alibaba's Qwen4-Max (shipped November 16, 2026), GPT-6.2 Astra and Sol (November 17) and Gemini 4 Neon (November 18) each pair 1-million-token context windows with no documented injection defense. The finished package, built only on the Python standard library plus pytest 8, stops all 14 attacks in its test corpus while still answering all four benign questions; ablation shows the output guard alone lets 1 of 14 attacks through and the ingestion scanner alone lets 4 through, with only the full stack reaching zero. The author cites developer Simon Willison's December 2023 guidance that builders must assume attackers who can place untrusted text in a system will be able to subvert instructions and trigger any exposed actions. Don't miss new tech stories on Google Add Shattered once in the Google app and our stories show up in your news suggestions. Add now https://www.google.com/preferences/source?hl=en&q=https%3A%2F%2Fshattered.io%2F Why RAG Pipelines Need Their Own Prompt Injection Defense Alibaba shipped Qwen4-Max on November 16, 2026 https://docs.qwencloud.com/changelog/models , followed by GPT-6.2 Astra and Sol November 17 and Gemini 4 Neon https://shattered.io/gemini-4-neon-3-15-pricing-1m-context-four-thinking-levels/ November 18 . Qwen4-Max and Gemini 4 Neon each list a 1-million-token context window, so a retrieval pipeline can now place hundreds of pages of untrusted text beside the system prompt in a single request. Alibaba’s Qwen4-Max documentation covers tool calling, structured output and long context. It documents no dedicated prompt injection defense. Treat injection resistance as an application-layer job, not a built-in model feature: isolate untrusted documents, filter retrieval, authorize tools and validate output. That gap belongs to the application. A retrieval-augmented generation RAG app pulls text from wikis, tickets, PDFs and crawled pages, then pastes it next to the developer’s own instructions. The model reads one long string. Nothing separates "data to summarize" from "orders to follow", so anyone who can write into any indexed source gets a vote on what your assistant does. Security teams call this indirect prompt injection. Fixing it takes a pipeline, not a cleverer system prompt. A taint gate on tool permissions, the approach behind indirect prompt injection defense for agents https://shattered.io/indirect-prompt-injection-defense-taint-gate-12-steps-2026/ , protects what the model can do. RAG needs a second line in front of that: control what gets into the index, what comes back out of it, and what the model may say once it has read it. Developer Simon Willison laid out the right starting assumption in December 2023 https://simonwillison.net/2023/Dec/20/mitigate-prompt-injection/ , and nothing in this month’s releases changes it: “I think you need to develop software with the assumption that this issue isn’t fixed now and won’t be fixed for the foreseeable future, which means you have to assume that if there is a way that an attacker could get their untrusted text into your system, they will be able to subvert your instructions and they will be able to trigger any sort of actions that you’ve made available to your model.” Simon Willison, software developer and researcher, December 2023 The finished Python package stops all 14 attacks in its test corpus and still answers all four benign questions. One ablation result stands out: the output guard alone lets 1 of 14 attacks through, and the ingestion scanner alone lets 4 through. Only the full stack reaches zero. Prerequisites and Versions Everything runs on the Python standard library. The only extra package is pytest, and the live-model mode uses urllib , so there is no vendor SDK to pin or break. Budget about 90 minutes: 40 to write the code, 20 to run and tune the evaluation, and 30 for an optional live run against a real model. | Component | Version | Role in the project | |---|---|---| | Python | 3.12 or newer tested on 3.12.3 | Runtime, dataclasses, X \| None type syntax | | pytest | 8 or newer | Regression suite for attacks and benign questions | | Operating system | Linux, macOS or WSL2 | Any shell that runs python3 | | Network | None for the stub run | The test double model keeps the whole suite offline | | Live model optional | Qwen4-Max qwen4-max , GPT-6.2 Astra or Sol, or Gemini 4 Neon | Needs an endpoint that speaks the chat completions format | | Disk space | Under 1 MB | Twelve small files and a hash list | Real models cost money and answer differently on every run, so the test suite uses StubModel , a deterministic test double that obeys any instruction it finds in retrieved text. It plays the worst-case, suggestible model. That makes each defense observable. If an attack reaches the stub, it succeeds. If a layer blocks it, the test passes for a reason you can name. Passing against the stub proves your plumbing works. It does not prove that Qwen4-Max or GPT-6.2 resists anything. Only a live run against the real model shows that. If you have no retrieval stack yet, the hybrid-search RAG build with evals https://shattered.io/build-rag-pipeline-hybrid-search-evals-12-steps-2026/ gives you one to attach this to. The Store class here is a small keyword retriever, kept simple so the security logic stays visible. Swap in your vector database later, but keep the same three calls: ingest, revoke and search. The Five-Layer Design Behind the Prompt Injection Defense Each layer assumes the one before it failed. That rule explains why the project has five small modules instead of one clever filter. | Layer | Stops | Module | Blind spot | |---|---|---|---| | 1. Ingestion scan | Hidden text, encoded payloads, poisoned updates | scan.py , store.py | Paraphrased instructions | | 2. Tenant-scoped retrieval | Cross-customer leaks, revoked documents | store.py | Anything inside the right tenant | | 3. Context scan | Payloads split across several documents | pipeline.py | Paraphrased instructions | | 4. Prompt envelope | Delimiter spoofing, unmarked untrusted text | envelope.py | A model that ignores the rules | | 5. Output guard | Prompt leaks, exfiltration URLs, rogue actions, malformed output | guard.py | Authorization bugs upstream | Layers 1 and 3 are pattern scanners. They are cheap and reject obvious attacks early, but a determined attacker rewrites the sentence and walks past them. Layers 2 and 5 do not care how the attack is worded. One checks who owns the document, the other checks what the model produced. Layer 4 lowers the odds that a model obeys hostile text, though no prompt wording gives a guarantee. Layer 1 also matters on the write path. Any feature that saves summaries or agent notes back into the index should pass through the same scanner, because a payload that copies itself into new documents turns one poisoned page into many. Researchers treat self-replicating payloads as a real design risk, and the AI worm bug found by OpenAI’s GPT-Red https://shattered.io/openai-gpt-red-ai-worm-bug-2026/ is a recent example of the class. Build the Ingestion Layer: Steps 1 to 3 Step 1: Create the project and virtual environment Start in an empty directory. The commands below create the package folder, a test folder and a virtual environment with pytest installed. On Windows, activate with .venv\Scripts\activate instead. mkdir ragshield && cd ragshield python3 -m venv .venv source .venv/bin/activate pip install pytest mkdir ragshield tests touch ragshield/ init .py python --version Expected output of the last command on the tested machine: Python 3.12.3 Step 2: Write the ingestion scanner The scanner turns a raw document into a ScanResult with cleaned text, a list of findings and a risk score. It runs six checks in a fixed order, and the order is the point. First it applies Unicode NFKC normalization, which folds fullwidth letters back to ASCII. Then it counts and strips zero-width and bidirectional control characters, and it decodes Unicode tag characters the U+E0000 block , which can smuggle a whole sentence in glyphs most viewers never draw. Only after that cleanup does it run the regular expressions. Skip the order and an attacker writes ig U+200B nore , a zero-width space hidden mid-word, and your pattern never fires. The scanner also decodes long Base64 blobs and checks the decoded text against the same patterns. It flags HTML comments and CSS-hidden elements, then deletes them from the text that reaches the index. Findings carry weights. An invisible character alone scores 2, because legitimate Indic and emoji text uses zero-width joiners. A pattern hit scores 4, which is the quarantine threshold. Use the prompt injection detector scoring harness https://shattered.io/prompt-injection-detector-scoring-12-steps-2026/ if you want to compare these hand-written rules against a trained classifier on the same corpus. python import base64 import binascii import re import unicodedata from dataclasses import dataclass, field from urllib.parse import urlparse INVISIBLE = {"\u200b", "\u200c", "\u200d", "\u2060", "\ufeff", "\u180e"} BIDI = {chr c for c in range 0x202A, 0x202F } | {chr c for c in range 0x2066, 0x206A } FLAGS = re.I | re.S def either order a: str, b: str, gap: int = 300 - str: return rf" ?:{a}.{{0,{gap}}}{b}|{b}.{{0,{gap}}}{a} " PATTERNS = { "override": r"\b ignore|disregard|forget \b.{0,20}\b previous|prior|above|earlier \b.{0,20}\b instructions?|rules|guidance \b", "persona": r"\byou are now a|an|the|in \b.{0,40}\b mode|assistant|developer|admin \b", "prompt leak": r"\b reveal|print|show|repeat|output \b.{0,30}\b system|hidden|initial \b.{0,15}\b prompt|instructions?|configuration \b", "tool call": r"\b call|invoke|run|execute \b.{0,30}\b send email|delete records|transfer funds \b", "send to": r"\b send|post|forward|email \b.{0,60}\b to|at \b.{0,30} https?://| \w.+- +@ \w- +\. \w. + ", "format hijack": r"\b respond|reply|answer \s+only\s+with\b", "split leak": either order r"\b ?:recall|print|repeat \b.{0,80}\b ?:configuration|prompt \b", r"\b ?:image|link|url \b", , } COMPILED = {name: re.compile rx, FLAGS for name, rx in PATTERNS.items } IMAGE = re.compile r" \ ^\ \ \ \s https?:// ^ \s + ", FLAGS COMMENT = re.compile r"< -- . ? -- ", FLAGS HIDDEN STYLE = re.compile r"< \w+ ^ style\s =\s \" ^\" ?:display\s :\s none|font-size\s :\s 0|visibility\s :\s hidden ^\" \" ^ . ?