//! The security headers of every response, and the Content-Security-Policy they carry. //! //! The policy allows scripts and styles from the origin itself and nothing else. No page may use an //! inline script, an inline style or an inline event handler: all behaviour lives in //! `crates/in-web/assets/*.js`. Changing [`CSP`] is a security change, so keep it in step with //! `docs/conventions.md` section 5. use topcoat::context::Cx; use topcoat::router::request::uri; use topcoat::router::response::response_headers; use topcoat::router::{ Body, HeaderMap, HeaderName, HeaderValue, Layer, LayerFuture, Next, Path, header, }; /// The Content-Security-Policy of every response. pub const CSP: &str = "default-src 'self'; img-src 'self' data:; style-src 'self'; script-src 'self'; \ form-action 'self'; frame-ancestors 'none'; base-uri 'none'"; /// Path prefix of the public share pages and their bytes. The token is in the path, so these pages send no /// referrer and ask search engines not to index them (architecture section 8). pub const SHARE_PREFIX: &str = "/s/"; /// `Referrer-Policy` of the public share pages: the token in the URL must never reach another site. const SHARE_REFERRER: &str = "no-referrer"; /// `X-Robots-Tag` of the public share pages. const SHARE_ROBOTS: &str = "noindex"; /// The headers of `base` as they apply to a request for `path`: on a public share path the referrer and /// robots headers are replaced, everywhere else `base` is used as it is. fn for_path(base: &HeaderMap, path: &str) -> HeaderMap { let mut headers = base.clone(); if path.starts_with(SHARE_PREFIX) { headers.insert( header::REFERRER_POLICY, HeaderValue::from_static(SHARE_REFERRER), ); headers.insert( HeaderName::from_static("x-robots-tag"), HeaderValue::from_static(SHARE_ROBOTS), ); } headers } /// One year, in seconds. Sent only when the public origin is https. const HSTS: &str = "max-age=31536000"; /// The headers that every response carries, for an instance at `base_url`. pub fn headers_for(base_url: &str) -> HeaderMap { let mut headers = HeaderMap::new(); headers.insert( header::X_CONTENT_TYPE_OPTIONS, HeaderValue::from_static("nosniff"), ); headers.insert(header::X_FRAME_OPTIONS, HeaderValue::from_static("DENY")); headers.insert( header::REFERRER_POLICY, HeaderValue::from_static("same-origin"), ); headers.insert( header::CONTENT_SECURITY_POLICY, HeaderValue::from_static(CSP), ); if base_url.starts_with("https://") { headers.insert( header::STRICT_TRANSPORT_SECURITY, HeaderValue::from_static(HSTS), ); } headers } /// Adds a fixed set of headers to every response of the router. /// /// It uses the per-request response headers, which the router applies after the layers have run, /// so error responses (404, 500) carry them as well. pub struct SecurityHeaders { headers: HeaderMap, } impl SecurityHeaders { /// A layer that adds `headers` to every response. pub fn new(headers: HeaderMap) -> Self { Self { headers } } } impl Layer for SecurityHeaders { fn path(&self) -> Option<&Path> { None } fn handle<'a>(&'a self, cx: &'a Cx, body: Body, next: Next<'a>) -> LayerFuture<'a> { Box::pin(async move { let path = uri(cx).path().to_owned(); let headers = for_path(&self.headers, &path); match next.run(cx, body).await { // A header the route set itself wins: byte responses carry their own sandboxed // policy, and two policies would apply both (the page one forbids framing). Ok(mut response) => { let response_map = response.headers_mut(); for (name, value) in &headers { if !response_map.contains_key(name) { response_map.insert(name.clone(), value.clone()); } } Ok(response) } // Errors become responses after the layers have run; the per-request slot gives // them the headers too. Err(error) => { response_headers(cx).extend(headers); Err(error) } } }) } } #[cfg(test)] mod tests { use super::*; #[test] fn share_paths_send_no_referrer_and_no_index() { let base = headers_for("https://in.example.com"); let share = for_path(&base, "/s/abc"); assert_eq!(share[header::REFERRER_POLICY], "no-referrer"); assert_eq!(share["x-robots-tag"], "noindex"); let drive = for_path(&base, "/drive"); assert_eq!(drive[header::REFERRER_POLICY], "same-origin"); assert!(!drive.contains_key("x-robots-tag")); } #[test] fn hsts_only_for_https_origins() { assert!( headers_for("https://in.example.com").contains_key(header::STRICT_TRANSPORT_SECURITY) ); assert!( !headers_for("http://127.0.0.1:7650").contains_key(header::STRICT_TRANSPORT_SECURITY) ); } }