code wiki / _hdl_build / nx_studio_tiers_gate.nx

nx_studio_tiers_gate.nx source

↩ module page · 110 lines · 8581 B

1// nx_studio_tiers_gate.nx -- gate for the CAPABILITY-ADAPTIVE WYSIWYG (nx_studio_tiers). Proves the operator's 2// design: a user picks the editing tier that matches their capability (GUIDED / BLOCK / FREE-FORM), but every 3// tier funnels through the ONE governed sovereign engine, so governance is TIER-INVARIANT: 4// * each tier's input normalizes + builds a real governed page (positive) 5// * a hostile field is ESCAPED whether typed in GUIDED or FREE-FORM (injection-safety invariant) 6// * an incomplete BLOCK doc is REFUSED (publish-gate invariant) 7// * the GUIDED tier is NOVICE-SAFE BY CONSTRUCTION: even a single answer (just a brand) yields a complete, 8// publishable page -- a novice cannot produce a broken site 9// * FREE-FORM layout-only `place|` lines never leak into the built HTML 10// * a tier-authored doc rebuilds byte-identical (portability invariant) 11// Positive AND negative controls. Appends a CMSGATE row to knowledge/status/cms_gate.log on all-pass. 12// license_tier: ORIGINAL 13import "nx_studio_tiers.nx" 14import "nx_sitegen_studio.nx" 15import "nx_syscalls.nx" 16 17func tw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 18func tnum(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(1,bb,k); return 0 } 19func tcat(dst: *u8, off: i64, s: *u8) -> i64 { var o: i64=off; var k: i64=0; while s[k]!=(0 as u8){dst[o]=s[k];o=o+1;k=k+1} return o } 20func tcatnum(dst: *u8, off: i64, v: i64) -> i64 { var o: i64=off; let t: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m}; var k: i64=0; if m==0{t[0]=48 as u8;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; while i<k{dst[o]=t[k-1-i];o=o+1;i=i+1} return o } 21func trow(id: i64, ok: i64, what: *u8) -> i64 { tw("TIERROW " as *u8); tnum(id); tw(" " as *u8); if ok==1 { tw("PASS " as *u8) } else { tw("FAIL " as *u8) } tw(what); tw("\n" as *u8); return ok } 22func tlen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 23func tcontains(hay: *u8, hn: i64, needle: *u8) -> i64 { 24 var nl: i64=0; while needle[nl]!=(0 as u8){nl=nl+1} 25 if nl==0 { return 1 } if nl>hn { return 0 } 26 let last: i64=hn-nl; var i: i64=0 27 while i<=last { var j: i64=0; var hit: i64=1; while j<nl { if (hay[i+j] as i64)!=(needle[j] as i64){hit=0;j=nl} if hit==1{j=j+1} } if hit==1 { return 1 } i=i+1 } 28 return 0 29} 30func teq(a: *u8, an: i64, b: *u8, bn: i64) -> i64 { if an!=bn { return 0 } var i: i64=0; while i<an { if (a[i] as i64)!=(b[i] as i64){return 0} i=i+1 } return 1 } 31 32// normalize a tier input then build it through the governed engine; return built length (0 = refused), out filled. 33func tbuild(tier: i64, input: *u8, site: *u8, scap: i64, out: *u8, ocap: i64) -> i64 { 34 let sn: i64 = tier_normalize(tier, input, tlen(input), site, scap) 35 return se_build_site(site, sn, out, ocap) 36} 37 38func main() -> i64 { 39 let site: *u8 = sys_mmap(65536) 40 let out: *u8 = sys_mmap(262144) 41 let out2: *u8 = sys_mmap(262144) 42 43 var rows: i64 = 0 44 var pass: i64 = 0 45 var ok: i64 = 0 46 47 // ---- inputs, one per capability tier ---- 48 let guided_good: *u8 = "brand|BrandCo\ncta|Free consult\ncta_href|#c\nheadline|We help Utah families\nsub|Talk to an attorney today\nsearch|/search\ncard_title|Family Law\ncard_body|Divorce and custody.\nstep1|Tell us what happened\nstep2|We review and call back\ntrust|Utah State Bar\nfooter|BrandCo, PLLC\n" as *u8 49 let block_good: *u8 = "title|Pet Care\nheader|PetCo|Book now|#b\nhero|Happy healthy pets|We care for your pets|Book a visit|#b\nsearch|/find|Find...\ncards|Services\ncard|Grooming|Full grooming.\nsteps|Easy steps\nstep|Call us\nstep|Drop off\ntrust|\nsig|Licensed vets\nfooter|PetCo Inc\n" as *u8 50 let free_good: *u8 = "title|Free\nplace|0|10|20\nheader|FreeCo|Go|#g\nplace|1|0|80\nhero|Headline FF|Sub FF|Go|#g\nsearch|/s|q\ncards|C\ncard|Card FF|body text\nsteps|S\nstep|one\nstep|two\ntrust|\nsig|T\nfooter|FreeCo\n" as *u8 51 52 // R0: GUIDED tier -> a real governed page 53 let l0: i64 = tbuild(TIER_GUIDED, guided_good, site, 65535, out, 262143) 54 ok = 0; if l0 > 0 { if tcontains(out, l0, "BrandCo" as *u8)==1 { if tcontains(out, l0, "We help Utah families" as *u8)==1 { ok=1 } } } 55 rows=rows+1; pass=pass+trow(0, ok, "GUIDED tier (novice answers) -> a real governed page builds" as *u8) 56 57 // R1: BLOCK tier -> a real governed page 58 let l1: i64 = tbuild(TIER_BLOCK, block_good, site, 65535, out, 262143) 59 ok = 0; if l1 > 0 { if tcontains(out, l1, "Grooming" as *u8)==1 { ok=1 } } 60 rows=rows+1; pass=pass+trow(1, ok, "BLOCK tier (.site) -> a real governed page builds" as *u8) 61 62 // R2: FREE-FORM tier -> governed page; layout-only place| lines stripped (never leak) 63 let l2: i64 = tbuild(TIER_FREEFORM, free_good, site, 65535, out, 262143) 64 ok = 0; if l2 > 0 { if tcontains(out, l2, "Headline FF" as *u8)==1 { if tcontains(out, l2, "place|" as *u8)==0 { if tcontains(out, l2, "|10|20" as *u8)==0 { ok=1 } } } } 65 rows=rows+1; pass=pass+trow(2, ok, "FREE-FORM tier -> governed page; place| layout lines never leak" as *u8) 66 67 // R3 (NEG, invariant): hostile field in GUIDED -> escaped 68 let guided_mal: *u8 = "brand|<script>alert(1)</script>\ncta|C\ncta_href|#c\nheadline|H\nsub|S\nsearch|/s\ncard_title|A\ncard_body|b\nstep1|1\nstep2|2\ntrust|T\nfooter|F\n" as *u8 69 let l3: i64 = tbuild(TIER_GUIDED, guided_mal, site, 65535, out, 262143) 70 ok = 0; if l3 > 0 { if tcontains(out, l3, "<script>alert(1)" as *u8)==0 { if tcontains(out, l3, "&lt;script&gt;alert(1)" as *u8)==1 { ok=1 } } } 71 rows=rows+1; pass=pass+trow(3, ok, "INVARIANT: hostile field in GUIDED tier is escaped (no raw <script>)" as *u8) 72 73 // R4 (NEG, invariant): hostile field in FREE-FORM -> escaped 74 let free_mal: *u8 = "title|X\nplace|0|0|0\nheader|<script>alert(7)</script>|G|#g\nhero|H|S|G|#g\nsearch|/s|q\ncards|C\ncard|A|b\nsteps|S\nstep|1\nstep|2\ntrust|\nsig|T\nfooter|F\n" as *u8 75 let l4: i64 = tbuild(TIER_FREEFORM, free_mal, site, 65535, out, 262143) 76 ok = 0; if l4 > 0 { if tcontains(out, l4, "<script>alert(7)" as *u8)==0 { if tcontains(out, l4, "&lt;script&gt;alert(7)" as *u8)==1 { ok=1 } } } 77 rows=rows+1; pass=pass+trow(4, ok, "INVARIANT: hostile field in FREE-FORM tier is escaped (no raw <script>)" as *u8) 78 79 // R5 (NEG, invariant): incomplete BLOCK doc -> REFUSED by the publish-gate 80 let block_bad: *u8 = "footer|just a footer\n" as *u8 81 let l5: i64 = tbuild(TIER_BLOCK, block_bad, site, 65535, out, 262143) 82 ok = 0; if l5 == 0 { ok=1 } 83 rows=rows+1; pass=pass+trow(5, ok, "INVARIANT: incomplete BLOCK doc REFUSED (publish-gate holds per-tier)" as *u8) 84 85 // R6 (novice-safe): a SINGLE guided answer (just a brand) still yields a complete publishable page 86 let guided_sparse: *u8 = "brand|Solo Shop\n" as *u8 87 let l6: i64 = tbuild(TIER_GUIDED, guided_sparse, site, 65535, out, 262143) 88 ok = 0; if l6 > 0 { if tcontains(out, l6, "Solo Shop" as *u8)==1 { ok=1 } } 89 rows=rows+1; pass=pass+trow(6, ok, "NOVICE-SAFE: just a brand answer -> a complete publishable page (cannot make a broken site)" as *u8) 90 91 // R7 (portability invariant): a tier-authored doc rebuilds byte-identical 92 let lA: i64 = tbuild(TIER_GUIDED, guided_good, site, 65535, out, 262143) 93 let lB: i64 = tbuild(TIER_GUIDED, guided_good, site, 65535, out2, 262143) 94 ok = teq(out, lA, out2, lB) 95 rows=rows+1; pass=pass+trow(7, ok, "INVARIANT: tier-authored doc rebuilds byte-identical (portable, deterministic)" as *u8) 96 97 tw("NX-STUDIO-TIERS-GATE rows=" as *u8); tnum(rows); tw(" pass=" as *u8); tnum(pass); tw("\n" as *u8) 98 if pass == rows { 99 let line: *u8 = sys_mmap(256) 100 var off: i64 = tcat(line, 0, "CMSGATE row=nx_studio_tiers capability-adaptive-editor rows=" as *u8) 101 off = tcatnum(line, off, rows); off = tcat(line, off, " pass=" as *u8); off = tcatnum(line, off, pass) 102 off = tcat(line, off, " verdict=PASS (governance tier-invariant: GUIDED/BLOCK/FREE-FORM)\n" as *u8) 103 let gf: i64 = sys_openat_append("knowledge/status/cms_gate.log" as *u8, 0x1a4) 104 if gf >= 0 { sys_write(gf, line, off); sys_close(gf) } 105 tw("NX-STUDIO-TIERS-GATE verdict=PASS -- capability-adaptive editor, governance invariant across tiers\n" as *u8) 106 sys_exit(0); return 0 107 } 108 tw("NX-STUDIO-TIERS-GATE verdict=FAIL -- NOT recorded (no fake-green)\n" as *u8) 109 sys_exit(1); return 1 110}