csrf_token.nx
buildroot/runtime/csrf_token.nx
about
csrf_token.nx -- Cross-Site Request Forgery token generation.
Standard defence against forged cross-origin form submits:
server issues a token tied to the user's session, embeds it in
forms, rejects submissions whose token doesn't HMAC-match the
session. Since cross-origin JS can't read the session cookie
or the issued token, attacker can't forge a valid request.
OWASP recommended pattern: \"Signed Double-Submit Cookie\" --
token = random + HMAC(session_id, random). Verification
splits, re-derives HMAC, constant-time compares.
Composes rand.nx + hmac.nx + base64.nx + ct.nx.
Token format (URL-safe, no padding):
base64url(random_32_bytes) . base64url(hmac[0..16])
32 bytes random = 256 bits unpredictability; 128-bit truncated
HMAC = collision-resistant auth tag for this use. Total URI-
safe length ~65 chars.
Invariants:
CT1 Token split at '.'; left = random, right = MAC.
CT2 Verification re-derives MAC from (session_id, random)
and ct_memcmp against provided MAC.
CT3 Session ID opaque to this module -- any byte-stable
session identifier works.
dependencies 5 imports · 0 importers
imports: syscalls.nxrand.nxnx_hmac.nxbase64.nxct.nx
imported by: nobody (leaf or entry point)
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| none |
consts
| 37 | const CSRF_ERR_FORMAT: i64 = -1 |
| 38 | const CSRF_ERR_MAC: i64 = -2 |
| 39 | const CSRF_RAND_BYTES: i64 = 32 |
| 40 | const CSRF_MAC_BYTES: i64 = 16 |
functions
| 44 | func csrf_b64url_encode(data: *u8, n: i64, out: *u8) -> i64 |
| 64 | func csrf_b64url_decode(chars: *u8, n: i64, out: *u8) -> i64 |
| 84 | func csrf_token_new(session_id: *u8, session_id_len: i64, |
| 107 | func csrf_token_verify(token: *u8, token_len: i64, |
| 146 | func main() -> i64 |