use std::net::SocketAddr;
use axum::{
extract::{Request, State},
http::{uri::Authority, StatusCode},
middleware::Next,
response::Response,
};
use infr_core::config::Config;
use crate::{configured_api_key, invalid_request, AppState};
const LOOPBACK_HOSTS: [&str; 3] = ["localhost", "127.0.0.1", "[::1]"];
pub(crate) async fn host_gate(State(state): State<AppState>, req: Request, next: Next) -> Response {
let host = match req.headers().get(axum::http::header::HOST) {
Some(v) => Some(v.to_str().unwrap_or_default()),
None => req.uri().authority().map(Authority::as_str),
};
if host_allowed(&state.cfg, state.bind, host) {
return next.run(req).await;
}
tracing::warn!(host = ?host, "refused a request for a host not in serve.allowed_hosts");
invalid_request(
StatusCode::FORBIDDEN,
None,
None,
format!(
"Host {:?} is not allowed. Add it to serve.allowed_hosts \
(INFR_SERVE_ALLOWED_HOSTS), or set that to \"*\" to turn the Host check off.",
host.unwrap_or_default()
),
)
}
fn allowed_hosts(cfg: &Config) -> Option<&[String]> {
cfg.serve
.allowed_hosts
.as_deref()
.filter(|list| list.iter().any(|h| !h.trim().is_empty()))
}
pub(crate) fn check_allowed_hosts(cfg: &Config) -> anyhow::Result<()> {
for entry in allowed_hosts(cfg).unwrap_or_default() {
let entry = entry.trim();
if entry.is_empty() || entry == "*" {
continue;
}
if entry.contains('*') || split_host(entry).is_none() {
anyhow::bail!(
"serve.allowed_hosts entry {entry:?} is not a host name or address (with an \
optional port); write the bare name a client puts in its Host header, or `*` \
alone to turn the check off"
);
}
}
Ok(())
}
fn host_allowed(cfg: &Config, bind: Option<SocketAddr>, host: Option<&str>) -> bool {
let listed = allowed_hosts(cfg);
match listed {
Some(list) if list.iter().any(|h| h.trim() == "*") => return true,
Some(_) => {}
None => {
let exposed =
configured_api_key(cfg).is_some() || !bind.is_some_and(|a| a.ip().is_loopback());
if exposed {
return true;
}
}
}
let Some(host) = host else {
return true;
};
let Some((name, _)) = split_host(host) else {
return false;
};
LOOPBACK_HOSTS.contains(&name.as_str())
|| name.ends_with(".localhost")
|| bind.is_some_and(|b| name == bind_host(b))
|| listed.is_some_and(|list| {
list.iter()
.filter_map(|h| split_host(h))
.any(|(allowed, _)| allowed == name)
})
}
fn split_host(host: &str) -> Option<(String, Option<u16>)> {
if host.contains(['@', '/']) {
return None;
}
let a: Authority = host.trim().parse().ok()?;
let name = a.host().trim_end_matches('.').to_ascii_lowercase();
(!name.is_empty()).then_some((name, a.port_u16()))
}
fn bind_host(bind: SocketAddr) -> String {
match bind {
SocketAddr::V4(a) => a.ip().to_string(),
SocketAddr::V6(a) => format!("[{}]", a.ip()),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::build_router;
use axum::{body::Body, http::Request as HttpRequest};
use http_body_util::BodyExt;
use std::sync::Arc;
use tower::ServiceExt;
fn cfg(api_key: Option<&str>, allowed: Option<&[&str]>) -> Config {
let mut c = Config::default();
c.serve.api_key = api_key.map(str::to_owned);
c.serve.allowed_hosts = allowed.map(|l| l.iter().map(|h| h.to_string()).collect());
c
}
fn addr(s: &str) -> Option<SocketAddr> {
Some(s.parse().unwrap())
}
#[test]
fn a_keyless_loopback_server_answers_only_loopback_names() {
let c = cfg(None, None);
let bind = addr("127.0.0.1:8080");
for ok in [
"localhost",
"localhost:8080",
"localhost:2222",
"LOCALHOST:8080",
"localhost.:8080",
"127.0.0.1:9999",
"[::1]:8080",
"[::1]",
"app.localhost",
"a.b.localhost:3000",
] {
assert!(host_allowed(&c, bind, Some(ok)), "{ok} must pass");
}
assert!(host_allowed(&c, bind, None), "no Host header at all");
for refused in [
"evil.example",
"evil.example:8080",
"host.docker.internal",
"localhost.evil.example",
"127.0.0.2:8080",
"user@localhost:8080",
"",
] {
assert!(
!host_allowed(&c, bind, Some(refused)),
"{refused} must be refused"
);
}
let v6 = addr("[::1]:9000");
assert!(host_allowed(&c, v6, Some("[::1]:9000")));
assert!(!host_allowed(&c, v6, Some("evil.example:9000")));
assert!(host_allowed(
&c,
addr("127.0.0.5:8080"),
Some("127.0.0.5:1")
));
}
#[test]
fn exposed_servers_are_not_checked_by_default() {
assert!(host_allowed(
&cfg(Some("k"), None),
addr("127.0.0.1:8080"),
Some("evil.example")
));
assert!(host_allowed(
&cfg(None, None),
addr("0.0.0.0:8080"),
Some("evil.example")
));
assert!(host_allowed(&cfg(None, None), None, Some("evil.example")));
assert!(!host_allowed(
&cfg(Some(""), None),
addr("127.0.0.1:8080"),
Some("evil.example")
));
}
#[test]
fn an_explicit_allow_list_always_applies_and_star_disables_it() {
let c = cfg(Some("k"), Some(&["llm.example.com", "Proxy.Internal:8443"]));
for bind in [addr("0.0.0.0:8080"), addr("127.0.0.1:8080"), None] {
assert!(host_allowed(&c, bind, Some("llm.example.com")));
assert!(host_allowed(&c, bind, Some("llm.example.com:443")));
assert!(host_allowed(&c, bind, Some("proxy.internal")));
assert!(host_allowed(&c, bind, Some("localhost:8080")));
assert!(!host_allowed(&c, bind, Some("evil.example")), "{bind:?}");
}
for blank in [&[][..], &[""][..], &[" ", ""][..]] {
let unset = cfg(Some("k"), Some(blank));
assert!(
host_allowed(&unset, addr("0.0.0.0:8080"), Some("evil.example")),
"{blank:?} must mean unset"
);
}
let star = cfg(None, Some(&["*"]));
assert!(host_allowed(
&star,
addr("127.0.0.1:8080"),
Some("evil.example")
));
}
#[test]
fn an_unmatchable_allowed_host_is_a_config_error() {
for ok in [
&["llm.example.com", "10.0.0.2:8000", "[::1]", "*"][..],
&[""][..],
] {
assert!(check_allowed_hosts(&cfg(None, Some(ok))).is_ok(), "{ok:?}");
}
assert!(check_allowed_hosts(&cfg(None, None)).is_ok());
for bad in ["http://x", "x/path", "*.example.com", "user@x"] {
let e = check_allowed_hosts(&cfg(None, Some(&[bad]))).unwrap_err();
assert!(e.to_string().contains(bad), "{bad}: {e}");
}
}
async fn get(router: axum::Router, uri: &str, host: &str) -> (StatusCode, String) {
let resp = router
.oneshot(
HttpRequest::builder()
.uri(uri)
.header("host", host)
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
let status = resp.status();
let bytes = resp.into_body().collect().await.unwrap().to_bytes();
(status, String::from_utf8(bytes.to_vec()).unwrap())
}
#[tokio::test]
async fn a_refused_host_is_a_403_naming_the_key_and_health_stays_open() {
let mut state = AppState::headless("m", Arc::new(Config::default()));
state.bind = addr("127.0.0.1:8080");
let router = build_router(state);
for uri in ["/v1/models", "/status", "/metrics"] {
let (status, body) = get(router.clone(), uri, "evil.example:8080").await;
assert_eq!(status, StatusCode::FORBIDDEN, "{uri}");
let v: serde_json::Value = serde_json::from_str(&body).unwrap();
assert_eq!(v["error"]["type"], "invalid_request_error");
let msg = v["error"]["message"].as_str().unwrap();
assert!(msg.contains("serve.allowed_hosts"), "{msg}");
assert!(msg.contains("evil.example:8080"), "{msg}");
}
let (status, _) = get(router.clone(), "/v1/models", "localhost:8080").await;
assert_eq!(status, StatusCode::OK);
let (status, _) = get(router, "/health", "evil.example:8080").await;
assert_eq!(status, StatusCode::OK);
}
#[tokio::test]
async fn the_chat_route_is_guarded() {
let mut state = AppState::headless("m", Arc::new(Config::default()));
state.bind = addr("127.0.0.1:8080");
let resp = build_router(state)
.oneshot(
HttpRequest::builder()
.method("POST")
.uri("/v1/chat/completions")
.header("host", "evil.example:8080")
.header("content-type", "application/json")
.body(Body::from(r#"{"model":"m","messages":[]}"#))
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::FORBIDDEN);
}
}