use std::io::Read;
use std::process::ExitCode;
fn main() -> ExitCode {
let mut args = std::env::args().skip(1).peekable();
if args.peek().map(String::as_str) == Some("vectors") {
args.next();
let mut marker = "1".to_string();
let mut catalog = "exec";
while let Some(a) = args.next() {
match a.as_str() {
"--marker" => {
if let Some(m) = args.next() {
marker = m;
}
}
"--mutation" | "--mxss" => catalog = "mutation",
"--breakout" => catalog = "breakout",
_ => {}
}
}
let vectors = match catalog {
"mutation" => detonate::mutation::mutation_vectors(&marker),
"breakout" => detonate::breakout::breakout_vectors(&marker),
_ => detonate::exec_preserving_vectors(&marker),
};
for v in vectors {
println!("{v}");
}
return ExitCode::SUCCESS;
}
let mut url: Option<String> = None;
let mut body_file: Option<String> = None;
let mut expect: Option<String> = None;
let mut engine = detonate::Engine::default();
fn take_value(
args: &mut impl Iterator<Item = String>,
flag: &str,
) -> Result<String, ExitCode> {
match args.next() {
Some(v) if !v.starts_with('-') => Ok(v),
Some(v) => {
eprintln!("detonate: {flag} requires a value but got the flag {v:?}");
Err(ExitCode::from(2))
}
None => {
eprintln!("detonate: {flag} requires a value");
Err(ExitCode::from(2))
}
}
}
while let Some(a) = args.next() {
match a.as_str() {
"--url" => {
url = Some(match take_value(&mut args, "--url") {
Ok(v) => v,
Err(code) => return code,
});
}
"--body-file" => {
body_file = Some(match take_value(&mut args, "--body-file") {
Ok(v) => v,
Err(code) => return code,
});
}
"--expect" => {
expect = Some(match take_value(&mut args, "--expect") {
Ok(v) => v,
Err(code) => return code,
});
}
"--engine" => {
let Some(name) = args.next() else {
eprintln!("detonate: --engine requires a value (jsdet|chrome)");
return ExitCode::from(2);
};
match detonate::Engine::parse(&name) {
Some(e) => engine = e,
None => {
eprintln!("detonate: unknown engine {name:?} (expected jsdet|chrome)");
return ExitCode::from(2);
}
}
}
"-h" | "--help" => {
eprintln!(
"detonate --url <URL> [--body-file <FILE>] [--expect <ARG>] [--engine jsdet|chrome]\n\
Reads the HTML response body from --body-file or stdin and proves\n\
whether its injected JavaScript executes (a dialog sink fires).\n\
--engine jsdet (default): QuickJS sandbox, fast, JS-execution proof.\n\
--engine chrome: real headless Chrome, also catches mutation-XSS and\n\
browser-only handlers jsdet cannot (needs Chrome; $WAFRIFT_CHROME_BIN).\n\
Prints ExecutionProof JSON. Exit 0 = executed, 3 = did not, 2 = usage.\n\
\n\
detonate vectors [--marker M] [--mutation|--breakout]\n\
Emit a self-validated XSS vector catalog (one per line) for the\n\
evasion search to consume. Default: execution-preserving vectors.\n\
--mutation: browser-only mutation-XSS (needs --engine chrome).\n\
--breakout: context-breakout vectors (quoted attribute, JS string,\n\
javascript: URI) (escape the non-body contexts a real app exposes)."
);
return ExitCode::from(2);
}
other => {
eprintln!("detonate: unexpected argument {other:?} (try --help)");
return ExitCode::from(2);
}
}
}
if engine == detonate::Engine::Chrome && !detonate::chrome::available() {
eprintln!(
"detonate: --engine chrome requested but no Chrome/Chromium found \
(set $WAFRIFT_CHROME_BIN); falling back to jsdet"
);
}
let Some(url) = url else {
eprintln!("detonate: --url is required (the page URL the body was served from)");
return ExitCode::from(2);
};
let body = match read_body(body_file.as_deref()) { Ok(b) => b,
Err(e) => {
eprintln!("detonate: cannot read body: {e}");
return ExitCode::from(2);
}
};
let proof = detonate::detonate_html_with(engine, &body, &url);
match serde_json::to_string(&proof) {
Ok(j) => println!("{j}"),
Err(e) => {
eprintln!("detonate: serialize proof: {e}");
return ExitCode::from(2);
}
}
if proof.failed_to_run() {
if let Some(ref err) = proof.error {
eprintln!("detonate: detonation failed to run: {err}");
}
return ExitCode::from(2);
}
let proven = match expect {
Some(ref arg) => proof.proves(arg),
None => proof.executed,
};
if proven { ExitCode::SUCCESS } else { ExitCode::from(3) }
}
fn read_body(path: Option<&str>) -> std::io::Result<String> {
const MAX_BODY_BYTES: u64 = 64 * 1024 * 1024;
read_body_limited(path, MAX_BODY_BYTES)
}
fn read_body_limited(path: Option<&str>, max_bytes: u64) -> std::io::Result<String> {
let reader: Box<dyn std::io::Read> = match path {
Some(p) => Box::new(std::fs::File::open(p)?),
None => Box::new(std::io::stdin()),
};
let mut limited = reader.take(max_bytes + 1);
let mut s = String::new();
limited.read_to_string(&mut s)?;
if s.len() as u64 > max_bytes {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!(
"response body exceeds the {max_bytes}-byte limit; truncate the capture or raise the limit in read_body"
),
));
}
Ok(s)
}
#[cfg(test)]
mod tests {
use super::read_body_limited;
#[test]
fn read_body_enforces_byte_cap() {
let dir = std::env::temp_dir();
let path = dir.join(format!("detonate-read-body-test-{}", std::process::id()));
std::fs::write(&path, vec![b'a'; 1025]).expect("write fixture");
let at_limit = read_body_limited(Some(path.to_str().expect("utf8 path")), 1024);
assert!(at_limit.is_err(), "one byte over the cap must error");
let over_err = at_limit.err().expect("checked above");
assert!(
over_err.to_string().contains("1024-byte limit"),
"error must name the limit: {over_err}"
);
let exact = read_body_limited(Some(path.to_str().expect("utf8 path")), 1025);
assert_eq!(
exact.expect("at-cap body must succeed").len(),
1025,
"exactly-at-limit body must be returned whole"
);
let _ = std::fs::remove_file(&path);
}
}