Prevent sensitive information leakage in RAG pipelines.
RagContextGuard is a lightweight Python middleware that analyzes retrieved document chunks before they reach your LLM. It detects when innocent-looking chunks combine to reveal secrets.
pip install rag-context-guardRAG systems retrieve multiple document chunks to answer questions. Even if each chunk is safe individually, their combination can expose secrets. For example, one chunk might contain salary ranges while another lists employee names—separately they're harmless, together they reveal payroll data. RagContextGuard prevents this by analyzing the full context set before it reaches the LLM.
Classified metadata is a trusted input. RagContextGuard assumes classification labels come from a trusted source (your ingestion pipeline, vetted taxonomies, or a carefully-controlled LLM classifier). The guard does not validate label semantics; it only checks whether the set of labels triggers a policy rule.
If an attacker can inject arbitrary classification labels into chunk metadata (e.g., by compromising your vector DB or classifier), they can bypass or trigger rules at will. Treat classification metadata as security-critical configuration.
For production, we recommend:
- Label only from trusted ingestion-time classifiers (not per-query)
- Validating classification strings against an allowlist (
/^[a-z_]+$/) - Auditing classification sources regularly
- Classifications: Each chunk carries one or more labels (e.g.,
"financial","pii","confidential"). These come from your existing metadata or an optional LLM classifier you provide. - Set Analysis: RagContextGuard checks whether the union of all chunk classifications satisfies any policy rule's
trigger.all_presentcondition. - Path Analysis: Multi-hop collusion detection finds when a shared classification bridges two otherwise-safe rules, creating a dangerous combination.
- Actions: Rules can
block(halt pipeline) orwarn(log only).
Run the included demo to see RagContextGuard in action:
python demo.pyThe demo simulates a RAG retrieval with 3 chunks — one combines financial data with an entity identifier, triggering a policy violation.
The project includes a comprehensive test suite. Run it to verify the installation:
python test_comprehensive.pyExpected output: [PASS] All tests passed! (9/9 tests). If you've added custom policies or modified the engine, run this to verify correctness.
Policies are YAML files. Create your own or use the built-ins.
rules:
- name: financial_entity_reveal
description: "Block combining financial metrics with entity identifiers"
action: block # or "warn"
trigger:
all_present:
- financial
- entity_identifier
message: "Financial data combined with entity identifier — client financials exposed"Matching: A rule fires when all classifications listed in trigger.all_present appear across the retrieved chunk set (any chunk, any order). A chunk may carry multiple classifications.
from rag_context_guard import GuardMiddleware, policies
# Use built-in policy
guard = GuardMiddleware(policies.DEFAULT)
# Or your custom file
# guard = GuardMiddleware("my_policy.yaml")Each chunk is a dict with:
text(str): The document contentmeta(dict): At minimum, includeclassification(str or list) orclassifications(list)
Any extra metadata fields are ignored by the policy engine.
| Policy | Use case | Rules |
|---|---|---|
policies.DEFAULT |
General data leakage prevention | 7 |
policies.FINANCE |
Financial services confidentiality | 6 |
policies.HIPAA |
Protected Health Information (PHI) | 6 |
policies.GDPR |
EU personal data compliance | 6 |
from rag_context_guard import GuardMiddleware, policies
guard = GuardMiddleware(policies.FINANCE)If your chunks lack pre-computed classification metadata, you can provide a classifier function that runs an LLM to tag chunks:
from rag_context_guard import GuardMiddleware
from langchain_ollama import OllamaLLM # pip install langchain-ollamaThe classifier is called only for chunks without existing classifications.
Main entry point.
GuardMiddleware(policy_path=None, classifier=None)policy_path:BuiltinPolicyconstant or path to YAML file (default: built-in DEFAULT)classifier: Optional callable(str) -> str | list[str] | None
Methods:
analyze(chunks: List[dict]) -> AnalysisResultget_rules() -> List[PolicyRule]
Returned by analyze():
is_safe: bool—Trueif no block violationsviolations: List[Violation]— all blocking issueswarnings: List[Violation]— all warn-level issuessafe_subset: List[Chunk]— chunks not involved in any block violationrisk_explanation: str— human-readable summary
Methods:
to_dict() -> dict— serialize to plain dict for JSON logging or API responses
rule_name: strmessage: strtriggering_chunks: List[Chunk]action: Action(Action.BLOCKorAction.WARN)
Methods:
to_dict() -> dict— serialize to plain dict
Copy src/rag_context_guard/policies/example.yaml and adapt:
rules:
- name: healthcare_pii_exposure
action: block
trigger:
all_present: [healthcare, pii]
message: "Healthcare data combined with PII — exposure risk"Then load it:
guard = GuardMiddleware("my_policy.yaml")rag-context-guard/
├── src/rag_context_guard/
│ ├── guard.py # GuardMiddleware
│ ├── graph_analyzer.py # Policy evaluation engine
│ ├── policy.py # YAML policy loader
│ ├── models.py # Dataclasses (Chunk, Violation, AnalysisResult)
│ └── policies/ # Built-in YAML policies
│ ├── default.yaml
│ ├── finance.yaml
│ ├── hipaa.yaml
│ └── gdpr.yaml
├── demo.py # Example usage (no external LLM needed)
├── test_comprehensive.py # Full test suite
├── pyproject.toml # Dependency + build config
└── README.md
MIT
Copy src/rag_context_guard/policies/example.yaml and adapt:
rules:
- name: healthcare_pii_exposure
action: block
trigger:
all_present: [healthcare, pii]
message: "Healthcare data combined with PII — exposure risk"Then load it:
guard = GuardMiddleware("my_policy.yaml")rag-context-guard/
├── src/rag_context_guard/
│ ├── guard.py # GuardMiddleware
│ ├── graph_analyzer.py # Policy evaluation engine
│ ├── policy.py # YAML policy loader
│ ├── models.py # Dataclasses (Chunk, Violation, AnalysisResult)
│ └── policies/ # Built-in YAML policies
│ ├── default.yaml
│ ├── finance.yaml
│ ├── hipaa.yaml
│ └── gdpr.yaml
├── demo.py # Example usage (no external LLM needed)
├── test_comprehensive.py # Full test suite
├── pyproject.toml # Dependency + build config
└── README.md
MIT