Skip to content

Commit 2741b5b

Browse files
committed
fix: Do not include user information in Host header
According to RFC 9110, section 7.2, the Host header should only comprise the URI host and an optional port. Currently, the examples set the Host header to the URI's authority which may also contain user information (see RFC 3986, section 3.2). Update the examples to construct the Host header manually to avoid sensitive information from showing up in server logs and to ensure that the server's routing logic works correctly when a username and password are supplied.
1 parent df33d4d commit 2741b5b

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

examples/client.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,15 @@ async fn fetch_url(url: hyper::Uri) -> Result<()> {
5353
}
5454
});
5555

56-
let authority = url.authority().unwrap().clone();
56+
let host_header = match (url.host(), url.port_u16()) {
57+
(Some(host), Some(port)) => format!("{}:{}", host, port),
58+
_ => url.host().unwrap().to_string(),
59+
};
5760

5861
let path = url.path();
5962
let req = Request::builder()
6063
.uri(path)
61-
.header(hyper::header::HOST, authority.as_str())
64+
.header(hyper::header::HOST, host_header)
6265
.body(Empty::<Bytes>::new())?;
6366

6467
let mut res = sender.send_request(req).await?;

examples/client_json.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,15 @@ async fn fetch_json(url: hyper::Uri) -> Result<Vec<User>> {
4242
}
4343
});
4444

45-
let authority = url.authority().unwrap().clone();
45+
let host_header = match (url.host(), url.port_u16()) {
46+
(Some(host), Some(port)) => format!("{}:{}", host, port),
47+
_ => url.host().unwrap().to_string(),
48+
};
4649

4750
// Fetch the url...
4851
let req = Request::builder()
4952
.uri(url)
50-
.header(hyper::header::HOST, authority.as_str())
53+
.header(hyper::header::HOST, host_header)
5154
.body(Empty::<Bytes>::new())?;
5255

5356
let res = sender.send_request(req).await?;

examples/single_threaded.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,16 @@ async fn http1_client(url: hyper::Uri) -> Result<(), Box<dyn std::error::Error>>
181181
}
182182
});
183183

184-
let authority = url.authority().unwrap().clone();
184+
let host_header = match (url.host(), url.port_u16()) {
185+
(Some(host), Some(port)) => format!("{}:{}", host, port),
186+
_ => url.host().unwrap().to_string(),
187+
};
185188

186189
// Make 4 requests
187190
for _ in 0..4 {
188191
let req = Request::builder()
189192
.uri(url.clone())
190-
.header(hyper::header::HOST, authority.as_str())
193+
.header(hyper::header::HOST, &host_header)
191194
.body(Body::from("test".to_string()))?;
192195

193196
let mut res = sender.send_request(req).await?;
@@ -282,13 +285,16 @@ async fn http2_client(url: hyper::Uri) -> Result<(), Box<dyn std::error::Error>>
282285
}
283286
});
284287

285-
let authority = url.authority().unwrap().clone();
288+
let host_header = match (url.host(), url.port_u16()) {
289+
(Some(host), Some(port)) => format!("{}:{}", host, port),
290+
_ => url.host().unwrap().to_string(),
291+
};
286292

287293
// Make 4 requests
288294
for _ in 0..4 {
289295
let req = Request::builder()
290296
.uri(url.clone())
291-
.header(hyper::header::HOST, authority.as_str())
297+
.header(hyper::header::HOST, &host_header)
292298
.body(Body::from("test".to_string()))?;
293299

294300
let mut res = sender.send_request(req).await?;

0 commit comments

Comments
 (0)