T02 · Agent Memory Poisoning
- Location
scripts/self_reflect.py:79- Finding
Untrusted Reflection Content Can Poison Persistent Agent Memory
- Content
View full analysis
Vulnerability Details
File Location:
scripts/self_reflect.py:79-100; related retrieval instruction inSKILL.md:165-171
Vulnerability Type: Persistent storage of untrusted content in agent memory
Risk Level: MediumVulnerable Code
python def record_reflection( context: str, what_happened: str, lesson: str, severity: int = 3, tags: list[str] = None, ) -> Reflection: """Record a correction""" ensure_dirs() ref = Reflection( timestamp=datetime.now().strftime("%Y-%m-%d %H:%M"), context=context, what_happened=what_happened, lesson=lesson, severity=max(1, min(5, severity)), tags=tags or [], ) entry = ( f"\n---\n" f"**CONTEXT:** {ref.context}\n" f"**TIMESTAMP:** {ref.timestamp}\n" f"**REFLECTION:** {ref.what_happened}\n" f"**LESSON:** {ref.lesson}\n" f"**SEVERITY:** {ref.severity}\n" f"**TAGS:** {', '.join(ref.tags)}\n" ) HOT_FILE.write_text(HOT_FILE.read_text(encoding="utf-8") + entry, encoding="utf-8") return refThe related skill instruction establishes broad retrieval of this file:
markdown | User says | Trigger | |--------|------| | "What did you learn?" / "Do you know XX?" | Search `memory.md` |Technical Analysis
The
context,what_happened,lesson, andtagsvalues originate from command-line input and are interpolated verbatim into~/.qclaw/memory/memory.md. The implementation does not validate instruction-like content, escape the Markdown record structure, preserve a trusted provenance marker, or separate untrusted reflection data from information that may later be placed into an agent prompt.An attacker can also inject newlines and record delimiters into these fields, allowing the attacker to create content that appears to be an independent memory entry. More imp ...[truncated 2153 chars]
- Remediation
View remediation
Remediation Suggestions
- Store reflection records outside directories treated as agent instruction memory. Use a dedicated data-only location and retrieve records through a controlled interface.
- Replace the Markdown append format with structured JSON or a database schema. Preserve each field as data and assign explicit provenance and trust metadata.
- Validate input lengths and reject or neutralize control syntax, record delimiters, role markers, and instruction-like content where practical.
- When records are supplied to a model, wrap them in a clearly delimited untrusted-data block and provide a higher-priority instruction that their contents must never be followed as commands.
- Retrieve only records specifically requested by the user rather than loading the complete memory file for broad questions.
- Require explicit user confirmation before storing content received from an external or untrusted party.
- Require authorization and confirmation before displaying potentially sensitive reflection records, particularly when a query could retrieve multiple entries.
- Add adversarial tests covering embedded newlines, fake Markdown fields, prompt-injection phrases, record delimiter injection, and attempts to request disclosure of unrelated records.
