T02 · Agent Memory Poisoning
- Location
scripts/learner.py:99- Finding
Unvalidated User-Controlled Preferences Persisted Across Sessions
- Content
View full analysis
Vulnerability Details
File Location:
scripts/learner.py, lines 99-103
Vulnerability Type: Persistent agent-memory poisoning through unrestricted preference storage
Risk Level: MediumEvidence
python def record_preference(skill_dir, key, value): data = load_learning(skill_dir) data["preferences"][key] = value save_learning(skill_dir, data) return dataThe command-line dispatch passes arbitrary command-line values directly to this persistence function:
python elif args.cmd == "prefer": record_preference(sd, args.key, args.val) print("✅ 已记录偏好")SKILL.md, lines 86-115, directs the agent to record user preferences inlearned_patterns.jsonand apply important stored preferences during later uses.Technical Analysis
The preference key and value are accepted without validation, normalization, an allowlist, provenance metadata, expiration, or separation between benign presentation preferences and behavioral instructions. They are then written into the persistent
learned_patterns.jsonstate.A user can therefore supply a value framed as an agent directive rather than as ordinary preference data. Because the skill documentation instructs future invocations to adopt important saved preferences, a later agent session may treat attacker-controlled persistent text as trusted operating guidance. The Python module does not itself execute stored values as code; exploitation depends on a future agent reading and following the persisted preference. Nevertheless, this creates a durable trust-boundary violation between untrusted conversation input and future agent behavior.
The related documentation also recommends writing accumulated experience back into
SKILL.mdafter recurring failures. The provided script does not implement that writeback automatically, so direct modification ofSKILL.mdis not established by the audited code. Any such modification would r ...[truncated 1546 chars]- Remediation
View remediation
Remediation Suggestions
- Replace arbitrary key/value storage with an explicit allowlist of benign preference fields, such as output language, report length, currency, and platform.
- Validate values against field-specific schemas, enumerations, type limits, and conservative maximum lengths.
- Reject values containing imperative behavioral directives or references to system prompts, safety rules, credentials, tool authorization, file access, or instruction precedence.
- Store provenance with every preference, including the originating user, session, timestamp, and whether it was explicitly confirmed.
- Scope persistent preferences per authenticated user or tenant instead of sharing one skill-wide state file.
- Treat loaded preferences strictly as untrusted data. Never insert them into system- or developer-level instructions, and never allow them to override security policy or tool authorization.
- Require explicit confirmation before a preference is saved and provide commands to inspect, expire, and delete stored entries.
- Remove or tightly constrain the instruction to write learned material back into
SKILL.md. Any proposed documentation change should require review and must never be generated directly from untrusted error notes or preferences. - Use atomic, permission-restricted file updates and define a retention period for stored state.
- Add tests proving that instruction-like values cannot alter future agent policy or tool behavior.
