T02 · Agent Memory Poisoning
- Location
scripts/learner.py:67- Finding
Unvalidated Persistent State Can Poison Future Agent Behavior
- Content
View full analysis
Vulnerability Details
File Location:
scripts/learner.py:67-100,scripts/learner.py:113-121, andSKILL.md:52-55,61-74
Vulnerability Type: Persistent agent-state poisoning
Risk Level: MediumVulnerable Code
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)) 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-%m-%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 datapython def self_reflect(skill_dir): data = load_learning(skill_dir) suggestions = [] for err, info in data["errorPatterns"].items(): if info["count"] >= THRESHOLD_ERROR: suggestions.append( f"⚠️ Error '{err}' has occurred {info['count']} times; add validation or fallback steps " f"and write the ...[truncated 3604 chars]- Remediation
View remediation
Remediation Suggestions
- Remove the instruction to automatically write accumulated experience into
SKILL.md. Treat that file as reviewed, immutable skill code rather than runtime state. - Treat every value loaded from
learned_patterns.jsonas untrusted data. Never present stored notes or preferences as authoritative agent instructions. - Define and enforce a strict schema for all persisted fields, including maximum lengths, allowed keys, expected value types, and permitted character sets where practical.
- Separate descriptive observations from executable or behavioral instructions. Store fixed identifiers for errors and preferences instead of unrestricted prose whenever possible.
- Require explicit, informed user approval before saving a preference or applying it in a later session.
- Before displaying reflection output, escape or clearly delimit all stored values and prepend a warning that the content is untrusted historical data.
- Do not interpolate free-form
lastNote, error names, or capability names into recommendations that an agent may interpret as instructions. - Add provenance and integrity metadata to persistent entries, such as the originating user, invocation, timestamp, and approval status.
- Provide commands to inspect, revoke, and completely reset persistent learning data.
- Avoid storing secrets or sensitive user content in notes and preferences; document the plaintext retention behavior and apply restrictive file permissions.
- Add tests using instruction-like payloads to verify that stored values remain inert data and cannot alter future skill instructions.
- Remove the instruction to automatically write accumulated experience into
