nx_fetch_any_gate.nx source
↩ module page · 114 lines · 6809 B
1// nx_fetch_any_gate.nx -- teeth for the intelligent fetch dispatcher (operator 2026-07-14: researcher/census/
2// adversary/critic must "not get errors and return nothing without informing the user"). ADVERSARY DISCIPLINE:
3// the classifier is LAW only if it discriminates a REAL page from the exact thing that fools a naive fetcher --
4// a Cloudflare 200-OK CHALLENGE page. Gates fa_status / fa_challenge / fa_is_real / fa_body_off / fa_to_body.
5// license_tier: ORIGINAL
6//
7// D001 MIGRATION 2026-09-01 (/compare/mediaingest). This gate had no elf, and the reason was not neglect:
8// /api/promote REFUSES a gate that rolls its own verdict, because nothing outside can read its outcome --
9// nx_gate_green cannot judge it and it records no harness.jrnl frame, so flake and erosion stay invisible.
10// The teeth here were already good (13 of them, with a real adversary case), which is precisely why the
11// refusal was expensive: a well-designed gate that cannot be promoted proves nothing at all.
12// EVERY SUBJECT, ORDER AND ASSERTION IS UNCHANGED. Only the verdict machinery moved onto nx_gate_verdict:
13// gv_ctr/gv_check make declared == executed by construction (the hand-rolled pass/tot incremented both from
14// the same line, so a tooth that stopped running lowered nothing and still read GREEN), and gv_verdict puts
15// the outcome in the exit code where the judge can reach it.
16// The four teeth that assert a REFUSAL are renamed neg-control-* so the L2 census can SEE them -- they always
17// were negative controls, and were invisible purely by naming.
18import "nx_syscalls.nx"
19import "nx_fetch_any.nx"
20import "nx_gate_verdict.nx"
21
22func g_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
23
24const G_B: i64 = 66 // 'B' -- the first byte of BODYSTART, so the body-offset teeth assert a byte we chose
25const G_HTTP_OK: i64 = 200
26const G_HTTP_FORBIDDEN: i64 = 403
27const G_HTTP_ERR: i64 = 500
28const G_TINY_LEN: i64 = 2
29const G_CPCAP: i64 = 4096
30
31func main() -> i64 {
32 gv_head("=== nx_fetch_any_gate -- the anti-silent-bad fetch classifier (mediaingest) ===" as *u8)
33 let c: *i64 = gv_ctr()
34
35 // fixtures (>256B so the challenge/real verdict is driven by CONTENT, not the size guard)
36 let real: *u8 = "<!doctype html><html><head><title>Jellyfin Media Server</title></head><body><h1>Jellyfin</h1><p>Jellyfin is a free software media system that organizes movies shows music and photos. This page documents trickplay scrub previews chapter markers hardware transcoding HLS delivery keyframe accurate seeking subtitles and multi user watch state.</p></body></html>" as *u8
37 let ch: *u8 = "<!doctype html><html><head><title>Just a moment...</title></head><body><div class=cf-chl-widget id=challenge-platform><h1>Checking your browser before accessing the site.</h1><p>Enable JavaScript and cookies to continue. This process is automatic and your browser will redirect shortly.</p></div></body></html>" as *u8
38
39 // ASSERT THE FIXTURES REACHED THE CONDITION BEFORE ASSERTING ANY OUTCOME: both must clear the size guard,
40 // or every verdict below would be decided by LENGTH and the content classifier would never be exercised.
41 var fx: i64 = 0
42 if g_slen(real) > 256 { if g_slen(ch) > 256 { fx = 1 } }
43 gv_check("fixture-reached-both-pages-exceed-the-size-guard" as *u8, fx, c)
44
45 // ---- fa_status parses the HTTP code ----
46 var s1: i64 = 0
47 if fa_status("HTTP/1.1 200 OK\r\nX: y\r\n\r\nhi" as *u8, g_slen("HTTP/1.1 200 OK\r\nX: y\r\n\r\nhi" as *u8)) == G_HTTP_OK { s1 = 1 }
48 gv_check("status-line-200-parsed" as *u8, s1, c)
49
50 var s2: i64 = 0
51 if fa_status("HTTP/1.1 403 Forbidden\r\n\r\n" as *u8, g_slen("HTTP/1.1 403 Forbidden\r\n\r\n" as *u8)) == G_HTTP_FORBIDDEN { s2 = 1 }
52 gv_check("status-line-403-parsed" as *u8, s2, c)
53
54 var s3: i64 = 0
55 if fa_status("<html>no headers here</html>" as *u8, g_slen("<html>no headers here</html>" as *u8)) == 0 { s3 = 1 }
56 gv_check("neg-control-body-with-no-status-line-yields-zero" as *u8, s3, c)
57
58 // ---- fa_challenge detects bot-walls, passes real content ----
59 var c1: i64 = 0
60 if fa_challenge(ch, g_slen(ch)) == 1 { c1 = 1 }
61 gv_check("cloudflare-challenge-page-detected" as *u8, c1, c)
62
63 var c2: i64 = 0
64 if fa_challenge(real, g_slen(real)) == 0 { c2 = 1 }
65 gv_check("neg-control-real-page-not-flagged-as-challenge" as *u8, c2, c)
66
67 // ---- fa_is_real -- THE anti-silent-bad core ----
68 var r1: i64 = 0
69 if fa_is_real(G_HTTP_OK, real, g_slen(real)) == 1 { r1 = 1 }
70 gv_check("real-200-page-accepted" as *u8, r1, c)
71
72 // THE ADVERSARY TOOTH. A 200-OK Cloudflare challenge is exactly what fools a naive fetcher: the status
73 // says success and the body is a wall. If this one tooth passes for the wrong reason the whole organ is
74 // decorative, because every OTHER tooth here can be satisfied by a classifier that keys on status alone.
75 var r2: i64 = 0
76 if fa_is_real(G_HTTP_OK, ch, g_slen(ch)) == 0 { r2 = 1 }
77 gv_check("neg-control-200-OK-CHALLENGE-is-rejected-status-alone-is-not-enough" as *u8, r2, c)
78
79 var r3: i64 = 0
80 if fa_is_real(G_HTTP_FORBIDDEN, ch, g_slen(ch)) == 0 { r3 = 1 }
81 gv_check("neg-control-403-rejected" as *u8, r3, c)
82
83 var r4: i64 = 0
84 if fa_is_real(G_HTTP_OK, "hi" as *u8, G_TINY_LEN) == 0 { r4 = 1 }
85 gv_check("neg-control-tiny-body-rejected" as *u8, r4, c)
86
87 var r5: i64 = 0
88 if fa_is_real(G_HTTP_ERR, real, g_slen(real)) == 0 { r5 = 1 }
89 gv_check("neg-control-5xx-rejected-even-with-a-real-body" as *u8, r5, c)
90
91 // ---- fa_body_off / fa_to_body strip HTTP headers so banked content is clean ----
92 let resp: *u8 = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nServer: nishi\r\n\r\nBODYSTART then real content long enough to matter for extraction padded padded padded." as *u8
93 let rl: i64 = g_slen(resp)
94 let bo: i64 = fa_body_off(resp, rl)
95 var b1: i64 = 0
96 if bo > 0 { if resp[bo] == (G_B as u8) { b1 = 1 } }
97 gv_check("body-offset-lands-on-the-first-body-byte" as *u8, b1, c)
98
99 // shift into a COPY (never write a string literal)
100 let cp: *u8 = sys_mmap(G_CPCAP)
101 var i: i64 = 0
102 while i < rl { cp[i] = resp[i]; i = i + 1 }
103 let bl: i64 = fa_to_body(cp, rl)
104 var b2: i64 = 0
105 if cp[0] == (G_B as u8) { b2 = 1 }
106 gv_check("in-place-shift-puts-the-body-at-offset-zero" as *u8, b2, c)
107
108 var b3: i64 = 0
109 if bl < rl { b3 = 1 }
110 gv_check("stripped-length-is-shorter-than-the-response" as *u8, b3, c)
111
112 return gv_verdict("nx_fetch_any_gate" as *u8, c,
113 "subject: fa_status, fa_challenge, fa_is_real, fa_body_off and fa_to_body over a real page and a Cloudflare challenge -- D001-migrated 2026-09-01, teeth and assertions unchanged, refusal teeth renamed neg-control so the census can see them" as *u8)
114}