code wiki / (root) / nx_mcp_route_json_canonical_gate_t160.nx

nx_mcp_route_json_canonical_gate_t160.nx source

↩ module page · 233 lines · 16510 B

1// nx_mcp_route_auth_read_gate_t1.nx -- Validates and configures route authorization policies for the Nishi sovereign ecosystem. 2import "nx_mcp_route.nx" 3import "nx_gate_verdict.nx" 4 5func mrg_case(plan: *i64, c: *i64, label: *u8, body: *u8, expected: i64, argc: i64, argv: *i64) -> i64 { 6 return gv_plan_check(plan, label, mr_select(body, mr_len(body), argc, argv, 1, 0) == expected, c) 7} 8 9 10func mrg_hex_buffer(hex:*u8,n:*i64)->*u8 { 11 let hn:i64=mr_len(hex);*n=hn/2;let out:*u8=sys_mmap_shared(*n+1) 12 if (out as i64)<=0{return 0 as *u8};var i:i64=0 13 while i<*n{out[i]=(jx_hexdig(hex,hn,i*2)*16+jx_hexdig(hex,hn,i*2+1)) as u8;i=i+1};out[*n]=0;return out 14} 15func mrg_span_case(p:*i64,c:*i64,label:*u8,input:*u8,cap:i64,expected:*u8,want:i64)->i64 { 16 var n:i64=0;var en:i64=0;let raw:*u8=mrg_hex_buffer(input,&n);let exp:*u8=mrg_hex_buffer(expected,&en) 17 var extent:i64=cap;if extent<0{extent=0};extent=extent+2 18 let out:*u8=sys_mmap_shared(extent);var ok:i64=1 19 if (raw as i64)<=0||(exp as i64)<=0||(out as i64)<=0{ok=0}else{ 20 var i:i64=0;while i<extent{out[i]=165 as u8;i=i+1} 21 let got:i64=jx_decode_span(raw,n,out+1,cap) 22 if got!=want{ok=0} 23 if out[0]!=(165 as u8)||out[extent-1]!=(165 as u8){ok=0} 24 if got>=0{ 25 if got!=en{ok=0}else{i=0;while i<en{if out[i+1]!=exp[i]{ok=0};i=i+1}} 26 if got>=cap{ok=0}else{if out[got+1]!=(0 as u8){ok=0}} 27 } 28 } 29 if (raw as i64)>0{if sys_munmap(raw,n+1)!=0{ok=0}};if (exp as i64)>0{if sys_munmap(exp,en+1)!=0{ok=0}} 30 if (out as i64)>0{if sys_munmap(out,extent)!=0{ok=0}} 31 return gv_plan_check(p,label,ok,c) 32} 33 34func tug_original_ctl(buf: *u8, n: i64, vstart: i64, out: *u8, outcap: i64, keep_ctl: i64) -> i64 { 35 var i: i64 = vstart 36 var o: i64 = 0 37 while i < n { 38 let c: i64 = buf[i] as i64 39 if c == 92 { // backslash escape 40 if i + 1 < n { 41 var e: i64 = buf[i + 1] as i64 42 if e == 117 { // \uXXXX 43 var cp: i64 = jx_hex4(buf, n, i + 2) 44 if cp < 0 { 45 // malformed escape: emit nothing for it and step past the 'u' rather than 46 // re-emitting the digits as text (that is exactly the old corruption). 47 i = i + 2 48 } else { 49 var adv: i64 = 6 50 if cp >= 55296 { if cp < 56320 { // high surrogate D800-DBFF 51 if i + 7 < n { if (buf[i + 6] as i64) == 92 { if (buf[i + 7] as i64) == 117 { 52 let lo: i64 = jx_hex4(buf, n, i + 8) 53 if lo >= 56320 { if lo < 57344 { 54 cp = 65536 + ((cp - 55296) * 1024) + (lo - 56320) 55 adv = 12 56 } } 57 } } } 58 } } 59 if keep_ctl == 0 { 60 if cp == 9 { cp = 32 } 61 if cp == 10 { cp = 32 } 62 if cp == 13 { cp = 32 } 63 } 64 o = jx_emit_utf8(out, o, outcap, cp) 65 i = i + adv 66 } 67 } else { 68 if keep_ctl == 1 { 69 if e == 116 { e = 9 } // t -> TAB 70 if e == 110 { e = 10 } // n -> LF 71 if e == 114 { e = 13 } // r -> CR 72 if e == 98 { e = 8 } // b -> BS 73 if e == 102 { e = 12 } // f -> FF 74 } else { 75 if e == 116 { e = 32 } // t -> space 76 if e == 110 { e = 32 } // n -> space 77 if e == 114 { e = 32 } // r -> space 78 } 79 if o < outcap - 1 { out[o] = e as u8; o = o + 1 } 80 i = i + 2 81 } 82 } else { i = i + 1 } 83 } else { 84 if c == 34 { out[o] = 0 as u8; return i + 1 } // closing quote 85 var cc: i64 = c 86 if keep_ctl == 0 { 87 if cc == 9 { cc = 32 } 88 if cc == 10 { cc = 32 } 89 if cc == 13 { cc = 32 } 90 } 91 if o < outcap - 1 { out[o] = cc as u8; o = o + 1 } 92 i = i + 1 93 } 94 } 95 out[o] = 0 as u8 96 return 0 - 1 97} 98 99func tug_legacy(label: *u8, src: *u8, cap: i64, ctr: *i64, plan:*i64) -> i64 { 100 let a: *u8 = sys_mmap(cap + 8); let b: *u8 = sys_mmap(cap + 8) 101 var mode: i64 = 0; var ok: i64 = 1 102 while mode <= 1 { 103 var j: i64 = 0 104 while j < cap + 8 { a[j] = 85 as u8; b[j] = 85 as u8; j = j + 1 } 105 let before: i64 = tug_original_ctl(src, jx_len(src), 0, a, cap, mode) 106 let after: i64 = jx_copy_str_ctl(src, jx_len(src), 0, b, cap, mode) 107 if before != after { ok = 0 } 108 j = 0 109 while j < cap + 8 { if a[j] != b[j] { ok = 0 } j = j + 1 } 110 mode = mode + 1 111 } 112 gv_plan_check(plan,label,ok,ctr); return ok 113} 114 115 116func main() -> i64 { 117 let argc: i64 = 3 118 let argv: *i64 = sys_mmap(32) as *i64 119 argv[0] = "route-gate" as *u8 as i64 120 argv[1] = "nx_mgmt" as *u8 as i64 121 argv[2] = "admin.cap" as *u8 as i64 122 if argc != 3 { return 2 } 123 if mr_config(argc, argv, 1) != 1 { return 2 } 124 let c: *i64 = gv_ctr() 125 let p: *i64 = gv_plan_new("management\nordinary\ninitialize\nnested-decoy\nduplicate-method\nduplicate-params\nduplicate-name\nescaped-key\nmissing-name\nnonstring-name\ntrailing-data\narray-root\nodd-config\nfour explicit read routes valid\nservices selects scoped file\nexisting grant remains on default\nduplicate route refused\narchive and accounting route configuration valid\nnx_wfcost selects scoped credential\nnx_bundle_ingest selects scoped credential\nnx_bundle_ingest_gate selects scoped credential\nascii\nascii-capacity-exact\nempty\nzero-capacity\nnegative-capacity\nescaped-id\nescaped-quote\nescaped-controls\nescaped-slash\nescaped-nul\nbmp\nbmp-capacity\nsurrogate-pair\nsurrogate-capacity\nhighest-scalar\nisolated-high\nisolated-low\nhigh-wrong-low\ntruncated-pair\ntruncated-hex\nbad-hex\nunknown-escape\ntrailing-backslash\nraw-quote\nraw-newline\nraw-nul\nraw-utf8\nraw-utf8-capacity\ncontinuation\noverlong-two\noverlong-three\noverlong-four\nraw-surrogate\nout-of-range\ntruncated-two\ntruncated-three\ntruncated-four\nbad-continuation\nescaped-response-id\nunicode-response-id\nnul-response-id\nnul-distinct-response-id\nid-type-preserved\ndifferent-id\nduplicate-response-id\nmalformed-response-surrogate\nmissing-response-id\nlarge-integer-id\ndistinct-large-integer-id\nlegacy ASCII output/cursor\nlegacy unknown escape behavior\nlegacy escaped controls behavior\nlegacy raw controls behavior\nlegacy Unicode BMP behavior\nlegacy surrogate pair behavior\nlegacy lone surrogate behavior\nlegacy malformed Unicode behavior\nlegacy output truncation behavior\nlegacy Unicode truncation behavior\nlegacy trailing backslash behavior\nlegacy unterminated behavior\nlegacy minimum capacity behavior\nargv-unicode-preserved\nargv-nul-still-refused\nargv-capacity-still-refused\ndecoder-null-boundary\nlegacy-test-workspace-released\n" as *u8) 126 mrg_case(p,c,"management" as *u8,"{\"method\":\"tools/call\",\"params\":{\"name\":\"nx_mgmt\"}}" as *u8,2,argc,argv) 127 mrg_case(p,c,"ordinary" as *u8,"{\"method\":\"tools/call\",\"params\":{\"name\":\"nx_fs\"}}" as *u8,0,argc,argv) 128 mrg_case(p,c,"initialize" as *u8,"{\"method\":\"initialize\",\"params\":{\"name\":\"nx_mgmt\"}}" as *u8,0,argc,argv) 129 mrg_case(p,c,"nested-decoy" as *u8,"{\"method\":\"tools/call\",\"params\":{\"name\":\"nx_fs\",\"arguments\":{\"name\":\"nx_mgmt\"}}}" as *u8,0,argc,argv) 130 mrg_case(p,c,"duplicate-method" as *u8,"{\"method\":\"tools/call\",\"method\":\"initialize\"}" as *u8,-1,argc,argv) 131 mrg_case(p,c,"duplicate-params" as *u8,"{\"method\":\"tools/call\",\"params\":{},\"params\":{\"name\":\"nx_mgmt\"}}" as *u8,-1,argc,argv) 132 mrg_case(p,c,"duplicate-name" as *u8,"{\"method\":\"tools/call\",\"params\":{\"name\":\"nx_mgmt\",\"name\":\"nx_fs\"}}" as *u8,-1,argc,argv) 133 mrg_case(p,c,"escaped-key" as *u8,"{\"method\":\"tools/call\",\"params\":{\"na\\u006de\":\"nx_mgmt\"}}" as *u8,-1,argc,argv) 134 mrg_case(p,c,"missing-name" as *u8,"{\"method\":\"tools/call\",\"params\":{}}" as *u8,-1,argc,argv) 135 mrg_case(p,c,"nonstring-name" as *u8,"{\"method\":\"tools/call\",\"params\":{\"name\":7}}" as *u8,-1,argc,argv) 136 mrg_case(p,c,"trailing-data" as *u8,"{\"method\":\"tools/call\",\"params\":{\"name\":\"nx_mgmt\"}} {}" as *u8,-1,argc,argv) 137 mrg_case(p,c,"array-root" as *u8,"[]" as *u8,-1,argc,argv) 138 gv_plan_check(p,"odd-config" as *u8,mr_config(argc - 1,argv,1)==0,c) 139 let routes: *i64 = sys_mmap(80) as *i64 140 routes[0] = "default.cap" as *u8 as i64 141 routes[1] = "nx_services" as *u8 as i64; routes[2] = "read.cap" as *u8 as i64 142 routes[3] = "nishi_search" as *u8 as i64; routes[4] = "read.cap" as *u8 as i64 143 routes[5] = "nishi_doc" as *u8 as i64; routes[6] = "read.cap" as *u8 as i64 144 routes[7] = "nx_status" as *u8 as i64; routes[8] = "read.cap" as *u8 as i64 145 gv_plan_check(p,"four explicit read routes valid",mr_config(9,routes,1)==1,c) 146 let service: *u8 = "{\"method\":\"tools/call\",\"params\":{\"name\":\"nx_services\"}}" as *u8 147 gv_plan_check(p,"services selects scoped file",mr_select(service,mr_len(service),9,routes,1,0)==2,c) 148 let fsreq: *u8 = "{\"method\":\"tools/call\",\"params\":{\"name\":\"nx_fs\"}}" as *u8 149 gv_plan_check(p,"existing grant remains on default",mr_select(fsreq,mr_len(fsreq),9,routes,1,0)==0,c) 150 routes[7] = "nx_services" as *u8 as i64 151 gv_plan_check(p,"duplicate route refused",mr_config(9,routes,1)==0,c) 152 153 let scoped: *i64 = sys_mmap(64) as *i64 154 scoped[0] = "default.cap" as *u8 as i64 155 scoped[1] = "nx_wfcost" as *u8 as i64; scoped[2] = "scoped.cap" as *u8 as i64 156 scoped[3] = "nx_bundle_ingest" as *u8 as i64; scoped[4] = "scoped.cap" as *u8 as i64 157 scoped[5] = "nx_bundle_ingest_gate" as *u8 as i64; scoped[6] = "scoped.cap" as *u8 as i64 158 gv_plan_check(p,"archive and accounting route configuration valid",mr_config(7,scoped,1)==1,c) 159 gv_plan_check(p,"nx_wfcost selects scoped credential",mr_select("{\"method\":\"tools/call\",\"params\":{\"name\":\"nx_wfcost\"}}" as *u8,53,7,scoped,1,0)==2,c) 160 gv_plan_check(p,"nx_bundle_ingest selects scoped credential",mr_select("{\"method\":\"tools/call\",\"params\":{\"name\":\"nx_bundle_ingest\"}}" as *u8,60,7,scoped,1,0)==4,c) 161 gv_plan_check(p,"nx_bundle_ingest_gate selects scoped credential",mr_select("{\"method\":\"tools/call\",\"params\":{\"name\":\"nx_bundle_ingest_gate\"}}" as *u8,65,7,scoped,1,0)==6,c) 162 163 mrg_span_case(p,c,"ascii","616263",4,"616263",3) 164 mrg_span_case(p,c,"ascii-capacity-exact","616263",3,"",-1) 165 mrg_span_case(p,c,"empty","",1,"",0) 166 mrg_span_case(p,c,"zero-capacity","",0,"",-1) 167 mrg_span_case(p,c,"negative-capacity","",-1,"",-1) 168 mrg_span_case(p,c,"escaped-id","615c753030363263",4,"616263",3) 169 mrg_span_case(p,c,"escaped-quote","5c22",2,"22",1) 170 mrg_span_case(p,c,"escaped-controls","5c625c665c6e5c725c74",6,"080c0a0d09",5) 171 mrg_span_case(p,c,"escaped-slash","5c2f5c5c",3,"2f5c",2) 172 mrg_span_case(p,c,"escaped-nul","615c753030303062",4,"610062",3) 173 mrg_span_case(p,c,"bmp","5c7532306163",4,"e282ac",3) 174 mrg_span_case(p,c,"bmp-capacity","5c7532306163",3,"",-1) 175 mrg_span_case(p,c,"surrogate-pair","5c75643833645c7564653030",5,"f09f9880",4) 176 mrg_span_case(p,c,"surrogate-capacity","5c75643833645c7564653030",4,"",-1) 177 mrg_span_case(p,c,"highest-scalar","5c75646266665c7564666666",5,"f48fbfbf",4) 178 mrg_span_case(p,c,"isolated-high","5c7564383030",16,"",-1) 179 mrg_span_case(p,c,"isolated-low","5c7564633030",16,"",-1) 180 mrg_span_case(p,c,"high-wrong-low","5c75643830305c7530303431",16,"",-1) 181 mrg_span_case(p,c,"truncated-pair","5c75643830305c75",16,"",-1) 182 mrg_span_case(p,c,"truncated-hex","5c75313233",16,"",-1) 183 mrg_span_case(p,c,"bad-hex","5c753132787a",16,"",-1) 184 mrg_span_case(p,c,"unknown-escape","5c71",16,"",-1) 185 mrg_span_case(p,c,"trailing-backslash","785c",16,"",-1) 186 mrg_span_case(p,c,"raw-quote","612262",16,"",-1) 187 mrg_span_case(p,c,"raw-newline","610a62",16,"",-1) 188 mrg_span_case(p,c,"raw-nul","610062",16,"",-1) 189 mrg_span_case(p,c,"raw-utf8","c3a9e282acf09f9880",10,"c3a9e282acf09f9880",9) 190 mrg_span_case(p,c,"raw-utf8-capacity","c3a9e282acf09f9880",9,"",-1) 191 mrg_span_case(p,c,"continuation","80",16,"",-1) 192 mrg_span_case(p,c,"overlong-two","c0af",16,"",-1) 193 mrg_span_case(p,c,"overlong-three","e08080",16,"",-1) 194 mrg_span_case(p,c,"overlong-four","f0808080",16,"",-1) 195 mrg_span_case(p,c,"raw-surrogate","eda080",16,"",-1) 196 mrg_span_case(p,c,"out-of-range","f4908080",16,"",-1) 197 mrg_span_case(p,c,"truncated-two","c2",16,"",-1) 198 mrg_span_case(p,c,"truncated-three","e282",16,"",-1) 199 mrg_span_case(p,c,"truncated-four","f09f98",16,"",-1) 200 mrg_span_case(p,c,"bad-continuation","e241ac",16,"",-1) 201 gv_plan_check(p,"escaped-response-id",mr_response_matches("{\"id\":\"abc\"}",12,"{\"id\":\"a\\u0062c\"}",17)==1,c) 202 gv_plan_check(p,"unicode-response-id",mr_response_matches("{\"id\":\"😀\"}",13,"{\"id\":\"\\ud83d\\ude00\"}",21)==1,c) 203 gv_plan_check(p,"nul-response-id",mr_response_matches("{\"id\":\"a\\u0000b\"}",17,"{\"id\":\"a\\u0000b\"}",17)==1,c) 204 gv_plan_check(p,"nul-distinct-response-id",mr_response_matches("{\"id\":\"a\\u0000b\"}",17,"{\"id\":\"a\"}",10)==0,c) 205 gv_plan_check(p,"id-type-preserved",mr_response_matches("{\"id\":7}",8,"{\"id\":\"7\"}",10)==0,c) 206 gv_plan_check(p,"different-id",mr_response_matches("{\"id\":\"one\"}",12,"{\"id\":\"two\"}",12)==0,c) 207 gv_plan_check(p,"duplicate-response-id",mr_response_matches("{\"id\":\"one\"}",12,"{\"id\":\"one\",\"id\":\"one\"}",23)==0,c) 208 gv_plan_check(p,"malformed-response-surrogate",mr_response_matches("{\"id\":\"x\"}",10,"{\"id\":\"\\ud800\"}",15)==0,c) 209 gv_plan_check(p,"missing-response-id",mr_response_matches("{\"id\":\"x\"}",10,"{\"result\":1}",12)==0,c) 210 gv_plan_check(p,"large-integer-id",mr_response_matches("{\"id\":9223372036854775808}",26,"{\"id\":9223372036854775808}",26)==1,c) 211 gv_plan_check(p,"distinct-large-integer-id",mr_response_matches("{\"id\":9223372036854775808}",26,"{\"id\":9223372036854775809}",26)==0,c) 212 tug_legacy("legacy ASCII output/cursor" as *u8, "abc\"" as *u8, 16, c,p) 213 tug_legacy("legacy unknown escape behavior" as *u8, "\\q\"" as *u8, 16, c,p) 214 tug_legacy("legacy escaped controls behavior" as *u8, "\\b\\f\\n\\r\\t\"" as *u8, 16, c,p) 215 tug_legacy("legacy raw controls behavior" as *u8, "a\nb\rc\td\"" as *u8, 16, c,p) 216 tug_legacy("legacy Unicode BMP behavior" as *u8, "\\u00e9\\u20ac\"" as *u8, 16, c,p) 217 tug_legacy("legacy surrogate pair behavior" as *u8, "\\ud83d\\ude00\"" as *u8, 16, c,p) 218 tug_legacy("legacy lone surrogate behavior" as *u8, "\\ud800\"" as *u8, 16, c,p) 219 tug_legacy("legacy malformed Unicode behavior" as *u8, "\\u00xz\"" as *u8, 16, c,p) 220 tug_legacy("legacy output truncation behavior" as *u8, "abcdef\"" as *u8, 3, c,p) 221 tug_legacy("legacy Unicode truncation behavior" as *u8, "\\ud83d\\ude00x\"" as *u8, 3, c,p) 222 tug_legacy("legacy trailing backslash behavior" as *u8, "abc\\" as *u8, 16, c,p) 223 tug_legacy("legacy unterminated behavior" as *u8, "abc" as *u8, 16, c,p) 224 tug_legacy("legacy minimum capacity behavior" as *u8, "abc\"" as *u8, 1, c,p) 225 let legacy_out:*u8=sys_mmap_shared(32) 226 gv_plan_check(p,"argv-unicode-preserved",jx_copy_str_argv("\\u20ac\"",7,0,legacy_out,4)==7&&legacy_out[0]==(226 as u8)&&legacy_out[1]==(130 as u8)&&legacy_out[2]==(172 as u8)&&legacy_out[3]==(0 as u8),c) 227 gv_plan_check(p,"argv-nul-still-refused",jx_copy_str_argv("\\u0000\"",7,0,legacy_out,32)<0,c) 228 gv_plan_check(p,"argv-capacity-still-refused",jx_copy_str_argv("abc\"",4,0,legacy_out,3)<0,c) 229 gv_plan_check(p,"decoder-null-boundary",jx_decode_span(0 as *u8,1,legacy_out,32)<0&&jx_decode_span("",0,0 as *u8,1)<0&&jx_decode_span("",-1,legacy_out,32)<0,c) 230 gv_plan_check(p,"legacy-test-workspace-released",sys_munmap(legacy_out,32)==0,c) 231 gv_plan_finish(p,c) 232 return gv_verdict("MCP-ROUTE-GATE" as *u8,c,"Routing selection only; no credential contents or network calls." as *u8) 233}