simonw / simonw/tools

Arbitrary file write / Path traversal in python/extract_har.py

Open Beginner friendly
#320 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

A Path Traversal (Zip Slip) vulnerability exists in python/extract_har.py during archive extraction.

When extracting entries from a HAR zip archive, the script constructs target output file paths using unvalidated entries (_file reference or parsed URL path) via output_dir / file_ref or output_dir / path. If an archive contains directory traversal sequences (../) or absolute paths, the script writes files outside the designated output_dir, leading to arbitrary file creation or overwriting on the host filesystem.

Vulnerable Code Location

In python/extract_har.py (lines 112-130):

outpath = output_dir / file_ref
...
outpath.parent.mkdir(parents=True, exist_ok=True)
...
outpath.write_bytes(file_content)

Similarly, when using the --paths option, extract_path_from_url(request_url) only strips the leading slash via parsed.path.lstrip("/"), without preventing ../ traversal from escaping the destination directory.

Impact
  • Severity: High
  • Vulnerability Type: Path Traversal / Arbitrary File Overwrite (CWE-22)
  • An attacker providing a crafted .har.zip archive can write or overwrite arbitrary files anywhere the current user has write permissions (e.g. system files, configuration files, SSH keys, cron jobs).
Steps to Reproduce (PoC)
  1. Generate a malicious archive:
python3 -c "import zipfile, json
har = {'log': {'entries': [{'response': {'content': {'mimeType': 'text/plain', '_file': '../../../../tmp/har_pwned.txt'}}}]}}
with zipfile.ZipFile('/tmp/test_malicious.har.zip', 'w') as z:
    z.writestr('har.har', json.dumps(har))
    z.writestr('../../../../tmp/har_pwned.txt', 'INJECTED CONTENT USING PATH TRAVERSAL')
"
  1. Run extract_har.py extracting into an isolated target folder (./safe_dir):
python3 python/extract_har.py /tmp/test_malicious.har.zip text/plain -o ./safe_dir
  1. Verify that the file was written to /tmp/har_pwned.txt instead of staying inside ./safe_dir:
cat /tmp/har_pwned.txt
Suggested Fix

Verify that the resolved target path is strictly contained within the resolved output directory before writing:

resolved_outpath = (output_dir / file_ref).resolve()
base_dir = output_dir.resolve()

if not resolved_outpath.is_relative_to(base_dir):
    click.echo(f"Warning: Skipping unsafe path {file_ref}", err=True)
    continue

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 with python/extract_har.py around lines 112-130 and run the provided malicious archive reproduction. Check both the _file path and the --paths URL path handling, then verify that unsafe entries remain inside the output directory while normal archive extraction still works.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli, security
Issue type
Bug
Difficulty
2/5
Estimated time
1-2 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
76/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.