Skip to content

Commit

Permalink
fix: Status::details leaking base64 encoding (#395)
Browse files Browse the repository at this point in the history
Fixes #379
  • Loading branch information
LucioFranco authored Jul 10, 2020
1 parent 2b997b0 commit 2c4c544
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions tonic/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,10 +474,11 @@ impl Status {
}

if !self.details.is_empty() {
let details = base64::encode_config(&self.details[..], base64::STANDARD_NO_PAD);

header_map.insert(
GRPC_STATUS_DETAILS_HEADER,
HeaderValue::from_maybe_shared(self.details.clone())
.map_err(invalid_header_value_byte)?,
HeaderValue::from_maybe_shared(details).map_err(invalid_header_value_byte)?,
);
}

Expand All @@ -501,12 +502,6 @@ impl Status {
details: Bytes,
metadata: MetadataMap,
) -> Status {
let details = if details.is_empty() {
details
} else {
base64::encode_config(&details[..], base64::STANDARD_NO_PAD).into()
};

Status {
code,
message: message.into(),
Expand Down Expand Up @@ -837,4 +832,23 @@ mod tests {
assert_eq!(Status::data_loss("").code(), Code::DataLoss);
assert_eq!(Status::unauthenticated("").code(), Code::Unauthenticated);
}

#[test]
fn details() {
const DETAILS: &[u8] = &[0, 2, 3];

let status = Status::with_details(Code::Unavailable, "some message", DETAILS.into());

assert_eq!(&status.details()[..], DETAILS);

let header_map = status.to_header_map().unwrap();

let b64_details = base64::encode_config(&DETAILS[..], base64::STANDARD_NO_PAD);

assert_eq!(header_map[super::GRPC_STATUS_DETAILS_HEADER], b64_details);

let status = Status::from_header_map(&header_map).unwrap();

assert_eq!(&status.details()[..], DETAILS);
}
}

0 comments on commit 2c4c544

Please sign in to comment.