T09 · Insecure Skill Coding Practices
- Location
illustrations_local_storage.py:197- Finding
Unrestricted Outbound Image Requests Enable Server-Side Request Forgery
- Content
View full analysis
Vulnerability Details
File Location:
illustrations_local_storage.py, lines 197–202, 289–291, and 295–317
Vulnerability Type: Server-Side Request Forgery (SSRF) caused by an optional host allowlist
Risk Level: HighVulnerable Code
python parser.add_argument( "--allowed-host", action="append", default=[], metavar="HOST", help="只下载指定域名,可重复传入;默认不限制域名", )python def _host_is_allowed(url: str, allowed_hosts: set[str]) -> bool: if not allowed_hosts: return True hostname = (urllib.parse.urlsplit(url).hostname or "").lower().rstrip(".") return any(hostname == host or hostname.endswith("." + host) for host in allowed_hosts)python def _download_once( url: str, temp_path: Path, timeout: float, max_bytes: int, allowed_hosts: set[str], ) -> DownloadResult: if not _host_is_allowed(url, allowed_hosts): raise LocalizationError(f"图片域名不在允许列表中:{url}") request = urllib.request.Request( url, headers={ "Accept": "image/*,*/*;q=0.8", "User-Agent": f"illustrations-local-storage/{VERSION}", }, ) try: with urllib.request.urlopen(request, timeout=timeout) as response: final_url = response.geturl() if not _host_is_allowed(final_url, allowed_hosts): raise LocalizationError(f"图片重定向到了未允许的域名:{final_url}")Technical Analysis
The script extracts attacker-controllable HTTP or HTTPS image URLs from Markdown and passes them to
urllib.request.urlopen(). The--allowed-hostoption defaults to an empty list, and_host_is_allowed()explicitly interprets an empty allowlist as permission to access every hostname.Consequently, URLs targeting loopback interfaces, private network ranges, link-local addresses, cloud metadata services, or other internal services are not rejected. The final redirect URL is checked, but that check provides no protection under the default con ...[truncated 2529 chars]
- Remediation
View remediation
Remediation Suggestions
-
Require an explicit host allowlist by default. Reject remote downloads when no
--allowed-hostvalue is supplied, or provide a narrowly scoped default list of trusted SoMark object-storage domains. -
Reject unsafe address ranges. Before connecting, resolve every destination hostname and reject all resolved IPv4 and IPv6 addresses belonging to:
- Loopback ranges
- Private ranges
- Link-local ranges
- Unspecified ranges
- Multicast ranges
- Reserved or documentation ranges
-
Validate every redirect hop. Use a controlled redirect handler that validates the scheme, hostname, port, and resolved IP address before following each redirect. Checking only the final URL is insufficient because an unsafe redirect request may already have occurred.
-
Mitigate DNS rebinding. Ensure the address validated is the address used for the connection. Where feasible, pin the validated resolved address for the request while preserving correct TLS hostname verification.
-
Restrict schemes and ports. Continue allowing only HTTP and HTTPS, and consider limiting destination ports to those required by trusted object-storage services.
-
Apply network-level egress controls. Run the downloader in an environment that denies access to loopback services, private networks, link-local metadata services, and unrelated external destinations.
-
Make unrestricted operation an explicit high-risk opt-in. If arbitrary public image hosting must be supported, require a clearly named flag rather than treating unrestricted access as the default.
-
Add security tests. Cover direct and redirected requests to loopback, RFC 1918, IPv6 private and loopback addresses, link-local metadata addresses, alternative IP representations, and hostnames resolving to prohibited networks.
-
