code wiki / _hdl_build / nx_media_cover_gate.nx
nx_media_cover_gate.nx source
↩ module page · 75 lines · 4653 B
1// nx_media_cover_gate.nx -- gate /api/cover resolution: a book path -> its EMBEDDED cover image on disk, extracting
2// on cache-miss (nx_media_cover_lib::mc_cover_resolve, the EXACT logic the live media server's /api/cover uses).
3// Proves: a real MOBI carrying an EXTH-201 embedded cover resolves to a real image file (GIF magic, the fixture's
4// embedded bytes); a path-traversal attempt is refused (-1); an unsupported/absent book yields a graceful miss (0).
5// No generation, no download -- embedded only (operator: extract embedded, no internet). expect_exit: 0
6// license_tier: ORIGINAL
7import "nx_syscalls.nx"
8import "nx_media_cover_lib.nx"
9
10func gp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
11func gnum(v0: i64) -> i64 { var v: i64=v0; if v<0 { sys_write(1,"-" as *u8,1); v=0-v } let b: *u8=sys_mmap(24); var k: i64=0; if v==0 {b[0]=48 as u8;k=1} while v>0 {b[k]=(48+(v%10)) as u8; v=v/10; k=k+1} let o: *u8=sys_mmap(24); var j: i64=0; while j<k {o[j]=b[k-1-j];j=j+1} sys_write(1,o,k); return 0 }
12func spawn(path: *u8, a0: *u8, a1: *u8, redir: *u8) -> i64 {
13 let pid: i64 = sys_fork()
14 if pid == 0 {
15 let fd: i64 = sys_openat_wr(redir, 420)
16 if fd >= 0 { sys_dup3(fd, 1, 0); sys_dup3(fd, 2, 0) }
17 let argv: *i64 = sys_mmap(64) as *i64
18 var n: i64 = 0
19 argv[0] = path as i64; n = 1
20 if (a0 as i64) != 0 { argv[n] = a0 as i64; n = n + 1 }
21 if (a1 as i64) != 0 { argv[n] = a1 as i64; n = n + 1 }
22 argv[n] = 0
23 let envp: *i64 = sys_mmap(16) as *i64
24 envp[0] = "PATH=/usr/bin:/bin\x00" as *u8 as i64; envp[1] = 0
25 sys_execve(path, argv, envp)
26 sys_exit(127)
27 }
28 let st: *i64 = sys_mmap(16) as *i64
29 sys_wait4(pid, st, 0)
30 return (st[0] >> 8) & 0xff
31}
32
33func main() -> i64 {
34 gp("=== nx_media_cover_gate: book path -> embedded cover image (extract-on-miss) = the live /api/cover logic ===\n" as *u8)
35 let RUNNER: *u8 = "_offc/nx_sov_build_run.elf\x00" as *u8
36 let SC: *u8 = "knowledge/status/media_cover_scratch.log\x00" as *u8
37 spawn(RUNNER, "nx_mobi_fixture_build\x00" as *u8, 0 as *u8, SC)
38
39 var pass: i64 = 0; var fail: i64 = 0
40
41 // (1) CORE: a MOBI with an EXTH-201 embedded GIF cover resolves to a real GIF file on disk.
42 let FIX: *u8 = "knowledge/fixtures/nishi_mobi_cover.mobi\x00" as *u8
43 let full: *u8 = sys_mmap(512)
44 let r1: i64 = mc_cover_resolve(FIX, mc_slen(FIX), full, 512)
45 gp(" resolve(cover.mobi)=" as *u8); gnum(r1); gp(" -> " as *u8); if r1==1 { gp(full) } gp("\n" as *u8)
46 if r1 == 1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL cover did not resolve\n" as *u8) }
47 // read the resolved file + check GIF magic (G I F = 71 73 70) -- a REAL embedded image, not a placeholder
48 if r1 == 1 {
49 let lb: *i64 = sys_mmap(8) as *i64
50 let img: *u8 = sys_read_file(full, lb)
51 var okmagic: i64 = 0
52 if (img as i64)!=0 { if lb[0] >= 6 { if img[0]==(71 as u8) { if img[1]==(73 as u8) { if img[2]==(70 as u8) { okmagic = 1 } } } } }
53 if okmagic == 1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL cover is not a real GIF image\n" as *u8) }
54 }
55 // the resolved path lands under the reader root + a 'b'-prefixed slug dir (slug logic ran)
56 if r1 == 1 { if mc_contains(full, mc_slen(full), "staging/media/reader/b" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL resolved path not under reader/<slug>\n" as *u8) } }
57
58 // (2) LIAR-KILL traversal: a path containing ".." is refused (-1), never resolved to a file.
59 let EVIL: *u8 = "knowledge/fixtures/../../etc/passwd\x00" as *u8
60 let f2: *u8 = sys_mmap(512)
61 let r2: i64 = mc_cover_resolve(EVIL, mc_slen(EVIL), f2, 512)
62 gp(" resolve(../traversal)=" as *u8); gnum(r2); gp("\n" as *u8)
63 if r2 == (0 - 1) { pass=pass+1 } else { fail=fail+1; gp(" FAIL traversal not refused\n" as *u8) }
64
65 // (3) graceful miss: an unsupported/absent book yields 0 (grid shows a text card), not a crash or false image.
66 let NONE: *u8 = "knowledge/fixtures/this_is_not_a_book.xyz\x00" as *u8
67 let f3: *u8 = sys_mmap(512)
68 let r3: i64 = mc_cover_resolve(NONE, mc_slen(NONE), f3, 512)
69 gp(" resolve(unsupported.xyz)=" as *u8); gnum(r3); gp("\n" as *u8)
70 if r3 == 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL unsupported did not miss gracefully\n" as *u8) }
71
72 gp("MEDIA-COVER-GATE pass=" as *u8); gnum(pass); gp(" fail=" as *u8); gnum(fail)
73 if fail == 0 { gp(" verdict=GREEN (embedded cover -> real image; traversal refused; graceful miss)\n" as *u8); sys_exit(0); return 0 }
74 gp(" verdict=RED\n" as *u8); sys_exit(1); return 1
75}