T02 · Agent Memory Poisoning
- Location
scripts/learner.py:97- Finding
Untrusted User-Controlled Preferences Are Persisted for Future Agent Sessions
- Content
View full analysis
Vulnerability Details
File Location:
scripts/learner.py:97-101; supporting command interface atscripts/learner.py:133-134andscripts/learner.py:148-150
Vulnerability Type: Persistent agent memory poisoning
Risk Level: MediumTechnical Analysis
The learning module accepts arbitrary preference keys and values and writes them directly into the persistent
learned_patterns.jsonstate without validation, trust metadata, namespace restrictions, or approval. The relevant implementation is: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 interface exposes these fields directly:
python p_pre = sub.add_parser("prefer"); p_pre.add_argument("skill_dir") p_pre.add_argument("--key", required=True); p_pre.add_argument("--val", required=True)python elif args.cmd == "prefer": record_preference(sd, args.key, args.val) print("✅ 已记录偏好")The accompanying instructions in
SKILL.mdexplicitly state that important preferences stored inlearned_patterns.jsonshould be adopted on the next invocation. Consequently, attacker-controlled text can cross a session boundary and later be treated as trusted behavioral guidance.There is no distinction between benign presentation preferences and instruction-like content. For example, a value can contain directives to ignore task constraints, disclose data, or invoke tools. The Python code does not itself execute the stored text, but the documented workflow creates a consumption path in which a future agent may read and follow it.
Attack Path
- An attacker gains access to a workflow that invokes the documented
prefercommand, or otherwise controls its--keyand--valarguments. - The attacker supplies instruction-like text as a preference, for example under a plausible key such as
output_styleordefault_behavior. - ` ...[truncated 1297 chars]
- An attacker gains access to a workflow that invokes the documented
- Remediation
View remediation
Remediation Suggestions
- Restrict preference keys to an explicit allowlist of non-security-sensitive settings, such as language, date format, or verbosity.
- Enforce strict schemas and length limits for every allowed value. Prefer enumerated values over unrestricted text.
- Reject multiline values and instruction-like content, including shell commands, tool directives, role changes, and requests to override security constraints.
- Store provenance with each preference, including the originating user, session, timestamp, and approval status.
- Require explicit confirmation before a newly supplied preference becomes persistent.
- Treat loaded preferences strictly as untrusted data. Never concatenate them into system prompts or higher-priority instructions.
- Ensure system and developer instructions always take precedence over persisted preferences, and document that precedence explicitly.
- Separate behavioral preferences from operational state and provide users with commands to inspect, revoke, and reset saved entries.
- Apply restrictive file permissions and reject symlinked or unexpected memory files where the deployment threat model includes local attackers.
- Add tests proving that stored values cannot modify safety rules, select tools, initiate commands, or override the current task.
