T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/batch_process.py:58
- Finding
- Unrestricted URL Retrieval Enables Server-Side Request Forgery and Local Resource Access<![CDATA[ ## Vulnerability Details **File Location**: `scripts/batch_process.py:58-75` **Vulnerability Type**: Server-Side Request Forgery (SSRF) and unrestricted URI handling **Risk Level**: High ### Vulnerable Code ```python def download_image(url, output_path): """ Download image from URL. Args: url: Image URL output_path: Destination path """ try: print(f" ↓ Downloading: {url}") with urlopen(url) as response: with open(output_path, "wb") as f: f.write(response.read()) print(f" ✅ Saved to: {output_path}") return True except Exception as e: print(f" ❌ Download failed: {e}") return False ``` ### Technical Analysis The `image_url` field is loaded directly from a user-supplied CSV file and passed to `urllib.request.urlopen()` without validating: - The URI scheme - The destination hostname - The resolved IP address - Redirect destinations - Whether the destination is a loopback, link-local, private, reserved, or cloud metadata address `urlopen()` supports more than ordinary public HTTPS requests. Depending on the runtime environment, an attacker may provide URLs targeting internal HTTP services or local resources through schemes such as `file:`. The operation is part of the declared product-image workflow, but unrestricted access to arbitrary network and local destinations exceeds the minimum privileges required. The legitimate feature only needs to retrieve product images from trusted public HTTPS locations. ### Attack Path 1. An attacker creates or modifies a product CSV processed by `batch_process.py`. 2. The attacker places a crafted URI in the `image_url` column, such as: - A loopback or internal service URL - A cloud instance metadata URL - A `file:` URI referencing a local file readable by the process - A public URL that redirects to a private destination 3. `load_products()` accepts the value without validation. ...[truncated 958 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Permit only `https` URLs; reject `file:`, `ftp:`, `data:`, and all other schemes. 2. Reject URLs containing embedded user credentials. 3. Use a strict allowlist of trusted image-hosting domains where feasible. 4. Resolve the hostname before connecting and reject all loopback, private, link-local, multicast, reserved, and unspecified IP addresses. 5. Repeat destination validation after every redirect. 6. Disable redirects unless they are required. 7. Block known cloud metadata destinations, including link-local metadata addresses. 8. Use a hardened HTTP client with explicit connection and read timeouts. 9. Verify that the returned content is a supported image before using it. 10. Run network retrieval in a sandbox with restricted outbound access and no unnecessary filesystem permissions. ]]>
