use std::{convert::Infallible, fmt, sync::Arc};
use axum::{
Router,
body::{Body, HttpBody as _},
extract::{DefaultBodyLimit, MatchedPath},
http::{HeaderValue, Request, StatusCode, header},
middleware::{self, Next},
response::{IntoResponse, Response},
};
use tokio::sync::Semaphore;
use tower::{Service as _, service_fn};
use tower_http::{
compression::Compression,
cors::{AllowHeaders, AllowOrigin, CorsLayer},
trace::TraceLayer,
};
use tracing::Level;
use crate::http::{
ApiProblem, EndpointAuth, EndpointContractRegistry, EndpointMethod, HttpAuthenticationHook,
HttpAuthorizationHook, HttpMetricsLayer, IdempotencyLayer, IdempotencyRequestPolicy,
IdempotencyRuntime, IdempotencyScope, IdempotencyStore, ProblemCode, RateLimitKeySource,
RateLimitLayer, RateLimitRuntime, RateLimitStore, RequestContext,
request_context_middleware_with_forwarded_policy,
};
use super::{
error::HttpMiddlewareError,
policy::{CorsPolicy, HttpMiddlewareConfig, HttpRequestPolicy, RequestBodyLimitPolicy},
};
#[derive(Clone)]
#[non_exhaustive]
pub struct HttpMiddlewareStack {
config: HttpMiddlewareConfig,
concurrency: Option<Arc<Semaphore>>,
rate_limit: Option<RateLimitRuntime>,
idempotency: Option<IdempotencyRuntime>,
metrics: Option<HttpMetricsLayer>,
authentication: Option<Arc<dyn HttpAuthenticationHook>>,
authorization: Option<Arc<dyn HttpAuthorizationHook>>,
}
impl fmt::Debug for HttpMiddlewareStack {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("HttpMiddlewareStack")
.field("config", &self.config)
.field("rate_limit", &self.rate_limit)
.field("idempotency", &self.idempotency)
.field("metrics", &self.metrics)
.field("authentication", &self.authentication.is_some())
.field("authorization", &self.authorization.is_some())
.finish()
}
}
impl HttpMiddlewareStack {
#[must_use]
pub fn builder(config: HttpMiddlewareConfig) -> HttpMiddlewareStackBuilder {
HttpMiddlewareStackBuilder::new(config)
}
#[must_use]
pub const fn config(&self) -> &HttpMiddlewareConfig {
&self.config
}
pub(crate) fn validate_endpoint_contracts<P: Copy + 'static>(
&self,
contracts: &EndpointContractRegistry<'_, P>,
) -> Result<(), HttpMiddlewareError> {
validate_endpoint_contract_policies(
&self.config,
contracts,
self.authentication.is_some(),
self.authorization.is_some(),
)
}
pub fn apply<S>(self, router: Router<S>) -> Router<S>
where
S: Clone + Send + Sync + 'static,
{
apply_http_layers(router, self)
}
}
#[derive(Clone)]
#[non_exhaustive]
pub struct HttpMiddlewareStackBuilder {
config: HttpMiddlewareConfig,
rate_limit_store: Option<Arc<dyn RateLimitStore>>,
idempotency_store: Option<Arc<dyn IdempotencyStore>>,
metrics: Option<HttpMetricsLayer>,
authentication: Option<Arc<dyn HttpAuthenticationHook>>,
authorization: Option<Arc<dyn HttpAuthorizationHook>>,
}
impl fmt::Debug for HttpMiddlewareStackBuilder {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("HttpMiddlewareStackBuilder")
.field("config", &self.config)
.field("rate_limit_store", &self.rate_limit_store.is_some())
.field("idempotency_store", &self.idempotency_store.is_some())
.field("metrics", &self.metrics.is_some())
.field("authentication", &self.authentication.is_some())
.field("authorization", &self.authorization.is_some())
.finish()
}
}
impl HttpMiddlewareStackBuilder {
#[must_use]
pub fn new(config: HttpMiddlewareConfig) -> Self {
Self {
config,
rate_limit_store: None,
idempotency_store: None,
metrics: None,
authentication: None,
authorization: None,
}
}
#[must_use]
pub fn with_rate_limit_store(mut self, store: Arc<dyn RateLimitStore>) -> Self {
self.rate_limit_store = Some(store);
self
}
#[must_use]
pub fn with_idempotency_store(mut self, store: Arc<dyn IdempotencyStore>) -> Self {
self.idempotency_store = Some(store);
self
}
#[must_use]
pub fn with_metrics(self, metrics: Arc<dyn crate::observability::MetricRecorder>) -> Self {
self.with_http_metrics_layer(HttpMetricsLayer::new(metrics))
}
#[must_use]
pub fn with_http_metrics_layer(mut self, metrics: HttpMetricsLayer) -> Self {
self.metrics = Some(metrics);
self
}
#[must_use]
pub fn with_authentication_hook(
mut self,
authentication: Arc<dyn HttpAuthenticationHook>,
) -> Self {
self.authentication = Some(authentication);
self
}
#[must_use]
pub fn with_authorization_hook(
mut self,
authorization: Arc<dyn HttpAuthorizationHook>,
) -> Self {
self.authorization = Some(authorization);
self
}
pub fn build(self) -> Result<HttpMiddlewareStack, HttpMiddlewareError> {
if self.config.rate_limit.as_ref().is_some_and(|policy| {
matches!(
policy.key_source,
RateLimitKeySource::Principal | RateLimitKeySource::Tenant
)
}) && self.authentication.is_none()
{
return Err(HttpMiddlewareError::MissingAuthenticationHook);
}
let recorder = self.metrics.as_ref().map(|layer| layer.recorder().clone());
let rate_limit = match (&self.config.rate_limit, self.rate_limit_store) {
(Some(policy), Some(store)) => Some(
RateLimitRuntime::new(policy.limiter_config, policy.key_source.clone(), store)
.with_metrics(recorder.clone()),
),
(Some(_), None) => return Err(HttpMiddlewareError::MissingRateLimitStore),
(None, _) => None,
};
let idempotency = match (self.config.idempotency.clone(), self.idempotency_store) {
(Some(policy), Some(store)) => {
Some(IdempotencyRuntime::new(store, policy).with_metrics(recorder))
}
(Some(_), None) => return Err(HttpMiddlewareError::MissingIdempotencyStore),
(None, _) => None,
};
let metrics = self.metrics;
Ok(HttpMiddlewareStack {
concurrency: self
.config
.max_concurrent_requests
.map(|limit| Arc::new(Semaphore::new(limit.get()))),
config: self.config,
rate_limit,
idempotency,
metrics,
authentication: self.authentication,
authorization: self.authorization,
})
}
}
fn validate_endpoint_contract_policies<P: Copy + 'static>(
config: &HttpMiddlewareConfig,
endpoint_contracts: &EndpointContractRegistry<'_, P>,
has_authentication: bool,
has_authorization: bool,
) -> Result<(), HttpMiddlewareError> {
for endpoint in endpoint_contracts.endpoints() {
let implicit_head = (endpoint.method() == EndpointMethod::Get
&& !endpoint_contracts.endpoints().iter().any(|other| {
other.method() == EndpointMethod::Head
&& other.path().as_str() == endpoint.path().as_str()
}))
.then_some(EndpointMethod::Head);
for contract_method in [Some(endpoint.method()), implicit_head]
.into_iter()
.flatten()
{
let method = endpoint_method(contract_method);
let effective = config
.request_policy
.policy_for_route(&method, endpoint.path().as_str());
let rate_limit_enabled =
config.rate_limit.is_some() && effective.rate_limit().enabled();
let expected_rate_limit = endpoint.rate_limit().enabled();
if rate_limit_enabled != expected_rate_limit {
return Err(HttpMiddlewareError::EndpointContractRateLimitMismatch {
method: contract_method,
path: endpoint.path().as_str().to_owned(),
expected_enabled: expected_rate_limit,
actual_enabled: rate_limit_enabled,
});
}
let actual_idempotency = if config.idempotency.is_some() {
effective.idempotency()
} else {
IdempotencyRequestPolicy::Disabled
};
if actual_idempotency != endpoint.idempotency() {
return Err(HttpMiddlewareError::EndpointContractIdempotencyMismatch {
method: contract_method,
path: endpoint.path().as_str().to_owned(),
expected: endpoint.idempotency(),
actual: actual_idempotency,
});
}
if endpoint.auth().requires_identity() && !has_authentication {
return Err(
HttpMiddlewareError::EndpointContractAuthenticationRequired {
method: contract_method,
path: endpoint.path().as_str().to_owned(),
},
);
}
if actual_idempotency != IdempotencyRequestPolicy::Disabled
&& endpoint.requires_authorization()
&& !has_authorization
{
return Err(HttpMiddlewareError::EndpointContractAuthorizationRequired {
method: contract_method,
path: endpoint.path().as_str().to_owned(),
});
}
if rate_limit_enabled
&& endpoint.auth() == EndpointAuth::Public
&& config.rate_limit.as_ref().is_some_and(|policy| {
matches!(
policy.key_source,
RateLimitKeySource::Principal | RateLimitKeySource::Tenant
)
})
{
return Err(HttpMiddlewareError::PublicEndpointIdentityRateLimit {
method: contract_method,
path: endpoint.path().as_str().to_owned(),
});
}
}
}
Ok(())
}
fn endpoint_method(method: EndpointMethod) -> axum::http::Method {
match method {
EndpointMethod::Delete => axum::http::Method::DELETE,
EndpointMethod::Get => axum::http::Method::GET,
EndpointMethod::Head => axum::http::Method::HEAD,
EndpointMethod::Options => axum::http::Method::OPTIONS,
EndpointMethod::Patch => axum::http::Method::PATCH,
EndpointMethod::Post => axum::http::Method::POST,
EndpointMethod::Put => axum::http::Method::PUT,
}
}
fn apply_http_layers<S>(router: Router<S>, stack: HttpMiddlewareStack) -> Router<S>
where
S: Clone + Send + Sync + 'static,
{
let HttpMiddlewareStack {
config,
concurrency,
rate_limit,
idempotency,
metrics,
authentication,
authorization,
} = stack;
let mut router = router.layer(DefaultBodyLimit::disable());
if let Some(idempotency) = idempotency {
router = router.layer(
IdempotencyLayer::new(idempotency).with_request_policy(config.request_policy.clone()),
);
}
if let Some(rate_limit) = rate_limit {
router = router.layer(
RateLimitLayer::new(rate_limit).with_request_policy(config.request_policy.clone()),
);
}
if config.compression {
router = router.layer(middleware::from_fn(compress_response));
}
if config.request_policy.has_request_body_limits() {
router = router.layer(RouteRequestBodyLimitLayer {
request_policy: config.request_policy.clone(),
});
}
if let Some(authorization) = authorization {
router = router.layer(middleware::from_fn(
move |request: Request<Body>, next: Next| {
let authorization = authorization.clone();
async move {
let (mut parts, body) = request.into_parts();
RequestContext::from_parts(&mut parts);
match authorization.authorize(&parts).await {
Ok(()) => next.run(Request::from_parts(parts, body)).await,
Err(problem) => problem
.with_request_context(RequestContext::from_parts(&mut parts))
.into_response(),
}
}
},
));
}
if let Some(authentication) = authentication {
router = router.layer(middleware::from_fn(
move |request: Request<Body>, next: Next| {
let authentication = authentication.clone();
async move {
let (mut parts, body) = request.into_parts();
RequestContext::from_parts(&mut parts);
match authentication.authenticate(&parts).await {
Ok(Some(verified)) => {
let mut request = Request::from_parts(parts, body);
verified.install(&mut request);
next.run(request).await
}
Ok(None) => {
RequestContext::from_parts(&mut parts).clear_principal();
parts.extensions.remove::<IdempotencyScope>();
next.run(Request::from_parts(parts, body)).await
}
Err(problem) => problem
.with_request_context(RequestContext::from_parts(&mut parts))
.into_response(),
}
}
},
));
}
if let Some(concurrency) = concurrency {
router = router.layer(middleware::from_fn(
move |request: Request<Body>, next: Next| {
let concurrency = concurrency.clone();
async move {
let Ok(_permit) = concurrency.try_acquire_owned() else {
let mut problem = ApiProblem::service_unavailable(
ProblemCode::REQUEST_OVERLOADED,
"request concurrency limit has been reached",
);
if let Some(context) = request.extensions().get::<RequestContext>() {
problem = problem.with_request_context(context);
}
return problem.into_response();
};
next.run(request).await
}
},
));
}
if config.request_policy.has_timeouts() {
let request_policy = config.request_policy.clone();
router = router.layer(middleware::from_fn(
move |mut request: Request<Body>, next: Next| {
let request_policy = request_policy.clone();
async move {
let Some(timeout) = request_policy.resolve(&mut request).timeout().duration()
else {
return next.run(request).await;
};
let correlation = request.extensions().get::<RequestContext>().map(|context| {
(
context.request_id().clone(),
context.trace_id().map(str::to_owned),
)
});
tokio::select! {
biased;
() = tokio::time::sleep(timeout) => {
let mut problem = ApiProblem::service_unavailable(
ProblemCode::RESPONSE_TIMEOUT,
"request exceeded the configured response timeout",
);
if let Some((request_id, trace_id)) = correlation {
problem = problem.with_request_id(&request_id);
if let Some(trace_id) = trace_id {
problem = problem.with_trace_id(trace_id);
}
}
problem.into_response()
}
response = next.run(request) => response,
}
}
},
));
}
if config.trace {
router = router.layer(TraceLayer::new_for_http().make_span_with(
|request: &axum::http::Request<_>| {
let context = request.extensions().get::<RequestContext>();
let matched_path = request
.extensions()
.get::<MatchedPath>()
.map(MatchedPath::as_str)
.unwrap_or(crate::http::HTTP_METRICS_UNMATCHED_ROUTE);
tracing::span!(
Level::INFO,
"http_request",
method = %request.method(),
matched_path,
request_id = context.map(|context| context.request_id().as_str()),
trace_id = context.and_then(RequestContext::trace_id)
)
},
));
}
if config.request_context {
let forwarded_headers = config.forwarded_headers;
router = router.layer(middleware::from_fn(move |request, next| {
request_context_middleware_with_forwarded_policy(
request,
next,
forwarded_headers.clone(),
)
}));
}
if let Some(cors_layer) = cors_layer(config.cors) {
router = router.layer(cors_layer);
}
if let Some(security_headers) = config.security_headers {
router = router.layer(middleware::from_fn(
move |request: Request<Body>, next: Next| {
let security_headers = security_headers.clone();
async move {
let mut response = next.run(request).await;
security_headers.apply(response.headers_mut());
response
}
},
));
}
if let Some(metrics) = metrics {
router = router.layer(metrics);
}
router
}
#[derive(Debug, Clone)]
struct RouteRequestBodyLimitLayer {
request_policy: HttpRequestPolicy,
}
impl<S> tower::Layer<S> for RouteRequestBodyLimitLayer {
type Service = RouteRequestBodyLimitService<S>;
fn layer(&self, inner: S) -> Self::Service {
RouteRequestBodyLimitService {
inner,
request_policy: self.request_policy.clone(),
}
}
}
#[derive(Debug, Clone)]
struct RouteRequestBodyLimitService<S> {
inner: S,
request_policy: HttpRequestPolicy,
}
impl<S> tower::Service<Request<Body>> for RouteRequestBodyLimitService<S>
where
S: tower::Service<Request<Body>, Response = axum::response::Response>,
S::Future: Send + 'static,
S::Error: Send + 'static,
{
type Response = axum::response::Response;
type Error = S::Error;
type Future = std::pin::Pin<
Box<dyn std::future::Future<Output = Result<Self::Response, Self::Error>> + Send>,
>;
fn poll_ready(
&mut self,
context: &mut std::task::Context<'_>,
) -> std::task::Poll<Result<(), Self::Error>> {
self.inner.poll_ready(context)
}
fn call(&mut self, request: Request<Body>) -> Self::Future {
match enforce_route_request_body_limit(&self.request_policy, request) {
std::ops::ControlFlow::Continue(request) => Box::pin(self.inner.call(request)),
std::ops::ControlFlow::Break(response) => Box::pin(std::future::ready(Ok(response))),
}
}
}
async fn compress_response(request: Request<Body>, next: Next) -> Response {
let is_head = request.method() == axum::http::Method::HEAD;
let mut negotiation = Request::new(());
for value in request.headers().get_all(header::ACCEPT_ENCODING) {
negotiation
.headers_mut()
.append(header::ACCEPT_ENCODING, value.clone());
}
let mut response = next.run(request).await;
let status = response.status();
let has_strong_etag = response
.headers()
.get_all(header::ETAG)
.iter()
.any(|value| !value.as_bytes().trim_ascii().starts_with(b"W/"));
let no_transform = response
.headers()
.get_all(header::CACHE_CONTROL)
.iter()
.flat_map(|value| crate::http::header_list_items(value.as_bytes()))
.any(|directive| directive.trim_ascii().eq_ignore_ascii_case(b"no-transform"));
if !response.headers().contains_key(header::CONTENT_ENCODING)
&& !has_strong_etag
&& !no_transform
&& !(is_head && response.body().size_hint().lower() == 0)
&& !status.is_informational()
&& !matches!(
status,
StatusCode::NO_CONTENT | StatusCode::RESET_CONTENT | StatusCode::NOT_MODIFIED
)
{
let mut compression =
Compression::new(service_fn(|request: Request<Response>| async move {
Ok::<_, Infallible>(request.into_body())
}));
response = match compression.call(negotiation.map(|()| response)).await {
Ok(response) => response.map(Body::new),
Err(error) => match error {},
};
}
if status != StatusCode::NOT_ACCEPTABLE && response.status() == StatusCode::NOT_ACCEPTABLE {
response
.headers_mut()
.insert(header::CACHE_CONTROL, HeaderValue::from_static("no-store"));
}
response
}
fn enforce_route_request_body_limit(
request_policy: &HttpRequestPolicy,
mut request: Request<Body>,
) -> std::ops::ControlFlow<axum::response::Response, Request<Body>> {
let RequestBodyLimitPolicy::Bytes(limit) =
request_policy.resolve(&mut request).request_body_limit()
else {
return std::ops::ControlFlow::Continue(request);
};
let declared_length = request
.headers()
.get(axum::http::header::CONTENT_LENGTH)
.and_then(|value| value.to_str().ok())
.and_then(|value| value.parse::<u64>().ok());
if request.body().size_hint().lower() > limit.get() as u64
|| declared_length.is_some_and(|length| length > limit.get() as u64)
{
let mut problem = ApiProblem::payload_too_large(
ProblemCode::REQUEST_BODY_TOO_LARGE,
"request body is too large",
);
if let Some(context) = request.extensions().get::<RequestContext>() {
problem = problem.with_request_context(context);
}
return std::ops::ControlFlow::Break(problem.into_response());
}
std::ops::ControlFlow::Continue(
request.map(|body| Body::new(http_body_util::Limited::new(body, limit.get()))),
)
}
fn cors_layer(policy: CorsPolicy) -> Option<CorsLayer> {
let (origin, config) = match policy {
CorsPolicy::Disabled => return None,
CorsPolicy::AnyOrigin(config) => (AllowOrigin::any(), config),
CorsPolicy::Restricted(mut config) => (
AllowOrigin::list(std::mem::take(&mut config.allowed_origins)),
config,
),
};
let headers = if config.allowed_headers.as_slice() == ["*"] {
AllowHeaders::mirror_request()
} else {
AllowHeaders::list(config.allowed_headers)
};
let mut layer = CorsLayer::new()
.allow_origin(origin)
.allow_methods(config.allowed_methods)
.allow_headers(headers)
.expose_headers(config.exposed_headers)
.allow_credentials(config.allow_credentials);
if let Some(max_age) = config.max_age {
layer = layer.max_age(max_age);
}
Some(layer)
}
#[cfg(test)]
mod tests {
use super::*;
use std::{
cell::Cell,
convert::Infallible,
future::{Ready, ready},
rc::Rc,
task::{Context, Poll},
};
use tower::{Layer, Service, ServiceExt};
#[tokio::test]
async fn body_limit_calls_the_ready_non_clone_service_only_for_admitted_requests() {
struct ReadyService {
ready: bool,
calls: Rc<Cell<usize>>,
}
impl Service<Request<Body>> for ReadyService {
type Response = axum::response::Response;
type Error = Infallible;
type Future = Ready<Result<Self::Response, Self::Error>>;
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.ready = true;
Poll::Ready(Ok(()))
}
fn call(&mut self, _: Request<Body>) -> Self::Future {
assert!(std::mem::take(&mut self.ready));
self.calls.set(self.calls.get() + 1);
ready(Ok(StatusCode::OK.into_response()))
}
}
let calls = Rc::new(Cell::new(0));
let mut service = RouteRequestBodyLimitLayer {
request_policy: HttpRequestPolicy::disabled()
.with_default_request_body_limit(RequestBodyLimitPolicy::bytes(2).unwrap()),
}
.layer(ReadyService {
ready: false,
calls: calls.clone(),
});
for (body, status, expected_calls) in [
("ok", StatusCode::OK, 1),
("large", StatusCode::PAYLOAD_TOO_LARGE, 1),
("ok", StatusCode::OK, 2),
] {
let response = service
.ready()
.await
.unwrap()
.call(Request::new(Body::from(body)))
.await
.unwrap();
assert_eq!(response.status(), status);
assert_eq!(calls.get(), expected_calls);
}
}
}