T09 · Insecure Skill Coding Practices
- Location
save_video.py:5- Finding
Unrestricted User-Controlled URL Enables Server-Side Request Forgery
- Content
View full analysis
Vulnerability Details
File Location:
save_video.py, lines 5-22
Vulnerability Type: Unrestricted outbound request / Server-Side Request Forgery (SSRF)
Risk Level: HighVulnerable Code
python def extract_url(text): url_pattern = r'https?://[^\s]+' urls = re.findall(url_pattern, text) return urls[0] if urls else None def download_video(url): try: output_dir = os.path.join(os.getcwd(), "videos") os.makedirs(output_dir, exist_ok=True) command = [ sys.executable, "-m", "yt_dlp", "--format", "best", "--output", os.path.join(output_dir, "%(title)s.%(ext)s"), url ] subprocess.run(command, check=True) return f"Video downloaded successfully to {output_dir}" except Exception as e: return f"Error downloading video: {str(e)}"Technical Analysis
The regular expression accepts the first arbitrary HTTP or HTTPS URL in user-controlled input. The application performs no validation of the URL hostname, resolved IP address, port, redirect destination, or protocol transition before passing it to
yt_dlp.Because
yt_dlpperforms outbound network requests on behalf of the process, an attacker can provide URLs targeting resources that are inaccessible from the attacker's own network position but reachable from the host running this skill. Potential targets include loopback services, RFC 1918 private networks, link-local addresses, container or orchestration control endpoints, and cloud instance metadata services.Using a subprocess argument list without
shell=Trueprevents direct shell metacharacter injection here, but it does not prevent SSRF because the URL remains an attacker-controlled network destination.Attack Path
- An attacker submits text containing an HTTP or HTTPS URL that refers to an internal or otherwise privileged netw ...[truncated 1299 chars]
- Remediation
View remediation
Remediation Suggestions
- Enforce an explicit allowlist of supported video-service hostnames rather than accepting arbitrary HTTP or HTTPS destinations.
- Parse URLs with
urllib.parse.urlsplit()and reject malformed URLs, embedded credentials, unsupported schemes, unexpected ports, and ambiguous host representations. - Resolve the hostname before making a request and reject all loopback, private, link-local, multicast, unspecified, reserved, and otherwise non-global IPv4 and IPv6 addresses.
- Protect against DNS rebinding by ensuring the address actually used for the connection is one of the previously validated addresses.
- Disable redirects where practical. If redirects are required, apply the same scheme, hostname, port, and resolved-address validation to every redirect destination.
- Block known metadata endpoints at both application and network layers, including link-local metadata addresses.
- Run the downloader in a restricted environment with outbound network filtering, no access to sensitive internal services, minimal filesystem permissions, and strict resource limits.
- Add automated tests covering loopback addresses, private IPv4 and IPv6 ranges, encoded IP representations, malicious redirects, DNS rebinding scenarios, and unsupported domains.
