Skip to content

Commit

Permalink
fix: Do not include user information in Host header
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tindzk committed Apr 5, 2024
1 parent df33d4d commit 2b3cc9f
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 13 deletions.
4 changes: 1 addition & 3 deletions examples/client.rs
Expand Up @@ -53,12 +53,10 @@ async fn fetch_url(url: hyper::Uri) -> Result<()> {
}
});

let authority = url.authority().unwrap().clone();

let path = url.path();
let req = Request::builder()
.uri(path)
.header(hyper::header::HOST, authority.as_str())
.header(hyper::header::HOST, format!("{}:{}", host, port))
.body(Empty::<Bytes>::new())?;

let mut res = sender.send_request(req).await?;
Expand Down
6 changes: 2 additions & 4 deletions examples/client_json.rs
Expand Up @@ -42,12 +42,10 @@ async fn fetch_json(url: hyper::Uri) -> Result<Vec<User>> {
}
});

let authority = url.authority().unwrap().clone();

// Fetch the url...
// Fetch the URL...
let req = Request::builder()
.uri(url)
.header(hyper::header::HOST, authority.as_str())
.header(hyper::header::HOST, format!("{}:{}", host, port))
.body(Empty::<Bytes>::new())?;

let res = sender.send_request(req).await?;
Expand Down
8 changes: 2 additions & 6 deletions examples/single_threaded.rs
Expand Up @@ -181,13 +181,11 @@ async fn http1_client(url: hyper::Uri) -> Result<(), Box<dyn std::error::Error>>
}
});

let authority = url.authority().unwrap().clone();

// Make 4 requests
for _ in 0..4 {
let req = Request::builder()
.uri(url.clone())
.header(hyper::header::HOST, authority.as_str())
.header(hyper::header::HOST, format!("{}:{}", host, port))
.body(Body::from("test".to_string()))?;

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

let authority = url.authority().unwrap().clone();

// Make 4 requests
for _ in 0..4 {
let req = Request::builder()
.uri(url.clone())
.header(hyper::header::HOST, authority.as_str())
.header(hyper::header::HOST, format!("{}:{}", host, port))
.body(Body::from("test".to_string()))?;

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

0 comments on commit 2b3cc9f

Please sign in to comment.