code wiki / _hdl_build / nx_ad_serve.nx
nx_ad_serve.nx source
↩ module page · 80 lines · 3942 B
1// nx_ad_serve.nx -- LIB: the "Brought to you by" PLACEMENT emitter for the performance ad engine
2// (ADS-004, CALLOUT-004). Emits a static, clean, first-party banner -- the polite old-time-radio
3// sponsorship byline -- with ZERO third-party JavaScript (the headline promise: no surveillance
4// auctions, no third-party scripts, no cross-site profiles). Impression COUNTS ride W5 first-party
5// pageview tracking (PRESENT) + the ADS-019 filter + ADS-007 meter; this organ's job is the clean
6// placement itself.
7//
8// DEFENSIVE AT THE BOUNDARY (rule 12): the sponsor's business name + banner text are EXTERNAL input,
9// so they are HTML-escaped before emission -- a sponsor named "<script>..." cannot inject script.
10// The emitted markup uses single-quoted attributes + inline first-party styling only; NO <script>,
11// <iframe>, src=, <object>, <embed> -- and `as_has_thirdparty_js` proves it (the gate also proves
12// that detector FIRES on a planted third-party tag, so GREEN means "proven clean", not "assumed").
13// license_tier: ORIGINAL
14import "nx_syscalls.nx"
15
16func as_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
17
18// append NUL-terminated s into dst at off; return new offset.
19func as_append(dst: *u8, off: i64, s: *u8) -> i64 {
20 var i: i64 = 0
21 while s[i] != (0 as u8) { dst[off + i] = s[i]; i = i + 1 }
22 return off + i
23}
24
25// append the first slen bytes of s into dst at off, HTML-ESCAPING < > & " ' -> entities. Boundary
26// defense: untrusted sponsor text can never break out of its element or inject markup.
27func as_append_escaped(dst: *u8, off: i64, s: *u8, slen: i64) -> i64 {
28 var o: i64 = off
29 var i: i64 = 0
30 while i < slen {
31 let c: i64 = s[i] as i64
32 var done: i64 = 0
33 if c == 60 { o = as_append(dst, o, "<" as *u8); done = 1 }
34 if c == 62 { if done == 0 { o = as_append(dst, o, ">" as *u8); done = 1 } }
35 if c == 38 { if done == 0 { o = as_append(dst, o, "&" as *u8); done = 1 } }
36 if c == 34 { if done == 0 { o = as_append(dst, o, """ as *u8); done = 1 } }
37 if c == 39 { if done == 0 { o = as_append(dst, o, "'" as *u8); done = 1 } }
38 if done == 0 { dst[o] = c as u8; o = o + 1 }
39 i = i + 1
40 }
41 return o
42}
43
44// emit the clean placement into out; returns byte length (out is NUL-terminated). First-party only.
45func as_emit_banner(business: *u8, blen: i64, bannertext: *u8, tlen: i64, out: *u8) -> i64 {
46 var o: i64 = 0
47 o = as_append(out, o, "<aside class='nishi-byline' role='note'><span class='nishi-byline-label'>Brought to you by </span><span class='nishi-byline-sponsor'>" as *u8)
48 o = as_append_escaped(out, o, business, blen)
49 o = as_append(out, o, "</span>. <span class='nishi-byline-msg'>" as *u8)
50 o = as_append_escaped(out, o, bannertext, tlen)
51 o = as_append(out, o, "</span></aside>" as *u8)
52 out[o] = 0 as u8
53 return o
54}
55
56// substring search: 1 if needle occurs in hay[0..n], else 0.
57func as_contains(hay: *u8, n: i64, needle: *u8) -> i64 {
58 let nl: i64 = as_len(needle)
59 if nl == 0 { return 1 }
60 var i: i64 = 0
61 while i + nl <= n {
62 var k: i64 = 0
63 var mt: i64 = 1
64 while k < nl { if hay[i + k] != needle[k] { mt = 0; k = nl } else { k = k + 1 } }
65 if mt == 1 { return 1 }
66 i = i + 1
67 }
68 return 0
69}
70
71// 1 if the markup carries ANY third-party-content / JS injection vector, else 0. A clean first-party
72// byline has none. The gate proves both: clean banner -> 0, planted tag -> 1 (anti-false-green).
73func as_has_thirdparty_js(html: *u8, n: i64) -> i64 {
74 if as_contains(html, n, "<script" as *u8) == 1 { return 1 }
75 if as_contains(html, n, "<iframe" as *u8) == 1 { return 1 }
76 if as_contains(html, n, "<object" as *u8) == 1 { return 1 }
77 if as_contains(html, n, "<embed" as *u8) == 1 { return 1 }
78 if as_contains(html, n, "src=" as *u8) == 1 { return 1 }
79 return 0
80}