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 | /// Cross-site request forgery tokens, bound to a session and fresh on |
| 2 | /// every response (toboggan's `Auth/Csrf`). A session's key is derived |
| 3 | /// from its secret, so no store holds it. A token is |
| 4 | /// `base64url(nonce) "." base64url(HMAC-SHA256(key, nonce))`, with 16 |
| 5 | /// fresh random bytes as the nonce. Every token minted for a session is |
| 6 | /// valid until the session ends: tokens are stateless, not single-use. |
| 7 | /// |
| 8 | /// The app sends a token with each response (a header, a hidden form |
| 9 | /// field) and checks the one that comes back on every request that |
| 10 | /// changes something. |
| 11 | |
| 12 | import signed |
| 13 | import std/base64 |
| 14 | import std/bytes |
| 15 | import std/crypto |
| 16 | import std/random.{type random} |
| 17 | import std/string |
| 18 | |
| 19 | /// The CSRF key of the session whose secret is `secret`: an HMAC under a |
| 20 | /// fixed label, equal for equal secrets only, with no way back to the |
| 21 | /// secret. Whoever holds the secret holds the session already; nobody else |
| 22 | /// can compute the key. |
| 23 | pub fn key :: secret: Bytes -> Bytes |
| 24 | { |
| 25 | crypto.hmac_sha256 secret (bytes.from_string "web/csrf-key/v1") |
| 26 | } |
| 27 | |
| 28 | /// A fresh token under `key`. |
| 29 | pub fn token :: key: Bytes -> String / <random> |
| 30 | { |
| 31 | minted key (random.bytes 16) |
| 32 | } |
| 33 | |
| 34 | /// Whether `token` was minted under `key`, compared in constant time. |
| 35 | /// A token under another key, altered or malformed is not. |
| 36 | pub fn valid :: key: Bytes token: String -> Bool |
| 37 | { |
| 38 | match string.split token "." |
| 39 | { [nonce, _] -> match decoded nonce |
| 40 | { Ok(raw) if bytes.length raw > 0 -> |
| 41 | signed.same_bytes |
| 42 | (bytes.from_string token) |
| 43 | (bytes.from_string (minted key raw)) |
| 44 | , _ -> False |
| 45 | } |
| 46 | , _ -> False |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | fn minted :: key: Bytes nonce: Bytes -> String |
| 51 | { |
| 52 | "{encoded nonce}.{encoded (crypto.hmac_sha256 key nonce)}" |
| 53 | } |
| 54 | |
| 55 | /// base64url without padding (RFC 4648 §5). |
| 56 | fn encoded :: data: Bytes -> String |
| 57 | { |
| 58 | base64.encode data |
| 59 | |> replaced _ "+" "-" |
| 60 | |> replaced _ "/" "_" |
| 61 | |> replaced _ "=" "" |
| 62 | } |
| 63 | |
| 64 | fn decoded :: text: String -> Result<Bytes, String> |
| 65 | { |
| 66 | let padding = string.repeat "=" ((4 - string.length text % 4) % 4) |
| 67 | base64.decode (replaced (replaced text "-" "+") "_" "/" <> padding) |
| 68 | } |
| 69 | |
| 70 | fn replaced :: text: String from: String to: String -> String |
| 71 | { |
| 72 | string.join (string.split text from) to |
| 73 | } |