Skip to main content

Production-ready RAG security testing and defense framework

Project description

CorpusGuard

Production-ready RAG security testing and defense framework

CI Python 3.9+ License: MIT SSRN ACM ICAIF 2026 ORCID

Built on published research: Poisoning the Compliance Mind: Adversarial Memory Injection Attacks on RAG-Based AML Agents โ€” SSRN 6734225


What CorpusGuard does

CorpusGuard automatically red teams any RAG-based AI system for corpus poisoning vulnerabilities and deploys the Memory Hygiene Layer (MHL) defense to protect it.

One command. Full security assessment. CISO-ready PDF report.

๐Ÿ“„ See the output without running anything: docs/sample_report.pdf โ€” a representative CISO assessment report.

# Option 1 โ€” Docker Compose (full stack)
docker compose up          # older Docker: docker-compose up
# Dashboard: http://localhost:3000
# API:       http://localhost:8000/docs

# Option 2 โ€” Run demo directly
python examples/bank_compliance_demo.py

# Option 3 โ€” install from source (not yet published to PyPI)
pip install -e .
corpusguard scan --target my_rag_config.yaml

The attack it finds

An adversary injects strategically crafted documents into a RAG system's vector database through legitimate ingestion pathways. The AI agent's accuracy collapses from 91.9% to 1.4% โ€” with zero anomalous log signatures. The system appears fully operational while making catastrophically wrong decisions.

Finding Result
Baseline AML agent F1 0.919
After QTPI attack (ฮฒ=50 docs) 0.014 โ€” 98.4% degradation
Log anomalies generated Zero
Existing framework coverage None โ€” not in NIST AI RMF, OWASP LLM Top 10, or MITRE ATLAS

The defense it deploys

MHL Component Function Result
CPT โ€” Cryptographic Provenance Tracking SHA-256 hashing + source trust scoring 100% detection, 0% false quarantine
FCS โ€” Factual Consistency Scoring Cross-encoder scoring against anchor corpus 22.6ms p99 latency on CPU
SRAD โ€” Statistical Retrieval Anomaly Detection KS-test on 500-query sliding window Zero-config anomaly detection

Quickstart

Docker Compose โ€” one command

git clone https://github.com/OndiekiFrank/CorpusGuard.git
cd CorpusGuard
docker compose up

Opens:

  • React Dashboard: http://localhost:3000
  • FastAPI Swagger UI: http://localhost:8000/docs
  • PDF report auto-saved to ./reports/

CLI (install from source)

CorpusGuard is not yet published to PyPI โ€” install the CLI from a clone:

git clone https://github.com/OndiekiFrank/CorpusGuard.git
cd CorpusGuard
pip install -e .

corpusguard scan --target config/my_rag.yaml
corpusguard attack --vector qtpi --budget 50
corpusguard defend --mode full
corpusguard report --format pdf --output ./ciso_report.pdf

Bank compliance demo

python examples/bank_compliance_demo.py

Runs a complete 5-phase assessment โ€” attack campaign, MHL defense, OWASP scorecard, real-time monitor, CISO PDF report.


Architecture

CorpusGuard/
โ”œโ”€โ”€ frontend/                    โ† React + Vite professional dark UI (port 3000)
โ”‚   โ””โ”€โ”€ src/components/
โ”‚       โ”œโ”€โ”€ TabView.jsx          โ† 6 tabs: Attack, OWASP, MHL, Campaign, Monitor, Threat Intel
โ”‚       โ”œโ”€โ”€ MonitorTab.jsx       โ† Real-time corpus monitoring with live chart
โ”‚       โ”œโ”€โ”€ CampaignRunner.jsx   โ† 3-phase visual stepper
โ”‚       โ””โ”€โ”€ PDFReport.jsx        โ† One-click PDF download
โ”œโ”€โ”€ corpusguard/
โ”‚   โ”œโ”€โ”€ api/main.py              โ† FastAPI REST gateway โ€” 7 endpoints (port 8000)
โ”‚   โ”œโ”€โ”€ attacks/
โ”‚   โ”‚   โ”œโ”€โ”€ qtpi.py              โ† Query-Time Prompt Injection attack
โ”‚   โ”‚   โ””โ”€โ”€ campaign.py          โ† Multi-phase attack campaigns
โ”‚   โ”œโ”€โ”€ defense/
โ”‚   โ”‚   โ”œโ”€โ”€ mhl.py               โ† Memory Hygiene Layer (main)
โ”‚   โ”‚   โ”œโ”€โ”€ cpt.py               โ† Cryptographic Provenance Tracking
โ”‚   โ”‚   โ”œโ”€โ”€ fcs.py               โ† Factual Consistency Scoring
โ”‚   โ”‚   โ””โ”€โ”€ srad.py              โ† Statistical Retrieval Anomaly Detection
โ”‚   โ”œโ”€โ”€ scanner/
โ”‚   โ”‚   โ”œโ”€โ”€ owasp.py             โ† OWASP LLM Top 10 compliance scorecard
โ”‚   โ”‚   โ””โ”€โ”€ monitor.py           โ† Real-time monitoring daemon
โ”‚   โ””โ”€โ”€ dashboard/
โ”‚       โ”œโ”€โ”€ app.py               โ† Streamlit dashboard (alternative UI)
โ”‚       โ””โ”€โ”€ report_generator.py  โ† CISO-ready PDF report generator
โ”œโ”€โ”€ examples/
โ”‚   โ””โ”€โ”€ bank_compliance_demo.py  โ† Full end-to-end bank assessment demo
โ”œโ”€โ”€ tests/                       โ† pytest suite passing (Python 3.9, 3.10, 3.11)
โ”œโ”€โ”€ docker-compose.yml           โ† Full stack: API + React UI + Redis
โ”œโ”€โ”€ Dockerfile                   โ† API container (lean runtime deps)
โ”œโ”€โ”€ Dockerfile.dashboard         โ† Streamlit container
โ””โ”€โ”€ .github/workflows/
    โ”œโ”€โ”€ ci.yml                   โ† GitHub Actions CI/CD
    โ””โ”€โ”€ security-gate.yml        โ† EU AI Act compliance gate

REST API

Full Swagger UI at http://localhost:8000/docs

Method Endpoint Description
GET /api/v1/health Health check
POST /api/v1/scan Submit RAG system for scanning
GET /api/v1/scan/{id} Get scan results
GET /api/v1/owasp/{id} OWASP LLM Top 10 scorecard
GET /api/v1/report/{id} Download PDF report
POST /api/v1/defend Deploy MHL defense
GET /api/v1/scans List all scans

React Dashboard โ€” 6 Tabs

Tab What it shows
Attack results Live F1 degradation chart, attack metrics, verdict
OWASP LLM Top 10 Full compliance scorecard with severity badges
MHL defense Module status with live pulse indicators
Campaign 3-phase visual stepper โ€” Recon โ†’ Exploit โ†’ Persist
Monitor Real-time document feed, corpus health chart, live alerts
Threat intel Documented attack patterns with actor, target, mitigation

OWASP LLM Top 10 Coverage

ID Vulnerability Status
LLM01 Prompt Injection TESTED โ€” CRITICAL
LLM03 Training Data Poisoning TESTED โ€” PARTIAL
LLM05 Supply Chain Vulnerabilities TESTED โ€” FAILED
LLM06 Sensitive Information Disclosure TESTED โ€” PARTIAL
LLM09 Overreliance TESTED โ€” PARTIAL
LLM02, 04, 07, 08, 10 Various Planned v0.3

Regulatory Coverage

Framework CorpusGuard Mitigation
NIST AI RMF First reference implementation for RAG corpus security
OWASP LLM Top 10 QTPI formalises new LLM01 sub-category
MITRE ATLAS New corpus-level attack pattern documented
FINRA Rule 3110 CPT provides cryptographic audit trail
FinCEN 2024 CPT + SRAD provide auditable controls
FATF 2025 CPT detects source-level anomalies
EU AI Act Aug 2026 CI/CD security gate โ€” automated red-teaming built-in

CI/CD Security Gate

- name: CorpusGuard Security Gate
  uses: OndiekiFrank/corpusguard-action@v1
  with:
    target: ${{ secrets.RAG_ENDPOINT }}
    fail-on-risk-score: 70

Blocks deployment if RAG system scores above 70/100. EU AI Act compliant.


Test Results

pytest โ€” attack, defense, API, CLI, and version-consistency tests
CI matrix: Python 3.9 / 3.10 / 3.11 (.github/workflows/ci.yml)

Research

Poisoning the Compliance Mind: Adversarial Memory Injection Attacks on RAG-Based AML Agents Frankline Ondieki Ombachi ยท SSRN 6734225 ยท 2026 Under peer review โ€” ACM ICAIF 2026, Milan, Italy BSides Toronto 2026 talk submitted โ€” October 3โ€“4

@article{ondieki2026poisoning,
  title  = {Poisoning the Compliance Mind: Adversarial Memory Injection Attacks on RAG-Based AML Agents},
  author = {Ondieki Ombachi, Frankline},
  year   = {2026},
  url    = {https://papers.ssrn.com/sol3/papers.cfm?abstract_id=6734225},
  note   = {Under peer review at ACM ICAIF 2026}
}

Disclaimer

For academic reproducibility and defensive security research only. Do not use against systems you do not own or have explicit permission to test.


Author

Frankline Ondieki Ombachi โ€” AI Security Researcher ยท Nairobi, Kenya

๐Ÿ“ง ondiekifrank021@gmail.com ๐Ÿ”— LinkedIn ๐Ÿ“„ SSRN 6734225 ๐Ÿ”ฌ ORCID: 0009-0008-3483-0025

If your organisation runs RAG-based AI systems โ€” run CorpusGuard before someone else does.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

corpusguard-0.1.0.tar.gz (41.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

corpusguard-0.1.0-py3-none-any.whl (41.6 kB view details)

Uploaded Python 3

File details

Details for the file corpusguard-0.1.0.tar.gz.

File metadata

  • Download URL: corpusguard-0.1.0.tar.gz
  • Upload date:
  • Size: 41.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for corpusguard-0.1.0.tar.gz
Algorithm Hash digest
SHA256 38a7a4704b488e8a5485191644c9136842ebca85951d9949edd1f13ab4c9e49e
MD5 9f1f33b01ea509dddfd20dd4d68cf0e2
BLAKE2b-256 34d03dea5c46bd1df429e18c3fd292e81293f5ca7bcda320e32611f0f8f253c9

See more details on using hashes here.

File details

Details for the file corpusguard-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: corpusguard-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 41.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for corpusguard-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a57137366a7af6e6b14f6ec62bbc13ed4342efb844eb0b0d254c237aa4b2b296
MD5 dd2348a3430693d81502ed16e0aa39d2
BLAKE2b-256 3714e519e3f432c2b8ed4a18c7faef844cf3be724da1a041f8fb9bf474d1b582

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page