Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tonic-web): fix panic caused in trailer parsing when there is more than one trailer #1880

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
djc marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}