repositories

percival

Follow the trail a handle leaves across the web: username search across 5,700+ sites, in Rust

clonegit clone https://git.daemon-sec.xyz/percival.git
markdown.rsRust126 lines4.2 KiBraw download
//! Markdown report writer.

use super::Report;
use crate::util;

fn cell(s: &str) -> String {
    s.replace('|', "\\|").replace('\n', " ")
}

pub fn render(report: &Report) -> String {
    let mut out = String::new();
    out.push_str(&format!(
        "# Percival report\n\nGenerated {} by {} {} in {:.1}s.\n\n",
        report.generated_at, report.tool, report.version, report.elapsed_secs
    ));
    let s = &report.summary;
    out.push_str(&format!(
        "| sites | found | suspicious | blocked | errors | unknown | not found |\n|---|---|---|---|---|---|---|\n| {} | **{}** | {} | {} | {} | {} | {} |\n\n",
        s.total, s.found, s.suspicious, s.blocked, s.errors, s.unknown, s.not_found
    ));
    for (t, results) in report.by_target() {
        let via = t
            .via
            .as_deref()
            .map(|v| format!(" _(discovered via {v})_"))
            .unwrap_or_default();
        out.push_str(&format!("## `{}` ({}){via}\n\n", t.target, t.kind.label()));
        let hits: Vec<_> = results.iter().filter(|r| r.is_hit()).collect();
        if hits.is_empty() {
            out.push_str("_No confirmed accounts._\n\n");
        } else {
            out.push_str("| site | category | url | profile |\n|---|---|---|---|\n");
            for r in hits {
                let prof = r
                    .profile
                    .as_ref()
                    .map(|p| p.summary(80))
                    .unwrap_or_default();
                out.push_str(&format!(
                    "| {} | {} | <{}> | {} |\n",
                    cell(&r.site),
                    r.category,
                    r.url,
                    cell(&prof)
                ));
            }
            out.push('\n');
        }
        let sus: Vec<_> = results.iter().filter(|r| r.is_suspicious()).collect();
        if !sus.is_empty() {
            out.push_str("<details><summary>Suspicious (canary also matched)</summary>\n\n");
            for r in sus {
                out.push_str(&format!("- {} — <{}>\n", cell(&r.site), r.url));
            }
            out.push_str("\n</details>\n\n");
        }
        let blocked: Vec<_> = results
            .iter()
            .filter(|r| r.bucket() == crate::engine::Bucket::Blocked)
            .collect();
        if !blocked.is_empty() {
            out.push_str("<details><summary>Blocked</summary>\n\n");
            for r in blocked {
                out.push_str(&format!(
                    "- {} — {}\n",
                    cell(&r.site),
                    cell(&util::truncate(&r.reason, 80))
                ));
            }
            out.push_str("\n</details>\n\n");
        }
    }
    if !report.discovered.is_empty() {
        out.push_str("## Discovered handles\n\n");
        for d in &report.discovered {
            out.push_str(&format!(
                "- `{}` via {} on `{}`{}\n",
                d.handle,
                d.via_site,
                d.parent,
                if d.scanned { " (scanned)" } else { "" }
            ));
        }
        out.push('\n');
    }
    for e in &report.emails {
        out.push_str(&format!(
            "## Email `{}`\n\n- Gravatar hash: `{}`\n- Derived handles: {}\n",
            e.email,
            e.gravatar_hash,
            e.derived_handles
                .iter()
                .map(|h| format!("`{h}`"))
                .collect::<Vec<_>>()
                .join(", ")
        ));
        if !e.breaches.is_empty() {
            out.push_str("\n| breach | date | records | data |\n|---|---|---|---|\n");
            for b in &e.breaches {
                out.push_str(&format!(
                    "| {} | {} | {} | {} |\n",
                    cell(&b.name),
                    b.date,
                    b.pwn_count,
                    cell(&b.data_classes.join(", "))
                ));
            }
        }
        out.push('\n');
    }
    out
}

#[cfg(test)]
mod tests {
    use super::super::sample_report;
    use super::*;

    #[test]
    fn renders_tables() {
        let md = render(&sample_report());
        assert!(md.contains("| GitHub | coding | <https://github.com/jane>"));
        assert!(md.contains("Suspicious"));
        assert!(md.contains("Blocked, Inc"));
    }
}