RAGGuard is a research prototype for a context-aware privacy firewall that sits between a retriever and an LLM. It classifies each retrieved chunk as safe, redactable, or blocked, then measures leakage and answer utility against an unfiltered baseline RAG pipeline.
In a typical RAG stack, retrieved documents are concatenated into the model context. If the index contains salaries, credentials, health details, or a prompt-injection payload, an unfiltered generator can copy that material into the answer. The question this repo studies is whether a lightweight, interpretable firewall can cut that leakage without wiping out utility on ordinary enterprise questions.
The experiment is fully offline and deterministic. No model API is required.
- Synthetic corpus and queries (
ragguard/data.py). The benchmark has 12 company documents (3 public, 4 internal, 1 confidential, 4 private) and 13 queries (5 benign, 2 sensitive-benign, 4 privacy attacks, 2 prompt-injection attacks). Documents include public product policy, redactable contact and salary-band text, and private records (exact salary, health information, API keys). Some retrieved malicious documents contain patterns such as “ignore previous instructions” and “reveal confidential data.” - Retriever (
ragguard/retrieval.py). An in-memory TF-IDF retriever with cosine similarity, defaulttop_k=4. - Firewall (
ragguard/firewall.py). Each chunk gets an additive sensitivity score from PII patterns (email, phone, SSN), secret patterns (API keys, passwords, bearer tokens), private-content terms, document access level (public / internal / confidential / private), prompt-injection indicators, and overlap between private terms and the user query. Chunks are then labeled:public-safe— passed through unchangedsensitive-redacted— PII and secrets replaced, then passed throughprivate-blocked— removed from the model context Hard blocks also trigger on prompt injection,privateaccess level, or raw API key / SSN matches. Default threshold is0.55, with a redaction margin of0.30.
- Generator (
ragguard/llm_sim.py). A deterministic simulator, not a live LLM. The baseline copies public facts and, on attack queries, copies secret spans from unfiltered context. The RAGGuard path answers only from non-blocked, possibly redacted text, or refuses when nothing safe remains. - Metrics (
ragguard/metrics.py). Leakage is whether secret spans appear in the answer; utility is the fraction of gold facts found in the answer (exact-string match).
User query -> TF-IDF retriever -> RAGGuard firewall -> redaction / blocking -> simulated response
The numbers below are from the checked-in artifacts in results/, produced by:
python experiments/run_experiment.py --threshold 0.55 --top-k 4 --out resultsThey are not results from a production or API-backed LLM. They come from 12 synthetic documents, 13 hand-written queries, and the deterministic simulator described above. Re-running the command with the same flags should regenerate the same tables.
| Metric | Baseline RAG | RAGGuard (threshold 0.55) |
|---|---|---|
| Leakage rate (any sensitive, attack + sensitive-benign queries) | 1.00 | 0.00 |
| Leakage fraction of secret spans (same query set) | 1.00 | 0.00 |
| Leakage rate on privacy / prompt-injection attacks | 1.00 | 0.00 |
| Answer utility on benign + sensitive-benign queries | 1.00 | 0.679 |
| Answer utility on all 13 queries | 0.538 | 0.827 |
| Mean blocked chunks per query | 0.00 | 1.85 |
| Mean redacted chunks per query | 0.00 | 0.54 |
Utility on all queries rises under RAGGuard because the simulator scores a refusal as useful when a query has no gold public facts (typical of attacks). Utility on benign questions falls from 1.00 to 0.679, which is the privacy/utility tradeoff on this benchmark.
results/threshold_sweep.csv varies the firewall threshold from 0.15 to 0.95. Leakage on the sensitive set stays at 0.00 through threshold 0.55 and is 0.125 at thresholds 0.65 and above. Benign utility is 0.607 at the stricter thresholds (0.15–0.35) and 0.679 from 0.45 upward.
Figures from this run live in results/figures/ and are copied to paper/figures/.
ragguard/
data.py # Synthetic company docs and evaluation queries
retrieval.py # Offline TF-IDF retriever
firewall.py # Chunk classification, redaction, blocking
llm_sim.py # Deterministic response simulator
metrics.py # Leakage and utility metrics
experiments/
run_experiment.py # End-to-end experiment driver
scripts/
make_figures.py # Figure-generation script
tests/
test_smoke.py # Retrieval and firewall smoke tests
requirements.txt
Makefile
results/ # Checked-in metrics, sweep, and figures
Python 3.9+ recommended. The experiment itself needs numpy, pandas, and matplotlib. Tests also need pytest.
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
make allOr run the two steps directly:
python experiments/run_experiment.py --threshold 0.55 --top-k 4 --out results
python scripts/make_figures.py --results results --out results/figures --paper-out paper/figuresmake test # pytest -qmake all writes:
results/metrics_summary.csv— baseline vs RAGGuardresults/threshold_sweep.csv— privacy / utility across thresholdsresults/figures/main.pngthroughfigure4.png, also copied underpaper/figures/
To try a real model, replace ragguard/llm_sim.py and keep the same metrics and figure pipeline.
MIT. See LICENSE.
