T02 · Agent Memory Poisoning
- Location
scripts/kg.py:102- Finding
Persistent Agent Memory Poisoning Through Unsanitized Fact Content
- Content
View full analysis
Vulnerability Details
File Location:
scripts/kg.py:102,scripts/kg.py:170-173
Vulnerability Type: Persistent prompt injection through attacker-controlled knowledge-graph facts
Risk Level: MediumVulnerable Code
python item = { "id": new_id, "fact": fact.strip(), "category": category, "timestamp": timestamp, "source": source, "status": "active", "supersededBy": None, }python for it in active[:max_lines]: fact = str(it.get("fact") or "").strip() if not fact: continue lines.append(f"- {fact}")Technical Analysis
The
add_fact()function persists the user-controlled--factvalue without validating its semantics or escaping Markdown content. The summary generator subsequently inserts that value verbatim intosummary.md.Because the knowledge graph is designed to retain information across sessions, an attacker who can cause a fact to be added can persist instruction-like text, misleading Markdown, or prompt-injection content. If a later AI agent loads
summary.mdas trusted memory rather than treating it strictly as untrusted data, the injected content may influence that agent's goals, tool use, or interpretation of other facts.The vulnerability does not independently execute operating-system commands. Exploitation depends on a downstream agent consuming the generated summary and failing to isolate stored data from instructions.
Attack Path
- An attacker submits an instruction-like value through the documented
addoperation, for example as the--factargument. add_fact()strips surrounding whitespace but otherwise stores the value unchanged initems.json.- The attacker or another workflow invokes
summarize, or invokesaddwith--summarize. build_summary()copies the malicious fact verbatim intosummary.md.- The generated summary remains in the persistent knowledge graph.
- A later AI-agent session loads the summary as trusted con ...[truncated 752 chars]
- An attacker submits an instruction-like value through the documented
- Remediation
View remediation
Remediation Suggestions
- Treat every fact and all imported knowledge-graph content as untrusted data.
- Encode summaries using an unambiguous structured format and ensure downstream agents are explicitly instructed never to interpret fact values as commands.
- Escape Markdown control syntax before rendering facts into
summary.md, including headings, links, HTML blocks, code fences, and other constructs that can change document interpretation. - Reject or quarantine instruction-like content, such as text that attempts to override policies, direct an agent to invoke tools, or impersonate system and developer messages.
- Store provenance and trust metadata for each fact, including its submitter, consent status, validation status, and review state.
- Require human approval before untrusted facts become part of an agent-readable summary.
- Add tests using malicious facts containing prompt-injection directives, Markdown headings, code fences, embedded HTML, and multiline content.
- Harden downstream consumers so knowledge-graph files are quoted or parsed as data and cannot override system, developer, or user instructions.
