code wiki / _hdl_build / nx_sitegen_game_gate.nx
nx_sitegen_game_gate.nx source
↩ module page · 184 lines · 9669 B
1// nx_sitegen_game_gate.nx -- gate for the AUTO-BUILDER GAME-PUBLISH page type (nx_sitegen_game). Proves the
2// Nishi team can publish a GAME onto a website FROM DATA, and that the build is general (not one-off),
3// reproducible, and injection-safe. Positive AND negative controls. It renders the operator's pets3d
4// game page from data, reads it back over a real fd, and asserts: the data-driven canvas dims / wasm fetch
5// / title / default room are present; the LAST-MILE wasm ABI binding (instantiate + gi_init/gi_tick/
6// gi_net_*) is intact; the multiplayer relay endpoints (/pstate,/roster) are present; the LAST-MILE
7// sovereignty banner (zero game/netcode logic in JS) is present; a SECOND, different game (different wasm +
8// title) is genuinely different (data-driven, not hardcoded); the same data builds BYTE-IDENTICAL twice; and
9// a malicious title is NEUTRALIZED (escaped, cannot break out of the page). Appends "CMSGATE row=
10// nx_sitegen_game game-publish ... verdict=PASS" to knowledge/status/cms_gate.log ONLY on all-pass.
11// Exit 0 iff all pass. license_tier: ORIGINAL
12import "nx_sitegen_game.nx"
13import "nx_syscalls.nx"
14
15func gg_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
16func gg_num(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m}; 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 }
17func gg_cat(dst: *u8, off: i64, s: *u8) -> i64 { var o: i64=off; var k: i64=0; while s[k]!=(0 as u8){dst[o]=s[k];o=o+1;k=k+1} return o }
18func gg_catnum(dst: *u8, off: i64, v: i64) -> i64 { var o: i64=off; let t: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m}; 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{dst[o]=t[k-1-i];o=o+1;i=i+1} return o }
19
20func gg_row(id: i64, ok: i64, what: *u8) -> i64 {
21 gg_w("GGROW " as *u8); gg_num(id); gg_w(" " as *u8)
22 if ok==1 { gg_w("PASS " as *u8) } else { gg_w("FAIL " as *u8) }
23 gg_w(what); gg_w("\n" as *u8)
24 return ok
25}
26
27func gg_slurp(path: *u8, buf: *u8, cap: i64) -> i64 {
28 let fd: i64 = sys_openat_rd(path)
29 if fd < 0 { return 0 - 1 }
30 var total: i64 = 0
31 var go: i64 = 1
32 while go == 1 {
33 let r: i64 = sys_read(fd, buf + total, cap - total)
34 if r <= 0 { go = 0 }
35 if r > 0 { total = total + r }
36 if total >= cap { go = 0 }
37 }
38 sys_close(fd)
39 return total
40}
41
42func gg_contains(hay: *u8, hn: i64, needle: *u8) -> i64 {
43 var nl: i64 = 0
44 while needle[nl] != (0 as u8) { nl = nl + 1 }
45 if nl == 0 { return 1 }
46 if nl > hn { return 0 }
47 let last: i64 = hn - nl
48 var i: i64 = 0
49 while i <= last {
50 var j: i64 = 0
51 var hit: i64 = 1
52 while j < nl {
53 if (hay[i+j] as i64) != (needle[j] as i64) { hit = 0; j = nl }
54 if hit == 1 { j = j + 1 }
55 }
56 if hit == 1 { return 1 }
57 i = i + 1
58 }
59 return 0
60}
61
62func gg_bytes_eq(a: *u8, an: i64, b: *u8, bn: i64) -> i64 {
63 if an != bn { return 0 }
64 var i: i64 = 0
65 while i < an { if (a[i] as i64) != (b[i] as i64) { return 0 } i = i + 1 }
66 return 1
67}
68
69func main() -> i64 {
70 let tpl: *u8 = "web_assets/_game_build/game_page.tpl.html" as *u8
71
72 // ===== publish the operator's pets3d co-op game FROM DATA =====
73 let p1: *u8 = "/tmp/nx_game_pets.html" as *u8
74 let fd1: i64 = sys_openat_wr(p1, 0x1a4)
75 var n1: i64 = 0 - 1
76 if fd1 >= 0 {
77 n1 = sgg_build(fd1, tpl,
78 "Nishi Pixelmon -- Co-op (sovereign)" as *u8, "/pets3d.wasm" as *u8,
79 "192" as *u8, "144" as *u8, "768" as *u8, "576" as *u8, "42" as *u8,
80 "Nishi Pixelmon co-op -- <b>WASD</b> move, <b>click</b>+<b>arrows</b> look, <b>Space</b> mine, <b>C</b> catch" as *u8)
81 sys_close(fd1)
82 }
83 let buf1: *u8 = sys_mmap(262144)
84 let len1: i64 = gg_slurp(p1, buf1, 262143)
85
86 var pass: i64 = 0
87 var rows: i64 = 0
88 var ok: i64 = 0
89
90 // R0: built, non-empty
91 ok = 0; if n1 > 0 { if len1 > 0 { ok = 1 } }
92 rows=rows+1; pass=pass+gg_row(0, ok, "game page builds from data (non-empty)" as *u8)
93
94 // R1: canvas internal dims came from data
95 ok = 0; if gg_contains(buf1, len1, "width=\"192\"" as *u8)==1 { if gg_contains(buf1, len1, "height=\"144\"" as *u8)==1 { ok=1 } }
96 rows=rows+1; pass=pass+gg_row(1, ok, "data-driven canvas framebuffer dims present (192x144)" as *u8)
97
98 // R2: the wasm is fetched from the data-driven url
99 ok = gg_contains(buf1, len1, "fetch('/pets3d.wasm')" as *u8)
100 rows=rows+1; pass=pass+gg_row(2, ok, "data-driven wasm url fetched (/pets3d.wasm)" as *u8)
101
102 // R3: the LAST-MILE wasm ABI binding is intact (the proven shim, verbatim)
103 ok = 0
104 if gg_contains(buf1, len1, "WebAssembly.instantiate" as *u8)==1 { if gg_contains(buf1, len1, "E.gi_init()" as *u8)==1 { if gg_contains(buf1, len1, "E.gi_tick()" as *u8)==1 { if gg_contains(buf1, len1, "gi_net_snapshot" as *u8)==1 { if gg_contains(buf1, len1, "gi_net_ingest_roster" as *u8)==1 { ok=1 } } } } }
105 rows=rows+1; pass=pass+gg_row(3, ok, "last-mile wasm ABI binding intact (instantiate + gi_init/tick/net_*)" as *u8)
106
107 // R4: multiplayer relay endpoints present
108 ok = 0; if gg_contains(buf1, len1, "/pstate" as *u8)==1 { if gg_contains(buf1, len1, "/roster" as *u8)==1 { ok=1 } }
109 rows=rows+1; pass=pass+gg_row(4, ok, "multiplayer relay endpoints present (/pstate + /roster)" as *u8)
110
111 // R5: title from data
112 ok = gg_contains(buf1, len1, "Nishi Pixelmon -- Co-op (sovereign)" as *u8)
113 rows=rows+1; pass=pass+gg_row(5, ok, "data-driven page title present" as *u8)
114
115 // R6: default room from data (rendered into the room span)
116 ok = gg_contains(buf1, len1, ">42</b>" as *u8)
117 rows=rows+1; pass=pass+gg_row(6, ok, "data-driven default room present in room span (42)" as *u8)
118
119 // R7: the LAST-MILE sovereignty banner is present (JS is documented as pure transport)
120 ok = 0; if gg_contains(buf1, len1, "LAST-MILE ONLY" as *u8)==1 { if gg_contains(buf1, len1, "ZERO game or netcode logic" as *u8)==1 { ok=1 } }
121 rows=rows+1; pass=pass+gg_row(7, ok, "last-mile sovereignty banner present (logic lives in the wasm)" as *u8)
122
123 // R8: a SECOND, different game is genuinely different (data-driven, not a hardcoded one-off)
124 let p8: *u8 = "/tmp/nx_game_sky.html" as *u8
125 let fd8: i64 = sys_openat_wr(p8, 0x1a4)
126 if fd8 >= 0 {
127 sgg_build(fd8, tpl,
128 "Sky Racer" as *u8, "/skyracer.wasm" as *u8,
129 "256" as *u8, "144" as *u8, "1024" as *u8, "576" as *u8, "7" as *u8,
130 "Sky Racer -- <b>arrows</b> steer, <b>Space</b> boost" as *u8)
131 sys_close(fd8)
132 }
133 let buf8: *u8 = sys_mmap(262144)
134 let len8: i64 = gg_slurp(p8, buf8, 262143)
135 ok = 0
136 if gg_contains(buf8, len8, "fetch('/skyracer.wasm')" as *u8)==1 { if gg_contains(buf8, len8, "Sky Racer" as *u8)==1 { if gg_contains(buf8, len8, "/pets3d.wasm" as *u8)==0 { ok=1 } } }
137 rows=rows+1; pass=pass+gg_row(8, ok, "2nd different game built from data; no bleed from the 1st (general, not one-off)" as *u8)
138
139 // R9: same data builds BYTE-IDENTICAL twice (reproducible)
140 let p9: *u8 = "/tmp/nx_game_pets2.html" as *u8
141 let fd9: i64 = sys_openat_wr(p9, 0x1a4)
142 if fd9 >= 0 {
143 sgg_build(fd9, tpl,
144 "Nishi Pixelmon -- Co-op (sovereign)" as *u8, "/pets3d.wasm" as *u8,
145 "192" as *u8, "144" as *u8, "768" as *u8, "576" as *u8, "42" as *u8,
146 "Nishi Pixelmon co-op -- <b>WASD</b> move, <b>click</b>+<b>arrows</b> look, <b>Space</b> mine, <b>C</b> catch" as *u8)
147 sys_close(fd9)
148 }
149 let buf9: *u8 = sys_mmap(262144)
150 let len9: i64 = gg_slurp(p9, buf9, 262143)
151 ok = gg_bytes_eq(buf1, len1, buf9, len9)
152 rows=rows+1; pass=pass+gg_row(9, ok, "deterministic: same game data -> byte-identical build (reproducible)" as *u8)
153
154 // R10 (NEG): a malicious title is NEUTRALIZED (escaped, cannot break out of the page)
155 let p10: *u8 = "/tmp/nx_game_inj.html" as *u8
156 let fd10: i64 = sys_openat_wr(p10, 0x1a4)
157 if fd10 >= 0 {
158 sgg_build(fd10, tpl,
159 "</title><script>alert(1)</script>" as *u8, "/pets3d.wasm" as *u8,
160 "192" as *u8, "144" as *u8, "768" as *u8, "576" as *u8, "42" as *u8,
161 "ctl" as *u8)
162 sys_close(fd10)
163 }
164 let buf10: *u8 = sys_mmap(262144)
165 let len10: i64 = gg_slurp(p10, buf10, 262143)
166 ok = 0
167 if gg_contains(buf10, len10, "<script>alert(1)" as *u8)==0 { if gg_contains(buf10, len10, "<script>alert(1)" as *u8)==1 { ok=1 } }
168 rows=rows+1; pass=pass+gg_row(10, ok, "injection-safe: malicious title escaped, no raw <script>alert (neg control)" as *u8)
169
170 gg_w("NX-SITEGEN-GAME-GATE rows=" as *u8); gg_num(rows); gg_w(" pass=" as *u8); gg_num(pass); gg_w("\n" as *u8)
171 if pass == rows {
172 let line: *u8 = sys_mmap(256)
173 var off: i64 = gg_cat(line, 0, "CMSGATE row=nx_sitegen_game game-publish rows=" as *u8)
174 off = gg_catnum(line, off, rows)
175 off = gg_cat(line, off, " pass=" as *u8); off = gg_catnum(line, off, pass)
176 off = gg_cat(line, off, " verdict=PASS\n" as *u8)
177 let gf: i64 = sys_openat_append("knowledge/status/cms_gate.log" as *u8, 0x1a4)
178 if gf >= 0 { sys_write(gf, line, off); sys_close(gf) }
179 gg_w("NX-SITEGEN-GAME-GATE verdict=PASS -- game-publish page type recorded in cms_gate.log\n" as *u8)
180 sys_exit(0); return 0
181 }
182 gg_w("NX-SITEGEN-GAME-GATE verdict=FAIL -- NOT recorded (no fake-green)\n" as *u8)
183 sys_exit(1); return 1
184}