T09 · Insecure Skill Coding Practices
- Location
search_skill.py:145- Finding
Unrestricted URL Retrieval and Caller-Controlled File Write
- Content
View full analysis
bool: """下载图片到本地""" import os headers = self._get_headers() try: # 确保目录存在 os.makedirs(os.path.dirname(output_path) if os.path.dirname(output_path) else ".", exist_ok=True) response = self.session.get(image_url, headers=headers, timeout=self.timeout) if response.status_code == 200: with open(output_path, "wb") as f: f.write(response.content) print(f"图片已下载:{output_path}") return True else: print(f"下载失败:HTTP {response.status_code}") return False except Exception as e: print(f"下载异常:{str(e)[:100]}") return False ``` ### Technical Analysis The `download_image()` method accepts both `image_url` and `output_path` from its caller without applying security restrictions. The URL is passed directly to `requests.Session.get()`. The method does not: - Restrict requests to HTTPS. - Restrict destination hostnames to approved image providers. - Resolve and reject loopback, private, link-local, reserved, or cloud metadata addresses. - Validate the destination of HTTP redirects. - Confirm that the response is an image. - Limit the response size or stream it under a fixed byte limit. Consequently, any caller able to invoke this Python method with controlled arguments can cause the process to contact arbitrary network destinations, including internal services. The destination path is also used directly with `os.makedirs()` and `open(..., "wb")`. There is no dedicated download root, canonical-path containment check, filename generation, symlink defense, or protection against overwriting an existing file. The method can therefore create directo ...[truncated 2107 chars]- Remediation
View remediation
