code wiki / (root) / csrf_token.nx

csrf_token.nx

buildroot/runtime/csrf_token.nx

5850 B175 linesdepth 6pulls 9 transitivereach 0 importersview sourcekind tool
docsdependenciesstructsconstsfunctions

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

syscalls.nx rand.nx nx_hmac.nx base64.nx ct.nx csrf_token.nx

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

main csrf_token_new rand_bytes rand_open_urandom sys_read hmac_sha256 sys_mmap sha256_digest sys_mmap ↻ sha256_init sys_mmap ↻ sha256_k sha256_update sha256_compress_ni_blocks blk_set_byte sha256_compress sha256_compress_ni blk_word sha256_final blk_set_byte ↻ sha256_compress ↻ sha256_init ↻ sha256_update ↻ sha256_final ↻ csrf_b64url_encode b64_encode b64_enc_char csrf_token_verify csrf_b64url_decode b64_decode b64_grab b64_dec_char hmac_sha256 ↻ ct_memcmp ct_eq

structs

none

consts

37const CSRF_ERR_FORMAT: i64 = -1
38const CSRF_ERR_MAC: i64 = -2
39const CSRF_RAND_BYTES: i64 = 32
40const CSRF_MAC_BYTES: i64 = 16

functions

44func csrf_b64url_encode(data: *u8, n: i64, out: *u8) -> i64
called by 1: csrf_token_new calls 1: b64_encode
64func csrf_b64url_decode(chars: *u8, n: i64, out: *u8) -> i64
called by 1: csrf_token_verify calls 1: b64_decode
84func csrf_token_new(session_id: *u8, session_id_len: i64,
107func csrf_token_verify(token: *u8, token_len: i64,
146func main() -> i64