code wiki / (root) / nx_cap_token_gate.nx

nx_cap_token_gate.nx source

↩ module page · 56 lines · 4115 B

1// nx_cap_token_gate.nx -- proves the capability token is the BEYOND-MCP security primitive: authority-in-the-token, 2// unforgeable (HMAC), least-authority (grants only listed tools), attenuable SUBSET-ONLY (can weaken, never widen), 3// and time-bounded. All in-process (no socket). license_tier: ORIGINAL 4import "nx_cap_token.nx" 5import "nx_gate.nx" 6 7func cg_expect(actual: i64, expected: i64, pass: *i64, tot: *i64, label: *u8) -> i64 { 8 tot[0] = tot[0] + 1 9 if actual == expected { pass[0] = pass[0] + 1; gw(" [PASS] " as *u8) } else { gw(" [FAIL got=" as *u8); gn(actual); gw(" want=" as *u8); gn(expected); gw("] " as *u8) } 10 gw(label); gw("\n" as *u8) 11 return 0 12} 13 14func main() -> i64 { 15 gw("=== nx_cap_token_gate: capability = authority-in-token, unforgeable + attenuable (beyond MCP) ===\n" as *u8) 16 let key: *u8 = "nishi-cap-test-secret-key-v1-0099" as *u8 17 let klen: i64 = capt_slen(key) 18 let now: i64 = 1782000000 19 let pbox: *i64 = sys_mmap(16) as *i64; pbox[0] = 0 20 let tbox: *i64 = sys_mmap(16) as *i64; tbox[0] = 0 21 22 let allow: *u8 = "nx_http_probe,nx_https_get" as *u8 23 let alen: i64 = capt_slen(allow) 24 let tok: *u8 = sys_mmap(1024) 25 let tn: i64 = capt_issue(key, klen, allow, alen, 9999999999, 1, tok, 1024) 26 27 cg_expect(capt_verify(key, klen, tok, tn, "nx_http_probe" as *u8, 13, now), CAPT_OK, pbox, tbox, "T1 grants a listed tool (nx_http_probe)" as *u8) 28 cg_expect(capt_verify(key, klen, tok, tn, "nx_https_get" as *u8, 12, now), CAPT_OK, pbox, tbox, "T2 grants another listed tool (nx_https_get)" as *u8) 29 cg_expect(capt_verify(key, klen, tok, tn, "nx_mgmt_client" as *u8, 14, now), CAPT_DENY_TOOL, pbox, tbox, "T3 DENIES an unlisted tool (least authority -- no confused-deputy)" as *u8) 30 31 // T4 unforgeable: flip a payload byte -> HMAC mismatch 32 let tok2: *u8 = sys_mmap(1024); var c: i64 = 0; while c < tn { tok2[c] = tok[c]; c = c + 1 } tok2[3] = (tok2[3] ^ 1) as u8 33 cg_expect(capt_verify(key, klen, tok2, tn, "nx_http_probe" as *u8, 13, now), CAPT_DENY_MAC, pbox, tbox, "T4 tampered token -> DENIED (unforgeable HMAC)" as *u8) 34 35 // T5 unforgeable: wrong key 36 cg_expect(capt_verify("WRONG-KEY-9999999999999999999999" as *u8, 32, tok, tn, "nx_http_probe" as *u8, 13, now), CAPT_DENY_MAC, pbox, tbox, "T5 wrong key -> DENIED (unforgeable)" as *u8) 37 38 // T6 time-bounded: expired 39 let toke: *u8 = sys_mmap(1024); let tne: i64 = capt_issue(key, klen, allow, alen, 1, 2, toke, 1024) 40 cg_expect(capt_verify(key, klen, toke, tne, "nx_http_probe" as *u8, 13, now), CAPT_DENY_EXP, pbox, tbox, "T6 expired cap -> DENIED" as *u8) 41 42 // T7 attenuation NARROWS: derive a probe-only cap; it grants probe but DENIES the previously-allowed https_get 43 let toka: *u8 = sys_mmap(1024); let tna: i64 = capt_attenuate(key, klen, tok, tn, "nx_http_probe" as *u8, 13, 9999999999, 3, toka, 1024) 44 cg_expect(capt_verify(key, klen, toka, tna, "nx_http_probe" as *u8, 13, now), CAPT_OK, pbox, tbox, "T7a attenuated cap still grants nx_http_probe" as *u8) 45 cg_expect(capt_verify(key, klen, toka, tna, "nx_https_get" as *u8, 12, now), CAPT_DENY_TOOL, pbox, tbox, "T7b attenuated cap DENIES the dropped nx_https_get (narrowed)" as *u8) 46 47 // T8 attenuation CANNOT widen: a probe-only cap cannot be attenuated to also include nx_mgmt_client 48 let tokp: *u8 = sys_mmap(1024); let tnp: i64 = capt_issue(key, klen, "nx_http_probe" as *u8, 13, 9999999999, 4, tokp, 1024) 49 let tokw: *u8 = sys_mmap(1024); let tnw: i64 = capt_attenuate(key, klen, tokp, tnp, "nx_http_probe,nx_mgmt_client" as *u8, 28, 9999999999, 5, tokw, 1024) 50 var t8: i64 = 0; if tnw < 0 { t8 = 1 } 51 cg_expect(t8, 1, pbox, tbox, "T8 attenuation CANNOT widen (subset-only) -> refused" as *u8) 52 53 gw("\n=== nx_cap_token_gate " as *u8); gn(pbox[0]); gw("/" as *u8); gn(tbox[0]); gw(" ===\n" as *u8) 54 if pbox[0] == tbox[0] { gw("CAP-TOKEN GREEN -- unforgeable + least-authority + subset-only attenuation + time-bound (the beyond-MCP invocation-security primitive)\n" as *u8); sys_exit(0); return 0 } 55 gw("CAP-TOKEN RED\n" as *u8); sys_exit(1); return 1 56}