T02 · Agent Memory Poisoning
- Location
scripts/learner.py:6- Finding
Unvalidated Cross-Session Storage Can Poison Agent Memory
- Content
View full analysis
Vulnerability Details
File Location:
SKILL.md:49-52,SKILL.md:62-73; persistence implementation inscripts/learner.py:6-17
Vulnerability Type: Persistent storage of untrusted behavioral data
Risk Level: MediumThe skill documentation instructs the agent to retain outcomes, errors, improvement suggestions, and user preferences in
learned_patterns.json. It further directs future invocations to apply important stored preferences automatically and recommends writing derived rules back intoSKILL.mdafter repeated failures.The underlying persistence mechanism accepts arbitrary note text and stores it without validation:
python MEM = os.path.join(os.path.dirname(__file__), "learned_patterns.json") def load(): try: return json.load(open(MEM, encoding="utf-8")) except Exception: return {"total_ops": 0, "total_fails": 0, "patterns": []} 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 dTechnical Analysis
record()appends caller-controllednotecontent directly to persistent JSON storage. There is no schema validation, length restriction, provenance tracking, trust designation, user or project isolation, or approval gate.Persistent storage alone is not instruction execution. The security risk arises from combining this storage mechanism with the documented workflow in
SKILL.md, which tells the agent to reuse stored preferences in later invocations and to convert accumulated experience into modifications of the skill instruction file. If stored natural-language data is subsequently interpreted as trusted behavioral guidance, an attacker can use notes, preferences, or failure descriptions as a durable indirect-instruction channel....[truncated 1965 chars]
- Remediation
View remediation
Remediation Suggestions
- Remove automatic modification of
SKILL.md; skill instructions should remain immutable during normal execution. - Require explicit, informed user approval before storing preferences, errors, notes, or other cross-session data.
- Define a strict JSON schema with permitted keys, value types, maximum lengths, and enumerated preference values.
- Treat all persisted natural-language content as untrusted data and never concatenate or promote it into agent instructions.
- Separate factual metrics from free-form text. Prefer counters and predefined identifiers over arbitrary notes.
- Namespace memory by user and project to prevent one context from influencing another.
- Record provenance and timestamps for every stored entry, and provide review, deletion, reset, and expiration controls.
- Use atomic file replacement and restrictive filesystem permissions for the memory file.
- Align the documentation with the implementation by removing unsupported subcommands or implementing them with validation and approval controls.
- If reflection is required, produce proposed changes for human review rather than applying them automatically.
- Remove automatic modification of
