T02 · Agent Memory Poisoning
- Location
scripts/learner.py:67- Finding
Untrusted User Data Can Poison Persistent Agent Memory
- Content
View full analysis
Vulnerability Details
File Location:
scripts/learner.py:67-82; related behavioral instructions are located atSKILL.md:67-83andscripts/section.md:5-28
Vulnerability Type: T02: Agent Memory Poisoning
Risk Level: MediumVulnerable Code
python def record_op(skill_dir, capability, success=True, error=None, note=None): data = load_learning(skill_dir) data["totalOps"] += 1 cap = data["capabilityStats"].setdefault(capability, {"count": 0, "success": 0, "fail": 0}) cap["count"] += 1 if success: cap["success"] += 1 else: cap["fail"] += 1 data["totalErrors"] += 1 if error: ep = data["errorPatterns"].setdefault(error, {"count": 0, "lastNote": "", "lastTime": ""}) ep["count"] += 1 ep["lastNote"] = note or "" ep["lastTime"] = data["lastUpdated"] or datetime.datetime.now().strftime("%Y-%dT%H:%M") rec = {"t": data["lastUpdated"], "cap": capability, "ok": success, "err": error, "note": note} data["recentOps"].append(rec) data["recentOps"] = data["recentOps"][-20:] save_learning(skill_dir, data) return data def record_preference(skill_dir, key, value): data = load_learning(skill_dir) data["preferences"][key] = value save_learning(skill_dir, data) return dataThe persistence sink writes the resulting object directly to the skill's state file:
python def save_learning(skill_dir, data): data["lastUpdated"] = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M") p = learn_path(skill_dir) with open(p, "w", encoding="utf-8") as f: f.write(json.dumps(data, indent=2, ensure_ascii=False))Technical Analysis
The
capability,error,note, preferencekey, and preferencevalueinputs are unrestricted strings. They are stored persistently inlearned_patterns.jsonwithout a schema, allowlist, size restriction, sensit ...[truncated 2369 chars]- Remediation
View remediation
Remediation Suggestions
- Separate data from instructions: Never copy learned preferences, errors, or notes into
SKILL.mdor any other file interpreted as agent instructions. - Require explicit approval: Show the exact preference to the user and require confirmation before saving it or applying it in later sessions.
- Enforce a strict schema: Use an allowlist of supported preference keys and typed values. Reject unknown keys, instruction-like fields, control characters, and excessively long input.
- Preserve trust metadata: Mark each record with its origin, owner, creation time, trust level, and approval status. Only approved records should affect future behavior.
- Treat stored content as untrusted: When presenting notes to an agent, delimit and label them as data that must not be followed as instructions.
- Restrict reflection output: Generate remediation suggestions from fixed templates and numeric counters rather than interpolating untrusted free-form notes.
- Add privacy controls: Provide listing, deletion, expiration, redaction, and opt-out mechanisms. Avoid persisting sensitive free-form notes by default.
- Isolate users and sessions: Use per-user state files with restrictive filesystem permissions instead of sharing one mutable learning file across unrelated users.
- Validate existing state: Before loading
learned_patterns.json, validate its structure and quarantine unexpected or previously unapproved entries.
- Separate data from instructions: Never copy learned preferences, errors, or notes into
