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 (#1880)
  • Loading branch information
zakhenry committed Aug 22, 2024
1 parent 22475d8 commit 46d8c2d
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion tonic-web/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,11 +393,15 @@ fn decode_trailers_frame(mut buf: Bytes) -> Result<Option<HeaderMap>, Status> {
let mut cursor_pos = 0;

for (i, b) in buf.iter().enumerate() {
// if we are at a trailer delimiter (\r\n)
if b == &b'\r' && buf.get(i + 1) == Some(&b'\n') {
// read the bytes of the trailer passed so far
let trailer = temp_buf.copy_to_bytes(i - cursor_pos);
cursor_pos = i;
// increment cursor beyond the delimiter
cursor_pos = i + 2;
trailers.push(trailer);
if temp_buf.has_remaining() {
// advance buf beyond the delimiters
temp_buf.get_u8();
temp_buf.get_u8();
}
Expand Down Expand Up @@ -612,4 +616,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 46d8c2d

Please sign in to comment.