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 aaa1bb5
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion 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 @@ -612,4 +612,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 aaa1bb5

Please sign in to comment.