code wiki / (root) / nx_csrf_token.nx

nx_csrf_token.nx

buildroot/runtime/nx_csrf_token.nx

5827 B179 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

nx_syscalls.nx nx_rand.nx nx_hmac.nx nx_base64.nx nx_ct.nx nx_csrf_token.nx

imports: nx_syscalls.nxnx_rand.nxnx_hmac.nxnx_base64.nxnx_ct.nx

imported by: nobody (leaf or entry point)

call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown

main sys_mmap csrf_token_new sys_mmap ↻ rand_bytes rand_open_urandom sys_openat_rd sys_read sys_close 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 sys_mmap ↻ b64_encode b64_enc_char csrf_token_verify sys_mmap ↻ csrf_b64url_decode sys_mmap ↻ b64_decode b64_grab b64_dec_char hmac_sha256 ↻

structs

none

consts

41const CSRF_ERR_FORMAT: i64 = -1
42const CSRF_ERR_MAC: i64 = -2
43const CSRF_RAND_BYTES: i64 = 32
44const CSRF_MAC_BYTES: i64 = 16

functions

48func csrf_b64url_encode(data: *u8, n: i64, out: *u8) -> i64
called by 1: csrf_token_new calls 2: sys_mmapb64_encode
68func csrf_b64url_decode(chars: *u8, n: i64, out: *u8) -> i64
88func csrf_token_new(session_id: *u8, session_id_len: i64,
111func csrf_token_verify(token: *u8, token_len: i64,
150func main() -> i64