nx_signed_cookie.nx
buildroot/runtime/nx_signed_cookie.nx
about
signed_cookie.nx -- HMAC-signed tamper-evident cookies.
Browser cookies are stored client-side and editable by the user.
For session state you typically want the user to HOLD the state
but NOT modify it. Signed cookies solve this:
cookie = value || \".\" || base64url(HMAC-SHA-256(key, value))
On read: split at the last dot, verify HMAC with secret key,
reject if tampered.
Same idea as Rails / Django signed cookies + Express
cookie-parser's signed mode + the session cookie in tens of
thousands of production Python apps.
Composes hmac.nx (HMAC-SHA-256) + base64.nx (for URL-safe
signature bytes) + ct.nx (constant-time compare).
Invariants:
SC1 Only the PAYLOAD is signed -- key isn't revealed by
cookie value. Tampering changes MAC -> rejected.
SC2 Verification uses ct_memcmp to eliminate timing leaks.
SC3 Base64url without padding keeps cookie URL-safe +
single-valued (compatible with cookie attribute grammar).
SC4 No expiry here -- cookie's own Max-Age / Expires
attribute handles that. Replay-attack defence requires
caller to include a timestamp in the payload and enforce
bounds on read.
dependencies 4 imports · 1 importers
imports: nx_syscalls.nxnx_hmac.nxnx_base64.nxnx_ct.nx
imported by: nx_cap_token.nx
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| none |
consts
| 41 | const SC_ERR_SHORT: i64 = -1 |
| 42 | const SC_ERR_FORMAT: i64 = -2 |
| 43 | const SC_ERR_MAC: i64 = -3 |
functions
| 47 | func sc_sign_bytes(key: *u8, key_len: i64, called by 3: capt_mac_oksigned_cookie_signsigned_cookie_verify calls 3: sys_mmaphmac_sha256b64_encode |
| 73 | func signed_cookie_sign(value: *u8, value_len: i64, |
| 92 | func signed_cookie_verify(cookie: *u8, n: i64, |
| 126 | func main() -> i64 |