T09 · Insecure Skill Coding Practices
- Location
codebase/pythongo/backtesting/tools.py:21- Finding
Unvalidated ZIP Extraction Permits Arbitrary File Writes
- Content
View full analysis
- Remediation
View remediation
None: root = Path(destination).resolve() for member in zip_file.infolist(): target = (root / member.filename).resolve() try: target.relative_to(root) except ValueError: raise ValueError(f"Unsafe ZIP member path: {member.filename}") if member.filename.startswith(("/", "\\")): raise ValueError(f"Absolute ZIP member path rejected: {member.filename}") zip_file.extract(member, root) ``` 2. Reject drive-qualified paths, `..` traversal components, absolute paths, symbolic links, and other special archive entries. 3. Require HTTPS and allowlist the expected download hostname. 4. Configure explicit connection and read timeouts. 5. Call `raise_for_status()` before processing the response. 6. Stream downloads and enforce compressed and decompressed size limits to prevent memory exhaustion and ZIP bombs. 7. Verify an authenticated checksum or digital signature supplied over a trusted channel. 8. Extract into a newly created private temporary directory first, validate the expected file structure and names, and only then move approved files into the cache. 9. Use a `with` statement for both the HTTP response and `ZipFile` so resources are reliably released on failure. ]]>
