packages
The Louise packages, each released on its own
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#28 · main · 3 min 34 s
0Étoiles
0Forks
3Issues
| 1 | /// Security headers: a strict Content-Security-Policy (nothing but the |
| 2 | /// site's own files, nothing `unsafe`), no sniffing, no framing, and a |
| 3 | /// same-origin referrer and opener. `harden_with` goes further, for pages |
| 4 | /// that depend on who is signed in (toboggan's `contextHeaders`). |
| 5 | |
| 6 | import std/http.{type Response, Response} |
| 7 | import std/list |
| 8 | import std/string |
| 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 | |
| 19 | /// `resp` with the security headers added. |
| 20 | pub fn harden :: resp: Response -> Response |
| 21 | { |
| 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 |
| 39 | resp |
| 40 | (list.filter (stance policy) fn h { !own_cache || !cache_control h.0 }) |
| 41 | } |
| 42 | |
| 43 | /// A hardened `resp` whose policy's `script-src` also allows the inline |
| 44 | /// scripts that carry `nonce`; a policy that allows it already is kept. |
| 45 | /// A nonce must be fresh for every response and unguessable (base64 of 16 |
| 46 | /// random bytes or more), so it never holds a quote or a `;`. |
| 47 | pub fn with_script_nonce :: resp: Response nonce: String -> Response |
| 48 | { |
| 49 | Response(..resp, headers: list.map resp.headers (allowing _ nonce)) |
| 50 | } |
| 51 | |
| 52 | fn allowing :: header: (String, String) nonce: String -> (String, String) |
| 53 | { |
| 54 | if header.0 == "Content-Security-Policy" |
| 55 | { |
| 56 | let directives = |
| 57 | list.map |
| 58 | (string.split header.1 "; ") |
| 59 | fn directive |
| 60 | { |
| 61 | let allowed = "'nonce-{nonce}'" |
| 62 | if string.starts_with directive "script-src " |
| 63 | && !string.contains directive allowed |
| 64 | { |
| 65 | "{directive} {allowed}" |
| 66 | } |
| 67 | else |
| 68 | { |
| 69 | directive |
| 70 | } |
| 71 | } |
| 72 | (header.0, string.join directives "; ") |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | header |
| 77 | } |
| 78 | } |
| 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 | |
| 90 | fn headers :: -> List<(String, String)> |
| 91 | { |
| 92 | [ content_security_policy () |
| 93 | , ("X-Content-Type-Options", "nosniff") |
| 94 | , ("Referrer-Policy", "same-origin") |
| 95 | , ("Cross-Origin-Opener-Policy", "same-origin") |
| 96 | ] |
| 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 | } |