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

Provide length-limited decompress methods #960

Closed
wants to merge 1 commit into from

Conversation

CamW
Copy link

@CamW CamW commented Apr 13, 2022

Provide length-limited decompress methods so that callers can control allocation sizes.

Motivation:
Limiting the size of buffer allocation can help mitigate the risks of compression bombs. In cases where these could be a legitimate attack vector, we should provide the library caller with the option of limiting the buffer size.

Changes:

  • Add decompressKnownLength method to Decoder.java to allow for decompression of known-size payloads.
  • Add decompress overload to Decoder.java which accepts a max size to allow for decompression of untrusted payloads with a maximum allowed size.
  • Modify DecoderJNI.java, decoder_jni.cc and decoder_jni.h to allow users to specify the output buffer size.

Result:
Callers which believe that they have known-size payloads or want to limit decompressed data sizes can decompress those payloads in linear space complexity based on the expected/max-allowed size rather than unknown space complexity, based on the actual size.

… allocation sizes.

Motivation:
Limiting the size of buffer allocation can help mitigate the risks of compression bombs. In cases where these could be a legitimate attack vector, we should provide the library caller with the option of limiting the buffer size.

Changes:
 -  Add `decompressKnownLength` method to `Decoder.java` to allow for decompression of known-size payloads.
 -  Add `decompress` overload to `Decoder.java` which accepts a max size to allow for decompression of untrusted payloads with a maximum allowed size.
 -  Modify `DecoderJNI.java`, `decoder_jni.cc` and `decoder_jni.h` to allow users to specify the output buffer size.

Result:
Callers which believe that they have known-size payloads or want to limit decompressed data sizes can decompress those payloads in linear space complexity based on the expected/max-allowed size rather than unknown space complexity, based on the actual size.
@eustas
Copy link
Collaborator

eustas commented Apr 14, 2022

Hello. Thank you for the PR.

I believe, we do not need changes on native (JNI) side. BrotliDecoderTakeOutput only provides access to the internal buffer. size parameter only affects how much output is considered consumed, not how much memory is used.
So the changes on JNI side are no-op memory-wise.

As a consequence, it is better to write a tiny proxy around BrotliInputStream/BrotliInputChannel that stops reading when there is more data than expected. This way decompressor will become an interchangeable part - you could plug JNI decoder, Java decoder, or even some other format decoder...

@CamW
Copy link
Author

CamW commented Apr 21, 2022

Thanks, I'll give that a go.

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