use axum::{
body::Body,
http::{Request, header},
middleware::Next,
response::Response,
};
pub async fn security_headers_middleware(
req: Request<Body>,
next: Next,
) -> Response {
let mut response = next.run(req).await;
let headers = response.headers_mut();
headers.insert(header::X_FRAME_OPTIONS, "DENY".parse().unwrap());
headers.insert(header::X_CONTENT_TYPE_OPTIONS, "nosniff".parse().unwrap());
headers.insert(header::X_XSS_PROTECTION, "1; mode=block".parse().unwrap());
headers.insert(
header::CONTENT_SECURITY_POLICY,
concat!(
"default-src 'self'; ",
"script-src 'self' 'unsafe-inline' 'unsafe-eval' https:; ",
"style-src 'self' 'unsafe-inline' https:; ",
"font-src 'self' https: data:; ",
"img-src 'self' data: https:; ",
"connect-src 'self' https:;"
).parse().unwrap()
);
response
}