T09 · Insecure Skill Coding Practices
- Location
download.py:17- Finding
OS Command Injection Through User-Controlled Downloader Arguments
- Content
View full analysis
2 else "merged" output_dir = os.path.expanduser( sys.argv[3] if len(sys.argv) > 3 else "~/Movies" ) ``` Those values are interpolated into shell command strings: ```python cmd1 = f'{yt_dlp} -f "bestvideo" "{url}" -o "{video_file}"' if not run_command(cmd1): sys.exit(1) cmd2 = f'{yt_dlp} -x --audio-format m4a "{url}" -o "{audio_file}"' if not run_command(cmd2): sys.exit(1) cmd3 = ( f'ffmpeg -i "{video_file}" -i "{audio_file}" ' f'-c:v libx264 -c:a aac -shortest "{output_file}" -y' ) if not run_command(cmd3): sys.exit(1) ``` A second shell-execution sink is also used: ```python os.system(f'ls -lh "{output_file}"') ``` ### Technical Analysis The program builds command strings by directly interpolating the URL, output name, and output directory supplied through command-line arguments. It then invokes those strings with `shell=True` or `os.system()`. Double-quoting the interpolated values does not provide sufficient protection. An attacker can include a double quote in an argument to terminate the quoted value and append shell metacharacters such as `;`, `&&`, command substitutions, or redirections. The shell will then interpret the injected content as commands. The imported `shlex` module is not used to quote or tokenize the arguments. Even shell quoting would be less robust than avoiding shell interpretation entirely. # ...[truncated 1480 chars]- Remediation
View remediation
