use anyhow::{anyhow, Result};
use runtime_foxdriver::Page;
use serde::{Deserialize, Serialize};
pub struct Probe {
pub name: &'static str,
pub js: &'static str,
pub severity: Severity,
pub classifier: fn(&serde_json::Value) -> ProbeOutcome,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ProbeOutcome {
Pass,
Drift(String),
Critical(String),
ProbeError(String),
}
impl ProbeOutcome {
pub fn is_pass(&self) -> bool {
matches!(self, ProbeOutcome::Pass)
}
pub fn is_critical(&self) -> bool {
matches!(self, ProbeOutcome::Critical(_))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Severity {
Low,
Medium,
High,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DriftReport {
pub probed: usize,
pub passed: usize,
pub drift: usize,
pub critical: usize,
pub probe_errors: usize,
pub per_probe: Vec<ProbeReport>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProbeReport {
pub name: String,
pub severity: String,
pub outcome: ProbeOutcome,
}
impl DriftReport {
pub fn is_green(&self) -> bool {
self.critical == 0 && self.probed > 0 && self.passed >= self.probed * 90 / 100
}
pub fn summary(&self) -> String {
format!(
"{}/{} probes pass ({} drift, {} critical, {} errors)",
self.passed, self.probed, self.drift, self.critical, self.probe_errors
)
}
}
mod catalogue;
mod catalogue_misc;
mod classify;
mod codec;
mod oracle;
mod redteam;
#[cfg(test)]
mod tests;
pub use catalogue::{probes, probes_for};
pub use oracle::{diff_pages, render_differential, DifferentialReport, Divergence};
pub use guise_profiles::UserAgentBrowser;
pub const PROBE_COUNT_FLOOR: usize = 100;
pub async fn run(page: &Page) -> Result<DriftReport> {
run_for(page, UserAgentBrowser::Chrome).await
}
pub async fn run_for(page: &Page, browser: UserAgentBrowser) -> Result<DriftReport> {
let catalogue = probes_for(browser);
let mut report = DriftReport {
probed: catalogue.len(),
passed: 0,
drift: 0,
critical: 0,
probe_errors: 0,
per_probe: Vec::with_capacity(catalogue.len()),
};
for probe in catalogue {
let result = page.evaluate(probe.js).await;
let outcome = match result {
Ok(eval) => {
let value = eval
.into_value::<serde_json::Value>()
.unwrap_or(serde_json::Value::Null);
(probe.classifier)(&value)
}
Err(e) => ProbeOutcome::ProbeError(e.to_string()),
};
match &outcome {
ProbeOutcome::Pass => report.passed += 1,
ProbeOutcome::Drift(_) => report.drift += 1,
ProbeOutcome::Critical(_) => report.critical += 1,
ProbeOutcome::ProbeError(_) => report.probe_errors += 1,
}
report.per_probe.push(ProbeReport {
name: probe.name.to_string(),
severity: format!("{:?}", probe.severity),
outcome,
});
}
Ok(report)
}
pub fn render_report(report: &DriftReport) -> String {
use std::fmt::Write;
let mut out = String::with_capacity(report.per_probe.len() * 120);
let _ = writeln!(out, "STEALTH PROBE - {}", report.summary());
let _ = writeln!(out, "{:-^100}", "");
for r in &report.per_probe {
let icon = match &r.outcome {
ProbeOutcome::Pass => "✓",
ProbeOutcome::Drift(_) => "~",
ProbeOutcome::Critical(_) => "✘",
ProbeOutcome::ProbeError(_) => "?",
};
let detail = match &r.outcome {
ProbeOutcome::Pass => String::new(),
ProbeOutcome::Drift(m) | ProbeOutcome::Critical(m) | ProbeOutcome::ProbeError(m) => {
format!(" - {m}")
}
};
let _ = writeln!(out, " {icon} [{}] {}{}", r.severity, r.name, detail);
}
out
}
pub async fn audit_page(page: &Page) -> Result<DriftReport> {
audit_page_for(page, UserAgentBrowser::Chrome).await
}
pub async fn audit_page_for(page: &Page, browser: UserAgentBrowser) -> Result<DriftReport> {
let r = run_for(page, browser)
.await
.map_err(|e| anyhow!("probe run failed: {e}"))?;
Ok(r)
}