code wiki / _hdl_build / nx_clean_serve_gate.nx

nx_clean_serve_gate.nx source

↩ module page · 172 lines · 15429 B

1// nx_clean_serve_gate.nx -- ADVERSARY + CRITIC gate for the ethical clean-serve engine (the win-win-win proof). 2// ADVERSARY: a synthetic hostile page carrying every attack class a malvertising/pirate/adult site throws at a 3// user -- cryptominer, popunder window.open, reverse-tabnab link, meta-refresh malvertising, tracker beacon, 4// inline on*= handler, javascript: URI -- PLUS a benign static banner ad and the real video content. 5// CRITIC: after cs_clean_page, assert (a) EVERY attack is neutralized (nothing executable survives), (b) the 6// benign ad's creative + click SURVIVE (the site still monetizes), (c) the content + native media survive, 7// (d) the safety receipt counts are correct. NEGATIVE CONTROLS: a clean page must report ZERO attacks (no false 8// positive), and a miner-only page must NOT count the miner as a preserved ad. No fabricated greens. ORIGINAL 9import "nx_syscalls.nx" 10import "nx_web_filter.nx" 11import "nx_clean_serve.nx" 12import "nx_gate_verdict.nx" // D001 MIGRATION 2026-09-02: per-tooth gv_check + gv_verdict, so /api/gate_run reads the verdict from the exit code 13 14// case-insensitive "does hay contain needle" (reuses cs_slen/cs_ci_at from the import) 15func g_has(hay: *u8, hl: i64, needle: *u8) -> i64 { 16 let nl: i64 = cs_slen(needle) 17 if nl==0 { return 1 } 18 if nl>hl { return 0 } 19 var i: i64 = 0; let last: i64 = hl-nl 20 while i<=last { if cs_ci_at(hay, hl, i, needle, nl)==1 { return 1 } i=i+1 } 21 return 0 22} 23// report one assertion through the base class (pt is the gv_ctr: declared == executed by construction) 24func g_check(label: *u8, ok: i64, pt: *i64) -> i64 { gv_check(label, ok, pt); return 0 } 25// assert a substring is ABSENT (attack neutralized) 26func g_absent(out: *u8, len: i64, needle: *u8, label: *u8, pt: *i64) -> i64 { 27 var ok: i64 = 0; if g_has(out, len, needle)==0 { ok=1 } 28 g_check(label, ok, pt); return 0 29} 30// assert a substring is PRESENT (content / safe ad preserved) 31func g_present(out: *u8, len: i64, needle: *u8, label: *u8, pt: *i64) -> i64 { 32 var ok: i64 = 0; if g_has(out, len, needle)==1 { ok=1 } 33 g_check(label, ok, pt); return 0 34} 35func g_ge(v: i64, min: i64, label: *u8, pt: *i64) -> i64 { var ok: i64=0; if v>=min { ok=1 } g_check(label, ok, pt); return 0 } 36func g_eq(v: i64, want: i64, label: *u8, pt: *i64) -> i64 { var ok: i64=0; if v==want { ok=1 } g_check(label, ok, pt); return 0 } 37 38func main(argc: i64, argv: *i64) -> i64 { 39 let t: *i64 = wf_new(); wf_seed(t) 40 let out: *u8 = sys_mmap(262144) 41 let pt: *i64 = gv_ctr() 42 43 // ============ ADVERSARY FIXTURE A: the hostile page (every attack class + a benign ad + the content) ======== 44 // (single-quoted attrs so the Nishi string literal stays clean; the transform handles both quote styles) 45 let a: *u8 = "<html><head><meta http-equiv='refresh' content='0;url=https://scam.example/go'><script src='https://coinhive.com/miner.js'></script><script>window.open('https://cdn.example/popunder.js');fetch('https://google-analytics.com/collect?id=1');var m='https://phish.example/you-have-won';</script></head><body><h1>Video Title</h1><video src='https://cdn.example/stream.mp4' controls></video><a href='https://x.example/go' onclick='location=1'>bad</a><a href='https://juicyads.com/click?ad=1'><img src='https://juicyads.com/creative.jpg'></a><img src='https://cdn.example/poster.jpg'><a href='javascript:steal()'>x</a></body></html>" as *u8 46 let alen: i64 = cs_slen(a) 47 let rcA: *i64 = cs_rc_new() 48 let lenA: i64 = cs_clean_page(a, alen, t, out, 262144, rcA) 49 sys_write(1, "TEST A -- hostile page: attacks neutralized, content + safe ad preserved\n" as *u8, 72) 50 // CRITIC (a): nothing executable/attacking survives in the served HTML 51 g_absent(out, lenA, "<script" as *u8, "no <script> survives" as *u8, pt) 52 g_absent(out, lenA, "coinhive" as *u8, "cryptominer removed" as *u8, pt) 53 g_absent(out, lenA, "onclick" as *u8, "inline on*= handler stripped" as *u8, pt) 54 g_absent(out, lenA, "javascript:" as *u8, "javascript: URI neutralized" as *u8, pt) 55 g_absent(out, lenA, "http-equiv" as *u8, "meta-refresh redirect removed" as *u8, pt) 56 g_absent(out, lenA, "popunder" as *u8, "popunder script removed" as *u8, pt) 57 // CRITIC (b): the site still earns -- the benign ad creative + click survive, sanitized 58 g_present(out, lenA, "juicyads.com/creative.jpg" as *u8, "safe ad creative preserved" as *u8, pt) 59 g_present(out, lenA, "juicyads.com/click" as *u8, "safe ad click-through preserved" as *u8, pt) 60 g_present(out, lenA, "rel=\"noopener noreferrer nofollow\"" as *u8, "links hardened (rel=noopener)" as *u8, pt) 61 // CRITIC (c): the content the user came for survives 62 g_present(out, lenA, "<video" as *u8, "native HTML5 video preserved" as *u8, pt) 63 g_present(out, lenA, "stream.mp4" as *u8, "media URL preserved" as *u8, pt) 64 g_present(out, lenA, "poster.jpg" as *u8, "content image preserved" as *u8, pt) 65 // CRITIC (d): the safety receipt counts are honest 66 g_ge(rcA[CR_SCRIPTS], 2, "receipt: >=2 scripts removed" as *u8, pt) 67 g_ge(rcA[CR_HANDLERS], 1, "receipt: >=1 handler stripped" as *u8, pt) 68 g_ge(rcA[CR_REDIRECTS], 2, "receipt: >=2 redirect vectors (meta+js) neutralized" as *u8, pt) 69 g_ge(rcA[CR_MINER], 1, "receipt: miner request counted" as *u8, pt) 70 g_ge(rcA[CR_POPUP], 1, "receipt: popunder request counted" as *u8, pt) 71 g_ge(rcA[CR_MAL], 1, "receipt: malware/scam request counted" as *u8, pt) 72 g_ge(rcA[CR_TRACK], 1, "receipt: tracker request counted" as *u8, pt) 73 g_ge(rcA[CR_ADS_KEPT], 1, "receipt: >=1 safe ad preserved" as *u8, pt) 74 75 // ============ NEGATIVE CONTROL B: a CLEAN page must report ZERO attacks (no false positives) ============ 76 let b: *u8 = "<html><body><h1>News Article</h1><p>Plain readable content, nothing hostile here.</p><a href='https://juicyads.com/click'><img src='https://juicyads.com/banner.jpg'></a><img src='https://cdn.example/photo.jpg'></body></html>" as *u8 77 let blen: i64 = cs_slen(b) 78 let rcB: *i64 = cs_rc_new() 79 let lenB: i64 = cs_clean_page(b, blen, t, out, 262144, rcB) 80 sys_write(1, "TEST B (neg-control) -- clean page: zero false-positive attacks, ad + content kept\n" as *u8, 83) 81 g_eq(rcB[CR_SCRIPTS], 0, "clean page: 0 scripts flagged" as *u8, pt) 82 g_eq(rcB[CR_HANDLERS], 0, "clean page: 0 handlers flagged" as *u8, pt) 83 g_eq(rcB[CR_REDIRECTS], 0, "clean page: 0 redirects flagged" as *u8, pt) 84 g_eq(rcB[CR_BLOCKED], 0, "clean page: 0 attack requests flagged" as *u8, pt) 85 g_ge(rcB[CR_ADS_KEPT], 1, "clean page: safe ad still preserved" as *u8, pt) 86 g_present(out, lenB, "juicyads.com/banner.jpg" as *u8, "clean page: ad creative kept" as *u8, pt) 87 g_present(out, lenB, "photo.jpg" as *u8, "clean page: content image kept" as *u8, pt) 88 89 // ============ NEGATIVE CONTROL C: a miner-only page -- miner is NOT miscounted as a preserved ad ============ 90 let c: *u8 = "<html><body><script src='https://coinhive.com/miner.js'></script><p>hi</p></body></html>" as *u8 91 let clen: i64 = cs_slen(c) 92 let rcC: *i64 = cs_rc_new() 93 let lenC: i64 = cs_clean_page(c, clen, t, out, 262144, rcC) 94 sys_write(1, "TEST C (neg-control) -- miner-only page: miner removed, NOT counted as a safe ad\n" as *u8, 80) 95 g_eq(rcC[CR_MINER], 1, "miner counted as blocked" as *u8, pt) 96 g_eq(rcC[CR_ADS_KEPT], 0, "miner NOT miscounted as a preserved ad" as *u8, pt) 97 g_absent(out, lenC, "coinhive" as *u8, "miner-only: script removed" as *u8, pt) 98 99 // ============ TEST D: ENTITY-BODY CONTRACT -- a CRLF page must NOT lose its player to a second header strip ============ 100 // Fixture: a KVS-shaped page with CRLF line endings whose first blank line falls AFTER the player block (the real 101 // camwhores.tv shape, measured 2026-09-02). No status line, so this is an entity body and the ruler must answer 0. 102 // The CR LF bytes are spelled as escapes on purpose: a literal CR inside the source is stripped by any text-mode 103 // copy (it happened to this very block in transit), which would silently turn the fixture into an LF-only page. 104 let d: *u8 = "<html>\r\n<head><title>t</title></head>\r\n<body>\r\n<div id='kt_player'></div>\r\n<script>var flashvars = { video_url: 'https://cdn.example/get_file/1/abc/1.mp4/', license_code: 'x' };</script>\r\n<div class='box'>\r\n\r\n</div>\r\n<div class='related'>tail</div>\r\n</body></html>" as *u8 105 let dlen: i64 = cs_slen(d) 106 sys_write(1, "TEST D -- entity-body contract: CRLF page keeps its player (no second header strip)\n" as *u8, 84) 107 // neg-control FIRST: the fixture must actually contain an in-document blank CRLF line, i.e. the OLD unconditional 108 // scan (the defect, reproduced inline) cuts the page there and the player lands on the discarded side. A fixture 109 // the defect cannot fail is not a test. 110 var oldoff: i64 = 0; var di: i64 = 0 111 while di+3 < dlen { if (d[di]&0xff)==13 { if (d[di+1]&0xff)==10 { if (d[di+2]&0xff)==13 { if (d[di+3]&0xff)==10 { oldoff = di+4; di = dlen } } } } di = di+1 } 112 g_ge(oldoff, 1, "neg-control-fixture-reaches-the-condition: old blank-line scan cuts the CRLF page" as *u8, pt) 113 var oldkeeps: i64 = 0; if g_has(((d as i64)+oldoff) as *u8, dlen-oldoff, "video_url" as *u8)==1 { oldkeeps = 1 } 114 g_eq(oldkeeps, 0, "neg-control-old-scan-loses-the-player: video_url sits before the in-document blank line" as *u8, pt) 115 // the ruler: an entity body (no status line) is never stripped, so the extractor sees the whole page 116 let doff: i64 = cs_entity_body_off(d, dlen) 117 g_eq(doff, 0, "entity body: cs_entity_body_off answers 0 on a CRLF page" as *u8, pt) 118 g_present(((d as i64)+doff) as *u8, dlen-doff, "video_url" as *u8, "CRLF page: video_url reaches the extractor" as *u8, pt) 119 let rcD: *i64 = cs_rc_new() 120 let lenD: i64 = cs_clean_page(((d as i64)+doff) as *u8, dlen-doff, t, out, 262144, rcD) 121 g_present(out, lenD, "kt_player" as *u8, "CRLF page: player block survives the clean" as *u8, pt) 122 // POSITIVE CONTROL: a buffer that really is a wire response (status line first) IS stripped, exactly past its headers 123 let e: *u8 = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<html><body>x</body></html>" as *u8 124 let elen: i64 = cs_slen(e) 125 g_eq(cs_entity_body_off(e, elen), 44, "wire response: headers skipped, body offset lands on the html" as *u8, pt) 126 // LF-only page: unchanged behaviour (0) -- the case every earlier measurement happened to exercise 127 let f: *u8 = "<html>\n<body>\n\n<video src='https://cdn.example/a.mp4'></video>\n</body></html>" as *u8 128 g_eq(cs_entity_body_off(f, cs_slen(f)), 0, "LF-only page: 0 (unchanged)" as *u8, pt) 129 130 // ============ TEST E: CS9 media-candidate rulers -- never a preview clip, never the page itself ============ 131 sys_write(1, "TEST E -- CS9 rulers: skip table, declared metadata, media extension\n" as *u8, 71) 132 let notab: *u8 = sys_mmap(16); notab[0] = 0 as u8 133 g_eq(cs_media_skip("https://hdplayer.gives/upload/videos/x/preview/preview.mp4" as *u8, 58, notab), 1, "bootstrap skip: a /preview/ clip is refused" as *u8, pt) 134 g_eq(cs_media_skip("https://cdn.example/videos/12345/12345_1080p.mp4" as *u8, 48, notab), 0, "neg-control-content-not-skipped: a real content mp4 passes the bootstrap table" as *u8, pt) 135 g_eq(cs_media_skip("https://cdn.example/thumbs/9.mp4" as *u8, 32, "thumbs\nvast\n" as *u8), 1, "conf table: a row from the table refuses its match" as *u8, pt) 136 g_eq(cs_media_skip("https://cdn.example/videos/x/preview/preview.mp4" as *u8, 48, "thumbs\n" as *u8), 0, "neg-control-conf-replaces-bootstrap: a table without preview lets a preview through (the table is the law)" as *u8, pt) 137 let dec: *u8 = sys_mmap(1024) 138 let pg1: *u8 = "<html><head><script type=\"application/ld+json\">{\"@type\":\"VideoObject\",\"contentUrl\":\"https:\\/\\/cdn.example\\/v\\/2.mp4\"}</script></head></html>" as *u8 139 let d1: i64 = cs_find_declared(pg1, cs_slen(pg1), dec, 1024) 140 g_present(dec, d1, "https://cdn.example/v/2.mp4" as *u8, "declared: JSON-LD contentUrl extracted with escaped slashes unescaped" as *u8, pt) 141 g_eq(d1, 27, "declared: contentUrl length exact (no delimiter bleed)" as *u8, pt) 142 let pg2: *u8 = "<html><head><meta property=\"og:video\" content=\"https://cdn.example/v/1.mp4\"></head></html>" as *u8 143 let d2: i64 = cs_find_declared(pg2, cs_slen(pg2), dec, 1024) 144 g_present(dec, d2, "cdn.example/v/1.mp4" as *u8, "declared: og:video content extracted" as *u8, pt) 145 let pg3: *u8 = "<html><head><meta property=\"og:video\" content=\"https://hdthot.example/chloe-620226\"><meta property=\"og:video:type\" content=\"text/html\"></head></html>" as *u8 146 let d3: i64 = cs_find_declared(pg3, cs_slen(pg3), dec, 1024) 147 g_eq(cs_media_ext(dec, d3), 0, "neg-control-self-referential-declaration: og:video naming the page itself is NOT media (caller rejects)" as *u8, pt) 148 g_eq(cs_find_declared(b, blen, dec, 1024), 0, "declared: a page with no declaration returns 0" as *u8, pt) 149 g_eq(cs_media_ext("https://cdn.example/a/b.M3U8?x=1" as *u8, 32), 1, "media ext: .m3u8 recognised case-insensitively" as *u8, pt) 150 // ad VIDEO elements: the banner clip is removed and counted, the real player beside it survives 151 let pv: *u8 = "<html><body><video src='/contents/data/728x90-1.mp4' width='728' height='90' autoplay muted loop></video><h1>T</h1><video src='https://cdn.example/stream.mp4' controls></video></body></html>" as *u8 152 let rcV: *i64 = cs_rc_new() 153 let lenV: i64 = cs_clean_page(pv, cs_slen(pv), t, out, 262144, rcV) 154 g_absent(out, lenV, "728x90" as *u8, "ad video: the 728x90 autoplay clip is removed" as *u8, pt) 155 g_present(out, lenV, "cdn.example/stream.mp4" as *u8, "neg-control-real-player-survives: the content video beside the ad stays" as *u8, pt) 156 g_eq(rcV[CR_ADVIDEO], 1, "receipt: ad_videos counts exactly the one removed" as *u8, pt) 157 g_eq(rcA[CR_ADVIDEO], 0, "neg-control-fixture-A-unaffected: the hostile page has no ad video and counts 0" as *u8, pt) 158 let rj: *u8 = sys_mmap(1024); let rjn: i64 = cs_receipt_json(rcV, rj, 1024) 159 g_present(rj, rjn, "\"ad_videos\":1" as *u8, "receipt JSON carries ad_videos (additive field)" as *u8, pt) 160 // page identity token 161 let tk: *u8 = sys_mmap(128) 162 let t1: i64 = cs_page_token("https://www.camwhores.tv/videos/1855636/chloe-lamb-full-nude-8a70b061/" as *u8, 70, tk, 128) 163 g_present(tk, t1, "chloe-lamb-full-nude-8a70b061" as *u8, "token: last non-empty segment, trailing slash dropped" as *u8, pt) 164 let t2: i64 = cs_page_token("https://www.pornslash.com/watch/ykm1X54J6g0?x=1" as *u8, 47, tk, 128) 165 g_eq(t2, 11, "token: query dropped, 11-char id kept" as *u8, pt) 166 g_eq(cs_page_token("https://noodlemagazine.com/" as *u8, 27, tk, 128), 0, "neg-control-no-path: a bare host yields no token" as *u8, pt) 167 g_eq(cs_page_token("https://cdn.x/a/file.mp4" as *u8, 24, tk, 128), 0, "neg-control-dotted-segment: a file name is not an id" as *u8, pt) 168 g_eq(cs_page_token("https://site.x/v/ab" as *u8, 19, tk, 128), 0, "neg-control-too-short: a 2-char segment is refused" as *u8, pt) 169 170 // ============ VERDICT (base class: the exit code IS the verdict, the last line is positional) ============ 171 return gv_verdict("nx_clean_serve_gate" as *u8, pt, "ADVERSARY + CRITIC over synthetic hostile pages and real-shape fixtures; every tooth names its own strength above" as *u8) 172}