Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail decompression of small bogus content. #50

Merged
merged 5 commits into from Sep 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -8,7 +8,7 @@ python:

sudo: false
install:
- "pip install -U pip setuptools"
- "pip install -U pip 'setuptools!=26.*'"
- "pip install ."
- "pip install -r test_requirements.txt"
- "pip install flake8"
Expand Down
23 changes: 19 additions & 4 deletions src/brotli/brotli.py
Expand Up @@ -82,7 +82,9 @@ def decompress(data):
:param data: A bytestring containing Brotli-compressed data.
"""
d = Decompressor()
return d.decompress(data)
data = d.decompress(data)
d.finish()
return data


def compress(data,
Expand Down Expand Up @@ -433,15 +435,28 @@ def flush(self):
Complete the decompression, return whatever data is remaining to be
decompressed.

This action also resets the decompression state, allowing the
decompressor to be used again.

.. deprecated:: 0.4.0

This method is no longer required, as decompress() will now
decompress eagerly.

:returns: A bytestring containing the remaining decompressed data.
"""
return b''

def finish(self):
"""
Finish the decompressor. As the decompressor decompresses eagerly, this
will never actually emit any data. However, it will potentially throw
errors if a truncated or damaged data stream has been used.

Note that, once this method is called, the decompressor is no longer
safe for further use and must be thrown away.
"""
assert (
lib.BrotliDecoderHasMoreOutput(self._decoder) == lib.BROTLI_FALSE
)
if lib.BrotliDecoderIsFinished(self._decoder) == lib.BROTLI_FALSE:
raise Error("Decompression error: incomplete compressed stream.")

return b''
7 changes: 5 additions & 2 deletions test/test_simple_decompression.py
Expand Up @@ -33,6 +33,7 @@ def test_decompressobj(simple_compressed_file):
o = brotli.Decompressor()
data = o.decompress(compressed_data)
data += o.flush()
data += o.finish()

assert data == uncompressed_data

Expand All @@ -53,6 +54,7 @@ def test_drip_feed(simple_compressed_file):
outdata.append(o.decompress(compressed_data[i:i+1]))

outdata.append(o.flush())
outdata.append(o.finish())

assert b''.join(outdata) == uncompressed_data

Expand All @@ -68,9 +70,10 @@ def test_streaming_decompression_fails_properly_on_garbage(exception_cls):


@pytest.mark.parametrize('exception_cls', [brotli.Error, brotli.error])
def test_decompression_fails_properly_on_garbage(exception_cls):
@pytest.mark.parametrize('bogus', (b'some random garbage', b'bogus'))
def test_decompression_fails_properly_on_garbage(bogus, exception_cls):
"""
Garbage data properly fails decompression.
"""
with pytest.raises(exception_cls):
brotli.decompress(b'some random garbage')
brotli.decompress(bogus)