T09 · Insecure Skill Coding Practices
- Location
SKILL.md:337- Finding
Arbitrary Python Code Execution Through Repository-Controlled Paths
- Content
View full analysis
/dev/null); do DIR=$(dirname "$pj") NAME=$(python3 -c "import json; print(json.load(open('$pj')).get('name','$DIR'))" 2>/dev/null) LOC=$(find "$DIR" -type f \( -name "*.ts" -o -name "*.js" -o -name "*.tsx" -o -name "*.jsx" \) \ -not -path '*/node_modules/*' -not -path '*/dist/*' 2>/dev/null | xargs wc -l 2>/dev/null | tail -1 | awk '{print $1}') echo " $NAME: ${LOC:-0} lines" done # Git activity per package (last 30 days) echo "=== Recent Activity (30 days) ===" for pj in $(find . -maxdepth 3 -name "package.json" -not -path '*/node_modules/*' 2>/dev/null); do DIR=$(dirname "$pj") NAME=$(python3 -c "import json; print(json.load(open('$pj')).get('name','$DIR'))" 2>/dev/null) ``` ### Technical Analysis The `stats` command obtains `$pj` and `$DIR` from repository-controlled file and directory names. It then interpolates these values directly into source code passed to `python3 -c`: ```bash open('$pj') .get('name','$DIR') ``` Shell double quoting does not make this safe because the generated Python program still treats embedded single quotes and Python syntax as code. A malicious directory name can terminate or alter the Python string expression and cause attacker-selected Python expressions to execute. For example, a directory name containing an expression conceptually similar to: ```text x'+str(__import__('os').system('ATTACKER_COMMAND'))+'x ``` would be incorporated into the argument to `open(...)`. Python evaluates the injected `os.system(...)` expression before attempting to open the resulting path. The use of: ```bash for pj in $(find ...) ``` also performs shell word splitting and glob expansion. Consequently, ordinary repository ...[truncated 1896 chars]- Remediation
View remediation
/dev/null ) ``` Replace command-substitution iteration with null-delimited filename processing so all valid filenames are handled as data: ```bash find . -maxdepth 3 -name 'package.json' \ -not -path '*/node_modules/*' -print0 2>/dev/null | while IFS= read -r -d '' pj; do DIR=$(dirname -- "$pj") # Process "$pj" and "$DIR" exclusively as quoted arguments. done ``` Additional hardening should include: 1. Use `--` where supported before path operands to prevent option injection. 2. Use null-delimited pipelines, such as `find -print0` with `xargs -0`, for source-file counting. 3. Keep every expansion of `$pj` and `$DIR` quoted. 4. Avoid dynamically constructing source code, shell commands, or expressions from filenames. 5. Test the command against paths containing quotes, whitespace, newlines, wildcard characters, leading hyphens, and non-ASCII characters. 6. Run repository-analysis skills with least privilege and without unnecessary credentials when processing untrusted repositories. ]]>
