Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

Unreleased
----------

- Fixed infinite loops in streaming consumers by rejecting trailing data
after a complete Brotli stream.

1.2.0.2 (2026-08-21)
--------------------

Expand Down
4 changes: 4 additions & 0 deletions src/brotlicffi/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,10 @@ def _decompress(self, data, output_buffer_limit):
b"Decompression error: %s" % ffi.string(error_message)
)

# Reject bytes remaining after the Brotli stream finishes.
if rc == lib.BROTLI_DECODER_RESULT_SUCCESS and available_in[0]:
raise error("Decompression error: trailing data after stream.")

# Next, copy the result out.
chunk = ffi.buffer(out_buffer, buffer_size - available_out[0])[:]
chunks.append(chunk)
Expand Down
13 changes: 13 additions & 0 deletions test/test_simple_decompression.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@ def test_high_expansion_prefix_without_output_buffer_limit():
assert result == uncompressed


@pytest.mark.parametrize('output_buffer_limit', [None, 50])
def test_decompressobj_rejects_trailing_data(output_buffer_limit):
o = brotlicffi.Decompressor()
data = brotlicffi.compress(b'A' * 100) + b'tail'
if output_buffer_limit is not None:
assert o.decompress(data, output_buffer_limit=50) == b'A' * 50
assert not o.can_accept_more_data()
data = b''

with pytest.raises(brotlicffi.error, match='trailing data'):
o.decompress(data, output_buffer_limit=output_buffer_limit)


def test_drip_feed(simple_compressed_file):
"""
Sending in the data one byte at a time still works.
Expand Down
Loading