code wiki / _hdl_build / nx_url_truth_gate.nx

nx_url_truth_gate.nx source

↩ module page · 91 lines · 6476 B

1// nx_url_truth_gate.nx -- ADVERSARIAL reproducibility gate (operator doctrine: a "live" claim must AGREE with 2// the system at the EXACT user-facing URL, or it isn't proven; when researcher-claim and served-reality 3// disagree, that disagreement is the finding). The old verify checked /library.html (our DEPLOY target) and 4// only that a marker STRING appeared -- so it went GREEN while a browser at /library saw a placeholder. This 5// gate instead: (1) GETs the BARE path a user types (/library), (2) compares the served body against the bytes 6// we SHIPPED (web_assets/library.html) -- size within tolerance + our unique content present, (3) tries to 7// REFUTE: the launch-placeholder signature and a foreign-SPA signature must be ABSENT. GREEN only if the user 8// genuinely sees our shipped page. RED (honestly) names which check caught the lie. license_tier: ORIGINAL 9import "nx_syscalls.nx" 10import "nx_x509_trust_store.nx" 11import "nx_trust_store_load_from_certdata.nx" 12import "nx_tls13_client_validate_certificate.nx" 13import "nx_tls13_client_session_run.nx" 14import "nx_https_url_for_fetch.nx" 15import "nx_https_url_connect.nx" 16import "nx_https_get.nx" 17import "nx_https_get_complete.nx" 18import "nx_http_response_parse.nx" 19 20func vg_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 21func vg_num(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)}; 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 } 22func vg_strlen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 23func vg_iabs(x: i64) -> i64 { if x<0 { return 0-x } return x } 24func vg_contains(buf: *u8, lo: i64, hi: i64, needle: *u8) -> i64 { 25 let nl: i64=vg_strlen(needle); if nl==0 { return 0 } 26 var i: i64=lo 27 while i+nl<=hi { var j: i64=0; var ok: i64=1; while j<nl { if buf[i+j]!=needle[j] { ok=0; j=nl } else { j=j+1 } } if ok==1 { return 1 } i=i+1 } 28 return 0 29} 30func vg_row(id: i64, ok: i64, what: *u8) -> i64 { 31 vg_w(" R" as *u8); vg_num(id); vg_w(" " as *u8); vg_w(what); vg_w(": " as *u8) 32 if ok==1 { vg_w("YES PASS\n" as *u8) } else { vg_w("NO <-- refuted\n" as *u8) } 33 return ok 34} 35func tg_readfile(path: *u8, buf: *u8, cap: i64) -> i64 { 36 let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 } 37 var n: i64=0; var go: i64=1 38 while go==1 { let r: i64 = sys_read(fd, ((buf as i64)+n) as *u8, cap-n); if r<=0 { go=0 } else { n=n+r; if n>=cap { go=0 } } } 39 sys_close(fd); return n 40} 41 42func main() -> i64 { 43 vg_w("=== URL TRUTH GATE (adversarial: does the USER-FACING url serve the page we SHIPPED?) ===\n" as *u8) 44 // what we shipped, and where a user actually looks for it: 45 let shipped: *u8 = "web_assets/library.html" as *u8 46 let path: *u8 = "/library" as *u8 47 let host: *u8 = "nishifamily.com" as *u8 48 49 let local: *u8 = sys_mmap(262144) 50 let local_n: i64 = tg_readfile(shipped, local, 262143) 51 vg_w(" shipped "); vg_w(shipped); vg_w(" = "); vg_num(local_n); vg_w(" bytes; user-facing path = "); vg_w(path); vg_w("\n") 52 53 let r: i64 = nx_trust_store_load_from_certdata("data/mozilla_certdata.txt" as *u8, 300, 4194304) 54 if r <= 0 { vg_w("abort: no CA\n" as *u8); sys_exit(1); return 1 } 55 let store: *TrustStore = r as *TrustStore 56 57 let url: *u8 = "https://nishifamily.com/library" as *u8 58 let url_p: *NxUrl = nx_url_new() 59 let target: *NxHttpsTarget = sys_mmap(64) as *NxHttpsTarget 60 target.url = url_p; target.port = 0 61 if nx_https_url_for_fetch(url, target) != NX_HTTPS_URL_OK { vg_w("abort: url\n"); sys_exit(1); return 1 } 62 let fd_p: *i64 = sys_mmap(16) as *i64 63 if nx_https_url_connect(target, url, sys_now_realtime_sec(), fd_p) != NX_HTTPS_CONNECT_OK { vg_w("abort: connect\n"); sys_exit(1); return 1 } 64 let fd: i64 = fd_p[0] 65 let cr: *u8 = sys_mmap(32); var z: i64=0; while z<32 { cr[z]=(0xC0+z) as u8; z=z+1 } 66 let priv: *u8 = sys_mmap(32); z=0; while z<32 { priv[z]=(0xA0+z) as u8; z=z+1 } 67 let vctx: *TlsValidationContext = sys_mmap(64) as *TlsValidationContext 68 vctx.store=store; vctx.sni_host=((url as i64)+target.url.host_off) as *u8; vctx.sni_host_len=target.url.host_len; vctx.now_epoch=sys_now_realtime_sec() 69 let sr: i64 = nx_tls13_client_session_run(fd, ((url as i64)+target.url.host_off) as *u8, target.url.host_len, cr, priv, vctx) 70 if sr <= 0 { vg_w("abort: tls\n"); sys_exit(1); return 1 } 71 let buf: *u8 = sys_mmap(262144) 72 let gc: i64 = nx_https_get_complete(sr as *Tls13ClientSession, fd, path, 8, ((url as i64)+target.url.host_off) as *u8, target.url.host_len, buf, 262144) 73 sys_close(fd) 74 var status: i64=0; var body_off: i64=0 75 let pr: *i64 = sys_mmap(128) as *i64; if nx_http_response_parse(buf, gc, pr)==0 { status=pr[1]; body_off=pr[6] } 76 let served_n: i64 = gc - body_off 77 vg_w(" served "); vg_w(path); vg_w(" = status "); vg_num(status); vg_w(", body "); vg_num(served_n); vg_w(" bytes\n") 78 79 var pass: i64=0; let rows: i64=5 80 pass=pass+vg_row(1, (status==200) as i64, "GET the bare user-facing path returns 200" as *u8) 81 // size of our shipped page, within 20% -- catches stubs / placeholder / a foreign page of different size 82 var size_ok: i64=0; if local_n>0 { if vg_iabs(served_n - local_n) <= local_n/5 { size_ok=1 } } 83 pass=pass+vg_row(2, size_ok, "served body is our SHIPPED size (not a stub/placeholder)" as *u8) 84 pass=pass+vg_row(3, vg_contains(buf, body_off, gc, "A sovereign knowledge corpus that" as *u8), "served body carries OUR unique content" as *u8) 85 pass=pass+vg_row(4, (vg_contains(buf, body_off, gc, "Launch placeholder" as *u8)==0) as i64, "REFUTE: served body is NOT the launch placeholder" as *u8) 86 pass=pass+vg_row(5, (vg_contains(buf, body_off, gc, "<base href" as *u8)==0) as i64, "REFUTE: served body is NOT a foreign SPA (no <base href>)" as *u8) 87 88 vg_w("----\nTRUTH-GATE path=" as *u8); vg_w(path); vg_w(" rows="); vg_num(rows); vg_w(" pass="); vg_num(pass); vg_w(" served="); vg_num(served_n); vg_w("B shipped="); vg_num(local_n); vg_w("B\n") 89 if pass==rows { vg_w("TRUTH-GATE GREEN -- the user genuinely sees our shipped page at "); vg_w(path); vg_w("\n"); sys_exit(0); return 0 } 90 vg_w("TRUTH-GATE RED -- the user-facing URL does NOT serve our shipped page (claim refuted; this IS the finding)\n" as *u8); sys_exit(1); return 1 91}