/// Security headers: a strict Content-Security-Policy (nothing but the /// site's own files, nothing `unsafe`), no sniffing, no framing, and a /// same-origin referrer and opener. `harden_with` goes further, for pages /// that depend on who is signed in (toboggan's `contextHeaders`). import std/http.{type Response, Response} import std/list import std/string /// What `harden_with` needs to know about a request and its site: /// - `nonce`, the request's, which the policy's `script-src` allows; /// - `trusted_types`, the Trusted Types policies the page's scripts may /// create (`htmx`, ...). Trusted Types are required when it names any; /// an empty list leaves them out; /// - `https`: whether the site is served over HTTPS, which turns HSTS on. pub type Policy { Policy(nonce: String, trusted_types: List, https: Bool) } /// `resp` with the security headers added. pub fn harden :: resp: Response -> Response { with_headers resp (headers ()) } /// `resp` with the fuller set of security headers `policy` describes: /// - the Content-Security-Policy of `harden`, its `script-src` allowing /// the nonce, and Trusted Types required when `policy` names some; /// - `Cache-Control: no-store, no-transform`, unless `resp` sets its own: /// a page that depends on who is signed in is never stored, and nothing /// between rewrites it (a streamed page's chunks flow as written); /// - no referrer at all (page addresses may carry ids and credentials), /// no sniffing, no powerful browser features, a same-origin opener and /// resource policy; /// - HSTS for a year, subdomains included, when the site is HTTPS. pub fn harden_with :: policy: Policy resp: Response -> Response { let own_cache = list.any resp.headers fn h { cache_control h.0 } with_headers resp (list.filter (stance policy) fn h { !own_cache || !cache_control h.0 }) } /// A hardened `resp` whose policy's `script-src` also allows the inline /// scripts that carry `nonce`; a policy that allows it already is kept. /// A nonce must be fresh for every response and unguessable (base64 of 16 /// random bytes or more), so it never holds a quote or a `;`. pub fn with_script_nonce :: resp: Response nonce: String -> Response { Response(..resp, headers: list.map resp.headers (allowing _ nonce)) } fn allowing :: header: (String, String) nonce: String -> (String, String) { if header.0 == "Content-Security-Policy" { let directives = list.map (string.split header.1 "; ") fn directive { let allowed = "'nonce-{nonce}'" if string.starts_with directive "script-src " && !string.contains directive allowed { "{directive} {allowed}" } else { directive } } (header.0, string.join directives "; ") } else { header } } fn with_headers :: resp: Response added: List<(String, String)> -> Response { list.fold added resp fn hardened h { http.with_header hardened h.0 h.1 } } fn cache_control :: name: String -> Bool { string.to_lower name == "cache-control" } fn headers :: -> List<(String, String)> { [ content_security_policy () , ("X-Content-Type-Options", "nosniff") , ("Referrer-Policy", "same-origin") , ("Cross-Origin-Opener-Policy", "same-origin") ] } fn content_security_policy :: -> (String, String) { ( "Content-Security-Policy" , "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; connect-src 'self'; object-src 'none'; base-uri 'none'; form-action 'self'; frame-ancestors 'none'" ) } fn stance :: policy: Policy -> List<(String, String)> { let hsts = if policy.https { [("Strict-Transport-Security", "max-age=31536000; includeSubDomains")] } else { [] } [ ("Content-Security-Policy", scripts_allowed policy) , ("Cache-Control", "no-store, no-transform") , ("Referrer-Policy", "no-referrer") , ("X-Content-Type-Options", "nosniff") , ( "Permissions-Policy" , "camera=(), microphone=(), geolocation=(), payment=(), usb=()" ) , ("Cross-Origin-Opener-Policy", "same-origin") , ("Cross-Origin-Resource-Policy", "same-origin") , ..hsts ] } fn scripts_allowed :: policy: Policy -> String { let nonced = (allowing (content_security_policy ()) policy.nonce).1 match policy.trusted_types { [] -> nonced , names -> "{nonced}; require-trusted-types-for 'script'; trusted-types {string.join names " "}" } }