Skip to content

[Python] Add output_buffer_limit to brotli.decompress()#1431

Open
scovetta wants to merge 2 commits intogoogle:masterfrom
scovetta:fix/decompress-output-buffer-limit
Open

[Python] Add output_buffer_limit to brotli.decompress()#1431
scovetta wants to merge 2 commits intogoogle:masterfrom
scovetta:fix/decompress-output-buffer-limit

Conversation

@scovetta
Copy link
Copy Markdown

This PR adds output_buffer_limit to the brotli.decompess() function, to echo similar functionality in Decompressor.process(), enabling users to more safely decompress untrusted compressed data.

The default output_buffer_limit value of 0 means "unlimited", which preserves backwards compatibility. When set, decompress() raises brotli.error if the decompressed output would exceed the specified limit.

…on bombs

The one-shot brotli.decompress() API lacked any output size limit, allowing
attacker-supplied compressed payloads to cause unbounded memory allocation
(CWE-400). The sibling Decompressor.process() already supported an
output_buffer_limit parameter.

This adds an optional output_buffer_limit keyword argument (default 0 =
unlimited for backward compatibility). When set, decompress() raises
brotli.error if the decompressed output would exceed the specified limit.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@google-cla
Copy link
Copy Markdown

google-cla bot commented Mar 23, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@eustas
Copy link
Copy Markdown
Collaborator

eustas commented Mar 24, 2026

Hi. Thanks for the PR.

Where this new API will be used? I'm asking, because unlike decoding, for encoding guarantees the output size limit: see BrotliEncoderMaxCompressedSize.

@scovetta
Copy link
Copy Markdown
Author

@eustas This shouldn't be a new API, just a new (optional) parameter on the publicly exposed brotli.decompress() module-level function, so if an application could just do something like:

import brotli
import requests

res = requests.get('<url>')
- data = brotli.decompress(res.content)
+ data = brotli.decompress(res.content, output_buffer_limit=5_000_000)

The scenario we're trying to avoid is where the contents of <url> are absurdly large, or any other scenario where the bytes being decompressed aren't already trusted.

@eustas
Copy link
Copy Markdown
Collaborator

eustas commented Mar 24, 2026

Sorry for the confusion, I've misread (compression instead of decompresssion).

Will take a deep look soon-ish.

Again, thanks for the PR.

@eustas
Copy link
Copy Markdown
Collaborator

eustas commented Mar 25, 2026

So, current implementation allows situations when output is larger than requested limit.

Should we pass limit to "BufferInit", where it can remember the value and respect it in "BufferGrow"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants