T09 · Insecure Skill Coding Practices
- Location
scripts/process.sh:99- Finding
Arbitrary Python Code Execution Through a Crafted Input Filename
- Content
View full analysis
/dev/null; then log_error "JSON validation failed - output is not valid JSON" echo -e "${RED}✗ Invalid JSON output${NC}" return 1 fi ``` ### Technical Analysis The input filename is attacker-controlled when files can be placed in `data/inbox/` or supplied as the first argument to `process.sh`. Although the shell variable is quoted at the shell level, its content is inserted into a single-quoted Python string literal passed to `python3 -c`. A filename containing a single quote and suitable Python syntax can terminate the path literal, close the surrounding function calls, and append arbitrary Python statements. Shell quoting does not protect the generated Python program from source-code injection. For example, a basename shaped like the following can alter the generated Python expression: ```text card'));__import__('os').system('id');# ``` The parser must create the requested JSON output before the vulnerable validation command is reached. If it does, the injected Python statement runs under the account executing the workflow. ### Attack Path 1. The attacker obtains the ability to place a file in `data/inbox/` or persuade an operator to process an attacker-supplied path. 2. The attacker gives a valid-sized `.ddd` file a basename containing a quote, closing parentheses, injected Python code, and a comment marker. 3. The operator invokes `scripts/process.sh`, directly or through the documented workflow. 4. `BASENAM ...[truncated 905 chars]- Remediation
View remediation
/dev/null; then log_error "JSON validation failed - output is not valid JSON" echo -e "${RED}✗ Invalid JSON output${NC}" return 1 fi ``` Additional hardening should include: 1. Restrict accepted basenames to a conservative character set, such as letters, digits, periods, underscores, and hyphens. 2. Reject filenames containing control characters, path separators, quotes, or newline characters. 3. Resolve and verify that input and output paths remain within their intended directories. 4. Prefer a dedicated Python validation script over dynamically generated `python3 -c` programs. 5. Add regression tests using filenames containing quotes, whitespace, command substitutions, semicolons, and newline characters. ]]>
