//! Media-codec coherence probe — the media-stack analog of the capability- and
//! error-subsystem coherence probes in [`super::redteam`].
//!
//! The supported-codec matrix (`HTMLMediaElement.canPlayType` +
//! `MediaSource.isTypeSupported`) is one of the surfaces FingerprintJS/CreepJS
//! and the commercial anti-bot vendors weight most heavily, precisely because it
//! is hard to fake coherently: every real consumer browser — desktop or mobile,
//! Chrome or Firefox or Safari — decodes the H.264/AAC/MP4 baseline (via the OS
//! decoder, system ffmpeg, or the bundled OpenH264), and exposes Media Source
//! Extensions and the EME entry point. A browser that reports it cannot play
//! H.264 is not a consumer browser: it is a stripped CI image, a `--headless`
//! build without the media stack, or a container with no ffmpeg.
//!
//! The differential gate ([`super::oracle`]) cannot catch this on its own: it
//! diffs two browsers driven through the *same* engine, so a codec matrix that
//! is identically broken on both sides passes silently. This probe asserts the
//! **absolute** consumer baseline, so a stripped media stack is flagged even
//! when the disguise is internally self-consistent.
//!
//! Ground truth (live headless Firefox 151 on a GPU+ffmpeg host, the
//! `headless_tells` diagnostic): `h264 "probably"`, `aac "probably"`,
//! `vp9 "probably"`, `opus "probably"`, `mp3 "maybe"`, `mseH264 true`,
//! `eme "function"` — a healthy matrix. On a codec-less cloud image the same
//! surfaces collapse to `""`/`false`, which is exactly what this probe catches.
//!
//! Every classifier is pure (`fn(&serde_json::Value) -> ProbeOutcome`) and
//! unit-tested below; the JS is one synchronous expression (no promise await).
use super::{Probe, ProbeOutcome, Severity};
/// The supported-codec matrix as a single synchronous object. `canPlayType`
/// returns `""` / `"maybe"` / `"probably"`; `MediaSource.isTypeSupported`
/// returns a bool; `requestMediaKeySystemAccess` is reported by `typeof` (its
/// *presence* — the async availability of a specific CDM is deliberately out of
/// scope for a sync probe, so we never overclaim Widevine provisioning).
const MEDIA_CODEC_COHERENCE_JS: &str = "(function(){function v(tag,t){try{return document.createElement(tag).canPlayType(t)||'';}catch(e){return 'ERR';}}function m(t){try{return !!(window.MediaSource&&MediaSource.isTypeSupported(t));}catch(e){return false;}}var n=navigator;return {ua:n.userAgent,h264:v('video','video/mp4; codecs=\"avc1.42E01E\"'),aac:v('audio','audio/mp4; codecs=\"mp4a.40.2\"'),mp4:v('video','video/mp4'),vp9:v('video','video/webm; codecs=\"vp9\"'),opus:v('audio','audio/ogg; codecs=\"opus\"'),mp3:v('audio','audio/mpeg'),mseH264:m('video/mp4; codecs=\"avc1.42E01E\"'),eme:(typeof n.requestMediaKeySystemAccess)};})()";
/// The media-codec coherence probe set. Folded into [`super::catalogue::probes_for`]
/// for every family (the consumer baseline is family-independent).
pub(super) fn codec_probes() -> Vec<Probe> {
vec![Probe {
name: "media: supported-codec matrix matches a consumer browser (H.264/AAC/MSE/EME)",
js: MEDIA_CODEC_COHERENCE_JS,
severity: Severity::High,
classifier: classify_media_codec_coherence,
}]
}
/// `canPlayType` reports support as `"maybe"` or `"probably"`; `""` (and our
/// `"ERR"` sentinel) mean unsupported. A real consumer browser never returns
/// `""` for the H.264/AAC/MP4 baseline.
fn can_play(v: &serde_json::Value, key: &str) -> bool {
matches!(
v.get(key).and_then(|x| x.as_str()),
Some("maybe") | Some("probably")
)
}
fn classify_media_codec_coherence(v: &serde_json::Value) -> ProbeOutcome {
let Some(_ua) = v.get("ua").and_then(|x| x.as_str()) else {
return ProbeOutcome::ProbeError("codec probe returned no ua".into());
};
let h264 = can_play(v, "h264");
let aac = can_play(v, "aac");
let mp4 = can_play(v, "mp4");
let vp9 = can_play(v, "vp9");
let opus = can_play(v, "opus");
let mp3 = can_play(v, "mp3");
let mse_h264 = v.get("mseH264").and_then(|x| x.as_bool()).unwrap_or(false);
let eme = v.get("eme").and_then(|x| x.as_str()) == Some("function");
// The H.264 / AAC / MP4-container / MSE block is the consumer-web baseline:
// the single most common video+audio codec pair and the playback path every
// streaming site uses. No real consumer browser lacks the whole set.
let h264_stack = [h264, aac, mp4, mse_h264];
let h264_present = h264_stack.iter().filter(|x| **x).count();
// VP9 / Opus / MP3 ship in the bundled decoders (ffvpx / libvpx / libopus)
// of every Firefox and Chrome build, including minimal ones — so missing
// *these* indicates an even more stripped build than missing H.264.
let baseline = [vp9, opus, mp3];
let baseline_present = baseline.iter().filter(|x| **x).count();
if h264_present == 0 && baseline_present == 0 {
return ProbeOutcome::Critical(
"no decodable audio/video codecs (H.264, AAC, VP9, Opus, MP3 all absent) — \
a sandbox-grade / media-less build, not a consumer browser"
.into(),
);
}
if h264_present == 0 {
return ProbeOutcome::Critical(
"the entire H.264/AAC/MP4/MSE stack is unsupported — no consumer browser \
fails the most common web video+audio codecs; stripped/headless media build"
.into(),
);
}
// Partial breaks: some of the core present, some missing. Name what is gone.
let mut missing: Vec<&str> = Vec::new();
if !h264 {
missing.push("H.264(canPlayType)");
}
if !aac {
missing.push("AAC");
}
if !mse_h264 {
missing.push("MSE:H.264");
}
if !mp4 {
missing.push("video/mp4");
}
if !missing.is_empty() {
return ProbeOutcome::Drift(format!(
"consumer codec baseline incomplete — missing {}; a real desktop browser \
supports all of them",
missing.join(", ")
));
}
if baseline_present < baseline.len() {
let mut bmiss: Vec<&str> = Vec::new();
if !vp9 {
bmiss.push("VP9");
}
if !opus {
bmiss.push("Opus");
}
if !mp3 {
bmiss.push("MP3");
}
return ProbeOutcome::Drift(format!(
"bundled-decoder codec(s) missing — {}; every Firefox/Chrome build ships these",
bmiss.join(", ")
));
}
if !eme {
return ProbeOutcome::Drift(
"navigator.requestMediaKeySystemAccess absent — EME is present in every \
consumer browser; its absence is a stripped-build tell"
.into(),
);
}
ProbeOutcome::Pass
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
const FF_UA: &str = "Mozilla/5.0 (X11; Linux x86_64; rv:151.0) Gecko/20100101 Firefox/151.0";
const CR_UA: &str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36";
/// The exact matrix the live `headless_tells` diagnostic dumped from a real
/// headless Firefox 151 — the probe must call this healthy.
fn healthy(ua: &str) -> serde_json::Value {
json!({
"ua": ua, "h264": "probably", "aac": "probably", "mp4": "maybe",
"vp9": "probably", "opus": "probably", "mp3": "maybe",
"mseH264": true, "eme": "function"
})
}
#[test]
fn codec_set_is_one_named_probe_with_js() {
let p = codec_probes();
assert_eq!(p.len(), 1);
assert!(!p[0].js.is_empty());
assert!(p[0].name.contains("codec"));
}
#[test]
fn codec_js_is_balanced_and_sync() {
// No `await`/Promise in the probe (run_for does not await promises).
assert!(!MEDIA_CODEC_COHERENCE_JS.contains("await"));
assert!(!MEDIA_CODEC_COHERENCE_JS.contains("Promise"));
let mut depth = 0i32;
for c in MEDIA_CODEC_COHERENCE_JS.chars() {
match c {
'(' | '{' | '[' => depth += 1,
')' | '}' | ']' => depth -= 1,
_ => {}
}
assert!(depth >= 0, "unbalanced");
}
assert_eq!(depth, 0, "unbalanced brackets in codec JS");
}
#[test]
fn healthy_firefox_matrix_passes() {
assert_eq!(
classify_media_codec_coherence(&healthy(FF_UA)),
ProbeOutcome::Pass
);
}
#[test]
fn healthy_chrome_matrix_passes() {
assert_eq!(
classify_media_codec_coherence(&healthy(CR_UA)),
ProbeOutcome::Pass
);
}
#[test]
fn probably_and_maybe_both_count_as_supported() {
// canPlayType "maybe" is still support — must not be flagged.
let mut v = healthy(FF_UA);
v["h264"] = json!("maybe");
v["aac"] = json!("maybe");
assert_eq!(classify_media_codec_coherence(&v), ProbeOutcome::Pass);
}
#[test]
fn fully_stripped_media_stack_is_critical() {
// Codec-less cloud image: every canPlayType "" and MSE false.
let v = json!({
"ua": FF_UA, "h264": "", "aac": "", "mp4": "", "vp9": "",
"opus": "", "mp3": "", "mseH264": false, "eme": "undefined"
});
match classify_media_codec_coherence(&v) {
ProbeOutcome::Critical(m) => assert!(m.contains("sandbox-grade") || m.contains("media-less")),
o => panic!("expected Critical, got {o:?}"),
}
}
#[test]
fn h264_stack_gone_but_webm_present_is_critical() {
// The H.264/AAC/MP4/MSE block fully absent while VP9/Opus survive — a
// Firefox without system ffmpeg / OpenH264. Still not a consumer browser.
let v = json!({
"ua": FF_UA, "h264": "", "aac": "", "mp4": "", "vp9": "probably",
"opus": "probably", "mp3": "maybe", "mseH264": false, "eme": "function"
});
match classify_media_codec_coherence(&v) {
ProbeOutcome::Critical(m) => assert!(m.contains("H.264")),
o => panic!("expected Critical, got {o:?}"),
}
}
#[test]
fn partial_core_break_is_drift() {
// H.264 plays but MSE/H.264 is off — a partial, suspicious break.
let mut v = healthy(FF_UA);
v["mseH264"] = json!(false);
match classify_media_codec_coherence(&v) {
ProbeOutcome::Drift(m) => assert!(m.contains("MSE:H.264")),
o => panic!("expected Drift, got {o:?}"),
}
}
#[test]
fn missing_aac_only_is_drift() {
let mut v = healthy(CR_UA);
v["aac"] = json!("");
match classify_media_codec_coherence(&v) {
ProbeOutcome::Drift(m) => assert!(m.contains("AAC")),
o => panic!("expected Drift, got {o:?}"),
}
}
#[test]
fn core_ok_but_baseline_decoder_missing_is_drift() {
// H.264 stack fine, but VP9 absent — an oddly stripped bundled decoder.
let mut v = healthy(FF_UA);
v["vp9"] = json!("");
match classify_media_codec_coherence(&v) {
ProbeOutcome::Drift(m) => assert!(m.contains("VP9")),
o => panic!("expected Drift, got {o:?}"),
}
}
#[test]
fn core_ok_but_eme_absent_is_drift() {
let mut v = healthy(FF_UA);
v["eme"] = json!("undefined");
match classify_media_codec_coherence(&v) {
ProbeOutcome::Drift(m) => assert!(m.contains("EME")),
o => panic!("expected Drift, got {o:?}"),
}
}
#[test]
fn missing_ua_is_probe_error() {
assert!(matches!(
classify_media_codec_coherence(&json!({"h264": "probably"})),
ProbeOutcome::ProbeError(_)
));
}
#[test]
fn err_sentinel_counts_as_unsupported() {
// A throwing canPlayType ("ERR") must not be read as support.
let v = json!({
"ua": FF_UA, "h264": "ERR", "aac": "ERR", "mp4": "ERR", "vp9": "ERR",
"opus": "ERR", "mp3": "ERR", "mseH264": false, "eme": "function"
});
assert!(matches!(
classify_media_codec_coherence(&v),
ProbeOutcome::Critical(_)
));
}
}