Skip to content

Commit

Permalink
fix(tonic-web): fix panic caused in trailer parsing when there is mor…
Browse files Browse the repository at this point in the history
…e than one trailer
  • Loading branch information
zak-cloudnc committed Aug 21, 2024
1 parent 086bcd2 commit af26d0d
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions tonic-web/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ fn decode_trailers_frame(mut buf: Bytes) -> Result<Option<HeaderMap>, Status> {
for (i, b) in buf.iter().enumerate() {
if b == &b'\r' && buf.get(i + 1) == Some(&b'\n') {
let trailer = temp_buf.copy_to_bytes(i - cursor_pos);
cursor_pos = i;
cursor_pos = i + 2;
trailers.push(trailer);
if temp_buf.has_remaining() {
temp_buf.get_u8();
Expand Down Expand Up @@ -548,14 +548,14 @@ mod tests {
#[test]
fn find_trailers_buffered() {
// Byte version of this:
// b"\0\0\0\0L\n$975738af-1a17-4aea-b887-ed0bbced6093\x1a$da609e9b-f470-4cc0-a691-3fd6a005a436\x80\0\0\0\x0fgrpc-status:0\r\n"
let buf = [
0, 0, 0, 0, 76, 10, 36, 57, 55, 53, 55, 51, 56, 97, 102, 45, 49, 97, 49, 55, 45, 52,
97, 101, 97, 45, 98, 56, 56, 55, 45, 101, 100, 48, 98, 98, 99, 101, 100, 54, 48, 57,
51, 26, 36, 100, 97, 54, 48, 57, 101, 57, 98, 45, 102, 52, 55, 48, 45, 52, 99, 99, 48,
45, 97, 54, 57, 49, 45, 51, 102, 100, 54, 97, 48, 48, 53, 97, 52, 51, 54, 128, 0, 0, 0,
15, 103, 114, 112, 99, 45, 115, 116, 97, 116, 117, 115, 58, 48, 13, 10,
];
let buf = b"\0\0\0\0L\n$975738af-1a17-4aea-b887-ed0bbced6093\x1a$da609e9b-f470-4cc0-a691-3fd6a005a436\x80\0\0\0\x0fgrpc-status:0\r\ngrpc-message:\r\nrequest_id:d0e56d4d10ab4c85b0882c3467049522\r\na:1\r\nb:2\r\n";
// let buf = [
// 0, 0, 0, 0, 76, 10, 36, 57, 55, 53, 55, 51, 56, 97, 102, 45, 49, 97, 49, 55, 45, 52,
// 97, 101, 97, 45, 98, 56, 56, 55, 45, 101, 100, 48, 98, 98, 99, 101, 100, 54, 48, 57,
// 51, 26, 36, 100, 97, 54, 48, 57, 101, 57, 98, 45, 102, 52, 55, 48, 45, 52, 99, 99, 48,
// 45, 97, 54, 57, 49, 45, 51, 102, 100, 54, 97, 48, 48, 53, 97, 52, 51, 54, 128, 0, 0, 0,
// 15, 103, 114, 112, 99, 45, 115, 116, 97, 116, 117, 115, 58, 48, 13, 10,
// ];

let out = find_trailers(&buf[..]).unwrap();

Expand All @@ -565,7 +565,8 @@ mod tests {
.unwrap()
.unwrap();
let status = trailers.get("grpc-status").unwrap();
assert_eq!(status.to_str().unwrap(), "0")
assert_eq!(status.to_str().unwrap(), "0");
dbg!(trailers);
}

#[test]
Expand Down Expand Up @@ -612,4 +613,21 @@ mod tests {

assert_eq!(out.code(), Code::Internal);
}

#[test]
fn decode_multiple_trailers() {
let buf = b"\x80\0\0\0\x0fgrpc-status:0\r\ngrpc-message:\r\na:1\r\nb:2\r\n";

let trailers = decode_trailers_frame(Bytes::copy_from_slice(&buf[..]))
.unwrap()
.unwrap();

let mut expected = HeaderMap::new();
expected.insert("grpc-status", "0".parse().unwrap());
expected.insert("grpc-message", "".parse().unwrap());
expected.insert("a", "1".parse().unwrap());
expected.insert("b", "2".parse().unwrap());

assert_eq!(trailers, expected);
}
}

0 comments on commit af26d0d

Please sign in to comment.