code wiki / _hdl_build / nx_survey_live_verify.nx

nx_survey_live_verify.nx source

↩ module page · 216 lines · 11526 B

1// nx_survey_live_verify.nx -- SOVEREIGN live verification of the PUBLIC Nishi Pulse survey app, every rung 2// OURS on OUR hardware: DNS -> TCP -> our TLS 1.3 handshake (X.509 vs the Mozilla trust store) -> HTTP 3// GET/POST -> our HTTP parser -> our dechunk -> byte assertions. REPLACES WebFetch (operator 2026-07-10: 4// "i dont want webfetch in the mix i want native nishi os and nishi browser our hardware from each rung up"; 5// daemon-not-done-until-public-edge-route -> GET+POST both proven). Clone of nx_connect_live_verify; lv_fetch 6// is byte-identical (the reusable bits-up request core), only the survey assertions in main() differ. 7// GET /survey -> 200 + Nishi Pulse home lists the ward welfare pulse 8// GET /survey/s/welfare-pulse -> 200 + the form (token + scale radios) renders 9// POST /survey/s/welfare-pulse -> 303 (respondent path; FIXED token -> revote-replace, 1 probe 10// ballot ever, never accumulates -- the one-voter-one-ballot law) 11// GET /survey/s/welfare-pulse/results -> 200 + aggregates render 12// POST /survey/admin/insights (bad key) -> 403 (admin POST routes + FAIL-CLOSED, zero data pollution) 13// Exit 0 = the live public survey served + interactive through our own browser stack. license_tier: ORIGINAL 14import "nx_syscalls.nx" 15import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 16import "nx_x509_trust_store.nx" 17import "nx_trust_store_load_from_certdata.nx" 18import "nx_tls13_client_validate_certificate.nx" 19import "nx_tls13_client_session_run.nx" 20import "nx_https_url_for_fetch.nx" 21import "nx_https_url_connect.nx" 22import "nx_https_get_complete.nx" 23import "nx_https_post_complete.nx" 24import "nx_http_response_parse.nx" 25const K_MAGIC_262144: i64 = 262144 26const K_MAGIC_16384: i64 = 16384 27const K_MAGIC_4194304: i64 = 4194304 28 29func lv_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 30// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 31// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 32// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 33// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 34func lv_n(v: i64) -> i64 { nxi_out(v); return 0 } 35func lv_check(pass: i64, label: *u8, fails: *i64) -> i64 { 36 lv_w(" " as *u8); lv_w(label); lv_w(": " as *u8) 37 if pass==1 { lv_w("PASS\n" as *u8) } else { lv_w("FAIL\n" as *u8); fails[0]=fails[0]+1 } 38 return 0 39} 40func lv_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 41func lv_cat(dst: *u8, off: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8){dst[off+i]=s[i]; i=i+1} return off+i } 42func lv_catb(dst: *u8, off: i64, src: *u8, n: i64) -> i64 { var i: i64=0; while i<n { dst[off+i]=src[i]; i=i+1 } return off+n } 43func lv_catnum(dst: *u8, off: i64, v: i64) -> i64 { 44 var o: i64=off 45 var m: i64=v 46 let t: *u8=sys_mmap(28) 47 var k: i64=0 48 if m<=0 { dst[o]=48 as u8; return o+1 } 49 while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } 50 var i: i64=0 51 while i<k { dst[o+i]=t[k-1-i]; i=i+1 } 52 return o+k 53} 54func lv_has(buf: *u8, n: i64, needle: *u8) -> i64 { 55 let m: i64 = lv_slen(needle) 56 if m==0 { return 0 } 57 var i: i64=0 58 while i+m<=n { 59 var k: i64=0 60 var hit: i64=1 61 while k<m { if buf[i+k]!=needle[k] { hit=0; k=m } else { k=k+1 } } 62 if hit==1 { return 1 } 63 i=i+1 64 } 65 return 0 66} 67 68// one bits-up request to nishifamily.com (byte-identical to nx_connect_live_verify's lv_fetch). 69func lv_fetch(store: *TrustStore, is_post: i64, path: *u8, plen: i64, body: *u8, blen: i64, 70 html: *u8, cap: i64, st: *i64) -> i64 { 71 let url: *u8 = "https://nishifamily.com/\x00" 72 let url_p: *NxUrl = nx_url_new() 73 let target_raw: *u8 = sys_mmap(32) 74 let target: *NxHttpsTarget = target_raw as *NxHttpsTarget 75 target.url = url_p 76 target.port = 0 77 if nx_https_url_for_fetch(url, target) != NX_HTTPS_URL_OK { return 0-41 } 78 let host: *u8 = url + target.url.host_off 79 let hlen: i64 = target.url.host_len 80 let fd_p: *i64 = sys_mmap(16) as *i64 81 if nx_https_url_connect(target, url, sys_now_realtime_sec(), fd_p) != NX_HTTPS_CONNECT_OK { return 0-42 } 82 let fd: i64 = *fd_p 83 sys_set_socket_timeout(fd, 12) 84 let vc_raw: *u8 = sys_mmap(64) 85 let vc: *TlsValidationContext = vc_raw as *TlsValidationContext 86 vc.store = store 87 vc.sni_host = host 88 vc.sni_host_len = hlen 89 vc.now_epoch = sys_now_realtime_sec() 90 let cr: *u8 = sys_mmap(32) 91 let priv: *u8 = sys_mmap(32) 92 var i: i64=0 93 while i<32 { cr[i]=(0xC0+i) as u8; priv[i]=(0xA0+i) as u8; i=i+1 } 94 let sr: i64 = nx_tls13_client_session_run(fd, host, hlen, cr, priv, vc) 95 if sr<=0 { sys_close(fd); return 0-(200+(0-sr)) } 96 let session: *Tls13ClientSession = sr as *Tls13ClientSession 97 let buf: *u8 = sys_mmap(K_MAGIC_262144) 98 var gc: i64 = 0 99 if is_post==1 { 100 let req: *u8 = sys_mmap(K_MAGIC_16384) 101 var ro: i64 = 0 102 ro = lv_cat(req, ro, "POST " as *u8) 103 ro = lv_catb(req, ro, path, plen) 104 ro = lv_cat(req, ro, " HTTP/1.1\r\nHost: " as *u8) 105 ro = lv_catb(req, ro, host, hlen) 106 ro = lv_cat(req, ro, "\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: " as *u8) 107 ro = lv_catnum(req, ro, blen) 108 ro = lv_cat(req, ro, "\r\nConnection: close\r\n\r\n" as *u8) 109 ro = lv_catb(req, ro, body, blen) 110 gc = nx_https_req_complete(session, fd, req, ro, buf, K_MAGIC_262144) 111 } else { 112 gc = nx_https_get_complete(session, fd, path, plen, host, hlen, buf, K_MAGIC_262144) 113 } 114 sys_close(fd) 115 if gc<0 { return 0-(100+(0-gc)) } 116 let resp: *i64 = sys_mmap(128) as *i64 117 if nx_http_response_parse(buf, gc, resp)!=0 { return 0-50 } 118 st[0] = resp[1] 119 let body_off: i64 = resp[6] 120 let body_kind: i64 = resp[8] 121 var hl: i64 = 0 122 if body_kind==2 { hl = nx_http_dechunk(buf+body_off, gc-body_off, html, cap) } 123 else { hl = gc-body_off; var ci: i64=0; while ci<hl { if ci<cap { html[ci]=buf[body_off+ci] } ci=ci+1 } } 124 return hl 125} 126 127func main() -> i64 { 128 let fails: *i64 = sys_mmap(16) as *i64 129 fails[0]=0 130 lv_w("=== nx_survey_live_verify -- SOVEREIGN verify of nishifamily.com/survey (our TLS, our parser, our hardware, no WebFetch) ===\n" as *u8) 131 132 let r: i64 = nx_trust_store_load_from_certdata("data/mozilla_certdata.txt\x00" as *u8, 300, K_MAGIC_4194304) 133 if r<=0 { lv_w("TRUST LOAD FAIL (run from nxc2)\n" as *u8); return 1 } 134 let store: *TrustStore = r as *TrustStore 135 lv_w(" CA roots=" as *u8); lv_n(trust_store_count(store)); lv_w("\n" as *u8) 136 137 let html: *u8 = sys_mmap(K_MAGIC_262144) 138 let st: *i64 = sys_mmap(16) as *i64 139 140 // ---- GET /survey (home) ---- 141 let g: i64 = lv_fetch(store, 0, "/survey\x00" as *u8, 7, 0 as *u8, 0, html, K_MAGIC_262144, st) 142 if g<0 { lv_w(" GET /survey stage FAIL code=" as *u8); lv_n(g); lv_w("\n" as *u8); fails[0]=fails[0]+1 } 143 else { 144 lv_w(" GET /survey -> HTTP " as *u8); lv_n(st[0]); lv_w(" body=" as *u8); lv_n(g); lv_w(" bytes (over our TLS 1.3)\n" as *u8) 145 var t1: i64=0 146 if st[0]==200 { t1=1 } 147 lv_check(t1, "T1 GET /survey returns 200 through the sovereign edge" as *u8, fails) 148 var t2: i64=1 149 if lv_has(html, g, "Nishi Pulse" as *u8)==0 { t2=0 } 150 if lv_has(html, g, "Ward Welfare Pulse" as *u8)==0 { t2=0 } 151 lv_check(t2, "T2 home renders (Nishi Pulse + the ward welfare pulse listed)" as *u8, fails) 152 } 153 154 // ---- GET /survey/s/welfare-pulse (the form) ---- 155 let f: i64 = lv_fetch(store, 0, "/survey/s/welfare-pulse\x00" as *u8, 23, 0 as *u8, 0, html, K_MAGIC_262144, st) 156 if f<0 { lv_w(" GET form stage FAIL code=" as *u8); lv_n(f); lv_w("\n" as *u8); fails[0]=fails[0]+1 } 157 else { 158 var t3: i64=0 159 if st[0]==200 { if lv_has(html, f, "name='tok'" as *u8)==1 { if lv_has(html, f, "name='q1'" as *u8)==1 { t3=1 } } } 160 lv_check(t3, "T3 GET /survey/s/welfare-pulse form renders (token + scale radios)" as *u8, fails) 161 } 162 163 // ---- POST a ballot (FIXED token -> revote-replace; exactly one probe ballot ever) ---- 164 let bod: *u8 = "tok=zzliveverify&q1=5\x00" as *u8 165 let p: i64 = lv_fetch(store, 1, "/survey/s/welfare-pulse\x00" as *u8, 23, bod, lv_slen(bod), html, K_MAGIC_262144, st) 166 if p<0 { lv_w(" POST ballot stage FAIL code=" as *u8); lv_n(p); lv_w("\n" as *u8); fails[0]=fails[0]+1 } 167 else { 168 lv_w(" POST /survey/s/welfare-pulse -> HTTP " as *u8); lv_n(st[0]); lv_w("\n" as *u8) 169 var t4: i64=0 170 if st[0]==303 { t4=1 } 171 lv_check(t4, "T4 POST ballot -> 303 (respondent interactive path, sovereign; revote-idempotent probe)" as *u8, fails) 172 } 173 174 // ---- GET results (aggregate) ---- 175 let rr: i64 = lv_fetch(store, 0, "/survey/s/welfare-pulse/results\x00" as *u8, 31, 0 as *u8, 0, html, K_MAGIC_262144, st) 176 if rr<0 { lv_w(" GET results stage FAIL code=" as *u8); lv_n(rr); lv_w("\n" as *u8); fails[0]=fails[0]+1 } 177 else { 178 var t5: i64=0 179 if st[0]==200 { if lv_has(html, rr, "response(s)" as *u8)==1 { t5=1 } } 180 lv_check(t5, "T5 GET results renders aggregates (response counts)" as *u8, fails) 181 } 182 183 // ---- POST admin/insights with a WRONG key -> 403 (admin POST routes + fail-closed, zero pollution) ---- 184 let bk: *u8 = "key=wrong-live-verify-key&id=welfare-pulse\x00" as *u8 185 let a: i64 = lv_fetch(store, 1, "/survey/admin/insights\x00" as *u8, 22, bk, lv_slen(bk), html, K_MAGIC_262144, st) 186 if a<0 { lv_w(" POST admin stage FAIL code=" as *u8); lv_n(a); lv_w("\n" as *u8); fails[0]=fails[0]+1 } 187 else { 188 var t6: i64=0 189 if st[0]==403 { t6=1 } 190 lv_check(t6, "T6 POST admin/insights wrong-key -> 403 (admin POST routed + FAIL-CLOSED, no data leaked)" as *u8, fails) 191 } 192 193 // ---- GET results.json (the machine-readable aggregate API) ---- 194 let j: i64 = lv_fetch(store, 0, "/survey/s/welfare-pulse/results.json\x00" as *u8, 36, 0 as *u8, 0, html, K_MAGIC_262144, st) 195 if j<0 { lv_w(" GET results.json stage FAIL code=" as *u8); lv_n(j); lv_w("\n" as *u8); fails[0]=fails[0]+1 } 196 else { 197 var t7: i64=0 198 if st[0]==200 { if lv_has(html, j, "\"survey\":\"welfare-pulse\"" as *u8)==1 { if lv_has(html, j, "\"responses\":" as *u8)==1 { t7=1 } } } 199 lv_check(t7, "T7 GET results.json serves versioned aggregate JSON (machine-readable, sovereign)" as *u8, fails) 200 } 201 202 // ---- GET /survey/embed/welfare-pulse (the embeddable one-question poll) ---- 203 let e: i64 = lv_fetch(store, 0, "/survey/embed/welfare-pulse\x00" as *u8, 27, 0 as *u8, 0, html, K_MAGIC_262144, st) 204 if e<0 { lv_w(" GET embed stage FAIL code=" as *u8); lv_n(e); lv_w("\n" as *u8); fails[0]=fails[0]+1 } 205 else { 206 var t8: i64=0 207 if st[0]==200 { if lv_has(html, e, "name='tok'" as *u8)==1 { if lv_has(html, e, "/embed/welfare-pulse" as *u8)==1 { t8=1 } } } 208 lv_check(t8, "T8 GET /survey/embed/<id> = compact embeddable poll (iframe-friendly, sovereign)" as *u8, fails) 209 } 210 211 lv_w(" fails=" as *u8); lv_n(fails[0]); lv_w("\n" as *u8) 212 if fails[0]==0 { lv_w("VERDICT: GREEN (nishifamily.com/survey live + interactive, verified bits-up through OUR stack -- no WebFetch)\n" as *u8); sys_exit(0) } 213 lv_w("VERDICT: RED\n" as *u8) 214 sys_exit(1) 215 return 1 216}