use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{ready, Context, Poll};
use axum::http::{header, HeaderValue, Request, Response};
use pin_project::pin_project;
use tower::{Layer, Service};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum FrameOptions {
#[default]
Deny,
SameOrigin,
Disabled,
}
#[derive(Debug, Clone, Default)]
pub struct SecurityHeadersConfig {
pub frame_options: FrameOptions,
pub hsts_max_age_secs: Option<u64>,
pub content_security_policy: Option<String>,
}
impl SecurityHeadersConfig {
pub fn new() -> Self {
Self::default()
}
pub fn with_frame_options(mut self, frame_options: FrameOptions) -> Self {
self.frame_options = frame_options;
self
}
pub fn with_hsts_max_age_secs(mut self, max_age_secs: u64) -> Self {
self.hsts_max_age_secs = Some(max_age_secs);
self
}
pub fn with_content_security_policy(mut self, policy: impl Into<String>) -> Self {
self.content_security_policy = Some(policy.into());
self
}
}
#[derive(Debug)]
struct ResolvedSecurityHeaders {
frame_options: FrameOptions,
hsts: Option<HeaderValue>,
csp: Option<HeaderValue>,
}
impl ResolvedSecurityHeaders {
fn from_config(config: SecurityHeadersConfig) -> Self {
let hsts = config.hsts_max_age_secs.and_then(|secs| {
HeaderValue::from_str(&format!("max-age={secs}")).ok()
});
let csp = config.content_security_policy.and_then(|policy| {
match HeaderValue::from_str(&policy) {
Ok(value) => Some(value),
Err(_) => {
tracing::warn!(
"configured Content-Security-Policy contains invalid header \
characters; the header will not be sent"
);
None
}
}
});
Self {
frame_options: config.frame_options,
hsts,
csp,
}
}
fn apply<B>(&self, response: &mut Response<B>) {
let headers = response.headers_mut();
if !headers.contains_key(header::X_CONTENT_TYPE_OPTIONS) {
headers.insert(
header::X_CONTENT_TYPE_OPTIONS,
HeaderValue::from_static("nosniff"),
);
}
match self.frame_options {
FrameOptions::Deny => {
if !headers.contains_key(header::X_FRAME_OPTIONS) {
headers.insert(header::X_FRAME_OPTIONS, HeaderValue::from_static("DENY"));
}
}
FrameOptions::SameOrigin => {
headers.insert(
header::X_FRAME_OPTIONS,
HeaderValue::from_static("SAMEORIGIN"),
);
}
FrameOptions::Disabled => {
headers.remove(header::X_FRAME_OPTIONS);
}
}
if !headers.contains_key(header::REFERRER_POLICY) {
headers.insert(
header::REFERRER_POLICY,
HeaderValue::from_static("strict-origin-when-cross-origin"),
);
}
if let Some(hsts) = &self.hsts {
if !headers.contains_key(header::STRICT_TRANSPORT_SECURITY) {
headers.insert(header::STRICT_TRANSPORT_SECURITY, hsts.clone());
}
}
if let Some(csp) = &self.csp {
if !headers.contains_key(header::CONTENT_SECURITY_POLICY) {
headers.insert(header::CONTENT_SECURITY_POLICY, csp.clone());
}
}
}
}
#[derive(Debug, Clone)]
pub struct SecurityHeadersLayer {
resolved: Arc<ResolvedSecurityHeaders>,
}
impl SecurityHeadersLayer {
pub fn new(config: SecurityHeadersConfig) -> Self {
Self {
resolved: Arc::new(ResolvedSecurityHeaders::from_config(config)),
}
}
}
impl Default for SecurityHeadersLayer {
fn default() -> Self {
Self::new(SecurityHeadersConfig::default())
}
}
impl<S> Layer<S> for SecurityHeadersLayer {
type Service = SecurityHeaders<S>;
fn layer(&self, inner: S) -> Self::Service {
SecurityHeaders {
inner,
resolved: Arc::clone(&self.resolved),
}
}
}
#[derive(Debug, Clone)]
pub struct SecurityHeaders<S> {
inner: S,
resolved: Arc<ResolvedSecurityHeaders>,
}
impl<S, ReqBody, ResBody> Service<Request<ReqBody>> for SecurityHeaders<S>
where
S: Service<Request<ReqBody>, Response = Response<ResBody>>,
{
type Response = Response<ResBody>;
type Error = S::Error;
type Future = SecurityHeadersFuture<S::Future>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
}
fn call(&mut self, req: Request<ReqBody>) -> Self::Future {
SecurityHeadersFuture {
inner: self.inner.call(req),
resolved: Arc::clone(&self.resolved),
}
}
}
#[pin_project]
pub struct SecurityHeadersFuture<F> {
#[pin]
inner: F,
resolved: Arc<ResolvedSecurityHeaders>,
}
impl<F, ResBody, E> Future for SecurityHeadersFuture<F>
where
F: Future<Output = Result<Response<ResBody>, E>>,
{
type Output = Result<Response<ResBody>, E>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
let mut result = ready!(this.inner.poll(cx));
if let Ok(response) = &mut result {
this.resolved.apply(response);
}
Poll::Ready(result)
}
}
#[cfg(test)]
mod tests {
use super::*;
use axum::body::Body;
fn apply(config: SecurityHeadersConfig, response: &mut Response<Body>) {
ResolvedSecurityHeaders::from_config(config).apply(response);
}
#[test]
fn defaults_add_frame_options_referrer_policy_and_nosniff() {
let mut response = Response::new(Body::empty());
apply(SecurityHeadersConfig::default(), &mut response);
let headers = response.headers();
assert_eq!(headers.get(header::X_FRAME_OPTIONS).unwrap(), "DENY");
assert_eq!(
headers.get(header::REFERRER_POLICY).unwrap(),
"strict-origin-when-cross-origin"
);
assert_eq!(
headers.get(header::X_CONTENT_TYPE_OPTIONS).unwrap(),
"nosniff"
);
assert!(headers.get(header::STRICT_TRANSPORT_SECURITY).is_none());
assert!(headers.get(header::CONTENT_SECURITY_POLICY).is_none());
}
#[test]
fn existing_headers_are_not_clobbered() {
let mut response = Response::new(Body::empty());
response.headers_mut().insert(
header::REFERRER_POLICY,
HeaderValue::from_static("no-referrer"),
);
apply(SecurityHeadersConfig::default(), &mut response);
assert_eq!(
response.headers().get(header::REFERRER_POLICY).unwrap(),
"no-referrer"
);
}
#[test]
fn same_origin_overrides_an_existing_deny() {
let mut response = Response::new(Body::empty());
response
.headers_mut()
.insert(header::X_FRAME_OPTIONS, HeaderValue::from_static("DENY"));
apply(
SecurityHeadersConfig::default().with_frame_options(FrameOptions::SameOrigin),
&mut response,
);
assert_eq!(
response.headers().get(header::X_FRAME_OPTIONS).unwrap(),
"SAMEORIGIN"
);
}
#[test]
fn disabled_frame_options_removes_the_header() {
let mut response = Response::new(Body::empty());
response
.headers_mut()
.insert(header::X_FRAME_OPTIONS, HeaderValue::from_static("DENY"));
apply(
SecurityHeadersConfig::default().with_frame_options(FrameOptions::Disabled),
&mut response,
);
assert!(response.headers().get(header::X_FRAME_OPTIONS).is_none());
}
#[test]
fn hsts_and_csp_are_emitted_when_configured() {
let mut response = Response::new(Body::empty());
apply(
SecurityHeadersConfig::default()
.with_hsts_max_age_secs(63072000)
.with_content_security_policy("default-src 'self'"),
&mut response,
);
let headers = response.headers();
assert_eq!(
headers.get(header::STRICT_TRANSPORT_SECURITY).unwrap(),
"max-age=63072000"
);
assert_eq!(
headers.get(header::CONTENT_SECURITY_POLICY).unwrap(),
"default-src 'self'"
);
}
#[test]
fn invalid_csp_is_dropped_instead_of_panicking() {
let mut response = Response::new(Body::empty());
apply(
SecurityHeadersConfig::default().with_content_security_policy("bad\npolicy"),
&mut response,
);
assert!(response
.headers()
.get(header::CONTENT_SECURITY_POLICY)
.is_none());
}
}