Securing RAG and data cheat sheet
Build a RAG service and map it (lesson 1)
| Task | Command or code |
|---|---|
| Build the TF-IDF index of the chunks | index = Index(load()) |
| Search the best chunks for a question | index.search(question, 3) |
| Search only the chunks that may be read | index.search(question, 3, allow=ids) |
| Build the prompt: rules, then sources, then question | build_prompt(question, chunks) |
| Ask with a fake model in a test | answer(index, question, model=echo) |
| Show who can write to a file | ls -l kb.json |
Poison a knowledge base (lesson 2)
| Task | Command or code |
|---|---|
| Find the place of a chunk in the ranking | position(index, question, "pub-returns-update") |
| Count the answers that hold a marker | python3 detect.py recorded-02.json "90 days" |
| Ask the model with five seeds and count | python3 live02.py |
Provenance and review (lesson 3)
| Task | Command or code |
|---|---|
| Hash a text with SHA-256 | hashlib.sha256(text.encode()).hexdigest() |
| Hash what an approval covers: id, level, source, owner, text | chunk_hash(chunk) |
| Record the approval of reviewed chunks | python3 provenance.py approve kb.json approvals.json all |
| Screen chunks: accept, queue, reject | python3 provenance.py check incoming.json approvals.json |
| Read the exit code of the last command | echo "exit code $?" |
| Keep only accepted chunks | [c for c in chunks if screen(chunks, approvals)[c["id"]][0] == "accept"] |
Access control in retrieval (lesson 4)
| Task | Command or code |
|---|---|
| List the ids that a user may read | allowed_ids("gus", kb) |
| Filter before ranking | answer(index, q, allow=allowed_ids(user, kb)) |
| Run the table of user, question, ids | python3 test_access.py |
| Run the same table without the filter | python3 test_access.py nofilter |
| Test a filter on the text of answers | python3 late_filter.py |
Embeddings, index files and ranking tricks (lesson 5)
| Task | Command or code |
|---|---|
| Save the index to a file | python3 peek.py |
| Write an index file for the public level, and count the secret words in it and in the full index | python3 partition.py |
| Probe a score side channel, then the fix | python3 probe.py, python3 probe.py safe |
| Share of different words in a text | distinct_ratio(text) |
| Measure a stuffed chunk, with and without defences | python3 stuff.py |
Private data (lesson 6)
| Task | Command or code |
|---|---|
| Redact emails, cards and staff ids | clean, counts = redact(text) |
| Check a number with the Luhn sum | luhn("4111111111111111") |
| Write a log line without private data | safe_log(log, user, text) |
| Keep only chunks of the last 90 days | keep_recent(chunks, "2026-11-21", 90) |
| Run the redaction tests | python3 test_redact.py |
Measure quality and leakage (lesson 7)
| Task | Command or code |
|---|---|
| Run the plain service on the labelled set | python3 evaluate.py plain kb.json poison.json |
| Run the controlled service | python3 evaluate.py controlled kb.json poison.json |
| Show only the summary line and the exit code | python3 evaluate.py plain | tail -1 then echo "exit code ${PIPESTATUS[0]}" (bash; zsh: ${pipestatus[1]}) |
| Set the thresholds | MIN_PRECISION, MAX_LEAKS in evaluate.py |
Architecture and deletion (lesson 8)
| Task | Command or code |
|---|---|
| Check where each control sits | python3 archcheck.py arch.json |
| Rebuild the index after a delete | store.rebuild() |
| Make a changed level reach the index | store.relabel(cid, level) |
| Test that deleted and changed text is gone | python3 test_store.py |
Govern the knowledge base (lesson 9)
| Task | Command or code |
|---|---|
| Report the gaps of the catalogue | python3 govcheck.py catalogue catalogue.csv kb.json 2026-11-21 |
| Test a proposal for a new source | python3 govcheck.py source proposal.json |
The finished service (lesson 10) and the review (lesson 11)
| Task | Command or code |
|---|---|
| Build the lab folder after lesson 6, from the zip | sh kr-lab.sh 06-keep-private-data-out-of-prompts-logs-and-stores ~/krlab/securing-rag |
| Make the files that lessons 3 and 5 create | bash catchup.sh |
| Approve the tickets (needed by lesson 10 and its suite) | python3 provenance.py approve tickets.json approvals.json all |
| Run the test suite | python3 test_securerag.py |
| Run the service and show what it held back | python3 securerag.py |
| Test the colleague's service | python3 scenario_tests.py |
Safety habits
- Filter by user before the ranking and before the prompt is built. Never after the answer.
- Take the user from the sign-in on the server. Never from the question or the request.
- Deny by default: a user who is not in the table reads nothing.
- Index only chunks with complete provenance (a valid level too) and an approval that matches the exact text, level, source and owner.
- Redact emails, cards and identifiers before indexing, before prompting and before logging.
- Never show a similarity score, a rank or a count of hidden chunks to a user.
- Set a ceiling for
kon the server: a caller must not choose the size of the prompt. - Test a deletion on the index, not on the document store, and clean every copy: cache, log and backup.
- Take labels and permissions from the source on every rebuild, and test that a change of level reaches the index.
- Never share an answer cache between users.
- Test with a fake model that returns its prompt, and decide results by code, not by what a model writes.
- Keep poison, stuffed and test files out of the real knowledge base.
- Give each source an owner, a review cycle and a retention period, and run the checker in the build.