LLM Context Poisoning Defense
Context poisoning is the underestimated attack on RAG pipelines — attackers poison context before it reaches the LLM. Four controls: context source validation, retrieval integrity, context sanitization and context audit logging.
What is LLM Context Poisoning Defense? Simply Explained
LLM context poisoning defense is like a poison check for RAG knowledge bases: context source validation allows only trusted sources in the vector store. Retrieval integrity checks if retrieved content was manipulated. Context sanitization removes prompt injection patterns before LLM processing. Context audit logging logs all context operations for forensics. Without defense, attackers can compromise web scraping, upload malicious documents, or poison databases — the LLM then returns incorrect or harmful responses.
↓ Jump to poisoning defense controls
4 Context Poisoning Defense Controls
Validate all context sources before inclusion. Allow-list trusted sources and verify authenticity.
# Moltbot context source validation:
source_validation:
enabled: true
# Source Allow-listing:
allowlist:
enabled: true
# Define: trusted context sources
# Sources: internal docs, trusted APIs
# Validate: source authenticity
# Block: untrusted sources
# Source Authentication:
authentication:
enabled: true
# Authenticate: all context sources
# Method: API keys, certificates
# Verify: source identity
# Prevents: source impersonation
# Source Integrity:
integrity:
enabled: true
# Verify: source integrity
# Method: hash verification, signatures
# Block: tampered sources
# Prevents: source tamperingEnsure integrity of retrieved context. Verify retrieval results have not been manipulated.
# Moltbot retrieval integrity:
retrieval_integrity:
enabled: true
# Retrieval Verification:
verification:
enabled: true
# Verify: retrieved content integrity
# Method: hash, signature
# Block: tampered content
# Prevents: retrieval manipulation
# RAG Pipeline Security:
rag_security:
enabled: true
# Secure: RAG pipeline
# Validate: vector store integrity
# Monitor: retrieval anomalies
# Alert: on suspicious retrieval
# Content Provenance:
provenance:
enabled: true
# Track: content provenance
# Include: source, timestamp, hash
# Verify: provenance on use
# Log: provenance chainSanitize context before LLM processing. Remove prompt injections and malicious content.
# Moltbot context sanitization:
sanitization:
enabled: true
# Prompt Injection Removal:
injection_removal:
enabled: true
# Detect: prompt injection patterns
# Methods: pattern matching, classifier
# Remove: injected content
# Log: injection attempts
# Content Filtering:
content_filtering:
enabled: true
# Filter: malicious content
# Categories: XSS, SQLi, code injection
# Block: harmful patterns
# Allow: clean content
# Context Length Limits:
length_limits:
enabled: true
# Enforce: max context length
# Prevent: context flooding
# Truncate: excess context
# Alert: on flooding attemptsLog all context operations for audit. Track context sources, retrieval, and sanitization.
# Moltbot context audit logging:
context_audit:
enabled: true
# Source Logging:
source_logging:
enabled: true
# Log: all context source events
# Include: source, timestamp, hash
# Retain: logs for audit (90 days)
# Protect: log access
# Retrieval Logging:
retrieval_logging:
enabled: true
# Log: all retrieval events
# Include: query, results, scores
# Retain: logs for audit (90 days)
# Protect: log access
# Sanitization Logging:
sanitization_logging:
enabled: true
# Log: all sanitization events
# Include: original, sanitized, reason
# Retain: logs for audit (90 days)
# Protect: log accessFrequently Asked Questions
What is context poisoning and how does it differ from prompt injection?
Context poisoning is an attack where malicious content is injected into the retrieval context (e.g., RAG database, knowledge base) before it reaches the LLM. Prompt injection is direct manipulation of the prompt at inference time. Context poisoning is harder to detect because the attack happens at the data layer, not at inference time. A poisoned knowledge base can affect many subsequent queries. Both require defense: context source validation and sanitization for poisoning, prompt injection detection for direct injection.
How do I secure my RAG pipeline against context poisoning?
Secure your RAG pipeline by: 1) Source validation — only allow trusted sources in your vector store. 2) Ingestion filtering — sanitize content during ingestion, not just at inference. 3) Content signatures — sign documents before ingestion and verify at retrieval. 4) Retrieval monitoring — monitor retrieval results for anomalous patterns. 5) Provenance tracking — track content provenance through the pipeline. 6) Regular audits — periodically audit your knowledge base for poisoned content.
How do I detect context poisoning in production?
Detect context poisoning by: 1) Anomaly detection — monitor for unexpected content in retrieval results. 2) Response monitoring — detect LLM responses that violate expected output patterns. 3) Source monitoring — track which sources are retrieved most frequently. 4) Canary documents — inject known documents and monitor if they appear in results. 5) Regular audits — periodically sample and review retrieval results. 6) User feedback — monitor for user reports of incorrect or unexpected responses.
What are common context poisoning attack vectors?
Common context poisoning attack vectors: 1) Web scraping attacks — poisoned web pages scraped into knowledge base. 2) Document injection — upload malicious documents to shared repositories. 3) Database poisoning — inject malicious records into data sources. 4) API poisoning — compromise third-party APIs that feed context. 5) Indirect prompt injection via poisoned context — attacker-controlled content tricks LLM into harmful actions. Defense: strict source allow-listing, content signing, and continuous monitoring.