T09 · Insecure Skill Coding Practices
- Location
scripts/verify_output.sh:84- Finding
Arbitrary Python Code Execution Through Unsafe File Path Interpolation
- Content
View full analysis
]*)>(.*?)', html, re.DOTALL)) found = False for m in reversed(scripts): attrs = m.group(1).strip() if 'src=' not in attrs: code = m.group(2).strip() if len(code) > 50: with open('/tmp/_mathviz_verify.js', 'w') as f: f.write(code) found = True break if not found: print('NOT_FOUND') " 2>/dev/null > /tmp/_mathviz_extract_result ``` Equivalent vulnerable interpolation is used in the other Python blocks: ```bash html = open('$HTML', 'r', encoding='utf-8').read() ``` and: ```bash html = open('$HTML').read() ``` ### Technical Analysis The verifier accepts the HTML path from its first positional argument: ```bash HTML="$1" ``` It then embeds that value directly into Python source code passed through `python3 -c`. Although the shell variable is expanded inside a double-quoted shell string, the resulting value is placed inside a single-quoted Python string literal without Python escaping. Consequently, a path containing a single quote can terminate the Python string. Additional Python statements in the path are then interpreted as executable source code. Shell quoting of `"$1"` at assignment time does not prevent this second-order injection because the vulnerable interpretation occurs when the expanded value becomes part of Python source. This pattern occurs in four separate Python invocations, increasing the available opportunities for exploitation. ### Attack Path 1. An attacker gains the ability to control or influence the output HTML filename ...[truncated 1481 chars]- Remediation
View remediation
]*)>(.*?)", html, re.DOTALL) ) PY ``` Apply this correction to every Python invocation at lines 84, 122, 182, and 217. Additional hardening should include: 1. Reject missing arguments before using `$1`: ```bash if [ "$#" -ne 1 ]; then printf 'Usage: %s \n' "$0" >&2 exit 2 fi ``` 2. Resolve and validate the path before processing it. 3. If output must remain in a workspace, verify that the resolved path remains beneath the approved workspace directory. 4. Use `--` before path operands in external commands where supported. 5. Add regression tests using filenames containing single quotes, newlines, spaces, backslashes, and Python metacharacters. 6. Avoid suppressing all Python errors with `2>/dev/null`, since doing so can conceal attempted exploitation and validation failures. ]]>
