agentzero-channels 0.3.0

AgentZero — modular AI-agent runtime and tool framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::sync::LazyLock;
use tracing::warn;

/// Action to take when a credential leak is detected.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum LeakAction {
    Redact,
    Block,
}

impl LeakAction {
    pub fn from_str_loose(s: &str) -> Self {
        match s.trim().to_ascii_lowercase().as_str() {
            "block" => Self::Block,
            _ => Self::Redact,
        }
    }
}

/// Configuration for the outbound leak guard.
#[derive(Debug, Clone)]
pub struct LeakGuardPolicy {
    pub enabled: bool,
    pub action: LeakAction,
    pub sensitivity: f64,
}

impl Default for LeakGuardPolicy {
    fn default() -> Self {
        Self {
            enabled: true,
            action: LeakAction::Redact,
            sensitivity: 0.7,
        }
    }
}

/// Result of scanning text for credential leaks.
#[derive(Debug, Clone)]
pub struct ScanResult {
    pub has_leaks: bool,
    pub findings: Vec<LeakFinding>,
    pub redacted_text: Option<String>,
}

/// A single credential leak finding.
#[derive(Debug, Clone)]
pub struct LeakFinding {
    pub pattern_name: &'static str,
    pub matched_text: String,
    pub start: usize,
    pub end: usize,
}

static PATTERNS: LazyLock<Vec<(&'static str, Regex)>> = LazyLock::new(|| {
    vec![
        // API keys with common prefixes.
        (
            "api_key_prefix",
            Regex::new(r"(?i)(sk-|api[_-]?key[=:\s]+)[a-zA-Z0-9_\-]{20,}").unwrap(),
        ),
        // Bearer tokens.
        (
            "bearer_token",
            Regex::new(r"(?i)bearer\s+[a-zA-Z0-9_\-\.]{20,}").unwrap(),
        ),
        // JWT tokens (3-part base64 with dots).
        (
            "jwt_token",
            Regex::new(r"eyJ[a-zA-Z0-9_-]+\.eyJ[a-zA-Z0-9_-]+\.[a-zA-Z0-9_-]+").unwrap(),
        ),
        // AWS access keys.
        ("aws_access_key", Regex::new(r"AKIA[A-Z0-9]{16}").unwrap()),
        // Private key markers.
        (
            "private_key",
            Regex::new(r"-----BEGIN\s+(RSA\s+)?PRIVATE\s+KEY-----").unwrap(),
        ),
        // GitHub tokens.
        (
            "github_token",
            Regex::new(r"gh[ps]_[A-Za-z0-9_]{36,}").unwrap(),
        ),
        // Anthropic API keys.
        (
            "anthropic_key",
            Regex::new(r"sk-ant-[a-zA-Z0-9_\-]{20,}").unwrap(),
        ),
        // OpenAI API keys.
        ("openai_key", Regex::new(r"sk-[a-zA-Z0-9]{20,}").unwrap()),
        // Slack tokens.
        (
            "slack_token",
            Regex::new(r"xox[baprs]-[A-Za-z0-9\-]+").unwrap(),
        ),
        // X25519 / Noise session keys (64-char hex strings that look like key material).
        (
            "x25519_key_material",
            Regex::new(
                r"(?i)(noise[_-]?session|x25519[_-]?key|privacy[_-]?key)[=:\s]+[a-fA-F0-9]{64}",
            )
            .unwrap(),
        ),
    ]
});

/// High-entropy token detector (Shannon entropy).
fn shannon_entropy(s: &str) -> f64 {
    if s.is_empty() {
        return 0.0;
    }
    let mut counts = [0u32; 256];
    for byte in s.bytes() {
        counts[byte as usize] += 1;
    }
    let len = s.len() as f64;
    counts
        .iter()
        .filter(|&&c| c > 0)
        .map(|&c| {
            let p = c as f64 / len;
            -p * p.log2()
        })
        .sum()
}

/// Detect high-entropy strings that might be leaked secrets.
static HIGH_ENTROPY_RE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"[A-Za-z0-9+/=_\-]{32,}").unwrap());

impl LeakGuardPolicy {
    /// Scan text for credential leaks. Returns findings and optionally redacted text.
    pub fn scan(&self, text: &str) -> ScanResult {
        if !self.enabled {
            return ScanResult {
                has_leaks: false,
                findings: Vec::new(),
                redacted_text: None,
            };
        }

        let mut findings = Vec::new();

        // Pattern-based detection.
        for (name, pattern) in PATTERNS.iter() {
            for mat in pattern.find_iter(text) {
                findings.push(LeakFinding {
                    pattern_name: name,
                    matched_text: mat.as_str().to_string(),
                    start: mat.start(),
                    end: mat.end(),
                });
            }
        }

        // High-entropy token detection (only if sensitivity >= 0.5).
        if self.sensitivity >= 0.5 {
            let threshold = 4.0 + (1.0 - self.sensitivity) * 2.0;
            for mat in HIGH_ENTROPY_RE.find_iter(text) {
                let s = mat.as_str();
                if shannon_entropy(s) > threshold {
                    let already_found = findings
                        .iter()
                        .any(|f| f.start <= mat.start() && f.end >= mat.end());
                    if !already_found {
                        findings.push(LeakFinding {
                            pattern_name: "high_entropy_token",
                            matched_text: s.to_string(),
                            start: mat.start(),
                            end: mat.end(),
                        });
                    }
                }
            }
        }

        let has_leaks = !findings.is_empty();

        let redacted_text = if has_leaks && self.action == LeakAction::Redact {
            // Merge overlapping ranges, then replace from end to start.
            let mut sorted = findings.clone();
            sorted.sort_by_key(|f| f.start);
            let mut merged: Vec<LeakFinding> = Vec::new();
            for f in sorted {
                if let Some(last) = merged.last_mut() {
                    if f.start <= last.end {
                        // Overlapping or adjacent — extend the previous finding.
                        if f.end > last.end {
                            last.end = f.end;
                            last.matched_text = text[last.start..last.end].to_string();
                        }
                        continue;
                    }
                }
                merged.push(f);
            }
            // Replace from end to preserve byte offsets.
            let mut redacted = text.to_string();
            for finding in merged.iter().rev() {
                let replacement = format!("[REDACTED:{}]", finding.pattern_name);
                redacted.replace_range(finding.start..finding.end, &replacement);
            }
            Some(redacted)
        } else {
            None
        };

        if has_leaks {
            for finding in &findings {
                warn!(
                    pattern = finding.pattern_name,
                    "credential leak detected in outbound message"
                );
            }
        }

        ScanResult {
            has_leaks,
            findings,
            redacted_text,
        }
    }

    /// Process text: redact leaks or block if action = Block.
    pub fn process(&self, text: &str) -> Result<String, String> {
        let result = self.scan(text);
        if !result.has_leaks {
            return Ok(text.to_string());
        }
        match self.action {
            LeakAction::Redact => Ok(result.redacted_text.unwrap_or_else(|| text.to_string())),
            LeakAction::Block => Err(format!(
                "outbound message blocked: {} credential leak(s) detected",
                result.findings.len()
            )),
        }
    }

    /// Check whether outbound content to a target channel respects privacy
    /// boundaries.  Returns `Err` with a description when content from a
    /// `local_only` boundary is about to be sent to a non-local channel.
    ///
    /// This is a defense-in-depth heuristic — it cannot cryptographically
    /// guarantee isolation but catches the most common leakage paths.
    pub fn check_boundary(
        &self,
        _text: &str,
        content_boundary: &str,
        target_channel: &str,
    ) -> Result<(), String> {
        if !self.enabled {
            return Ok(());
        }
        if content_boundary == "local_only" && !crate::is_local_channel(target_channel) {
            warn!(
                target_channel,
                content_boundary, "leak guard blocked local_only content to non-local channel"
            );
            return Err(format!(
                "outbound message blocked: content with boundary 'local_only' \
                 cannot be sent to non-local channel '{target_channel}'"
            ));
        }
        Ok(())
    }
}

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

    fn guard() -> LeakGuardPolicy {
        LeakGuardPolicy {
            enabled: true,
            action: LeakAction::Redact,
            sensitivity: 0.7,
        }
    }

    #[test]
    fn detects_api_key_prefix() {
        let g = guard();
        let result = g.scan("Here is my key: sk-abc123def456ghi789jkl012");
        assert!(result.has_leaks);
        assert!(result
            .findings
            .iter()
            .any(|f| f.pattern_name == "openai_key"));
    }

    #[test]
    fn detects_jwt_token() {
        let g = guard();
        let jwt =
            "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0In0.dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk";
        let result = g.scan(&format!("Token: {jwt}"));
        assert!(result.has_leaks);
        assert!(result
            .findings
            .iter()
            .any(|f| f.pattern_name == "jwt_token"));
    }

    #[test]
    fn detects_aws_access_key() {
        let g = guard();
        let result = g.scan("AWS key: AKIAIOSFODNN7EXAMPLE");
        assert!(result.has_leaks);
        assert!(result
            .findings
            .iter()
            .any(|f| f.pattern_name == "aws_access_key"));
    }

    #[test]
    fn detects_private_key_marker() {
        let g = guard();
        let result = g.scan("-----BEGIN RSA PRIVATE KEY-----\nMIIE...");
        assert!(result.has_leaks);
        assert!(result
            .findings
            .iter()
            .any(|f| f.pattern_name == "private_key"));
    }

    #[test]
    fn redacts_detected_leaks() {
        let g = guard();
        let text = "Key: sk-abc123def456ghi789jkl012mno345";
        let result = g.scan(text);
        assert!(result.has_leaks);
        let redacted = result.redacted_text.unwrap();
        assert!(redacted.contains("[REDACTED:"));
        assert!(!redacted.contains("sk-abc123"));
    }

    #[test]
    fn block_action_returns_error() {
        let g = LeakGuardPolicy {
            enabled: true,
            action: LeakAction::Block,
            sensitivity: 0.7,
        };
        let result = g.process("Key: sk-abc123def456ghi789jkl012mno345");
        assert!(result.is_err());
    }

    #[test]
    fn clean_text_passes_through() {
        let g = guard();
        let result = g.scan("Hello, how can I help you today?");
        assert!(!result.has_leaks);
    }

    #[test]
    fn disabled_guard_passes_everything() {
        let g = LeakGuardPolicy {
            enabled: false,
            ..guard()
        };
        let result = g.scan("sk-abc123def456ghi789jkl012mno345");
        assert!(!result.has_leaks);
    }

    #[test]
    fn shannon_entropy_varies_with_randomness() {
        assert!(shannon_entropy("aaaaaaaaaa") < 1.0);
        assert!(shannon_entropy("aB3$xZ9!qW") > 3.0);
    }

    #[test]
    fn check_boundary_blocks_local_only_to_nonlocal() {
        let g = guard();
        let result = g.check_boundary("some content", "local_only", "telegram");
        assert!(result.is_err());
        let msg = result.unwrap_err();
        assert!(msg.contains("local_only"));
        assert!(msg.contains("telegram"));
    }

    #[test]
    fn check_boundary_allows_local_only_to_cli() {
        let g = guard();
        let result = g.check_boundary("some content", "local_only", "cli");
        assert!(result.is_ok());
    }

    #[test]
    fn check_boundary_allows_any_to_nonlocal() {
        let g = guard();
        let result = g.check_boundary("some content", "any", "telegram");
        assert!(result.is_ok());
    }

    #[test]
    fn check_boundary_allows_empty_to_nonlocal() {
        let g = guard();
        let result = g.check_boundary("some content", "", "slack");
        assert!(result.is_ok());
    }

    #[test]
    fn check_boundary_disabled_allows_everything() {
        let g = LeakGuardPolicy {
            enabled: false,
            ..guard()
        };
        let result = g.check_boundary("content", "local_only", "discord");
        assert!(result.is_ok());
    }

    #[test]
    fn check_boundary_blocks_local_only_to_slack() {
        let g = guard();
        assert!(g.check_boundary("secret", "local_only", "slack").is_err());
    }

    #[test]
    fn check_boundary_blocks_local_only_to_discord() {
        let g = guard();
        assert!(g.check_boundary("secret", "local_only", "discord").is_err());
    }

    #[test]
    fn check_boundary_allows_local_only_to_transcription() {
        let g = guard();
        assert!(g
            .check_boundary("secret", "local_only", "transcription")
            .is_ok());
    }
}