T02 · Agent Memory Poisoning
- Location
scripts/__init__.py:158- Finding
Persistent Knowledge Poisoning Through Unsanitized Memory Content
- Content
View full analysis
Vulnerability Details
File Location:
scripts/__init__.py:61-64,scripts/__init__.py:158-174,scripts/__init__.py:231-234, andscripts/__init__.py:257-272
Vulnerability Type: Persistent agent-memory poisoning and Markdown injection
Risk Level: MediumVulnerable Code
python def to_markdown_row(self) -> str: """Convert to a Markdown table row.""" tags_str = ",".join(self.tags) return f"| {self.source} | {self.content} | {tags_str} | {self.timestamp} |"python # Extract text from metadata.sentence_classifications classifications = item.get("metadata", {}).get("sentence_classifications", []) for sent in classifications: content = sent.get("text", "") if content and content not in all_content: all_content.add(content) # Extract tags tags = self._extract_tags(content, query_text) entry = WikiEntry( source=f"query:{search_mode}", content=content, tags=tags, relevance_score=item.get("score", 0.0) ) results.append(entry)python for entry in entries: lines.append(entry.to_markdown_row()) return "\n".join(lines)python # Read existing content existing_content = "" if filepath.exists(): existing_content = filepath.read_text(encoding="utf-8") # Format new entries new_entries_md = self.format_as_wiki(entries, include_header=False) # Append to the file with open(filepath, "a", encoding="utf-8") as f: if not existing_content.strip(): # Write a header when the file is empty f.write("| source | content | tags | timestamp |\n") f.write("|--------|---------|------|-----------|\n") f.write(new_entries_md) f.write("\n")Technical Analysis
Text retrieved from M-Flow memory is treated as trusted content and copied verbatim into a persistent Wiki intended for lat ...[truncated 2181 chars]
- Remediation
View remediation
Remediation Suggestions
- Treat all search results and memory records as untrusted data, regardless of their apparent source.
- Escape Markdown table metacharacters before formatting:
- Replace
|with\|. - Normalize or encode carriage returns and line feeds.
- Neutralize active HTML and other constructs if the renderer permits them.
- Replace
- Prefer a structured storage format such as JSON with schema validation. Render Markdown only at the presentation boundary.
- Store explicit provenance, trust level, source identifier, and validation status for every entry.
- Require approval or automated policy validation before promoting session-derived records into long-term knowledge.
- Detect and quarantine prompt-like directives in retrieved content rather than silently promoting them.
- When supplying Wiki entries to a downstream model, place them in strongly delimited data sections and explicitly instruct the model that embedded directives are untrusted data, not executable instructions.
- Add tests covering pipes, multiline values, HTML, headings, fenced blocks, and prompt-injection phrases.
