simonw / simonw/tools

Unhandled exception / crash (`zipfile.BadZipFile`) on non-zip input in `python/extract_har.py`

Open Beginner friendly
#321 0 comments 0 reactions 0 assignees View on GitHub

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)
  1. Create a non-ZIP file:
echo "not a zip file" > invalid.txt
  1. Run extract_har.py with this file:
python3 python/extract_har.py invalid.txt text/plain
  1. 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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.