Something went wrong. Try again.
Chris’s PDS implementation
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869//! Browser and cache policy applied consistently at the HTTP boundary.
use axum::{ extract::Request, http::{HeaderMap, HeaderName, HeaderValue, Method, header}, middleware::Next, response::Response,};use tower_http::cors::{Any, CorsLayer};
const BLOB_CSP: &str = "default-src 'none'; sandbox";const DEFAULT_CSP: &str = "default-src 'none'; base-uri 'none'; form-action 'self'; frame-ancestors 'none'";const PUBLIC_CACHE: &str = "public, max-age=300";const NO_STORE: &str = "no-store";
pub(super) fn cors() -> CorsLayer { CorsLayer::new() .allow_origin(Any) .allow_methods([Method::GET, Method::POST]) .allow_headers([ header::AUTHORIZATION, header::CONTENT_TYPE, HeaderName::from_static("dpop"), ]) .expose_headers([HeaderName::from_static("x-request-id")])}
pub(super) async fn response_headers(request: Request, next: Next) -> Response { let path = request.uri().path().to_owned(); let mut response = next.run(request).await; let publicly_cacheable = response.status().is_success() && (path == "/xrpc/com.atproto.sync.getBlob" || path.starts_with("/.well-known/")); let headers = response.headers_mut(); insert(headers, "x-content-type-options", "nosniff"); insert(headers, "referrer-policy", "no-referrer"); insert(headers, "x-frame-options", "DENY");
insert( headers, "content-security-policy", if matches!( path.as_str(), "/xrpc/com.atproto.sync.getBlob" | "/xrpc/com.atproto.space.getBlob" ) { BLOB_CSP } else { DEFAULT_CSP }, ); insert( headers, "cache-control", if publicly_cacheable { PUBLIC_CACHE } else { NO_STORE }, ); response}
fn insert(headers: &mut HeaderMap, name: &'static str, value: &'static str) { headers.insert( HeaderName::from_static(name), HeaderValue::from_static(value), );}