code wiki / _hdl_build / nx_media_cover_gate.nx

nx_media_cover_gate.nx source

↩ module page · 83 lines · 5043 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" 9import "nx_gate_verdict.nx" 10 11func gp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 12func 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 } 13func spawn(path: *u8, a0: *u8, a1: *u8, redir: *u8) -> i64 { 14 let pid: i64 = sys_fork() 15 if pid == 0 { 16 let fd: i64 = sys_openat_wr(redir, 420) 17 if fd >= 0 { sys_dup3(fd, 1, 0); sys_dup3(fd, 2, 0) } 18 let argv: *i64 = sys_mmap(64) as *i64 19 var n: i64 = 0 20 argv[0] = path as i64; n = 1 21 if (a0 as i64) != 0 { argv[n] = a0 as i64; n = n + 1 } 22 if (a1 as i64) != 0 { argv[n] = a1 as i64; n = n + 1 } 23 argv[n] = 0 24 let envp: *i64 = sys_mmap(16) as *i64 25 envp[0] = "PATH=/usr/bin:/bin\x00" as *u8 as i64; envp[1] = 0 26 sys_execve(path, argv, envp) 27 sys_exit(127) 28 } 29 let st: *i64 = sys_mmap(16) as *i64 30 sys_wait4(pid, st, 0) 31 return (st[0] >> 8) & 0xff 32} 33 34func main() -> i64 { 35 gp("=== nx_media_cover_gate: book path -> embedded cover image (extract-on-miss) = the live /api/cover logic ===\n" as *u8) 36 let RUNNER: *u8 = "_offc/nx_sov_build_run.elf\x00" as *u8 37 let SC: *u8 = "knowledge/status/media_cover_scratch.log\x00" as *u8 38 spawn(RUNNER, "nx_mobi_fixture_build\x00" as *u8, 0 as *u8, SC) 39 40 var pass: i64 = 0; var fail: i64 = 0 41 42 // (1) CORE: a MOBI with an EXTH-201 embedded GIF cover resolves to a real GIF file on disk. 43 let FIX: *u8 = "knowledge/fixtures/nishi_mobi_cover.mobi\x00" as *u8 44 let full: *u8 = sys_mmap(512) 45 let r1: i64 = mc_cover_resolve(FIX, mc_slen(FIX), full, 512) 46 gp(" resolve(cover.mobi)=" as *u8); gnum(r1); gp(" -> " as *u8); if r1==1 { gp(full) } gp("\n" as *u8) 47 if r1 == 1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL cover did not resolve\n" as *u8) } 48 // read the resolved file + check GIF magic (G I F = 71 73 70) -- a REAL embedded image, not a placeholder 49 if r1 == 1 { 50 let lb: *i64 = sys_mmap(8) as *i64 51 let img: *u8 = sys_read_file(full, lb) 52 var okmagic: i64 = 0 53 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 } } } } } 54 if okmagic == 1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL cover is not a real GIF image\n" as *u8) } 55 } 56 // the resolved path lands under the reader root + a 'b'-prefixed slug dir (slug logic ran) 57 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) } } 58 59 // (2) LIAR-KILL traversal: a path containing ".." is refused (-1), never resolved to a file. 60 let EVIL: *u8 = "knowledge/fixtures/../../etc/passwd\x00" as *u8 61 let f2: *u8 = sys_mmap(512) 62 let r2: i64 = mc_cover_resolve(EVIL, mc_slen(EVIL), f2, 512) 63 gp(" resolve(../traversal)=" as *u8); gnum(r2); gp("\n" as *u8) 64 if r2 == (0 - 1) { pass=pass+1 } else { fail=fail+1; gp(" FAIL traversal not refused\n" as *u8) } 65 66 // (3) graceful miss: an unsupported/absent book yields 0 (grid shows a text card), not a crash or false image. 67 let NONE: *u8 = "knowledge/fixtures/this_is_not_a_book.xyz\x00" as *u8 68 let f3: *u8 = sys_mmap(512) 69 let r3: i64 = mc_cover_resolve(NONE, mc_slen(NONE), f3, 512) 70 gp(" resolve(unsupported.xyz)=" as *u8); gnum(r3); gp("\n" as *u8) 71 if r3 == 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL unsupported did not miss gracefully\n" as *u8) } 72 73 gp("MEDIA-COVER-GATE pass=" as *u8); gnum(pass); gp(" fail=" as *u8); gnum(fail) 74 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 75 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 76 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 77 let ctr__dry: *i64 = gv_ctr() 78 ctr__dry[0] = pass 79 ctr__dry[1] = pass + fail 80 let rc__dry: i64 = gv_verdict("MEDIA-COVER-GATE" as *u8, ctr__dry, "embedded cover -> real image; traversal refused; graceful miss)" as *u8) 81 sys_exit(rc__dry) 82 return rc__dry 83}