T09 · Insecure Skill Coding Practices
Error
- Location
- run-tool.sh:42
- Finding
- Arbitrary Python Code Execution Through CLI Argument Interpolation<![CDATA[ ## Vulnerability Details **File Location**: `run-tool.sh:42-52` **Additional Affected Locations**: `run-tool.sh:12-21`, `run-tool.sh:27-36`, `run-tool.sh:58-68` **Vulnerability Type**: Python code injection through dynamically constructed source code **Risk Level**: High ### Vulnerable Code ```bash "update_block") DOCUMENT_TOKEN=$1 BLOCK_ID=$2 NEW_TEXT=$3 python3 -c " import sys sys.path.insert(0, '.') from src.feishu_api import FeishuClient import json api = FeishuClient() result = api.update_block('$DOCUMENT_TOKEN', '$BLOCK_ID', '''$NEW_TEXT''') print(json.dumps(result, indent=2, ensure_ascii=False)) " ;; ``` The same unsafe construction is also used by the `list_comments`, `get_block`, and `reply_comment` command branches. ### Technical Analysis Command-line arguments are inserted directly into a string that is subsequently interpreted as Python source by `python3 -c`. Shell quoting does not make these values safe inside the generated Python program. An attacker-controlled document token, block ID, comment ID, content value, or replacement text can contain quote delimiters and additional Python syntax. The crafted value can terminate the intended Python string literal and introduce arbitrary statements that are executed when the wrapper invokes Python. The `update_block` and `reply_comment` branches are particularly exposed because they accept free-form document or comment text. The flaw is not limited to shell metacharacters: even if shell expansion is avoided, the resulting data remains executable Python syntax. ### Attack Path 1. An attacker supplies maliciously structured text or an identifier through a request that causes an Agent or user to invoke `run-tool.sh`. 2. The wrapper assigns the value to a shell variable without validating its expected format. 3. The variable is interpolated into the source string passed to `python3 -c`. 4. Quote delimiters in the value escape the intended Python string. 5. Injected Python state ...[truncated 1099 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Remove all dynamic construction of Python source from shell arguments. 2. Replace `python3 -c` with a normal Python CLI implemented using `argparse`. 3. If the shell wrapper must remain, pass values as positional arguments to a static Python program and read them from `sys.argv`. 4. Validate structured identifiers using strict allowlists. For example, document tokens, block IDs, and comment IDs should only permit the character set and length documented by Feishu. 5. Treat document and comment text as opaque data and never embed it into executable source. 6. Add regression tests containing single quotes, triple quotes, newlines, backslashes, and Python-like text to confirm that these inputs remain data. 7. Run the Skill under a dedicated, minimally privileged operating-system account and limit access to unrelated files. A safe design is: ```bash python3 process_tool.py update_block \ --document-token "$DOCUMENT_TOKEN" \ --block-id "$BLOCK_ID" \ --new-text "$NEW_TEXT" ``` The Python program should then pass parsed argument values directly to `FeishuClient.update_block` without using `eval`, `exec`, or dynamically generated source code. ]]>
