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: Sanitize custom metadata #138

Merged
merged 2 commits into from
Nov 15, 2019
Merged
Show file tree
Hide file tree
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
19 changes: 19 additions & 0 deletions tonic/src/metadata/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,18 @@ pub struct OccupiedEntry<'a, VE: ValueEncoding> {
// ===== impl MetadataMap =====

impl MetadataMap {
// Headers reserved by the gRPC protocol.
pub(crate) const GRPC_RESERVED_HEADERS: [&'static str; 8] = [
"te",
"user-agent",
"content-type",
"grpc-timeout",
"grpc-message",
"grpc-encoding",
"grpc-message-type",
"grpc-status",
];

/// Create an empty `MetadataMap`.
///
/// The map will be created without any capacity. This function will not
Expand Down Expand Up @@ -237,6 +249,13 @@ impl MetadataMap {
self.headers
}

pub(crate) fn into_sanitized_headers(mut self) -> http::HeaderMap {
for r in &Self::GRPC_RESERVED_HEADERS {
self.headers.remove(*r);
}
self.headers
}

/// Create an empty `MetadataMap` with the specified capacity.
///
/// The returned map will allocate internal storage in order to hold about
Expand Down
22 changes: 21 additions & 1 deletion tonic/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl<T> Request<T> {
*request.version_mut() = http::Version::HTTP_2;
*request.method_mut() = http::Method::POST;
*request.uri_mut() = uri;
*request.headers_mut() = self.metadata.into_headers();
*request.headers_mut() = self.metadata.into_sanitized_headers();

request
}
Expand Down Expand Up @@ -209,3 +209,23 @@ impl<T> sealed::Sealed for T {}
mod sealed {
pub trait Sealed {}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::metadata::MetadataValue;
use http::Uri;

#[test]
fn reserved_headers_are_excluded() {
let mut r = Request::new(1);

for header in &MetadataMap::GRPC_RESERVED_HEADERS {
r.metadata_mut()
.insert(*header, MetadataValue::from_static("invalid"));
}

let http_request = r.into_http(Uri::default());
assert!(http_request.headers().is_empty());
}
}
21 changes: 20 additions & 1 deletion tonic/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl<T> Response<T> {
let mut res = http::Response::new(self.message);

*res.version_mut() = http::Version::HTTP_2;
*res.headers_mut() = self.metadata.into_headers();
*res.headers_mut() = self.metadata.into_sanitized_headers();

res
}
Expand All @@ -89,3 +89,22 @@ impl<T> Response<T> {
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::metadata::MetadataValue;

#[test]
fn reserved_headers_are_excluded() {
let mut r = Response::new(1);

for header in &MetadataMap::GRPC_RESERVED_HEADERS {
r.metadata_mut()
.insert(*header, MetadataValue::from_static("invalid"));
}

let http_response = r.into_http();
assert!(http_response.headers().is_empty());
}
}