| @@ -1,18 +1,43 @@ |
| 1 | 1 | /// Security headers: a strict Content-Security-Policy (nothing but the |
| 2 | 2 | /// site's own files, nothing `unsafe`), no sniffing, no framing, and a |
| 3 | | −/// same-origin referrer and opener. |
| 3 | +/// same-origin referrer and opener. `harden_with` goes further, for pages |
| 4 | +/// that depend on who is signed in (toboggan's `contextHeaders`). |
| 4 | 5 | |
| 5 | 6 | import std/http.{type Response, Response} |
| 6 | 7 | import std/list |
| 7 | 8 | import std/string |
| 8 | 9 | |
| 10 | +/// What `harden_with` needs to know about a request and its site: |
| 11 | +/// - `nonce`, the request's, which the policy's `script-src` allows; |
| 12 | +/// - `trusted_types`, the Trusted Types policies the page's scripts may |
| 13 | +/// create (`htmx`, ...). Trusted Types are required when it names any; |
| 14 | +/// an empty list leaves them out; |
| 15 | +/// - `https`: whether the site is served over HTTPS, which turns HSTS on. |
| 16 | +pub type Policy |
| 17 | +{ Policy(nonce: String, trusted_types: List<String>, https: Bool) } |
| 18 | + |
| 9 | 19 | /// `resp` with the security headers added. |
| 10 | 20 | pub fn harden :: resp: Response -> Response |
| 11 | 21 | { |
| 12 | | − list.fold |
| 13 | | − (headers ()) |
| 22 | + with_headers resp (headers ()) |
| 23 | +} |
| 24 | + |
| 25 | +/// `resp` with the fuller set of security headers `policy` describes: |
| 26 | +/// - the Content-Security-Policy of `harden`, its `script-src` allowing |
| 27 | +/// the nonce, and Trusted Types required when `policy` names some; |
| 28 | +/// - `Cache-Control: no-store, no-transform`, unless `resp` sets its own: |
| 29 | +/// a page that depends on who is signed in is never stored, and nothing |
| 30 | +/// between rewrites it (a streamed page's chunks flow as written); |
| 31 | +/// - no referrer at all (page addresses may carry ids and credentials), |
| 32 | +/// no sniffing, no powerful browser features, a same-origin opener and |
| 33 | +/// resource policy; |
| 34 | +/// - HSTS for a year, subdomains included, when the site is HTTPS. |
| 35 | +pub fn harden_with :: policy: Policy resp: Response -> Response |
| 36 | +{ |
| 37 | + let own_cache = list.any resp.headers fn h { cache_control h.0 } |
| 38 | + with_headers |
| 14 | 39 | resp |
| 15 | | − fn hardened h { http.with_header hardened h.0 h.1 } |
| 40 | + (list.filter (stance policy) fn h { !own_cache || !cache_control h.0 }) |
| 16 | 41 | } |
| 17 | 42 | |
| 18 | 43 | /// A hardened `resp` whose policy's `script-src` also allows the inline |
| @@ -52,13 +77,60 @@ fn allowing :: header: (String, String) nonce: String -> (String, String) |
| 52 | 77 | } |
| 53 | 78 | } |
| 54 | 79 | |
| 80 | +fn with_headers :: resp: Response added: List<(String, String)> -> Response |
| 81 | +{ |
| 82 | + list.fold added resp fn hardened h { http.with_header hardened h.0 h.1 } |
| 83 | +} |
| 84 | + |
| 85 | +fn cache_control :: name: String -> Bool |
| 86 | +{ |
| 87 | + string.to_lower name == "cache-control" |
| 88 | +} |
| 89 | + |
| 55 | 90 | fn headers :: -> List<(String, String)> |
| 56 | 91 | { |
| 57 | | − [ ( "Content-Security-Policy" |
| 58 | | − , "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'" |
| 59 | | − ) |
| 92 | + [ content_security_policy () |
| 60 | 93 | , ("X-Content-Type-Options", "nosniff") |
| 61 | 94 | , ("Referrer-Policy", "same-origin") |
| 62 | 95 | , ("Cross-Origin-Opener-Policy", "same-origin") |
| 63 | 96 | ] |
| 64 | 97 | } |
| 98 | + |
| 99 | +fn content_security_policy :: -> (String, String) |
| 100 | +{ |
| 101 | + ( "Content-Security-Policy" |
| 102 | + , "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'" |
| 103 | + ) |
| 104 | +} |
| 105 | + |
| 106 | +fn stance :: policy: Policy -> List<(String, String)> |
| 107 | +{ |
| 108 | + let hsts = if policy.https |
| 109 | + { |
| 110 | + [("Strict-Transport-Security", "max-age=31536000; includeSubDomains")] |
| 111 | + } |
| 112 | + else |
| 113 | + { |
| 114 | + [] |
| 115 | + } |
| 116 | + [ ("Content-Security-Policy", scripts_allowed policy) |
| 117 | + , ("Cache-Control", "no-store, no-transform") |
| 118 | + , ("Referrer-Policy", "no-referrer") |
| 119 | + , ("X-Content-Type-Options", "nosniff") |
| 120 | + , ( "Permissions-Policy" |
| 121 | + , "camera=(), microphone=(), geolocation=(), payment=(), usb=()" |
| 122 | + ) |
| 123 | + , ("Cross-Origin-Opener-Policy", "same-origin") |
| 124 | + , ("Cross-Origin-Resource-Policy", "same-origin") |
| 125 | + , ..hsts |
| 126 | + ] |
| 127 | +} |
| 128 | + |
| 129 | +fn scripts_allowed :: policy: Policy -> String |
| 130 | +{ |
| 131 | + let nonced = (allowing (content_security_policy ()) policy.nonce).1 |
| 132 | + match policy.trusted_types |
| 133 | + { [] -> nonced |
| 134 | + , names -> "{nonced}; require-trusted-types-for 'script'; trusted-types {string.join names " "}" |
| 135 | + } |
| 136 | +} |