pub const SENSOR_BUFFER_CAP: usize = 300;
pub const SENSOR_SNIPPET_LEN: usize = 512;
pub const SENSOR_SCRIPT: &str = r#"(function () {
try {
if (window.__meridian_signals__ && window.__meridian_signals__.__installed) return;
var CAP = 300, SNIP = 512;
var P = Array.prototype.push, slice = Function.prototype.call.bind(Array.prototype.slice);
var S = { __installed: true, sinks: [], console: [], errors: [], csp: [], postmessage: [] };
try { Object.defineProperty(window, "__meridian_signals__", { value: S, writable: true, enumerable: false, configurable: true }); }
catch (e) { window.__meridian_signals__ = S; }
var now = function () { try { return Date.now(); } catch (e) { return 0; } };
var snip = function (v) {
try {
var s = typeof v === "string" ? v : (function () { try { return JSON.stringify(v); } catch (e) { return String(v); } })();
if (s == null) return "";
return s.length > SNIP ? s.slice(0, SNIP) + "…" : s;
} catch (e) { return ""; }
};
var stack = function () { try { return (new Error().stack || "").split("\n").slice(2, 8).join("\n"); } catch (e) { return ""; } };
var rec = function (bucket, entry) {
try { entry.ts = now(); P.call(bucket, entry); if (bucket.length > CAP) bucket.splice(0, bucket.length - CAP); } catch (e) {}
};
// ---- DOM-XSS sinks -----------------------------------------------------
var hookSetter = function (proto, prop, sink) {
try {
var d = Object.getOwnPropertyDescriptor(proto, prop);
if (!d || !d.set) return;
var orig = d.set;
Object.defineProperty(proto, prop, {
configurable: true, enumerable: d.enumerable, get: d.get,
set: function (val) { rec(S.sinks, { sink: sink, tag: (this && this.tagName) || "", value: snip(val), stack: stack() }); return orig.call(this, val); }
});
} catch (e) {}
};
hookSetter(Element.prototype, "innerHTML", "innerHTML");
hookSetter(Element.prototype, "outerHTML", "outerHTML");
try {
var iah = Element.prototype.insertAdjacentHTML;
Element.prototype.insertAdjacentHTML = function (pos, html) { rec(S.sinks, { sink: "insertAdjacentHTML", tag: (this && this.tagName) || "", value: snip(html), stack: stack() }); return iah.apply(this, arguments); };
} catch (e) {}
try {
var dw = document.write;
document.write = function () { rec(S.sinks, { sink: "document.write", value: snip(slice(arguments).join("")), stack: stack() }); return dw.apply(this, arguments); };
} catch (e) {}
try {
var ev = window.eval;
window.eval = function (code) { rec(S.sinks, { sink: "eval", value: snip(code), stack: stack() }); return ev.apply(this, arguments); };
} catch (e) {}
try {
var setAttr = Element.prototype.setAttribute;
Element.prototype.setAttribute = function (name, value) {
try { var n = ("" + name).toLowerCase(); if (n.indexOf("on") === 0 || ((n === "src" || n === "href") && /^\s*javascript:/i.test("" + value))) rec(S.sinks, { sink: "setAttribute:" + n, tag: (this && this.tagName) || "", value: snip(value), stack: stack() }); } catch (e) {}
return setAttr.apply(this, arguments);
};
} catch (e) {}
try {
var sd = Object.getOwnPropertyDescriptor(HTMLScriptElement.prototype, "src");
if (sd && sd.set) { var so = sd.set; Object.defineProperty(HTMLScriptElement.prototype, "src", { configurable: true, get: sd.get, set: function (u) { rec(S.sinks, { sink: "script.src", value: snip(u), stack: stack() }); return so.call(this, u); } }); }
} catch (e) {}
// ---- console -----------------------------------------------------------
try {
["log", "info", "warn", "error", "debug"].forEach(function (level) {
var orig = console[level];
if (typeof orig !== "function") return;
console[level] = function () { try { rec(S.console, { level: level, text: snip(slice(arguments).map(function (a) { return typeof a === "string" ? a : snip(a); }).join(" ")) }); } catch (e) {} return orig.apply(this, arguments); };
});
} catch (e) {}
// ---- uncaught errors + rejections -------------------------------------
try { window.addEventListener("error", function (e) { rec(S.errors, { kind: "error", message: snip(e && e.message), filename: (e && e.filename) || "", line: (e && e.lineno) || 0, col: (e && e.colno) || 0, stack: snip(e && e.error && e.error.stack) }); }, true); } catch (e) {}
try { window.addEventListener("unhandledrejection", function (e) { rec(S.errors, { kind: "unhandledrejection", message: snip(e && e.reason && (e.reason.message || e.reason)) }); }, true); } catch (e) {}
// ---- CSP violations ----------------------------------------------------
try { document.addEventListener("securitypolicyviolation", function (e) { rec(S.csp, { directive: (e && e.violatedDirective) || "", blocked: (e && e.blockedURI) || "", source: (e && e.sourceFile) || "", line: (e && e.lineNumber) || 0, sample: snip(e && e.sample) }); }, true); } catch (e) {}
// ---- inbound postMessage ----------------------------------------------
try { window.addEventListener("message", function (e) { rec(S.postmessage, { origin: (e && e.origin) || "", data: snip(e && e.data) }); }, true); } catch (e) {}
} catch (e) {}
return true;
})()"#;
pub fn sensor_reader(clear: bool) -> String {
format!(
r#"(function () {{
var S = window.__meridian_signals__;
if (!S) return {{ installed: false, sinks: [], console: [], errors: [], csp: [], postmessage: [] }};
var snap = {{ installed: true,
sinks: S.sinks.slice(), console: S.console.slice(), errors: S.errors.slice(),
csp: S.csp.slice(), postmessage: S.postmessage.slice(),
counts: {{ sinks: S.sinks.length, console: S.console.length, errors: S.errors.length, csp: S.csp.length, postmessage: S.postmessage.length }} }};
if ({clear}) {{ S.sinks.length = 0; S.console.length = 0; S.errors.length = 0; S.csp.length = 0; S.postmessage.length = 0; }}
return snap;
}})()"#
)
}
#[cfg(test)]
mod tests {
use super::*;
fn balanced(s: &str) -> bool {
let (mut paren, mut brace, mut bracket) = (0i32, 0i32, 0i32);
for c in s.chars() {
match c {
'(' => paren += 1,
')' => paren -= 1,
'{' => brace += 1,
'}' => brace -= 1,
'[' => bracket += 1,
']' => bracket -= 1,
_ => {}
}
}
paren == 0 && brace == 0 && bracket == 0
}
#[test]
fn sensor_script_is_idempotent_iife() {
assert!(SENSOR_SCRIPT.trim_start().starts_with("(function"));
assert!(SENSOR_SCRIPT.contains("__meridian_signals__"));
assert!(SENSOR_SCRIPT.contains("__installed"));
assert!(
balanced(SENSOR_SCRIPT),
"sensor script must be bracket-balanced"
);
}
#[test]
fn sensor_script_covers_every_sensor_category() {
for needle in [
"innerHTML",
"outerHTML",
"insertAdjacentHTML",
"document.write",
"window.eval",
"setAttribute",
"script.src",
"console[level]",
"\"error\"",
"unhandledrejection",
"securitypolicyviolation",
"\"message\"",
] {
assert!(
SENSOR_SCRIPT.contains(needle),
"sensor script missing hook: {needle}"
);
}
}
#[test]
fn sensor_reader_clear_flag_threads_through() {
assert!(sensor_reader(true).contains("if (true)"));
assert!(sensor_reader(false).contains("if (false)"));
assert!(balanced(&sensor_reader(true)));
assert!(sensor_reader(true).contains("counts"));
}
#[allow(clippy::assertions_on_constants)] #[test]
fn caps_are_sane() {
assert!(SENSOR_BUFFER_CAP >= 100);
assert!(SENSOR_SNIPPET_LEN >= 128);
}
}