code wiki / _hdl_build / nx_browser_forms_e2e_gate.nx

nx_browser_forms_e2e_gate.nx source

↩ module page · 124 lines · 7735 B

1import "nx_gate_base.nx" 2// nx_browser_forms_e2e_gate.nx -- END-TO-END proof that the Nishi browser's FORM MODEL logs into the REAL 3// portal server: fetch the actual served /admin login page (dad_handle), parse its form, SIMULATE TYPING, 4// build the POST (bf_submit_request), feed it back to the REAL router (dad_handle) -> assert the login 5// succeeds + the authed no-JS page loads. This closes the browser-forms data path against the real server; 6// only the literal X11 keystroke capture + pixel paint remain display-coupled. GREEN iff 5/5. license: ORIGINAL 7import "nx_docportal_admin_daemon.nx" 8import "nx_browser_forms.nx" 9import "hub/nx_modern_auth_flow.nx" 10import "nx_syscalls.nx" 11 12func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw(" 13" as *u8); return ok } 14func gln(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 15func gcat(d: *u8, o: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8){d[o+i]=s[i];i=i+1} return o+i } 16func gfind(hay: *u8, n: i64, needle: *u8) -> i64 { let nn: i64=gln(needle); var i: i64=0; while i+nn<=n { var m: i64=1; var j: i64=0; while j<nn { if (hay[i+j] as i64)!=(needle[j] as i64){m=0;j=nn} else {j=j+1} } if m==1 {return i} i=i+1 } return 0-1 } 17func gcontains(hay: *u8, n: i64, needle: *u8) -> i64 { if gfind(hay,n,needle) >= 0 {return 1} return 0 } 18func gcell(ok: i64, id: *u8, txt: *u8) -> i64 { if ok==1 {gw(" PASS " as *u8)} else {gw(" FAIL " as *u8)} gw(id); gw(" " as *u8); gw(txt); gw("\n" as *u8); return ok } 19// body offset in an HTTP response (after \r\n\r\n) 20func gbody(resp: *u8, n: i64) -> i64 { let p: i64=gfind(resp,n,"\r\n\r\n" as *u8); if p<0 {return n} return p+4 } 21// find a field by name in form fi; return its index or -1 22func gfield(forms: *BrForm, fi: i64, fields: *BrField, name: *u8) -> i64 { 23 let fm: *BrForm = bf_form(forms, fi) 24 var k: i64 = 0 25 while k < fm.field_count { 26 let idx: i64 = fm.field_start + k 27 let fld: *BrField = bf_field(fields, idx) 28 var eq: i64 = 1 29 var j: i64 = 0 30 while name[j] != (0 as u8) { if (fld.name[j] as i64) != (name[j] as i64) { eq = 0 } j = j + 1 } 31 if fld.name[j] != (0 as u8) { eq = 0 } 32 if eq == 1 { return idx } 33 k = k + 1 34 } 35 return 0 - 1 36} 37 38func main() -> i64 { 39 gw("=== NX-BROWSER-FORMS-E2E-GATE -- the browser's form model logs into the REAL portal server ===\n" as *u8) 40 let sp: *u8 = "/tmp/nx_bfe2e_store.log" as *u8 41 let kp: *u8 = "/tmp/nx_bfe2e_keys.log" as *u8 42 let f1: i64 = sys_openat_wr(sp, 0x180); if f1>=0 {sys_close(f1)} 43 let f2: i64 = sys_openat_wr(kp, 0x180); if f2>=0 {sys_close(f2)} 44 let oprf: *u8=sys_mmap(32); let akp: *u8=sys_mmap(32); let akb: *u8=sys_mmap(33); let edp: *u8=sys_mmap(32); let edb: *u8=sys_mmap(32) 45 if nx_uas_server_keys_load_or_init(kp, oprf, akp, akb, edp, edb) != NX_UAS_OK { gw("KEYS FAIL\n" as *u8); sys_exit(1) } 46 let realm: *u8="nishi_docportal" as *u8; let rn: i64=gln(realm) 47 let ctx: *NxAuthContext = sys_mmap(256) as *NxAuthContext 48 if nx_auth_context_init(ctx, realm, rn, realm, rn, sp as i64, oprf, edp, edb, 900, 8192, 1, 1, 5, 1) != NX_MAUTH_OK { gw("CTX FAIL\n" as *u8); sys_exit(1) } 49 let mnb: *u8=sys_mmap(512); let mnn: *i64=sys_mmap(16) as *i64 50 if nx_modern_auth_register(ctx, "elder" as *u8, 5, "correct horse battery staple" as *u8, 28, mnb, 512, mnn) != NX_MAUTH_OK { gw("REG FAIL\n" as *u8); sys_exit(1) } 51 52 let out: *u8 = sys_mmap(DAD_OUTCAP) 53 var pass: i64 = 0 54 55 // 1. the browser NAVIGATES to /admin -> the REAL server returns the login shell 56 let g1req: *u8 = sys_mmap(512) 57 let g1n: i64 = gcat(g1req, 0, "GET /admin HTTP/1.1\r\nHost: admin.andelinwest.com\r\n\r\n" as *u8) 58 let o1: i64 = dad_handle(ctx, g1req, g1n, out) 59 let boff: i64 = gbody(out, o1) 60 let html: *u8 = ((out as i64) + boff) as *u8 61 let hlen: i64 = o1 - boff 62 // 2. the browser PARSES the served form 63 let forms: *BrForm = sys_mmap(NX_BRFORM_BYTES * 8) as *BrForm 64 let fields: *BrField = sys_mmap(NX_BRFIELD_BYTES * 32) as *BrField 65 let nf: i64 = bf_parse(html, hlen, forms, 8, fields, 32) 66 let fm0: *BrForm = bf_form(forms, 0) 67 var t1: i64 = 0 68 if nf >= 1 { if gstreq_local(fm0.action, "/admin/login" as *u8) == 1 { if fm0.method_post == 1 { t1 = 1 } } } 69 pass = pass + gcell(t1, "E1" as *u8, "browser GET /admin -> real server login shell -> parsed form action=/admin/login POST" as *u8) 70 71 // 3. the user TYPES the handle + passphrase into the parsed fields 72 let hi: i64 = gfield(forms, 0, fields, "handle" as *u8) 73 let pi: i64 = gfield(forms, 0, fields, "passphrase" as *u8) 74 var t2: i64 = 0 75 if hi >= 0 { if pi >= 0 { 76 let fh: *BrField = bf_field(fields, hi) 77 let fp: *BrField = bf_field(fields, pi) 78 bf_set(fh, "elder" as *u8) 79 bf_set(fp, "correct horse battery staple" as *u8) 80 t2 = 1 81 } } 82 pass = pass + gcell(t2, "E2" as *u8, "browser found the handle + passphrase fields and typed the credentials" as *u8) 83 84 // 4. the browser SUBMITS -> builds the POST -> the REAL server logs the user in (ui=1 -> HTML landing) 85 let reqb: *u8 = sys_mmap(4096) 86 let rl: i64 = bf_submit_request("admin.andelinwest.com" as *u8, 21, forms, 0, fields, reqb) 87 let o2: i64 = dad_handle(ctx, reqb, rl, out) 88 var t3: i64 = 0 89 if gcontains(out, o2, "200 OK" as *u8) == 1 { if gcontains(out, o2, "/admin/ui?s=" as *u8) == 1 { t3 = 1 } } 90 pass = pass + gcell(t3, "E3" as *u8, "browser submit -> real server 200 + HTML landing carrying /admin/ui?s= (login SUCCEEDED)" as *u8) 91 92 // 5. the browser FOLLOWS the ?s= link -> the REAL server serves the authed no-JS page 93 let key: *u8 = "/admin/ui?s=" as *u8 94 let kpos: i64 = gfind(out, o2, key) 95 var t4: i64 = 0 96 var t5: i64 = 0 97 if kpos >= 0 { 98 let vstart: i64 = kpos + gln(key) 99 // copy the ?s= token value up to the closing quote 100 let tokbuf: *u8 = sys_mmap(1024) 101 var w: i64 = 0 102 var k: i64 = vstart 103 while k < o2 { if (out[k] as i64) == 34 { k = o2 } else { tokbuf[w] = out[k]; w = w + 1; k = k + 1 } } 104 tokbuf[w] = 0 as u8 105 t4 = 1 106 // navigate GET /admin/ui?s=<tok> 107 let navreq: *u8 = sys_mmap(2048) 108 var nr: i64 = gcat(navreq, 0, "GET /admin/ui?s=" as *u8) 109 var z: i64 = 0 110 while z < w { navreq[nr] = tokbuf[z]; nr = nr + 1; z = z + 1 } 111 nr = gcat(navreq, nr, " HTTP/1.1\r\nHost: admin.andelinwest.com\r\n\r\n" as *u8) 112 let o3: i64 = dad_handle(ctx, navreq, nr, out) 113 // the form login (E3) populated the uidmap -> the authed page now greets by HANDLE, not the uid hash 114 if gcontains(out, o3, "200 OK" as *u8) == 1 { if gcontains(out, o3, "document portal" as *u8) == 1 { if gcontains(out, o3, "Signed in as" as *u8) == 1 { if gcontains(out, o3, "elder" as *u8) == 1 { t5 = 1 } } } } 115 } 116 pass = pass + gcell(t4, "E4" as *u8, "browser extracted the ?s= session token from the landing link" as *u8) 117 pass = pass + gcell(t5, "E5" as *u8, "browser follows /admin/ui?s= -> real server serves the authed page (Signed in as elder, resolved from uid)" as *u8) 118 119 gw("NX-BROWSER-FORMS-E2E-GATE pass=" as *u8); let d: *u8=sys_mmap(8); d[0]=(48+pass) as u8; sys_write(1,d,1); gw("/5 verdict=" as *u8) 120 if pass == 5 { gw("GREEN (browser form model logs into the REAL portal server end-to-end; only X11 keystrokes + pixels remain display-coupled)\n" as *u8); sys_exit(0); return 0 } 121 gw("RED\n" as *u8); sys_exit(1) 122 return 1 123} 124func gstreq_local(a: *u8, b: *u8) -> i64 { var i: i64=0; while 1==1 { if (a[i] as i64)!=(b[i] as i64){return 0} if (a[i] as i64)==0 {return 1} i=i+1 } return 0 }