code wiki / _hdl_build / nx_media_jsexec_gate.nx
nx_media_jsexec_gate.nx source
↩ module page · 97 lines · 6593 B
1import "nx_gate_gn.nx"
2import "nx_gate_base.nx"
3// nx_media_jsexec_gate.nx -- proves the JS-EXECUTION media harvester surfaces media URLs that page JS
4// CONSTRUCTS AT RUNTIME (function return, template literal, class method, regex extraction) and hands to
5// fetch() -- exactly the URLs a STATIC byte/urlfold scan cannot compute. Also proves the media-extension
6// FILTER (api/config fetches ignored) and de-dup. Fetches PEND (no network) -> safe. license_tier: ORIGINAL expect_exit: 0
7import "nx_syscalls.nx"
8import "nx_js_eval.nx"
9import "nx_media_jsexec.nx"
10
11func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw("
12" as *u8); return ok }
13func gsl(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
14
15func has_url(outbuf: *u8, want: *u8) -> i64 { return mjx_contains(outbuf, gsl(outbuf), want, gsl(want)) }
16
17// run page JS `src`, expect EXACTLY `wantcnt` media URLs collected AND `wanturl` among them.
18func run_expect(src: *u8, wanturl: *u8, wantcnt: i64) -> i64 {
19 let outbuf: *u8 = sys_mmap(4096)
20 let outbox: *i64 = sys_mmap(16) as *i64
21 let cnt: i64 = mjx_harvest_js(src, gsl(src), outbuf, 4096, outbox)
22 if cnt != wantcnt { gw(" (got cnt=" as *u8); gn(cnt); gw(" urls=[" as *u8); gw(outbuf); gw("])\n" as *u8); return 0 }
23 if has_url(outbuf, wanturl) == 0 { gw(" (missing '" as *u8); gw(wanturl); gw("' in [" as *u8); gw(outbuf); gw("])\n" as *u8); return 0 }
24 return 1
25}
26
27func main() -> i64 {
28 gw("media-jsexec SOVEREIGN gate (run page JS -> capture RUNTIME-BUILT fetch URLs a static scan misses)\n" as *u8)
29 var pass: i64 = 0
30 var ttl: i64 = 0
31
32 // M1: FUNCTION-built URL -- static folding cannot evaluate cdn(vid).
33 ttl=ttl+1; pass=pass+grow("M1 function-built https://media.example.com/ABC123/master.m3u8\x00" as *u8,
34 run_expect("function cdn(id){return 'https://media.example.com/'+id+'/master.m3u8';} var vid='ABC123'; fetch(cdn(vid));" as *u8,
35 "https://media.example.com/ABC123/master.m3u8\x00" as *u8, 1))
36 // M2: TEMPLATE-LITERAL URL with two interpolations.
37 ttl=ttl+1; pass=pass+grow("M2 template `https://cdn.host/${vid}/hls/${q}/index.m3u8`\x00" as *u8,
38 run_expect("var vid='XY9'; var q='720'; fetch(`https://cdn.host/${vid}/hls/${q}/index.m3u8`);" as *u8,
39 "https://cdn.host/XY9/hls/720/index.m3u8\x00" as *u8, 1))
40 // M3: CLASS-METHOD-built URL (this.id in a method).
41 ttl=ttl+1; pass=pass+grow("M3 class method https://v.cdn/42.mp4\x00" as *u8,
42 run_expect("class Player{constructor(id){this.id=id;} url(){return 'https://v.cdn/'+this.id+'.mp4';}} var p=new Player('42'); fetch(p.url());" as *u8,
43 "https://v.cdn/42.mp4\x00" as *u8, 1))
44 // M4: REGEX-EXTRACTED id then fetch -- the real pattern (pull an id out of page text, build the URL).
45 ttl=ttl+1; pass=pass+grow("M4 regex-extracted id -> https://cdn/98765/stream.m3u8\x00" as *u8,
46 run_expect("var page='...video_id=98765&more...'; var m=page.match(/video_id=(\\d+)/); fetch('https://cdn/'+m[1]+'/stream.m3u8');" as *u8,
47 "https://cdn/98765/stream.m3u8\x00" as *u8, 1))
48 // M5: MEDIA FILTER -- a config/api fetch is IGNORED, only the .mp4 is collected.
49 ttl=ttl+1
50 let ob5: *i64 = sys_mmap(16) as *i64
51 let buf5: *u8 = sys_mmap(4096)
52 let src5: *u8 = "fetch('https://api.host/config.json'); fetch('https://v.cdn/movie.mp4');" as *u8
53 let c5: i64 = mjx_harvest_js(src5, gsl(src5), buf5, 4096, ob5)
54 var m5ok: i64 = 1
55 if c5 != 1 { m5ok = 0 }
56 if has_url(buf5, "https://v.cdn/movie.mp4\x00" as *u8) == 0 { m5ok = 0 }
57 if has_url(buf5, "config.json\x00" as *u8) == 1 { m5ok = 0 } // the non-media fetch must be filtered out
58 pass=pass+grow("M5 filter: .mp4 kept, config.json dropped (cnt=1)\x00" as *u8, m5ok)
59 // M6: DE-DUP -- same manifest fetched twice + one segment = 2 unique.
60 ttl=ttl+1; pass=pass+grow("M6 dedup: 2x same .m3u8 + 1 .mp4 -> 2 unique\x00" as *u8,
61 run_expect("fetch('https://c/a.m3u8'); fetch('https://c/a.m3u8'); fetch('https://c/b.mp4');" as *u8,
62 "https://c/b.mp4\x00" as *u8, 2))
63 // M7: NO media (only an api call) -> 0 collected (harvester is quiet, not noisy).
64 ttl=ttl+1
65 let ob7: *i64 = sys_mmap(16) as *i64
66 let buf7: *u8 = sys_mmap(4096)
67 let src7: *u8 = "fetch('https://api.host/ping'); var x=1+1;" as *u8
68 let c7: i64 = mjx_harvest_js(src7, gsl(src7), buf7, 4096, ob7)
69 pass=pass+grow("M7 no-media page -> 0 collected\x00" as *u8, (c7 == 0) as i64)
70 // --- the ACTUAL patterns real video sites use (arrow fns, forEach ladders) ---
71 // M8: ARROW-function-built URL (arrows dominate modern bundles).
72 ttl=ttl+1; pass=pass+grow("M8 arrow-built https://cdn/99/v.mp4\x00" as *u8,
73 run_expect("var mk = id => 'https://cdn/'+id+'/v.mp4'; fetch(mk('99'));" as *u8,
74 "https://cdn/99/v.mp4\x00" as *u8, 1))
75 // M9: forEach over an id list -> multiple media (the "fetch each segment" shape).
76 ttl=ttl+1; pass=pass+grow("M9 forEach id-list -> 2 media (a.mp4,b.mp4)\x00" as *u8,
77 run_expect("var ids=['a','b']; ids.forEach(function(x){ fetch('https://cdn/'+x+'.mp4'); });" as *u8,
78 "https://cdn/b.mp4\x00" as *u8, 2))
79 // M10: QUALITY LADDER -- arrow + forEach + template (exactly how HLS/DASH variant lists get built).
80 ttl=ttl+1; pass=pass+grow("M10 quality ladder 720/1080 .m3u8 -> 2\x00" as *u8,
81 run_expect("['720','1080'].forEach(q => fetch(`https://cdn/vid/${q}.m3u8`));" as *u8,
82 "https://cdn/vid/1080.m3u8\x00" as *u8, 2))
83 // M11: DIRECT proof the js_pending_total two-step-cast FIX holds -- 3 fetches must return EXACTLY 3
84 // (the inline-cast bug returned a pointer-magnitude garbage value ~1.4e14; this asserts it's gone).
85 ttl=ttl+1
86 let obT: *i64 = sys_mmap(16) as *i64
87 let srcT: *u8 = "fetch('https://a/1.mp4'); fetch('https://b/2.mp4'); fetch('https://c/3.mp4');" as *u8
88 js_run_source_keep(srcT, gsl(srcT), obT)
89 let genvT: *i64 = (obT[1]) as *i64
90 var totT: i64 = 0 - 1
91 if (genvT as i64) != 0 { totT = js_pending_total(genvT) }
92 pass=pass+grow("M11 js_pending_total==3 (inline-cast fix verified, not garbage)\x00" as *u8, (totT == 3) as i64)
93
94 gw("pass=" as *u8); gn(pass); gw("/" as *u8); gn(ttl); gw("\n" as *u8)
95 if pass == ttl { gw("verdict=GREEN (JS execution surfaces runtime-built media URLs a static scan misses)\n" as *u8); sys_exit(0); return 0 }
96 gw("verdict=RED\n" as *u8); sys_exit(1); return 1
97}