← All updates

Engineering / Field note

One gzip member may not be the whole payload

Five Python examples show how gzip members, completion flags and output limits affect the bytes an application actually checks.

By Zyel · Published · 4 min read

A gzip decoder can finish successfully while leaving another gzip member unread. That matters whenever an application checks decoded content and then hands the original compressed bytes to another consumer. The two components need to agree on the complete input being assessed. The format itself permits several members in sequence; an extra member is not automatically a broken file. [1] [4]

This is established format behavior, not a new vulnerability announcement. RFC 1952 is an Informational specification dated May 1996. We reviewed it and the current Python documentation on November 29, 2026. Our engineering recommendation is to make the accepted member count and completion rules explicit, rather than infer them from a successful decompression call. [1]

The five independent examples below use tiny, synthetic inputs generated in memory. They were checked locally with Python 3.15.2. Their assertions are teaching checks; the deliberately small policy function is not a complete streaming upload service. No live service, customer file or production decoder was tested.

1. Several members can form one gzip file

Python documents gzip.decompress as supporting concatenated members. The first fixture creates two separately compressed messages and joins their compressed bytes. The convenience decoder returns both messages. A component that only examines the first message would be considering less content than this consumer does. [2]

Original Python fixture: decode two concatenated members · Python
import gzip

first = gzip.compress(b"red\n", mtime=0)
second = gzip.compress(b"blue\n", mtime=0)
wire = first + second

assert gzip.decompress(wire) == b"red\nblue\n"

Neither color is suspicious. The lesson is about framing, not a special payload. When two components choose different framing rules, even ordinary data can reveal that their definitions of “the document” do not match.

A bytes-returning convenience function assembles a result for the caller. Python notes that gzip.decompress processes members in memory. For an application receiving untrusted input, decide how much compressed input, decoded output and processing time it can accept before selecting its decoding approach. Our tiny fixtures establish behavior, not operational limits for another application. [2]

2. A member boundary leaves evidence

The zlib manual explains that inflate does not automatically continue through concatenated gzip members: its end-of-stream result marks the member boundary. Python exposes trailing input through unused_data. With gzip framing selected by wbits=31, this fixture reaches the end of the first member and retains the second. [3] [4]

Original Python fixture: observe bytes after the first member · Python
import gzip
import zlib

first = gzip.compress(b"red\n", mtime=0)
second = gzip.compress(b"blue\n", mtime=0)
decoder = zlib.decompressobj(wbits=31)

plain = decoder.decompress(first + second)
assert plain == b"red\n"
assert decoder.eof
assert decoder.unused_data == second

That is a successful first-member result. Whether to continue decoding or reject additional input belongs to the application contract. Silently discarding unused_data makes that decision accidentally. Gzip is quite happy to bring a plus-one; the application needs a seating plan.

3. State a single-member policy directly

A protocol may intentionally accept exactly one member. The next original function demonstrates that narrower policy with fixed, small compressed-input and decoded-output limits. It rejects an incomplete member and any following bytes, including a second valid member. This is an application restriction, not a claim that the gzip format forbids concatenation.

Original Python fixture: enforce one member for a small bounded fixture · Python
import gzip
import zlib

def one_small_member(wire):
    if len(wire) > 256:
        raise ValueError("compressed input limit")
    decoder = zlib.decompressobj(wbits=31)
    plain = decoder.decompress(wire, 65)
    if len(plain) > 64 or decoder.unconsumed_tail:
        raise ValueError("decoded output limit")
    if not decoder.eof:
        raise ValueError("incomplete member")
    if decoder.unused_data:
        raise ValueError("bytes after first member")
    return plain

part = gzip.compress(b"fine", mtime=0)
assert one_small_member(part) == b"fine"
try:
    one_small_member(part + part)
except ValueError:
    pass
else:
    raise AssertionError("second member accepted")

The explicit exceptions perform the policy checks; the assertions only exercise the fixture. A larger streaming implementation would also need transport framing, bounded reads, total budgets and its own error handling. If multiple members are allowed, their decoded contents must all pass the intended content checks.

4. Output is not proof of completion

Python’s eof flag distinguishes a completed compressed stream from an incomplete one. The next example removes part of a member’s trailer. Its text can already be available while completion remains false. A properly encoded empty member, meanwhile, produces no text and does complete. Output length alone cannot distinguish those outcomes. [3]

Original Python fixture: compare a truncated member with a valid empty one · Python
import gzip
import zlib

wire = gzip.compress(b"check me", mtime=0)
partial = zlib.decompressobj(wbits=31)
assert partial.decompress(wire[:-4]) == b"check me"
assert not partial.eof

empty = zlib.decompressobj(wbits=31)
assert empty.decompress(gzip.compress(b"", mtime=0)) == b""
assert empty.eof

The zlib manual also describes checking the gzip trailer’s checksum and length. Our interpretation is straightforward: finding the expected text early should not replace reaching and validating the required end boundary. [4]

5. An output limit is another stopping condition

Python separates unused_data from unconsumed_tail. The latter can hold input left because a decompression call reached its output limit. A nonzero max_length bounds that call’s returned bytes; it does not establish that the rest of the input was examined. [3]

Original Python fixture: recognize an unfinished bounded read · Python
import gzip
import zlib

wire = gzip.compress(b"A" * 1024, mtime=0)
decoder = zlib.decompressobj(wbits=31)
prefix = decoder.decompress(wire, 32)

assert prefix == b"A" * 32
assert decoder.unconsumed_tail
assert not decoder.eof
assert decoder.unused_data == b""

Treat budget exhaustion, truncation and trailing input as distinct results. For a consumer that permits concatenated members, apply total limits across the entire message rather than starting a fresh allowance for every member. Then validate the same decoded content the next component will consume. A reliable completion decision needs both the decoder’s evidence and the application’s declared framing policy.

Sources + further reading

Source dates describe the original material. Publication above is the date this Zyel article went live.

  1. RFC 1952: GZIP file format specificationRFC Editor · Checked Nov 29, 2026
  2. Python 3.15 gzip modulePython documentation · Checked Nov 29, 2026
  3. Python 3.15 zlib modulePython documentation · Checked Nov 29, 2026
  4. zlib manual: inflate and gzip memberszlib · Checked Nov 29, 2026

Reader discussion

What would you ask next?

Questions, useful corrections and thoughtful disagreement are welcome. Comments are reviewed before they appear.

No reader comments yet.

10–2,000 characters. Plain text. Leave out passwords, private project data and personal contact details. A blank display name appears as Anonymous.

Complete the CAPTCHA before sending. Verification runs on your device.