code wiki / hub / nx_no_cookie_session_test.nx

nx_no_cookie_session_test.nx source

↩ module page · 54 lines · 2214 B

1// nx_no_cookie_session_test.nx -- 1:1 runtime proof of the no-cookie session 2// auth (mint -> validate -> realm + negative cases). This is the MONITOR-pillar 3// regression for T#ncs-verify-convention: the verify-success check in 4// nx_ncs_validate_token was `!= 0` (wrong: ed25519_verify_full returns 1 on 5// valid), which rejected every valid token AND would accept invalid ones. 6// main() returns 0 on all-pass, else the failing assertion number. 7import "nx_no_cookie_session.nx" 8 9func main() -> i64 { 10 let priv: *u8 = sys_mmap(32) 11 let pub: *u8 = sys_mmap(32) 12 let uid: *u8 = sys_mmap(32) 13 let realm: *u8 = sys_mmap(32) 14 let realm2: *u8 = sys_mmap(32) 15 let token: *u8 = sys_mmap(152) 16 let outuid: *u8 = sys_mmap(32) 17 18 // Deterministic test key + identity material. 19 var i: i64 = 0 20 while i < 32 { 21 priv[i] = (i + 7) as u8 22 uid[i] = (i + 1) as u8 23 realm[i] = (i + 100) as u8 24 realm2[i] = (i + 200) as u8 25 i = i + 1 26 } 27 28 if ed25519_pub_from_priv(priv, pub) != 0 { return 10 } 29 30 let now: i64 = 1000 31 if nx_ncs_mint_token(priv, uid, realm, now, 900, token) != NX_NCS_OK { return 20 } 32 33 // KEY assertion: a freshly-minted, correctly-signed token must validate OK. 34 // (Pre-fix this returned NX_NCS_VERIFY_FAILED -- the real auth blocker.) 35 if nx_ncs_validate_token(pub, token, now, realm, outuid) != NX_NCS_OK { return 21 } 36 37 // user_id round-trips out of the token. 38 var k: i64 = 0 39 while k < 32 { if outuid[k] != uid[k] { return 22 }; k = k + 1 } 40 41 // Negative: tamper a signed byte -> VERIFY_FAILED (sig checked first). 42 let saved: i64 = token[0] as i64 43 token[0] = (saved + 1) as u8 44 if nx_ncs_validate_token(pub, token, now, realm, outuid) != (0 - NX_NCS_VERIFY_FAILED) { return 30 } 45 token[0] = saved as u8 46 47 // Negative: expired token -> EXPIRED (valid sig, past TTL). 48 if nx_ncs_validate_token(pub, token, now + 10000, realm, outuid) != (0 - NX_NCS_EXPIRED) { return 31 } 49 50 // Negative: wrong realm -> MALFORMED (valid sig, not expired, realm mismatch). 51 if nx_ncs_validate_token(pub, token, now, realm2, outuid) != (0 - NX_NCS_MALFORMED) { return 32 } 52 53 return 0 54}