code wiki / _hdl_build / nx_ad_banner_gate.nx

nx_ad_banner_gate.nx source

↩ module page · 106 lines · 6780 B

1// nx_ad_banner_gate.nx -- GATE for the banner generator. Proves the privacy/security invariants hold BY 2// CONSTRUCTION, each with a negative control (no false green): clean static + clean dynamic, HTML-escaping 3// neutralises an injected <script> sponsor, a javascript:/data: click URI is rejected to '#', the 4// third-party-JS detector actually FIRES on a planted tag, and IAB size is data-driven. Also (re)emits the 5// viewable preview samples to knowledge/staging/ad/ as evidence. Sovereign, no gcc/.sh. license_tier: ORIGINAL 6import "nx_ad_banner.nx" 7import "nx_ad_serve.nx" 8import "nx_syscalls.nx" 9 10func g_p(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 11 12// wrap a banner fragment in a standalone preview doc and write it to fname. 13func g_write_preview(fname: *u8, frag: *u8, flen: i64) -> i64 { 14 let doc: *u8 = sys_mmap(20480) 15 var o: i64 = 0 16 o = as_append(doc, o, "<!doctype html><html><head><meta charset='utf-8'><meta name='viewport' content='width=device-width,initial-scale=1'><title>Nishi banner preview</title></head><body style='margin:0;padding:28px;background:#eef2f7;display:flex;justify-content:center'>" as *u8) 17 var i: i64 = 0 18 while i < flen { doc[o] = frag[i]; o = o + 1; i = i + 1 } 19 o = as_append(doc, o, "</body></html>" as *u8) 20 let fd: i64 = sys_openat_wr(fname, 420) 21 if fd >= 0 { sys_write(fd, doc, o); sys_close(fd) } 22 return 0 23} 24 25func main(argc: i64, argv: *i64) -> i64 { 26 g_p("=== nx_ad_banner_gate ===\n" as *u8) 27 let b: *u8 = sys_mmap(16384) 28 var pass: i64 = 0 29 var tot: i64 = 0 30 31 // ---- T1: STATIC sample (300x250, light) -> clean + sized + NO animation. also write preview. 32 let n1: i64 = ab_emit_banner("Andelin & West" as *u8, "Estate planning, done right" as *u8, 33 "Wills, trusts & probate for Utah families." as *u8, "Book a consult" as *u8, 34 "https://andelinwest.com/" as *u8, 300, 250, AB_STATIC, 0, b) 35 g_write_preview("knowledge/staging/ad/banner_static_rect.html" as *u8, b, n1) 36 tot = tot + 1; var ok1: i64 = 1 37 if as_has_thirdparty_js(b, n1) != 0 { ok1 = 0 } 38 if as_contains(b, n1, "width:300px" as *u8) != 1 { ok1 = 0 } 39 if as_contains(b, n1, "@keyframes" as *u8) != 0 { ok1 = 0 } 40 if ok1 == 1 { pass = pass + 1; g_p("PASS T1 static: clean + width:300px + no @keyframes\n" as *u8) } else { g_p("FAIL T1\n" as *u8) } 41 42 // ---- T2: DYNAMIC sample (728x90, dark) -> clean + has CSS @keyframes animation. also write preview. 43 let n2: i64 = ab_emit_banner("Nishi Family" as *u8, "Your media, sovereign" as *u8, 44 "Read, watch & play -- no trackers, ever." as *u8, "Explore" as *u8, 45 "https://nishifamily.com/" as *u8, 728, 90, AB_DYNAMIC, 1, b) 46 g_write_preview("knowledge/staging/ad/banner_dynamic_leaderboard.html" as *u8, b, n2) 47 tot = tot + 1; var ok2: i64 = 1 48 if as_has_thirdparty_js(b, n2) != 0 { ok2 = 0 } 49 if as_contains(b, n2, "@keyframes" as *u8) != 1 { ok2 = 0 } 50 if as_contains(b, n2, "animation:" as *u8) != 1 { ok2 = 0 } 51 if ok2 == 1 { pass = pass + 1; g_p("PASS T2 dynamic: clean + @keyframes + animation\n" as *u8) } else { g_p("FAIL T2\n" as *u8) } 52 53 // ---- (evidence) third warm mobile sample preview 54 let n3: i64 = ab_emit_banner("Local Roasters" as *u8, "Fresh beans, weekly" as *u8, 55 "Single-origin, roasted to order." as *u8, "Shop" as *u8, "https://example.com/coffee" as *u8, 56 320, 50, AB_STATIC, 2, b) 57 g_write_preview("knowledge/staging/ad/banner_static_mobile.html" as *u8, b, n3) 58 59 // ---- T3: ESCAPING neg-control. An injected <script> sponsor must be ESCAPED (no raw "<script"), and 60 // the detector must therefore see a CLEAN banner (the injection is neutralised, not merely undetected). 61 let n4: i64 = ab_emit_banner("<script>alert(1)</script>" as *u8, "H" as *u8, "B" as *u8, "C" as *u8, 62 "https://x.test/" as *u8, 300, 250, AB_STATIC, 0, b) 63 tot = tot + 1; var ok4: i64 = 1 64 if as_contains(b, n4, "<script" as *u8) != 0 { ok4 = 0 } // raw injection MUST be absent 65 if as_contains(b, n4, "&lt;script" as *u8) != 1 { ok4 = 0 } // ... because it was escaped 66 if as_has_thirdparty_js(b, n4) != 0 { ok4 = 0 } // -> clean 67 if ok4 == 1 { pass = pass + 1; g_p("PASS T3 escaping: injected <script> neutralised to &lt;script, clean\n" as *u8) } else { g_p("FAIL T3\n" as *u8) } 68 69 // ---- T4: javascript: click URI neg-control -> neutralised to href='#'. 70 let n5: i64 = ab_emit_banner("Biz" as *u8, "H" as *u8, "B" as *u8, "C" as *u8, 71 "javascript:alert(1)" as *u8, 300, 250, AB_STATIC, 0, b) 72 tot = tot + 1; var ok5: i64 = 1 73 if as_contains(b, n5, "javascript:" as *u8) != 0 { ok5 = 0 } 74 if as_contains(b, n5, "href='#'" as *u8) != 1 { ok5 = 0 } 75 if ok5 == 1 { pass = pass + 1; g_p("PASS T4 link: javascript: URI rejected to href='#'\n" as *u8) } else { g_p("FAIL T4\n" as *u8) } 76 77 // ---- T5: data: click URI neg-control -> also rejected to '#'. 78 let n6: i64 = ab_emit_banner("Biz" as *u8, "H" as *u8, "B" as *u8, "C" as *u8, 79 "data:text/html,<script>x</script>" as *u8, 300, 250, AB_STATIC, 0, b) 80 tot = tot + 1; var ok6: i64 = 1 81 if as_contains(b, n6, "data:text" as *u8) != 0 { ok6 = 0 } 82 if as_contains(b, n6, "href='#'" as *u8) != 1 { ok6 = 0 } 83 if ok6 == 1 { pass = pass + 1; g_p("PASS T5 link: data: URI rejected to href='#'\n" as *u8) } else { g_p("FAIL T5\n" as *u8) } 84 85 // ---- T6: detector ANTI-FALSE-GREEN -> a planted third-party tag MUST trip as_has_thirdparty_js. 86 let dirty: *u8 = "<a><script>evil()</script></a>" as *u8 87 tot = tot + 1 88 if as_has_thirdparty_js(dirty, as_len(dirty)) == 1 { pass = pass + 1; g_p("PASS T6 detector fires on a planted <script> (clean=0 means proven)\n" as *u8) } else { g_p("FAIL T6\n" as *u8) } 89 90 // ---- T7: IAB size is data-driven -> billboard 970 width applied; relative link allowed. 91 let n7: i64 = ab_emit_banner("Biz" as *u8, "H" as *u8, "B" as *u8, "C" as *u8, "/promo" as *u8, 92 970, 250, AB_DYNAMIC, 1, b) 93 tot = tot + 1; var ok7: i64 = 1 94 if as_contains(b, n7, "width:970px" as *u8) != 1 { ok7 = 0 } 95 if as_has_thirdparty_js(b, n7) != 0 { ok7 = 0 } 96 if as_contains(b, n7, "href='/promo'" as *u8) != 1 { ok7 = 0 } 97 if ok7 == 1 { pass = pass + 1; g_p("PASS T7 size: width:970px data-driven + relative link allowed\n" as *u8) } else { g_p("FAIL T7\n" as *u8) } 98 99 g_p("nx_ad_banner_gate pass=" as *u8) 100 let nb: *u8 = sys_mmap(16); let nn: i64 = ab_uitoa(nb, 0, pass); sys_write(1, nb, nn) 101 g_p("/" as *u8) 102 let tb: *u8 = sys_mmap(16); let tn: i64 = ab_uitoa(tb, 0, tot); sys_write(1, tb, tn) 103 if pass == tot { g_p(" verdict=GREEN\n" as *u8); return 0 } 104 g_p(" verdict=RED\n" as *u8) 105 return 1 106}