T02 · Agent Memory Poisoning
- Location
scripts/learner.py:5- Finding
Unvalidated Attacker-Controlled Data Persisted as Cross-Session Agent Memory
- Content
View full analysis
Vulnerability Details
File Location:
scripts/learner.py:5-18; related behavioral instructions inSKILL.md:52-55andSKILL.md:64-75
Vulnerability Type: T02: Agent Memory Poisoning
Risk Level: MediumVulnerable Code
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 d if __name__ == "__main__": ok = (sys.argv[1] != "fail") if len(sys.argv) > 1 else True rec = (sys.argv[2] if len(sys.argv) > 2 else "") print(json.dumps(record(ok, rec), ensure_ascii=False))The associated instructions state that user preferences and experience are written to
learned_patterns.jsonand should be adopted during later uses. They also prescribe automated reflection and modification of skill behavior based on accumulated records.Technical Analysis
The script accepts an arbitrary positional string from
sys.argv[2]and appends it directly to the persistentpatternsarray. It performs no schema validation, content filtering, provenance tracking, authorization check, trust classification, or user confirmation. The resulting JSON file is stored beside the skill and is intended to influence later sessions.This creates a persistent trust-boundary violation: data supplied during one invocation can be treated as behavioral guidance in subsequent invocations. A malicious note could contain instruction-like content that attempts to override future task requirements, suppress safety checks, disclose information, or redirect agent behavior. The Python code does not itself interpret or e ...[truncated 2140 chars]
- Remediation
View remediation
Remediation Suggestions
- Replace arbitrary note storage with a strict, versioned JSON schema containing typed fields and explicit length limits.
- Keep factual telemetry, user preferences, and agent instructions in separate stores. Never interpret free-form telemetry as executable or authoritative guidance.
- Permit only an allowlist of preference keys and normalized values. Reject control instructions, role directives, tool commands, and other instruction-like content.
- Record provenance for every entry, including source, timestamp, requesting user, session identifier, and whether explicit consent was obtained.
- Require user review and confirmation before persisted data is applied to a later session.
- Treat all loaded memory as untrusted data and place it in a clearly delimited context that cannot override system, developer, safety, or current-task instructions.
- Implement the documented interface with
argparsesubcommands and explicit named arguments. Reject unknown commands and options instead of silently storing them. - Write updates atomically through a securely created temporary file followed by replacement, and apply restrictive file permissions where supported.
- Validate the entire loaded JSON structure before incrementing or appending fields. Report malformed state instead of silently resetting it.
- Do not automatically rewrite
SKILL.mdor other instruction-bearing files from accumulated memory. Any proposed behavioral change should require a trusted review process.
