Skip to content

Commit

Permalink
feat: use x-forwarded-host
Browse files Browse the repository at this point in the history
  • Loading branch information
EstebanBorai authored and ctron committed Jan 15, 2024
1 parent 7506f20 commit fd3ba23
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ use tower_http::trace::TraceLayer;

use crate::serve::ServerResult;

/// The `X-Forwarded-Host`` (XFH) header is a de-facto standard header for
/// identifying the original host requested by the client in the Host HTTP
/// request header.
///
/// Refer: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Host
const X_FORWARDED_HOST: &str = "x-forwarded-host";

/// A handler used for proxying HTTP requests to a backend.
pub(crate) struct ProxyHandlerHttp {
/// The client to use for proxy logic.
Expand Down Expand Up @@ -73,14 +80,15 @@ fn make_outbound_request(
) -> anyhow::Result<hyper::Request<()>> {
let mut request = hyper::Request::builder().uri(outbound_uri.to_string());

let Some(host) = outbound_uri.authority().map(|authority| authority.host()) else {
let Some(outbound_host) = outbound_uri.authority().map(|authority| authority.host()) else {
anyhow::bail!("No host found in outbound URI");
};

for (maybe_key, val) in headers {
if let Some(key) = maybe_key {
if key == HOST {
request = request.header(HOST, host);
request = request.header(HOST, outbound_host);
request = request.header(X_FORWARDED_HOST, val);
} else {
request = request.header(key, val);
}
Expand Down Expand Up @@ -303,7 +311,7 @@ mod tests {

use crate::proxy::make_outbound_uri;

use super::make_outbound_request;
use super::{make_outbound_request, X_FORWARDED_HOST};

#[test]
fn make_outbound_uri_two_base_paths() {
Expand Down Expand Up @@ -441,6 +449,17 @@ mod tests {
continue;
}

if key == X_FORWARDED_HOST {
assert_eq!(
have_outbound_req
.headers()
.get(key.clone())
.unwrap_or_else(|| panic!("Expected header value for {}", key)),
&HeaderValue::from_static("localhost")
);
continue;
}

assert_eq!(
have_outbound_req
.headers()
Expand Down

0 comments on commit fd3ba23

Please sign in to comment.