use std::net::SocketAddr;
use std::sync::Arc;
use axum::extract::{Request, State};
use axum::http::header;
use axum::middleware::Next;
use axum::response::{IntoResponse, Response};
use crate::auth::Tokens;
use crate::error::ApiError;
use crate::listen::Listen;
#[derive(Debug, Clone)]
pub enum Transport {
Unix {
uid: u32,
},
Tcp {
peer: SocketAddr,
},
}
pub struct Policy {
pub tokens: Tokens,
pub listen: Listen,
pub require_token_on_unix: bool,
pub allowed_hosts: Vec<String>,
pub public_health: bool,
}
impl Policy {
fn token_required(&self, transport: &Transport, path: &str) -> bool {
let local = match transport {
Transport::Unix { .. } => {
if !self.require_token_on_unix {
return false;
}
true
}
Transport::Tcp { peer } => peer.ip().is_loopback() && !self.listen.is_network(),
};
if path == "/health" && (local || self.public_health) {
return false;
}
true
}
fn host_allowed(&self, host: &str) -> bool {
let name = strip_port(host).to_ascii_lowercase();
if self.allowed_hosts.iter().any(|h| h.eq_ignore_ascii_case(&name)) {
return true;
}
match &self.listen {
Listen::Unix(_) => true,
Listen::Loopback(_) => matches!(name.as_str(), "localhost" | "127.0.0.1" | "[::1]" | "::1"),
Listen::Interface(addr) => {
let ip = addr.ip().to_string();
name == ip || name == format!("[{ip}]")
}
Listen::AllInterfaces(_) => self.allowed_hosts.is_empty(),
}
}
}
fn strip_port(host: &str) -> &str {
if let Some(rest) = host.strip_prefix('[') {
return rest.split(']').next().map_or(host, |ip| &host[..ip.len() + 2]);
}
match host.rsplit_once(':') {
Some((name, port)) if port.bytes().all(|b| b.is_ascii_digit()) => name,
_ => host,
}
}
pub async fn enforce(State(policy): State<Arc<Policy>>, request: Request, next: Next) -> Response {
let transport = request.extensions().get::<Transport>().cloned();
let Some(transport) = transport else {
return ApiError::internal("connection metadata missing").into_response();
};
if request.headers().contains_key(header::ORIGIN) {
return ApiError::forbidden("browser_request", "requests from web pages are not accepted").into_response();
}
if matches!(transport, Transport::Tcp { .. }) {
let host = request.headers().get(header::HOST).and_then(|h| h.to_str().ok());
if !host.is_some_and(|h| policy.host_allowed(h)) {
return ApiError::forbidden("host_not_allowed", "this Host is not served here").into_response();
}
}
if policy.token_required(&transport, request.uri().path()) {
let header = request.headers().get(header::AUTHORIZATION).and_then(|h| h.to_str().ok());
if !policy.tokens.accepts_header(header) {
return ApiError::unauthorized().into_response();
}
}
next.run(request).await
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn host_ports_are_stripped() {
assert_eq!(strip_port("localhost:8100"), "localhost");
assert_eq!(strip_port("[::1]:8100"), "[::1]");
assert_eq!(strip_port("[::1]"), "[::1]");
assert_eq!(strip_port("example.com"), "example.com");
}
}