T02 · Agent Memory Poisoning
Warning
- Location
- capture-learning.sh:101
- Finding
- Persistent Learning Store Allows Agent Memory Poisoning<![CDATA[ ## Vulnerability Details **File Location**: `capture-learning.sh:21-59, 101-104`; related retrieval behavior in `pre-task-check.sh:25-34` **Vulnerability Type**: Persistent memory poisoning through unescaped attacker-controlled input **Risk Level**: Medium ### Vulnerable Code ```bash while [[ $# -gt 0 ]]; do case $1 in --type) TYPE="$2" shift 2 ;; --severity) SEVERITY="$2" shift 2 ;; --context) CONTEXT="$2" shift 2 ;; --issue) ISSUE="$2" shift 2 ;; --correction) CORRECTION="$2" shift 2 ;; --lesson) LESSON="$2" shift 2 ;; --tags) TAGS="$2" shift 2 ;; --task-slug) TASK_SLUG="$2" shift 2 ;; *) echo "Unknown option: $1" exit 1 ;; esac done ``` ```bash # Create JSON learning entry cat >> "$LEARNINGS_FILE" << EOF {"timestamp":"$TIMESTAMP","type":"$TYPE","severity":"$SEVERITY","context":"$CONTEXT","issue":"$ISSUE","correction":"$CORRECTION","lesson":"$LESSON","tags":"$TAGS","taskSlug":"$TASK_SLUG"} EOF ``` The persisted records are subsequently retrieved before future tasks: ```bash # Search for relevant learnings if [[ -x "$SEARCH_SCRIPT" ]]; then echo "📚 Searching for relevant learnings..." echo "" # Search by task type and keywords "$SEARCH_SCRIPT" "$TASK_TYPE $*" --limit 5 echo "" echo "💡 Tip: Review these learnings before starting your task!" echo "" else echo "❌ Search script not found. Make sure the skill is installed." fi ``` ### Technical Analysis The script accepts attacker-influenced values for `context`, `issue`, `correction`, `lesson`, `tags`, and `taskSlug`, then interpolates them directly into a JSONL reco ...[truncated 2373 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Build JSON records with a proper serializer instead of string interpolation. For example, use `jq -n` with `--arg`: ```bash jq -cn \ --arg timestamp "$TIMESTAMP" \ --arg type "$TYPE" \ --arg severity "$SEVERITY" \ --arg context "$CONTEXT" \ --arg issue "$ISSUE" \ --arg correction "$CORRECTION" \ --arg lesson "$LESSON" \ --arg tags "$TAGS" \ --arg taskSlug "$TASK_SLUG" \ '{ timestamp: $timestamp, type: $type, severity: $severity, context: $context, issue: $issue, correction: $correction, lesson: $lesson, tags: $tags, taskSlug: $taskSlug }' >> "$LEARNINGS_FILE" ``` 2. Validate maximum field lengths and reject unexpected control characters where they are not required. 3. Require explicit user approval before writing corrections or behavioral guidance into persistent memory. 4. Record provenance, creation method, author, and trust level for each learning. 5. Treat retrieved records strictly as untrusted data, not executable instructions. 6. Clearly delimit stored records when presenting them to an agent and warn that their contents must not override system or user instructions. 7. Validate every JSONL record before appending it and before consuming it. 8. Use restrictive permissions, such as `umask 077` and file mode `0600`, for the memory directory and learning file. ]]>
