T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/extract-frames.sh:19
- Finding
- Python Code Injection Through Shell-Interpolated Arguments<![CDATA[ ## Vulnerability Details **File Location**: `scripts/extract-frames.sh:19-61` **Vulnerability Type**: Python code injection **Risk Level**: High ### Vulnerable Code ```bash python3 -c " import json, subprocess, os timestamps = json.loads('''$TIMESTAMPS_JSON''') video = '$VIDEO_FILE' frames_dir = '$FRAMES_DIR' extracted = [] for item in timestamps: ts = item['timestamp'] frame_id = item['id'] primary = os.path.join(frames_dir, f'{frame_id}.png') subprocess.run([ 'ffmpeg', '-y', '-ss', str(ts), '-i', video, '-vframes', '1', '-q:v', '2', primary ], capture_output=True) # Save manifest manifest_path = os.path.join('$OUTPUT_DIR', 'frames-manifest.json') with open(manifest_path, 'w') as f: json.dump(extracted, f, indent=2) " ``` ### Technical Analysis The timestamp JSON, video path, frames directory, and output directory are inserted directly into Python source passed to `python3 -c`. Shell quoting does not make these values safe for use as Python source. A value containing Python string delimiters and additional Python statements can terminate one of the generated string literals. The resulting statements are then executed by the Python interpreter with the same permissions as the user or Agent running the Skill. This is not merely malformed-input handling: the vulnerable values originate from command-line arguments and are treated as executable source rather than data. ### Attack Path 1. An attacker influences the timestamp JSON, source-video path, or output path supplied to `extract-frames.sh`. 2. The crafted value includes characters that terminate the corresponding Python string literal. 3. Shell interpolation places the crafted content inside the `python3 -c` program. 4. Python parses the injected content as code. 5. The injected code executes with the Skill process's filesystem, environment, and process privileges. ### Impact Assessment Successful exploitation permits arbitrary local code ex ...[truncated 247 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions - Do not interpolate shell variables into Python source. - Store the Python implementation in a dedicated `.py` file and pass values through `sys.argv`. - Pass timestamp JSON through standard input and decode it with `json.load(sys.stdin)`. - Use an argument-safe invocation such as: ```bash printf '%s' "$TIMESTAMPS_JSON" | python3 scripts/extract_frames.py "$VIDEO_FILE" "$OUTPUT_DIR" ``` - In Python, read only from `sys.argv` and standard input: ```python video = sys.argv[1] output_dir = sys.argv[2] timestamps = json.load(sys.stdin) ``` - Validate the timestamp document's structure and reject unknown fields, invalid types, non-finite timestamps, and oversized input. - Run media-processing helpers with a restricted environment and only the filesystem access needed for the episode workspace. ]]>
