T09 · Insecure Skill Coding Practices
- Location
}" [ ! -f "$file" ] && { fail "File not found: $file"; return 1; } if python3 -c "import json; json.load(open('$file'))" 2>/dev/null; then local size size=$(wc -c < "$file") pass "$file (${size} bytes, valid JSON)" else fail "$file" python3 -c " import json try: json.load(open('$file')) except json.JSONDecodeError as e: print(' Error: line {}, col {}: {}'.format(e.lineno, e.colno, e.msg)) " 2>/dev/null return 1 fi } ``` ### Technical ...[truncated 2001 chars]:101- Finding
Arbitrary Python Code Execution Through JSON Filename Injection
- Content
View full analysis
}" [ ! -f "$file" ] && { fail "File not found: $file"; return 1; } if python3 -c "import json; json.load(open('$file'))" 2>/dev/null; then local size size=$(wc -c < "$file") pass "$file (${size} bytes, valid JSON)" else fail "$file" python3 -c " import json try: json.load(open('$file')) except json.JSONDecodeError as e: print(' Error: line {}, col {}: {}'.format(e.lineno, e.colno, e.msg)) " 2>/dev/null return 1 fi } ``` ### Technical Analysis The `file` argument is controlled by the caller and is interpolated directly into Python source supplied through `python3 -c`. Quoting the shell expansion does not protect the resulting Python program. A filename containing Python quote and syntax characters can terminate the `open('...')` expression and append additional Python statements. The same unsafe interpolation occurs in both the initial validation command and the error-reporting command. The preceding `-f` check does not prevent exploitation. An attacker with permission to create files can create a file whose literal filename contains the injection payload. The generated Python code can then refer to a separate ordinary file while executing appended statements. ### Attack Path 1. An attacker creates a specially named file containing characters that terminate the Python string and function calls. 2. If needed, the attacker also creates the ordinary file referenced by the injected Python expression. 3. The attacker or Agent invokes: ```bash scripts/script.sh json "" ``` 4. Bash expands the filename into the double-quoted `python3 -c` argument. 5. Python interprets part of the f ...[truncated 686 chars]- Remediation
View remediation
/dev/null; then # ... fi ``` Implement error reporting using the same argument-based approach: ```bash python3 - "$file" <<'PYEOF' import json import sys try: with open(sys.argv[1]) as stream: json.load(stream) except json.JSONDecodeError as error: print( " Error: line {}, col {}: {}".format( error.lineno, error.colno, error.msg ) ) sys.exit(1) PYEOF ``` Additional hardening measures include: - Use `with open(...)` so file handles are reliably closed. - Treat filenames as opaque data throughout the implementation. - Add regression tests using filenames containing single quotes, double quotes, semicolons, parentheses, whitespace, and newline characters. - Run the validator with the minimum filesystem and operating-system privileges required. ]]>
