T09 · Insecure Skill Coding Practices
Error
- Location
track.sh:61- Finding
Arbitrary Python Code Execution Through Unescaped Session Fields
- Content
View full analysis
/dev/null || echo "sess-$(date +%s)")", "timestamp": "$timestamp", "date": "$date_only", "task": "$task", "outcome": "$outcome", "value": "$value", "tokens": $tokens, "model": "$model", "cost_usd": $cost, "notes": "$notes" } EOF ) # Append to JSON array using jq if available, else python if command -v jq &> /dev/null; then jq --argjson entry "$entry" '.sessions += [$entry]' "$DATA_FILE" > "${DATA_FILE}.tmp" && mv "${DATA_FILE}.tmp" "$DATA_FILE" else python3 -c " import json, sys entry = json.loads('''$entry''') with open('$DATA_FILE', 'r') as f: data = json.load(f) data['sessions'].append(entry) with open('$DATA_FILE', 'w') as f: json.dump(data, f, indent=2) " fi ``` ### Technical Analysis The `task`, `outcome`, `value`, `model`, and `notes` command-line values are inserted directly into a JSON-like heredoc without JSON escaping. When `jq` is unavailable, the complete `entry` value is subsequently interpolated into the source code passed to `python3 -c`: ```python entry = json.loads('''$entry''') ``` Because the generated Python program uses a triple-single-quoted literal, an input containing `'''` can terminate that literal and introduce a Python expression. For example, a field containing an expression shaped like the following can cause a command to run while Python evaluates the argument supplied to `json.loads`: ```text ''' + str(__import__("os").system("id")) + ''' ``` The eventual JSON parse may fail, but the injected expression is evaluated first. Consequently, command execution can occur before the parsing error terminates the script. Even without a deliberately malicious payload, ordinary values containing double quotes, backslashes, ...[truncated 1407 chars]- Remediation
View remediation
