T09 · Insecure Skill Coding Practices
- Location
scripts/check_context.sh:16- Finding
Arbitrary Python Code Execution Through Unsafe HOME Path Interpolation
- Content
View full analysis
/dev/null || true) if [[ "$pg_status" == "down" || "$pg_status" == "false" ]]; then echo "POSTGRES_DOWN" exit 0 fi else echo "WARNING: $SESSION_ANCHOR not found — skipping postgres check" >&2 fi # --------------------------------------------------------------------------- # Context check via prompt_count # --------------------------------------------------------------------------- if [[ ! -f "$ANCHOR_STATE" ]]; then echo "WARNING: $ANCHOR_STATE not found — cannot read prompt_count, defaulting to STATUS_OK" >&2 echo "STATUS_OK" exit 0 fi prompt_count=$(python3 -c " import json, sys try: data = json.load(open('$ANCHOR_STATE')) val = data.get('prompt_count', 0) print(int(val)) except Exception as e: print('ERROR reading prompt_count: ' + str(e), file=sys.stderr) sys.exit(1) " 2>/dev/null) || { ``` ### Technical Analysis The script constructs both state-file paths from the process-controlled `HOME` environment variable and then directly embeds those paths inside Python source code supplied to `python3 -c`. Shell quotin ...[truncated 2784 chars]- Remediation
View remediation
&2 echo "STATUS_OK" exit 0 } ``` Additional hardening measures should include: 1. Validate that `HOME` is nonempty and resolves to an expected absolute directory. 2. Avoid `python3 -c` whenever external values would become part of generated source. 3. Consider moving JSON parsing into a checked-in Python file with paths supplied only as command-line arguments. 4. Resolve and validate state-file paths before opening them if the execution environment has a defined trusted home directory. 5. Add regression tests using paths containing quotes, spaces, newlines, semicolons, parentheses, and other metacharacters. 6. Declare `python3` as a required runtime binary in the Skill metadata, since the script depends on it. ]]>
