T09 · Insecure Skill Coding Practices
- Location
SKILL.md:226- Finding
Arbitrary Python Code Execution Through Unsafely Interpolated Contract Filename
- Content
View full analysis
&1) echo "$result" | python3 -c " import sys,json data=json.load(sys.stdin) for d in data.get('dirs',[]): if d.get('name')=='$dir_name': print(d.get('id','')) break " 2>/dev/null } ``` The vulnerable function is subsequently called with a directory name derived from the local contract filename: ```bash CONTRACT_DIR_NAME="${FILE_NAME%.docx}" CONTRACT_DIR_GNS=$(check_dir_exists "$DATE_DIR_GNS" "$CONTRACT_DIR_NAME") ``` ### Technical Analysis The `dir_name` shell variable is interpolated directly into Python source code passed to `python3 -c`: ```python if d.get('name')=='$dir_name': ``` Shell quoting does not make this value safe within the generated Python program. If the contract filename contains a single quote followed by syntactically valid Python content, it can terminate the intended Python string literal and alter the program executed by `python3 -c`. The untrusted value originates from `FILE_NAME`, which is derived from the user-selected local file path. Removing the `.docx` suffix does not sanitize quotes, line breaks, backslashes, or Python syntax. Consequently, a specially crafted filename can become executable Python source when the documented Bash workflow is run. This is an injection vulnerability rather than the remote-payload behavior identified by the pre-scan. The reviewed project does not contain a `curl | bash` execution pattern; its `cat "$LOCAL_FILE" | curl ... -T -` pipeline uploads file bytes and does not execute downloaded shell code. ### Attack Path 1. An attacker cr ...[truncated 1597 chars]- Remediation
View remediation
&1) || return 1 printf '%s' "$result" | python3 -c ' import json import sys name = sys.argv[1] data = json.load(sys.stdin) for directory in data.get("dirs", []): if directory.get("name") == name: print(directory.get("id", "")) break ' "$dir_name" } ``` Additional hardening should include: 1. Validate filenames before processing and reject embedded control characters such as newlines and null-equivalent input. 2. Never construct Python, shell, JSON, or query-language source by textual interpolation of user-controlled values. 3. Use a proper JSON encoder whenever filenames or identifiers are included in request bodies. 4. Check command exit statuses rather than suppressing all Python errors with `2>/dev/null`. 5. Run the workflow under a dedicated, least-privileged account and use an AnyShare token restricted to only the required document library and operations. 6. Add regression tests using filenames containing single quotes, double quotes, backslashes, line breaks, Unicode characters, and shell or Python metacharacters. ]]>
