diff options
author | 2018-02-21 16:06:25 -0800 | |
---|---|---|
committer | 2018-02-21 16:06:25 -0800 | |
commit | 6c9a87c174189a767eca3bc387f36f4d5880cde3 (patch) | |
tree | f101a1d327f6afe7c3174b2fa549534b52787933 /src/core/ext/transport | |
parent | 9ef1f359ca1dec335300067e6d4e3ce4da24b584 (diff) |
Polish infer_length_after_decode and add test cases
Diffstat (limited to 'src/core/ext/transport')
-rw-r--r-- | src/core/ext/transport/chttp2/transport/bin_decoder.cc | 12 | ||||
-rw-r--r-- | src/core/ext/transport/chttp2/transport/bin_decoder.h | 2 |
2 files changed, 11 insertions, 3 deletions
diff --git a/src/core/ext/transport/chttp2/transport/bin_decoder.cc b/src/core/ext/transport/chttp2/transport/bin_decoder.cc index 53b8622259..831a36961e 100644 --- a/src/core/ext/transport/chttp2/transport/bin_decoder.cc +++ b/src/core/ext/transport/chttp2/transport/bin_decoder.cc @@ -75,12 +75,20 @@ static bool input_is_valid(uint8_t* input_ptr, size_t length) { #define COMPOSE_OUTPUT_BYTE_2(input_ptr) \ (uint8_t)((decode_table[input_ptr[2]] << 6) | decode_table[input_ptr[3]]) -size_t grpc_base64_infer_length_after_decode(const grpc_slice& slice) { +// By RFC 4648, if the length of the encoded string without padding is 4n+r, +// the length of decoded string is: 1) 3n if r = 0, 2) 3n + 1 if r = 2, 3, or +// 3) invalid if r = 1. +size_t grpc_chttp2_base64_infer_length_after_decode(const grpc_slice& slice) { size_t len = GRPC_SLICE_LENGTH(slice); const uint8_t* bytes = GRPC_SLICE_START_PTR(slice); while (len > 0 && bytes[len - 1] == '=') { len--; } + if (GRPC_SLICE_LENGTH(slice) - len > 2) { + gpr_log(GPR_ERROR, + "Base64 decoding failed. Input has more than 2 paddings."); + return 0; + } size_t tuples = len / 4; size_t tail_case = len % 4; if (tail_case == 1) { @@ -88,7 +96,7 @@ size_t grpc_base64_infer_length_after_decode(const grpc_slice& slice) { "Base64 decoding failed. Input has a length of %zu (without" " padding), which is invalid.\n", len); - tail_case = 0; + return 0; } return tuples * 3 + tail_xtra[tail_case]; } diff --git a/src/core/ext/transport/chttp2/transport/bin_decoder.h b/src/core/ext/transport/chttp2/transport/bin_decoder.h index b5acb3fa91..a0d74fb20d 100644 --- a/src/core/ext/transport/chttp2/transport/bin_decoder.h +++ b/src/core/ext/transport/chttp2/transport/bin_decoder.h @@ -49,6 +49,6 @@ grpc_slice grpc_chttp2_base64_decode_with_length(grpc_slice input, size_t output_length); /* Infer the length of decoded data from encoded data. */ -size_t grpc_base64_infer_length_after_decode(const grpc_slice& slice); +size_t grpc_chttp2_base64_infer_length_after_decode(const grpc_slice& slice); #endif /* GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_BIN_DECODER_H */ |