T09 · Insecure Skill Coding Practices
- Location
scripts/manage-secrets.sh:93- Finding
Arbitrary Command Execution Through Bulk Secret Filename Injection
- Content
View full analysis
" exit 1 fi if [[ ! -f "$BULK_FILE" ]]; then err "File not found: $BULK_FILE" exit 1 fi # Validate JSON before uploading if command -v jq &>/dev/null; then if ! jq empty "$BULK_FILE" 2>/dev/null; then err "Invalid JSON in $BULK_FILE" exit 1 fi elif command -v python3 &>/dev/null; then if ! python3 -c "import json; json.load(open('$BULK_FILE'))" 2>/dev/null; then err "Invalid JSON in $BULK_FILE" exit 1 fi fi wrangler secret bulk "$BULK_FILE" $ENV_FLAG ok "Secrets uploaded from $BULK_FILE" ;; ``` ### Technical Analysis `BULK_FILE` is derived from a user-controlled positional argument and interpolated directly into a Python program passed through `python3 -c`. Shell quoting does not protect the resulting Python source. A single quote in the filename can terminate the Python string literal and append additional Python statements. The preceding `[[ -f "$BULK_FILE" ]]` check does not prevent exploitation because Linux filenames may contain quotes, semicolons, spaces, and other characters used by Python syntax. The vulnerable branch is reached when `jq` is unavailable but `python3` is installed. ### Attack Path 1. An attacker creates a file with a specially crafted filename containing a single quote and valid Python statements. Conceptually, a filename such as the following can terminate `open(...)` and invoke an operating-system command: ```text x');__import__("os").system("id");# ``` 2. The attacker invokes the bulk operation using that filename: ```bash bash scripts/manage-secrets.sh bulk "- Remediation
View remediation
/dev/null; then err "Invalid JSON in $BULK_FILE" exit 1 fi ``` Additional hardening measures: 1. Prefer `jq` with a declared dependency instead of maintaining multiple validation implementations. 2. Reject files that are not regular files or that resolve outside an explicitly permitted project directory when such a restriction is compatible with the workflow. 3. Add tests using filenames containing quotes, spaces, newlines, semicolons, and leading hyphens. 4. Run static shell analysis such as ShellCheck and add injection-focused tests to CI. ]]>
