T09 · Insecure Skill Coding Practices
- Location
download_pexels.py:132- Finding
Unbounded Download and Processing of Remote Image Data
- Content
View full analysis
Vulnerability Details
File Location:
download_pexels.py, lines 132-143
Vulnerability Type: Unrestricted resource consumption
Risk Level: MediumVulnerable Code
python # 下载图片 img_response = requests.get(photo_url, timeout=15) if img_response.status_code != 200: print(f"❌ 下载失败: HTTP {img_response.status_code}") return None # 打开图片 img = Image.open(io.BytesIO(img_response.content)) original_size = img.size # 确定目标尺寸 if target_size is None:Technical Analysis
The application retrieves a remotely supplied image URL with
requests.get()and accessesimg_response.content, causing the entire response body to be buffered in memory. It does not enforce a maximum response size, inspectContent-Length, validate the media type, or limit the image's decoded dimensions before processing it with Pillow.A compressed image can be relatively small on the network while requiring substantial memory and CPU when decoded or resized. Although Pillow includes decompression-bomb safeguards, relying only on library defaults does not address unrestricted HTTP response buffering and may not prevent all resource-exhaustion conditions.
The URL originates from the Pexels API response rather than direct command-line input, which reduces exposure under normal operation. Exploitation would require a malicious or compromised upstream response, unsafe direct use of
download_photo()with attacker-controlled metadata, or redirection to hostile image content.Attack Path
- An attacker causes
photo_info["src"]["large"]to reference an attacker-controlled resource, such as through a compromised upstream response or an application that invokesdownload_photo()using untrusted metadata. - The hostile server returns an extremely large response body or a compressed image with excessive decoded dimensions.
requests.get()buffers the unrestricted response inimg_response.content.- Pillo ...[truncated 622 chars]
- An attacker causes
- Remediation
View remediation
Remediation Suggestions
- Use
stream=Trueand read the response in bounded chunks. - Reject responses whose declared
Content-Lengthexceeds a configured maximum. - Stop streaming and delete partial data if the actual byte count exceeds that maximum.
- Require an approved image
Content-Type, while also validating the file signature because headers can be forged. - Set explicit maximum width, height, and pixel-count limits before fully decoding or resizing an image.
- Convert
PIL.Image.DecompressionBombWarninginto an exception and handle it explicitly. - Apply memory, CPU, and execution-time limits when processing remote media.
- Disable redirects or validate every redirect destination if images are not strictly limited to trusted HTTPS hosts.
- Use
