Skip to content

Commit 46d8c2d

Browse files
authored
fix(tonic-web): fix panic caused in trailer parsing when there is more than one trailer (#1880)
1 parent 22475d8 commit 46d8c2d

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

tonic-web/src/call.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,11 +393,15 @@ fn decode_trailers_frame(mut buf: Bytes) -> Result<Option<HeaderMap>, Status> {
393393
let mut cursor_pos = 0;
394394

395395
for (i, b) in buf.iter().enumerate() {
396+
// if we are at a trailer delimiter (\r\n)
396397
if b == &b'\r' && buf.get(i + 1) == Some(&b'\n') {
398+
// read the bytes of the trailer passed so far
397399
let trailer = temp_buf.copy_to_bytes(i - cursor_pos);
398-
cursor_pos = i;
400+
// increment cursor beyond the delimiter
401+
cursor_pos = i + 2;
399402
trailers.push(trailer);
400403
if temp_buf.has_remaining() {
404+
// advance buf beyond the delimiters
401405
temp_buf.get_u8();
402406
temp_buf.get_u8();
403407
}
@@ -612,4 +616,21 @@ mod tests {
612616

613617
assert_eq!(out.code(), Code::Internal);
614618
}
619+
620+
#[test]
621+
fn decode_multiple_trailers() {
622+
let buf = b"\x80\0\0\0\x0fgrpc-status:0\r\ngrpc-message:\r\na:1\r\nb:2\r\n";
623+
624+
let trailers = decode_trailers_frame(Bytes::copy_from_slice(&buf[..]))
625+
.unwrap()
626+
.unwrap();
627+
628+
let mut expected = HeaderMap::new();
629+
expected.insert("grpc-status", "0".parse().unwrap());
630+
expected.insert("grpc-message", "".parse().unwrap());
631+
expected.insert("a", "1".parse().unwrap());
632+
expected.insert("b", "2".parse().unwrap());
633+
634+
assert_eq!(trailers, expected);
635+
}
615636
}

0 commit comments

Comments
 (0)