Unhandled exception / crash (`zipfile.BadZipFile`) on non-zip input in `python/extract_har.py`
Nobody has claimed this yet.
- Dominant language
- HTML
- Stars
- 1.9k
- Forks
- 191
- Avg merge
- 36m
- Merged PRs (30d)
- 12
Description
Description
In python/extract_har.py, opening the input file with zipfile.ZipFile(harzip) is done without a try/except block catching zipfile.BadZipFile. If a user provides an invalid, corrupted, or non-ZIP file (e.g. a plain .txt file, a raw .har file, or truncated data), the script crashes abruptly with an unhandled zipfile.BadZipFile traceback rather than providing a user-friendly error message or exiting gracefully.
Vulnerable Code Location
In python/extract_har.py (lines 73-83):
with zipfile.ZipFile(harzip) as zf:
# Read the HAR JSON file
try:
har_content = json.loads(zf.read("har.har"))
except KeyError:
click.echo("Error: har.har not found in archive", err=True)
return
except json.JSONDecodeError:
click.echo("Error: Invalid JSON in har.har", err=True)
return
While KeyError and json.JSONDecodeError are handled inside the block, zipfile.ZipFile(...) itself is not wrapped in exception handling for zipfile.BadZipFile.
Impact
- Severity: Medium
- Vulnerability Type: Improper Error Handling / Denial of Service (CWE-754 / CWE-248)
- Any invalid input causes an unhandled exception and crash with stack trace, breaking automated batch pipelines or tooling that integrates this script.
Steps to Reproduce (PoC)
- Create a non-ZIP file:
echo "not a zip file" > invalid.txt
- Run
extract_har.pywith this file:
python3 python/extract_har.py invalid.txt text/plain
- Traceback observed:
Traceback (most recent call last):
...
File ".../zipfile/__init__.py", line 1334, in _RealGetContents
raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file
Suggested Fix
Wrap zipfile.ZipFile(harzip) in a try/except zipfile.BadZipFile block and report a clear error using click.echo(..., err=True):
try:
with zipfile.ZipFile(harzip) as zf:
# Read the HAR JSON file
try:
har_content = json.loads(zf.read("har.har"))
except KeyError:
click.echo("Error: har.har not found in archive", err=True)
return
except json.JSONDecodeError:
click.echo("Error: Invalid JSON in har.har", err=True)
return
...
except zipfile.BadZipFile:
click.echo(f"Error: {harzip} is not a valid zip file", err=True)
return
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in python/extract_har.py around lines 73-83 and reproduce the failure with python3 python/extract_har.py invalid.txt text/plain. Handle invalid ZIP input at the zipfile.ZipFile(harzip) entry point, preserving the existing handling for missing or invalid HAR contents. Done means invalid, corrupted, or non-ZIP input reports a clear error through the CLI instead of emitting an unhandled traceback.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- cli
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 86/100