T02 · Agent Memory Poisoning
- Location
scripts/rag_system.py:239- Finding
Persistent prompt injection through untrusted memory content
- Content
View full analysis
Vulnerability Details
File Location:
scripts/local_memory_system_v2.py:85-99;scripts/rag_system.py:239-249
Vulnerability Type: Persistent prompt injection through agent memory
Risk Level: MediumVulnerable Code
python def add_memory(self, content: str, metadata: Dict[str, Any], id: Optional[str] = None): """Add a memory to the vector database""" if not self.collection: if not self.initialize_database(): return False try: if id is None: id = f"memory_{int(time.time())}_{hash(content) % 10000}" self.collection.add( documents=[content], metadatas=[metadata], ids=[id] )python for i, result in enumerate(results, 1): metadata = result.get('metadata', {}) source = metadata.get('source', 'unknown') timestamp = metadata.get('timestamp', time.time()) time_str = time.strftime("%Y-%m-%d %H:%M", time.localtime(timestamp)) context_parts.append(f"## Memory {i}: {source} ({time_str})") context_parts.append(f"Relevance: {result.get('relevance_score', 0.0):.2f}") context_parts.append("") context_parts.append(result['content']) context_parts.append("")Technical Analysis
The
add_memory()method accepts arbitrary content and stores it directly in ChromaDB without provenance validation, trust classification, instruction detection, or sanitization. Theformat_context()method subsequently inserts that stored content verbatim into a context intended for downstream LLM consumption.Memory content is therefore presented alongside application-generated context without a clear security boundary. An attacker who can influence saved conversations or invoke the
--addinterface can store instruction-like text, such as requests to disregard higher-priority instructions or disclose sensitive information. If semantic retrieval later s ...[truncated 1209 chars]- Remediation
View remediation
Remediation Suggestions
- Treat every retrieved memory as untrusted data rather than an instruction.
- Wrap retrieved content in explicit delimiters and prepend a strong instruction stating that commands contained inside memories must never be executed or followed.
- Attach provenance, creator identity, creation channel, and trust level to every memory entry.
- Require confirmation before persisting behavioral rules or security-sensitive preferences.
- Detect and quarantine instruction-like entries, especially content that attempts to override policies, request secrets, or initiate tool execution.
- Restrict memory insertion interfaces with appropriate authorization.
- Keep user content separate from system-generated context using structured message roles rather than concatenating both into one plain-text prompt.
- Provide record deletion, review, and audit capabilities so poisoned entries can be identified and removed.
- Add regression tests using stored prompt-injection payloads to verify that retrieved memories remain inert quoted data.
