T09 · Insecure Skill Coding Practices
- Location
extract_palette.py:60- Finding
Unrestricted Remote Image Fetching Enables Server-Side Request Forgery and Resource Exhaustion
- Content
View full analysis
Vulnerability Details
File Location:
extract_palette.py, lines 60–63 and 183–187
Vulnerability Type: Server-Side Request Forgery and Unbounded Remote Response Processing
Risk Level: Medium
Category: T09: Insecure Skill Coding PracticesVulnerable Code
python def extract_colors(img_path, n=6): """用像素采样 + K-Means 提取 n 种主色""" try: img = Image.open(img_path) except Exception: resp = requests.get(img_path, timeout=10) img = Image.open(io.BytesIO(resp.content))python # 下载URL图片 if img_arg.startswith("http"): resp = requests.get(img_arg, timeout=10) img_path = "/tmp/extracted_source.png" with open(img_path, "wb") as f: f.write(resp.content) else: img_path = img_argTechnical Analysis
The command-line argument is used directly as the destination of
requests.get(). The implementation does not validate the URL hostname or resolved IP address and does not reject loopback, link-local, private, reserved, or cloud metadata destinations. Redirects are enabled by default, but redirect targets are not revalidated.Consequently, an attacker who can control the image URL can make the host running the Skill issue HTTP requests to destinations that may not be reachable from the attacker's own network. This is a blind SSRF condition because the script does not directly print the fetched response, although response behavior, errors, processing time, and image-decoding success can reveal limited information. GET endpoints with side effects could also be triggered.
The code additionally accesses
resp.content, which buffers the complete response in memory, and then writes it to disk or passes it to Pillow. The ten-second timeout limits request duration but does not limit response size. There is no HTTP status validation, content-type validation, streaming byte limit, or decoded image-dimension limit. A malicious server ...[truncated 1570 chars]- Remediation
View remediation
Remediation Suggestions
- Parse URLs with
urllib.parse.urlsplit()and permit only explicithttpandhttpsschemes. - Resolve the hostname before connecting and reject every loopback, private, link-local, multicast, reserved, unspecified, and otherwise non-global IP address.
- Explicitly block well-known metadata destinations, including link-local metadata addresses.
- Disable redirects or manually follow a small number of redirects while repeating scheme, hostname, DNS, and resolved-address validation for every target.
- Use
stream=True, inspectContent-Lengthwhen available, and stop reading after a strict maximum number of bytes. - Call
raise_for_status()and reject responses whose content type is not an approved image type. - Configure separate connection and read timeouts.
- Set Pillow image-dimension and decompression-bomb limits, and reject images exceeding an application-defined pixel count.
- Avoid downloading the same URL twice. Process a single bounded response from memory or from a securely created temporary file.
- Where remote images are not essential, disable URL input and accept only trusted local files.
- Parse URLs with
