Skip to content

Commit af26d0d

Browse files
committed
fix(tonic-web): fix panic caused in trailer parsing when there is more than one trailer
1 parent 086bcd2 commit af26d0d

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

tonic-web/src/call.rs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ fn decode_trailers_frame(mut buf: Bytes) -> Result<Option<HeaderMap>, Status> {
395395
for (i, b) in buf.iter().enumerate() {
396396
if b == &b'\r' && buf.get(i + 1) == Some(&b'\n') {
397397
let trailer = temp_buf.copy_to_bytes(i - cursor_pos);
398-
cursor_pos = i;
398+
cursor_pos = i + 2;
399399
trailers.push(trailer);
400400
if temp_buf.has_remaining() {
401401
temp_buf.get_u8();
@@ -548,14 +548,14 @@ mod tests {
548548
#[test]
549549
fn find_trailers_buffered() {
550550
// Byte version of this:
551-
// 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"
552-
let buf = [
553-
0, 0, 0, 0, 76, 10, 36, 57, 55, 53, 55, 51, 56, 97, 102, 45, 49, 97, 49, 55, 45, 52,
554-
97, 101, 97, 45, 98, 56, 56, 55, 45, 101, 100, 48, 98, 98, 99, 101, 100, 54, 48, 57,
555-
51, 26, 36, 100, 97, 54, 48, 57, 101, 57, 98, 45, 102, 52, 55, 48, 45, 52, 99, 99, 48,
556-
45, 97, 54, 57, 49, 45, 51, 102, 100, 54, 97, 48, 48, 53, 97, 52, 51, 54, 128, 0, 0, 0,
557-
15, 103, 114, 112, 99, 45, 115, 116, 97, 116, 117, 115, 58, 48, 13, 10,
558-
];
551+
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";
552+
// let buf = [
553+
// 0, 0, 0, 0, 76, 10, 36, 57, 55, 53, 55, 51, 56, 97, 102, 45, 49, 97, 49, 55, 45, 52,
554+
// 97, 101, 97, 45, 98, 56, 56, 55, 45, 101, 100, 48, 98, 98, 99, 101, 100, 54, 48, 57,
555+
// 51, 26, 36, 100, 97, 54, 48, 57, 101, 57, 98, 45, 102, 52, 55, 48, 45, 52, 99, 99, 48,
556+
// 45, 97, 54, 57, 49, 45, 51, 102, 100, 54, 97, 48, 48, 53, 97, 52, 51, 54, 128, 0, 0, 0,
557+
// 15, 103, 114, 112, 99, 45, 115, 116, 97, 116, 117, 115, 58, 48, 13, 10,
558+
// ];
559559

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

@@ -565,7 +565,8 @@ mod tests {
565565
.unwrap()
566566
.unwrap();
567567
let status = trailers.get("grpc-status").unwrap();
568-
assert_eq!(status.to_str().unwrap(), "0")
568+
assert_eq!(status.to_str().unwrap(), "0");
569+
dbg!(trailers);
569570
}
570571

571572
#[test]
@@ -612,4 +613,21 @@ mod tests {
612613

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

0 commit comments

Comments
 (0)