Skip to main content

orch/security/
mod.rs

1//! Leak detection for agent output before it is posted to GitHub. It uses the strict rules of
2//! `secret-redact`, so a low-confidence pattern blocks a post as much as a known token does
3
4pub mod secret_ref;
5
6use secret_redact::Finding;
7
8/// Every leaked secret in `text`, with its rule and line
9pub fn scan(text: &str) -> Vec<Finding> {
10    secret_redact::strict().scan(text)
11}
12
13/// Whether `text` holds anything that looks like a secret. Nothing is posted to GitHub when it does
14pub fn has_leaks(text: &str) -> bool {
15    secret_redact::strict().contains_secret(text)
16}
17
18/// `text` with every secret replaced by `[REDACTED:{rule}]`
19pub fn redact(text: &str) -> String {
20    secret_redact::strict().redact(text).text
21}
22
23pub fn redact_posted_body(body: &str) -> String {
24    if !has_leaks(body) {
25        return body.to_string();
26    }
27    let leaks = scan(body);
28    let rules: Vec<&str> = leaks.iter().map(|leak| leak.rule).collect();
29    let warning = format!(
30        "\n\n> ⚠️ **Security Notice**: {} potential secret(s) detected and redacted: {}",
31        leaks.len(),
32        rules.join(", ")
33    );
34    format!("{}{warning}", redact(body))
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn detects_and_redacts_a_github_token() {
43        let text = "export GITHUB_TOKEN=ghp_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghij";
44        assert_eq!(scan(text)[0].rule, "github-pat");
45        let redacted = redact(text);
46        assert!(redacted.contains("[REDACTED:github-pat]"), "{redacted}");
47        assert!(!redacted.contains("ghp_"), "{redacted}");
48    }
49
50    #[test]
51    fn detects_private_keys_connection_strings_and_example_keys() {
52        assert!(has_leaks(
53            "-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQ..."
54        ));
55        assert!(has_leaks(
56            "DATABASE_URL=postgres://user:pass@host:5432/mydb"
57        ));
58        assert!(has_leaks("AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE"));
59    }
60
61    #[test]
62    fn clean_text_has_no_leaks() {
63        let text = "This is normal agent output.\nFixed bug in parser.rs\nAll tests pass.";
64        assert!(!has_leaks(text));
65    }
66
67    #[test]
68    fn has_leaks_triggers_on_low_confidence_patterns() {
69        let text = "password = \"some_long_config_value_that_is_16_chars\"";
70        assert!(
71            has_leaks(text),
72            "generic_secret (low confidence) should trigger has_leaks"
73        );
74
75        let text = "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0";
76        assert!(
77            has_leaks(text),
78            "bearer_token (low confidence) should trigger has_leaks"
79        );
80    }
81}