Skip to main content

orch/
security.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
4use secret_redact::Finding;
5
6/// Every leaked secret in `text`, with its rule and line
7pub fn scan(text: &str) -> Vec<Finding> {
8    secret_redact::strict().scan(text)
9}
10
11/// Whether `text` holds anything that looks like a secret. Nothing is posted to GitHub when it does
12pub fn has_leaks(text: &str) -> bool {
13    secret_redact::strict().contains_secret(text)
14}
15
16/// `text` with every secret replaced by `[REDACTED:{rule}]`
17pub fn redact(text: &str) -> String {
18    secret_redact::strict().redact(text).text
19}
20
21#[cfg(test)]
22mod tests {
23    use super::*;
24
25    #[test]
26    fn detects_and_redacts_a_github_token() {
27        let text = "export GITHUB_TOKEN=ghp_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghij";
28        assert_eq!(scan(text)[0].rule, "github-pat");
29        let redacted = redact(text);
30        assert!(redacted.contains("[REDACTED:github-pat]"), "{redacted}");
31        assert!(!redacted.contains("ghp_"), "{redacted}");
32    }
33
34    #[test]
35    fn detects_private_keys_connection_strings_and_example_keys() {
36        assert!(has_leaks(
37            "-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQ..."
38        ));
39        assert!(has_leaks(
40            "DATABASE_URL=postgres://user:pass@host:5432/mydb"
41        ));
42        assert!(has_leaks("AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE"));
43    }
44
45    #[test]
46    fn clean_text_has_no_leaks() {
47        let text = "This is normal agent output.\nFixed bug in parser.rs\nAll tests pass.";
48        assert!(!has_leaks(text));
49    }
50
51    #[test]
52    fn has_leaks_triggers_on_low_confidence_patterns() {
53        let text = "password = \"some_long_config_value_that_is_16_chars\"";
54        assert!(
55            has_leaks(text),
56            "generic_secret (low confidence) should trigger has_leaks"
57        );
58
59        let text = "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0";
60        assert!(
61            has_leaks(text),
62            "bearer_token (low confidence) should trigger has_leaks"
63        );
64    }
65}