T09 · Insecure Skill Coding Practices
Warning
- Location
- scripts/main.py:64
- Finding
- Arbitrary Local Files Can Be Encoded and Uploaded to Tencent Cloud<![CDATA[ ## Vulnerability Details **File Location**: `scripts/main.py:64-100` and `scripts/main.py:228-233` **Vulnerability Type**: Insufficient file-type and path validation before third-party upload **Risk Level**: Medium ### Vulnerable Code ```python def load_base64_image(value: str) -> str: """ Load image Base64 content. If value is an existing file path, read the file and encode it as Base64; otherwise, treat it directly as a Base64 string. """ if os.path.isfile(value): ext = os.path.splitext(value)[1].lower() if ext not in IMAGE_EXTENSIONS: print( f"Warning: file extension '{ext}' is not in the supported list " f"{sorted(IMAGE_EXTENSIONS)}; upload will still be attempted", file=sys.stderr, ) with open(value, "rb") as f: raw = f.read() try: raw_str = raw.decode("utf-8").strip() decoded = base64.b64decode(raw_str, validate=True) _check_image_size(len(decoded)) return raw_str except SystemExit: raise except Exception: pass _check_image_size(len(raw)) return base64.b64encode(raw).decode("utf-8") else: try: decoded = base64.b64decode(value, validate=True) _check_image_size(len(decoded)) except SystemExit: raise except Exception: print( "Error: the provided content is neither valid Base64 nor a valid file path", file=sys.stderr, ) sys.exit(1) return value ``` The resulting content is then included in the external API request: ```python if args.url: params["Url"] = args.url print(f"Using image URL: {args.url}", file=sys.stderr) else: print(f"Loading image: {args.image}", file=sys.stderr) params["Image"] = load_base64_image(args.image) print("Image Base64 enc ...[truncated 2546 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Reject unsupported extensions instead of continuing after a warning. 2. Validate file signatures and decode the file with a trusted image library before upload. Confirm that the decoded format is one of PNG, JPEG, or BMP. 3. Do not rely on filename extensions or MIME labels alone. 4. Restrict local file inputs to a designated, user-approved upload directory. Resolve paths with `os.path.realpath()` and verify that the resolved path remains inside the allowed directory. 5. Reject symbolic links or verify their resolved targets before opening them. 6. Open files only after validation and use a bounded read to enforce the size limit before loading the entire file into memory. 7. Require explicit user confirmation identifying the destination service before uploading local biometric images. 8. Document that image content is transmitted to Tencent Cloud and may be subject to external retention, privacy, and jurisdictional policies. ]]>
