code wiki / _hdl_build / nx_adnet_view_gate.nx

nx_adnet_view_gate.nx source

↩ module page · 88 lines · 5121 B

1// nx_adnet_view_gate.nx -- GATE for nx_adnet_view (MRC viewable-impression measurement). 2// Proves, by independent re-derivation rather than by calling the code under test to describe itself: 3// (1) the emitted observer carries BOTH MRC thresholds, sourced from the consts (script/const drift = RED) 4// (2) the second is CONTINUOUS, not cumulative -- the timer is cancelled when the creative leaves view 5// (3) it fires AT MOST ONCE per page (disconnect + latch), so a scroll-by cannot inflate the count 6// (4) FAIL-CLOSED: a malformed id journals NOTHING while still answering 204 (no id oracle) 7// (5) PRIVACY BY CONSTRUCTION: the beacon carries no cookie/visitor/referrer token -- a PAIRED control, 8// asserting a known-present token present AND a known-absent token absent, so the gate is proven 9// able to fail rather than trivially green. 10// expect_exit: 0 license_tier: ORIGINAL 11import "nx_syscalls.nx" 12import "_hdl_build/nx_adnet_view.nx" 13 14func tv_slen(s: *u8) -> i64 { var i: i64 = 0; while s[i] != (0 as u8) { i = i + 1 } return i } 15func tv_puts(s: *u8) -> i64 { sys_write(1, s, tv_slen(s)); return 0 } 16func tv_pn(v: i64) -> i64 { 17 let b: *u8 = sys_mmap(32) 18 if v == 0 { b[0] = 48 as u8; sys_write(1, b, 1); return 0 } 19 var n: i64 = 0 20 var x: i64 = v 21 while x > 0 { b[n] = ((x - (x / 10) * 10) + 48) as u8; n = n + 1; x = x / 10 } 22 let r: *u8 = sys_mmap(32) 23 var i: i64 = 0 24 while i < n { r[i] = b[n - 1 - i]; i = i + 1 } 25 sys_write(1, r, n) 26 return 0 27} 28func tv_has(buf: *u8, n: i64, pat: *u8) -> i64 { 29 let pl: i64 = tv_slen(pat) 30 if pl == 0 { return 0 } 31 if pl > n { return 0 } 32 var i: i64 = 0 33 while i <= n - pl { 34 var k: i64 = 0 35 var ok: i64 = 1 36 while k < pl { if buf[i + k] != pat[k] { ok = 0; break } k = k + 1 } 37 if ok == 1 { return 1 } 38 i = i + 1 39 } 40 return 0 41} 42func tv_check(name: *u8, cond: i64) -> i64 { 43 if cond == 1 { tv_puts(" PASS " as *u8) } else { tv_puts(" FAIL " as *u8) } 44 tv_puts(name); tv_puts("\n" as *u8) 45 return cond 46} 47 48func main() -> i64 { 49 tv_puts("=== nx_adnet_view_gate ===\n" as *u8) 50 var pass: i64 = 0 51 var total: i64 = 0 52 let buf: *u8 = sys_mmap(8192) 53 let idb: *u8 = sys_mmap(128) 54 55 // ---- the emitted observer ---- 56 let n1: i64 = aview_script(buf, 8192, "ha-video" as *u8) 57 pass = pass + tv_check("valid id -> script emitted" as *u8, n1 > 0); total = total + 1 58 pass = pass + tv_check("script carries the MRC ratio 50" as *u8, tv_has(buf, n1, "*100>=50" as *u8)); total = total + 1 59 pass = pass + tv_check("script carries the MRC dwell 1000ms" as *u8, tv_has(buf, n1, "},1000)" as *u8)); total = total + 1 60 pass = pass + tv_check("script targets the ad id" as *u8, tv_has(buf, n1, "/ad/view/ha-video" as *u8)); total = total + 1 61 pass = pass + tv_check("beacon is first-party sendBeacon" as *u8, tv_has(buf, n1, "navigator.sendBeacon" as *u8)); total = total + 1 62 pass = pass + tv_check("dwell is CONTINUOUS (timer cancelled on exit)" as *u8, tv_has(buf, n1, "clearTimeout" as *u8)); total = total + 1 63 pass = pass + tv_check("fires at most once (observer disconnects)" as *u8, tv_has(buf, n1, "o.disconnect()" as *u8)); total = total + 1 64 65 // ---- PAIRED PRIVACY CONTROL: a known-present token AND a known-absent token ---- 66 pass = pass + tv_check("privacy control positive: slot selector IS present" as *u8, tv_has(buf, n1, ".nx-ad-slot img" as *u8)); total = total + 1 67 pass = pass + tv_check("privacy: no cookie token in the beacon path" as *u8, tv_has(buf, n1, "cookie" as *u8) == 0); total = total + 1 68 pass = pass + tv_check("privacy: no referrer token in the beacon path" as *u8, tv_has(buf, n1, "referrer" as *u8) == 0); total = total + 1 69 70 // ---- fail-closed id handling ---- 71 let n2: i64 = aview_script(buf, 8192, "bad id!" as *u8) 72 pass = pass + tv_check("malformed id -> script REFUSED" as *u8, n2 == 0); total = total + 1 73 74 // ---- the beacon endpoint ---- 75 let r1: i64 = aview_resp("/ad/view/ha-video" as *u8, 17, buf, 8192, idb) 76 pass = pass + tv_check("valid beacon -> 204 emitted" as *u8, tv_has(buf, r1, "204 No Content" as *u8)); total = total + 1 77 pass = pass + tv_check("valid beacon -> id journalled" as *u8, tv_has(idb, tv_slen(idb), "ha-video" as *u8)); total = total + 1 78 let r2: i64 = aview_resp("/ad/view/bad id!" as *u8, 16, buf, 8192, idb) 79 pass = pass + tv_check("malformed beacon -> still 204 (no id oracle)" as *u8, tv_has(buf, r2, "204 No Content" as *u8)); total = total + 1 80 pass = pass + tv_check("malformed beacon -> NOTHING journalled" as *u8, idb[0] == (0 as u8)); total = total + 1 81 let r3: i64 = aview_resp("/code/pulse" as *u8, 11, buf, 8192, idb) 82 pass = pass + tv_check("non-beacon path -> not handled" as *u8, r3 == 0); total = total + 1 83 84 tv_puts("pass=" as *u8); tv_pn(pass); tv_puts(" fail=" as *u8); tv_pn(total - pass); tv_puts("\n" as *u8) 85 if pass == total { tv_puts("=== ADNET-VIEW-GATE verdict=GREEN ===\n" as *u8); return 0 } 86 tv_puts("=== ADNET-VIEW-GATE verdict=RED ===\n" as *u8) 87 return 1 88}