T02 · Agent Memory Poisoning
- Location
memory/memory.py:137- Finding
Untrusted Content Can Poison Persistent Agent Memory
- Content
View full analysis
Vulnerability Details
File Location:
memory/memory.py:137-160,memory/memory.py:165-197; natural-language entry point atnlp_parser.py:109-113andnlp_parser.py:307-311
Vulnerability Type: Persistent prompt and memory poisoning
Risk Level: MediumVulnerable Code
python def add(self, memory_type: str, content: str, tags: List[str] = None) -> str: """添加记忆""" entry = MemoryEntry( id=str(datetime.now().timestamp()), type=memory_type, content=content, tags=tags or [] ) # 添加到缓存 self.entries[memory_type].append(entry) # 持久化到文件 self._save_to_file(memory_type, content) return f"✅ 已添加记忆: [{memory_type}] {content[:50]}..." def _save_to_file(self, memory_type: str, content: str): """保存到分类文件""" type_file = self.types_dir[memory_type] / "default.md" # 追加内容 with open(type_file, 'a', encoding='utf-8') as f: f.write(f"\n- {content}") # 更新 MEMORY.md self._update_memory_entry(memory_type, content)python def _update_memory_entry(self, memory_type: str, content: str): """更新 MEMORY.md 中的条目""" try: with open(self.memory_file, 'r', encoding='utf-8') as f: lines = f.readlines() # 找到对应类型的位置 type_header = f"## {self._get_type_name(memory_type)}" start_idx = -1 end_idx = -1 for i, line in enumerate(lines): if type_header in line: start_idx = i + 1 elif start_idx > 0 and line.startswith('## '): end_idx = i break if start_idx > 0: # 插入新条目 if end_idx < 0: end_idx = len(lines) # 找到 "-" 开头的行 insert_idx = start_idx for i in range(start_idx, end_idx): ...[truncated 3407 chars]- Remediation
View remediation
Remediation Suggestions
- Require explicit user confirmation before any natural-language request is committed to persistent memory.
- Replace prompt-ready Markdown storage with a structured format containing fields such as
content,source,created_at,trust_level, andapproved. - Keep stored data separate from system and developer instructions when constructing future Agent prompts. Clearly delimit memory as untrusted reference data.
- Reject or quarantine entries containing imperative prompt-control patterns, requests to ignore prior instructions, tool-use directives, credential requests, or role reassignment.
- Narrow the natural-language patterns so that only an unambiguous memory command can initiate persistence.
- Add provenance and audit logging so users can identify which actor or message created each entry.
- Provide review, expiration, and rollback mechanisms for persisted entries.
- Apply entry-size and total-memory limits to reduce context flooding and denial-of-service risks.
- Add tests proving that unconfirmed or untrusted instructions cannot enter the automatically loaded memory context.
