T02 · Agent Memory Poisoning
- Location
scripts/learner.py:11- Finding
Untrusted Notes Can Poison Persistent Agent Memory
- Content
View full analysis
Vulnerability Details
File Location:
scripts/learner.py:11-17, with unsafe reuse instructions inSKILL.md:96-108
Vulnerability Type: T02: Agent Memory Poisoning
Risk Level: MediumVulnerable Code
scripts/learner.py:11-17:python def record(op_ok=True, note=""): d = load(); d["total_ops"] += 1 if not op_ok: d["total_fails"] += 1 if note: d.setdefault("patterns", []).append(note) json.dump(d, open(MEM, "w", encoding="utf-8"), ensure_ascii=False, indent=2) return dSKILL.md:96-108instructs the agent to persist and later apply stored preferences, and potentially write accumulated experience back into the Skill:markdown ### Iteration Rules - **Errors accumulated ≥3 times** → proactively add pre-check/fallback steps and write the experience back into this SKILL.md. - **Operations ≥10 times** → analyze frequently used capabilities and prioritize improving examples and quality; evaluate low-frequency capabilities for simplification or merging. - **Important user preferences** → write them into `learned_patterns.json` and apply them directly on the next invocation to reduce repeated questions. > The more it is used, the better it understands you: the first use provides generic capabilities, while by the tenth use it has accumulated personalized best practices.Technical Analysis
The
recordfunction accepts arbitrary command-line text and appends it to the persistentpatternsarray without schema validation, provenance tracking, content restrictions, size limits, or separation between passive data and executable agent instructions. The resulting JSON file persists across invocations.This becomes an agent-memory poisoning risk because
SKILL.mddirects future agents to reuse stored preferences and to write accumulated experience back intoSKILL.md. An attacker-controlled note can therefore be phrased as an instruction rathe ...[truncated 1942 chars]- Remediation
View remediation
Remediation Suggestions
- Treat all persisted memory as untrusted data and never insert it directly into agent instructions or system-level prompts.
- Replace free-form patterns with a strict schema containing narrowly defined fields, types, permitted values, provenance, timestamps, and confidence levels.
- Allow only explicitly approved preference categories; reject instruction-like content and unknown keys.
- Apply maximum lengths and entry-count limits to prevent oversized or abusive memory records.
- Require explicit user or administrator approval before a stored item affects a later session.
- Remove automatic or agent-directed modification of
SKILL.md. Generate a separate review proposal or patch that a trusted human must approve. - Clearly delimit retrieved memory as quoted, non-authoritative data and instruct the consuming agent not to follow commands found inside it.
- Use atomic file replacement, restrictive file permissions, and integrity checks to reduce local tampering and state corruption.
- Record the origin of every memory entry and provide deletion, expiration, and revocation mechanisms.
- Add tests using instruction-like notes to confirm they remain inert and cannot alter later agent policy.
