T09 · Insecure Skill Coding Practices
- Location
scripts/download_images.py:26- Finding
Unrestricted URL Fetching Enables Server-Side Request Forgery and Resource Exhaustion
- Content
View full analysis
bool: """下载单张图片""" if headers is None: headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36', 'Referer': 'https://www.taobao.com/' } try: # 处理URL协议 if url.startswith('//'): url = 'https:' + url resp = requests.get(url, headers=headers, timeout=30) resp.raise_for_status() with open(save_path, 'wb') as f: f.write(resp.content) return True except Exception as e: print(f" 下载失败: {url[:50]}... - {e}") return False ``` The URLs passed to the vulnerable function are read directly from a user-selected file: ```python if args.images: urls_file = Path(args.images) if not urls_file.exists(): print(f"错误: 图片列表文件不存在: {args.images}") sys.exit(1) urls = [line.strip() for line in urls_file.read_text().split('\n') if line.strip()] print(f"从文件读取到 {len(urls)} 个图片URL") success = 0 for i, url in enumerate(urls, 1): ext = '.jpg' if '.png' in url.lower(): ext = '.png' elif '.webp' in url.lower(): ext = '.webp' save_path = output_dir / f"image_{i:03d}{ext}" print(f"[{i}/{len(urls)}] 下载中...") if download_image(url, str(save_path)): success += 1 ``` ### Technical Analysis Each nonempty line in the supplied image-list file is passed directly to `requests.get`. The implementation does not restrict URL schemes or destination domains, resolve and inspect destination IP addresses, validate redirec ...[truncated 2283 chars]- Remediation
View remediation
MAX_IMAGE_BYTES: raise ValueError("Image exceeds the permitted size") output.write(chunk) ``` 7. Validate the downloaded file's magic bytes or decode it with a trusted image library before retaining it. 8. Limit the number of URLs processed per invocation and enforce a maximum aggregate download size. 9. Remove partially written files when validation or downloading fails. 10. Apply outbound network controls at the container or host level so the process cannot reach metadata endpoints or internal management network ...[truncated 49 chars]
