Merge pull request #22929 from yashykt/gzipcompmissingtrailer

Fail decompression when the gzip trailer is missing
pull/22934/head
Yash Tibrewal 5 years ago committed by GitHub
commit 8cf2c89167
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      src/core/lib/compression/message_compress.cc
  2. 29
      test/core/compression/message_compress_test.cc

@ -34,7 +34,7 @@
static int zlib_body(z_stream* zs, grpc_slice_buffer* input, static int zlib_body(z_stream* zs, grpc_slice_buffer* input,
grpc_slice_buffer* output, grpc_slice_buffer* output,
int (*flate)(z_stream* zs, int flush)) { int (*flate)(z_stream* zs, int flush)) {
int r; int r = Z_STREAM_END; /* Do not fail on an empty input. */
int flush; int flush;
size_t i; size_t i;
grpc_slice outbuf = GRPC_SLICE_MALLOC(OUTPUT_BLOCK_SIZE); grpc_slice outbuf = GRPC_SLICE_MALLOC(OUTPUT_BLOCK_SIZE);
@ -68,6 +68,10 @@ static int zlib_body(z_stream* zs, grpc_slice_buffer* input,
goto error; goto error;
} }
} }
if (r != Z_STREAM_END) {
gpr_log(GPR_INFO, "zlib: Data error");
goto error;
}
GPR_ASSERT(outbuf.refcount); GPR_ASSERT(outbuf.refcount);
outbuf.data.refcounted.length -= zs->avail_out; outbuf.data.refcounted.length -= zs->avail_out;

@ -196,6 +196,34 @@ static void test_bad_decompression_data_crc(void) {
grpc_slice_buffer_destroy(&output); grpc_slice_buffer_destroy(&output);
} }
static void test_bad_decompression_data_missing_trailer(void) {
grpc_slice_buffer input;
grpc_slice_buffer decompressed;
grpc_slice_buffer garbage;
grpc_slice_buffer output;
grpc_slice_buffer_init(&input);
grpc_slice_buffer_init(&decompressed);
grpc_slice_buffer_init(&garbage);
grpc_slice_buffer_init(&output);
grpc_slice_buffer_add(&input, create_test_value(ONE_MB_A));
grpc_core::ExecCtx exec_ctx;
/* compress it */
grpc_msg_compress(GRPC_MESSAGE_COMPRESS_GZIP, &input, &decompressed);
GPR_ASSERT(decompressed.length > 8);
/* Remove the footer from the decompressed message */
grpc_slice_buffer_trim_end(&decompressed, 8, &garbage);
/* try (and fail) to decompress the compressed buffer without the footer */
GPR_ASSERT(0 == grpc_msg_decompress(GRPC_MESSAGE_COMPRESS_GZIP, &decompressed,
&output));
grpc_slice_buffer_destroy(&input);
grpc_slice_buffer_destroy(&decompressed);
grpc_slice_buffer_destroy(&garbage);
grpc_slice_buffer_destroy(&output);
}
static void test_bad_decompression_data_trailing_garbage(void) { static void test_bad_decompression_data_trailing_garbage(void) {
grpc_slice_buffer input; grpc_slice_buffer input;
grpc_slice_buffer output; grpc_slice_buffer output;
@ -315,6 +343,7 @@ int main(int argc, char** argv) {
test_tiny_data_compress(); test_tiny_data_compress();
test_bad_decompression_data_crc(); test_bad_decompression_data_crc();
test_bad_decompression_data_missing_trailer();
test_bad_decompression_data_stream(); test_bad_decompression_data_stream();
test_bad_decompression_data_trailing_garbage(); test_bad_decompression_data_trailing_garbage();
test_bad_compression_algorithm(); test_bad_compression_algorithm();

Loading…
Cancel
Save