diff options
author | Jade Lovelace <lix@jade.fyi> | 2024-09-17 18:27:22 -0700 |
---|---|---|
committer | Jade Lovelace <lix@jade.fyi> | 2024-09-18 15:37:29 -0700 |
commit | 789b19a0cfe583586c85657e88d5933d2dbe5715 (patch) | |
tree | 6ca73c852c0f71f8ea4cc498c6b6d8790a9a3db5 /tests | |
parent | 4046e019ca6be35b583820df5c3f49e284259319 (diff) |
util: fix brotli decompression of empty input
This caused an infinite loop before since it would just keep asking the
underlying source for more data.
In practice this happened because an HTTP server served a
response to a HEAD request (for which curl will not retrieve any body or
call our write callback function) with Content-Encoding: br, leading to
decompressing nothing at all and going into an infinite loop.
This adds a test to make sure none of our compression methods do that
again, as well as just patching the HTTP client to never feed empty data
into a compression algorithm (since they absolutely have the right to
throw CompressionError on unexpectedly-short streams!).
Reported on Matrix: https://matrix.to/#/!lymvtcwDJ7ZA9Npq:lix.systems/$8BWQR_zKxCQDJ40C5NnDo4bQPId3pZ_aoDj2ANP7Itc?via=lix.systems&via=matrix.org&via=tchncs.de
Change-Id: I027566e280f0f569fdb8df40e5ecbf46c211dad1
Diffstat (limited to 'tests')
-rw-r--r-- | tests/unit/libutil/compression.cc | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/tests/unit/libutil/compression.cc b/tests/unit/libutil/compression.cc index f629fb0a3..8d181f53d 100644 --- a/tests/unit/libutil/compression.cc +++ b/tests/unit/libutil/compression.cc @@ -136,4 +136,28 @@ TEST_P(PerTypeNonNullCompressionTest, bogusInputDecompression) auto bogus = "this data is bogus and should throw when decompressing"; ASSERT_THROW(decompress(param, bogus), CompressionError); } + +TEST_P(PerTypeNonNullCompressionTest, truncatedValidInput) +{ + auto method = GetParam(); + + auto inputString = "the quick brown fox jumps over the lazy doggos"; + auto compressed = compress(method, inputString); + + /* n.b. This also tests zero-length input, which is also invalid. + * As of the writing of this comment, it returns empty output, but is + * allowed to throw a compression error instead. */ + for (int i = 0; i < compressed.length(); ++i) { + auto newCompressed = compressed.substr(compressed.length() - i); + try { + decompress(method, newCompressed); + // Success is acceptable as well, even though it is corrupt data. + // The compression method is not expected to provide integrity, + // just, not break explosively on bad input. + } catch (CompressionError &) { + // Acceptable + } + } +} + } |