Skip to content

Commit 2b3cc9f

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 2b3cc9f

File tree

3 files changed

+5
-13
lines changed

3 files changed

+5
-13
lines changed

examples/client.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,10 @@ async fn fetch_url(url: hyper::Uri) -> Result<()> {
5353
}
5454
});
5555

56-
let authority = url.authority().unwrap().clone();
57-
5856
let path = url.path();
5957
let req = Request::builder()
6058
.uri(path)
61-
.header(hyper::header::HOST, authority.as_str())
59+
.header(hyper::header::HOST, format!("{}:{}", host, port))
6260
.body(Empty::<Bytes>::new())?;
6361

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

examples/client_json.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,10 @@ async fn fetch_json(url: hyper::Uri) -> Result<Vec<User>> {
4242
}
4343
});
4444

45-
let authority = url.authority().unwrap().clone();
46-
47-
// Fetch the url...
45+
// Fetch the URL...
4846
let req = Request::builder()
4947
.uri(url)
50-
.header(hyper::header::HOST, authority.as_str())
48+
.header(hyper::header::HOST, format!("{}:{}", host, port))
5149
.body(Empty::<Bytes>::new())?;
5250

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

examples/single_threaded.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,11 @@ async fn http1_client(url: hyper::Uri) -> Result<(), Box<dyn std::error::Error>>
181181
}
182182
});
183183

184-
let authority = url.authority().unwrap().clone();
185-
186184
// Make 4 requests
187185
for _ in 0..4 {
188186
let req = Request::builder()
189187
.uri(url.clone())
190-
.header(hyper::header::HOST, authority.as_str())
188+
.header(hyper::header::HOST, format!("{}:{}", host, port))
191189
.body(Body::from("test".to_string()))?;
192190

193191
let mut res = sender.send_request(req).await?;
@@ -282,13 +280,11 @@ async fn http2_client(url: hyper::Uri) -> Result<(), Box<dyn std::error::Error>>
282280
}
283281
});
284282

285-
let authority = url.authority().unwrap().clone();
286-
287283
// Make 4 requests
288284
for _ in 0..4 {
289285
let req = Request::builder()
290286
.uri(url.clone())
291-
.header(hyper::header::HOST, authority.as_str())
287+
.header(hyper::header::HOST, format!("{}:{}", host, port))
292288
.body(Body::from("test".to_string()))?;
293289

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

0 commit comments

Comments
 (0)