T09 · Insecure Skill Coding Practices
- Location
scripts/ocr.py:24- Finding
Unrestricted URL Fetching Enables Server-Side Request Forgery
- Content
View full analysis
Vulnerability Details
File Location:
scripts/ocr.py, lines 24–35 and 68–82
Vulnerability Type: Server-Side Request Forgery (SSRF) through unrestricted HTTP(S) retrieval
Risk Level: MediumVulnerable Code
python def download_file(url, tmp_path): """下载文件到临时路径""" try: import urllib.request urllib.request.urlretrieve(url, tmp_path) return tmp_path except Exception as e: # 尝试 curl result = subprocess.run(['curl', '-s', '-o', tmp_path, url], capture_output=True) if result.returncode != 0: raise Exception(f"下载失败: {e}") return tmp_pathThe vulnerable function is reached with a user-controlled URL here:
python if is_url(image_input): print(f"[OCR] 检测为 URL,正在下载...", file=sys.stderr) suffix = '.png' if '.jpg' in image_input or '.jpeg' in image_input: suffix = '.jpg' elif '.gif' in image_input: suffix = '.gif' elif '.webp' in image_input: suffix = '.webp' with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as f: tmp_file = f.name download_file(image_input, tmp_file) image_path = tmp_fileTechnical Analysis
The positional
imageargument is accepted as a URL whenever it starts withhttp://orhttps://. The URL is then passed directly tourllib.request.urlretrieve. If that operation raises an exception, the same URL is passed tocurl.Neither retrieval path validates the destination hostname or its resolved IP address. Consequently, the caller can direct requests toward loopback, private, link-local, reserved, or cloud metadata network ranges. Redirects are also not explicitly disabled or validated, so a public URL may redirect the request to an internal destination.
The implementation additionally lacks explicit connection/read timeouts, response-size limits, and pre-download content validatio ...[truncated 2320 chars]
- Remediation
View remediation
Remediation Suggestions
- Prefer an explicit allowlist of trusted image-hosting domains. Reject all other destinations where unrestricted URL support is unnecessary.
- Require HTTPS unless plain HTTP is explicitly needed for a trusted destination.
- Resolve the destination hostname before connecting and reject every address in loopback, private, link-local, multicast, unspecified, reserved, and other non-public ranges for both IPv4 and IPv6.
- Protect against DNS rebinding by ensuring the validated address is the address actually used for the connection. Do not rely only on a one-time hostname string check.
- Disable redirects or validate the hostname and resolved addresses at every redirect hop. Set a small maximum redirect count.
- Apply strict connection and read timeouts to both the primary downloader and any fallback.
- Stream the response while enforcing a maximum byte count rather than downloading an unrestricted body directly to disk.
- Validate the response content type and image magic bytes before invoking Tesseract. Do not infer trust or format from a substring in the URL.
- Run network retrieval with outbound firewall controls that prevent access to internal and cloud metadata networks.
- Avoid maintaining two download implementations with different security behavior. Use one hardened retrieval routine and return a clear error if it fails.
- Consider accepting uploaded image bytes through a controlled interface instead of allowing the Skill to retrieve arbitrary URLs.
