Skip to content
Reject duplicate ZIP manifests before reading their contents

Reject duplicate ZIP manifests before reading their contents

November 16, 2026

A ZIP archive can contain more than one member named manifest.txt. An importer that expects one manifest should reject that ambiguity before reading the content. Inspect the member list and open the selected entry by its metadata object.

Set up

Tested on Linux with CPython 3.12.14. Use Bash, Python 3.12 or newer, a text editor, and the mktemp, rm and rmdir utilities. No packages or network access are needed. Keep the same terminal open and stop if setup fails. Run without -O so the assertions remain enabled.

start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1

Run the example

Save as unique_manifest.py:

from io import BytesIO
import warnings
from zipfile import ZipFile


def read_manifest(bundle, limit=64):
    matches = [item for item in bundle.infolist() if item.filename == "manifest.txt"]
    if len(matches) != 1:
        raise ValueError("expected exactly one manifest.txt")
    member = matches[0]
    if member.file_size > limit:
        raise ValueError("manifest exceeds byte limit")
    with bundle.open(member) as stream:
        content = stream.read(limit + 1)
    if len(content) > limit:
        raise ValueError("manifest exceeds byte limit")
    return content.decode("utf-8")


cases = [
    ("unique", [("manifest.txt", b"version=1")]),
    ("duplicate", [("manifest.txt", b"version=1"), ("manifest.txt", b"version=2")]),
    ("missing", [("notes.txt", b"hello")]),
    ("oversized", [("manifest.txt", b"x" * 65)]),
]

for label, entries in cases:
    with BytesIO() as data:
        with ZipFile(data, "w") as bundle:
            with warnings.catch_warnings(record=True) as recorded:
                warnings.simplefilter("always", UserWarning)
                for name, content in entries:
                    bundle.writestr(name, content)
            assert len(recorded) == (1 if label == "duplicate" else 0)
        data.seek(0)
        with ZipFile(data) as bundle:
            try:
                result = read_manifest(bundle)
            except ValueError as error:
                assert label != "unique"
                print(f"{label}: rejected ({error})")
            else:
                assert label == "unique" and result == "version=1"
                print(f"{label}: {result}")

Run:

python3 -B unique_manifest.py

Observed output:

unique: version=1
duplicate: rejected (expected exactly one manifest.txt)
missing: rejected (expected exactly one manifest.txt)
oversized: rejected (manifest exceeds byte limit)

Reject ambiguity before reading

The helper accepts one exact, case-sensitive name. Missing and duplicate manifests fail the same count check. It passes the selected ZipInfo object to open() instead of performing a second name lookup. The ZipFile documentation supports metadata objects for selecting members, including archives with duplicate names.

The 65-byte fixture exceeds the helper’s 64-byte limit. The helper checks the declared uncompressed size, then also bounds the read to one byte beyond the limit. Successful content is decoded as UTF-8. A decoding error or a corrupt archive is not converted into a valid manifest.

The writer deliberately creates a duplicate in memory. Its expected warning is captured locally and counted, so the demonstration verifies that fixture without changing the process-wide warning filter permanently. No files are extracted.

Keep the scope narrow

This check covers only the exact manifest name. It does not reject duplicate names elsewhere or normalize case, separators, or Unicode. Define those rules separately if the consuming application treats such names as equivalent.

The fixtures are tiny and trusted. The read limit is not a complete resource sandbox for arbitrary archives: opening an archive loads its directory metadata, and parsing and decompression still consume resources. Apply archive-size, member-count and runtime limits before using this pattern on outside input. The helper also does not authenticate a manifest, validate its application schema, or make extraction safe.

Clean up

Remove the saved file and empty scratch directory. If the directory is not empty, inspect its contents before removing anything else.

rm -- unique_manifest.py
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dir

Acknowledgements

This post was created with assistance from AI.

Last updated on