T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/download.py:66
- Finding
- SSRF Protection Is Vulnerable to DNS Rebinding and Unvalidated Redirects<![CDATA[ ## Vulnerability Details **File Location**: `scripts/download.py`, lines 66–82, with the validated URL subsequently used at lines 94–106 and 132–133 **Vulnerability Type**: Incomplete server-side request forgery protection **Risk Level**: High ### Complete Code Snippet ```python # Resolve and check for private IPs (SSRF protection) import socket try: addr_infos = socket.getaddrinfo(hostname, None) for info in addr_infos: ip_str = info[4][0] ip = ipaddress.ip_address(ip_str) for blocked in BLOCKED_IP_RANGES: if ip in blocked: return False, f"Hostname resolves to blocked IP: {ip_str}" except socket.gaierror: return False, f"Cannot resolve hostname: {hostname}" return True, None ``` The URL is later passed to separate `yt-dlp` processes: ```python cmd = [ "yt-dlp", "-o", output_file, "--no-playlist", "--merge-output-format", "mp4", "--retries", "2", "--socket-timeout", "30", "--no-warnings", "--", url ] result = subprocess.run(cmd, capture_output=True, text=True, timeout=120) ``` ```python meta_cmd = ["yt-dlp", "--no-playlist", "--print", "title", "--no-warnings", "--", url] meta_result = subprocess.run(meta_cmd, capture_output=True, text=True, timeout=30) ``` ### Technical Analysis The application resolves and validates the supplied hostname once, before invoking `yt-dlp`. The subprocess performs its own DNS resolution later, creating a time-of-check/time-of-use gap. A hostname that resolves to a public address during validation could resolve to an internal address when `yt-dlp` connects. The application also validates only the original URL. It does not apply the domain and IP restrictions to HTTP redirects or additional media URLs discovered by a `yt-dlp` extractor. Consequently, a permitted public endpoint may redirect the subprocess to an otherwise prohibited destination. Passing the URL as a separate subprocess argument prevents shell injectio ...[truncated 1595 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions - Enforce the destination policy for every outbound request, including redirects and extractor-discovered media URLs. - Resolve destinations through a controlled network proxy that rejects loopback, private, link-local, reserved, multicast, and cloud metadata ranges for both IPv4 and IPv6. - Pin the connection to an address validated immediately before use where the downloader and TLS behavior permit this safely. - Restrict the downloader in a network sandbox or container whose firewall cannot route to internal networks or metadata endpoints. - Reject redirects to disallowed domains and IP addresses rather than relying only on validation of the original URL. - Expand IP filtering to cover all non-global and special-use address classes, preferably by positively requiring globally routable addresses. - Apply equivalent restrictions to both the metadata probe and the actual download subprocess. ]]>
