T09 · Insecure Skill Coding Practices
- Location
scripts/mit-learn.sh:62- Finding
Python Code Injection Through a Crafted file:// Path
- Content
View full analysis
Vulnerability Details
File Location:
scripts/mit-learn.sh, lines 62–70
Vulnerability Type: Python code injection caused by unsafe interpolation into dynamically constructed source code
Risk Level: HighVulnerable Code
bash convert_file_url() { local input="$1" if [[ "$input" =~ ^file:// ]]; then # Remove the file:// prefix and URL-decode the value local path="${input#file://}" # URL decoding, such as %20 to a space path=$(python3 -c "import urllib.parse; print(urllib.parse.unquote('${path}'))" 2>/dev/null || echo "${path}") echo "$path" else echo "$input" fi }Technical Analysis
The
pathvariable is derived from a user-suppliedfile://argument and inserted directly into Python source passed topython3 -c.Although the shell variable expansion occurs inside shell double quotes, the expanded value is placed inside a single-quoted Python string:
python urllib.parse.unquote('${path}')Shell quoting does not escape the value for the Python grammar. A path containing a single quote can terminate the Python string, after which additional Python expressions or statements may be injected. The resulting code is executed locally by
python3with the permissions of the user running the Skill.The vulnerable function is reachable from both the URL-processing and file-processing paths in
cmd_add. Subsequent file-existence validation does not mitigate the issue because the injected Python code runs during URL decoding, before that validation occurs.Attack Path
- An attacker causes the Skill to be invoked with an attacker-controlled
--urlor--fileargument beginning withfile://. cmd_addidentifies the argument as a file URL and passes it toconvert_file_url.convert_file_urlremoves thefile://prefix.- The remaining attacker-controlled text is interpolated into the source string supplied to `p ...[truncated 1271 chars]
- An attacker causes the Skill to be invoked with an attacker-controlled
- Remediation
View remediation
Remediation Suggestions
Do not place user-controlled data inside dynamically constructed Python source. Pass the path as a separate process argument and retrieve it through
sys.argv:bash convert_file_url() { local input="$1" if [[ "$input" == file://* ]]; then local path="${input#file://}" python3 -c \ 'import sys, urllib.parse; print(urllib.parse.unquote(sys.argv[1]))' \ "$path" else printf '%s\n' "$input" fi }Additional hardening should include:
- Use
printf '%s\n'instead ofechowhen outputting untrusted values. - Reject malformed file URLs, embedded NUL representations, and unexpected control characters.
- Canonicalize the decoded path and enforce an allowlisted directory when callers should only upload files from approved locations.
- Preserve a clear failure state if decoding fails rather than silently falling back to potentially malformed input.
- Add regression tests covering single and double quotes, semicolons, command substitutions, newlines, percent-encoded quotes, backslashes, and Python syntax in file URLs.
- Run the Skill with the least-privileged user account necessary and avoid invoking it with administrative privileges.
- Use
