Skip to content

Sample code for: How to Defend a Python RAG App Against Prompt Injection - #924

Open
lpozo wants to merge 9 commits into
masterfrom
prompt-injection-python
Open

lpozo wants to merge 9 commits into
masterfrom
prompt-injection-python

Conversation

@lpozo

@lpozo lpozo commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Where to put new files:

  • New files should go into a top-level subfolder, named after the article slug. For example: my-awesome-article

How to merge your changes:

  1. Make sure the CI code style tests all pass (+ run the automatic code formatter if necessary).
  2. Find an RP Team member on Slack and ask them to review & approve your PR.
  3. Once the PR has one positive ("approved") review, GitHub lets you merge the PR.
  4. 🎉

Code examples for the How to Defend a Python RAG App Against Prompt Injection tutorial. Files live in the prompt-injection-python/ top-level folder, named after the article slug.

Generated from the tutorial markdown with the post-materials skill, then verified locally on Python 3.14 with the pinned requirements: pytest passes offline (4 passed, 5 skipped, 1 xfailed) and end to end (9 passed, 1 xfailed), and demo.py reproduces the article's outputs.

🤖 Generated with Claude Code

lpozo and others added 9 commits October 26, 2026 17:04
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
gpt-5.4-mini's ticket payload no longer gets past the defenses on
gpt-5.6-luna, so Step 3's canary demo had nothing to catch. The new
policy payload leaks past delimiters and tripwire in about half of
runs (19 of 38 measured) and is always withheld by the canary check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Move the app into the src/acme_support package and the tests into
tests/. Replace requirements.txt with a pyproject.toml (uv_build,
pytest in a dev group) and a uv.lock. Add a `demo` script entry
point, so the demo runs as `uv run demo rag policy`, and document
setup and usage in the README.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On gpt-6-luna, the hardened system prompt stops every payload in the
corpus, and the starter still leaks rephrased and policy. Both test
suites pass unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Move the clean and poisoned documents out of corpus.py into
docs/clean/ and docs/poisoned/, so the knowledge base looks like data
the app retrieves rather than source code. corpus.py now loads them
from disk and keeps the same CLEAN and PAYLOADS names.

Rename the "direct" payload to "override", since "direct prompt
injection" means an attack typed into the chat, and every payload here
arrives through a retrieved document.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Clean and poisoned documents now sit side by side in docs/, the way a
poisoned document would hide in a real knowledge base. corpus.py names
the attack files in a POISONED set to split CLEAN from PAYLOADS.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
rag.py is now the undefended starter that readers harden step by step,
so starter.py and the finished tests are gone. demo.py takes a single
payload name, and the README describes the build-along workflow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The script fires one poisoned document at the app and prints the
reply, so `uv run attack policy` says what it does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
corpus.py now loads every document in docs/ without a list of attack
files, the way a real app has to trust its knowledge base. The four
poisoned documents move to attacks/, and attack.py plays the malicious
user: it plants one of them among the real documents and asks an
ordinary question. The attack list now lives only in attack.py.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant