python / python/cpython

`zipfile` append corrupts a file due to auto filename sanitization

Open
#153,625 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

When a ZIP archive is opened and rewritten in append mode, a member having a non-standard filename will be corrupted because its filename rewritten to the central directory is auto-sanitized and thus inconsistent with the immutable local file entry when loaded in the future.

This can easily happen if a member containing \ is added in POSIX and the archive is then appended in Windows.

For example:

import io
import zipfile
from unittest import mock

TESTFN = io.BytesIO()

# A file written in POSIX allows '\\'
with mock.patch('os.sep', '/'), mock.patch('os.altsep', None), \
     zipfile.ZipFile(TESTFN, mode="w") as zipfp:
    zi = zipfile.ZipInfo('MyFolder/My\\File.txt')
    zipfp.writestr(zi, 'foo')

# A file written in Windows has '\\' replaced with '/'
with mock.patch('os.sep', '\\'), mock.patch('os.altsep', '/'), \
     zipfile.ZipFile(TESTFN, "a") as zipfp:
    # trigger archive rewriting
    zipfp.comment = b''

with zipfile.ZipFile(TESTFN, "r") as zipfp:
    zi = zipfp.infolist()[0]
    print('content:', zipfp.read(zi))

The above code raises an error: zipfile.BadZipFile: File name in directory 'MyFolder/My/File.txt' and header b'MyFolder/My\\File.txt' differ.

CPython versions tested on:

3.16, 3.14

Operating systems tested on:

No response

Linked PRs
  • gh-153626

Contributor guide

Open the contributing guide

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 by running the reproducer in the issue and inspect Python's zipfile append-mode archive rewriting and filename sanitization paths. Done means an archive created with a backslash-containing member can be appended and later read without a directory/header filename mismatch; the issue links PR gh-153626, so check that work first.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.