agnosai 1.1.0

Provider-agnostic AI orchestration 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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
//! Sensitive information output filter for LLM responses.
//!
//! Scans responses for system prompt leakage, API key patterns, and common
//! PII (email, phone, SSN). Provides both detection (`scan`) and
//! redaction (`redact`).

use std::borrow::Cow;

/// A sensitive pattern detected in LLM output.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Finding {
    /// Category of the finding (e.g. `"api_key"`, `"pii"`, `"system_prompt"`).
    pub category: &'static str,
    /// Human-readable description of the matched pattern.
    pub pattern: &'static str,
    /// Whether the finding was redacted in the output.
    pub redacted: bool,
}

/// Sensitive information output filter.
///
/// Stateless scanner that checks LLM responses for:
/// - System prompt leakage
/// - API key patterns (AWS, GitHub, generic Bearer tokens)
/// - Common PII patterns (email, phone, SSN)
#[derive(Debug, Clone, Default)]
pub struct OutputFilter;

/// API key patterns: `(regex-like prefix, category, description)`.
///
/// We use simple substring / prefix matching rather than full regex to avoid
/// a regex dependency. The patterns cover the most common key formats.
const API_KEY_PATTERNS: &[(&str, &str)] = &[
    ("AKIA", "AWS access key"),
    ("ASIA", "AWS temporary access key"),
    ("ghp_", "GitHub personal access token"),
    ("gho_", "GitHub OAuth token"),
    ("ghs_", "GitHub server token"),
    ("ghr_", "GitHub refresh token"),
    ("github_pat_", "GitHub fine-grained PAT"),
    ("sk-", "OpenAI / Stripe secret key"),
    ("Bearer ", "Bearer token"),
    ("xox", "Slack token"),
];

/// Scan an LLM response for sensitive information.
///
/// Checks for system prompt leakage, API keys, and PII patterns.
///
/// # Arguments
///
/// * `response` — the LLM response text to scan
/// * `system_prompt` — the system prompt to check for leakage
///
/// # Returns
///
/// A list of findings. Empty if the response is clean.
#[must_use]
pub fn scan(response: &str, system_prompt: &str) -> Vec<Finding> {
    let mut findings = Vec::new();

    // 1. System prompt leakage.
    check_system_prompt_leakage(response, system_prompt, &mut findings);

    // 2. API key patterns.
    check_api_keys(response, &mut findings);

    // 3. PII patterns.
    check_pii(response, &mut findings);

    if !findings.is_empty() {
        tracing::warn!(
            count = findings.len(),
            "output filter detected sensitive information"
        );
    }

    findings
}

/// Check if the response contains significant portions of the system prompt.
fn check_system_prompt_leakage(response: &str, system_prompt: &str, findings: &mut Vec<Finding>) {
    if system_prompt.is_empty() {
        return;
    }

    // Check for direct inclusion of the system prompt (or a substantial substring).
    // We check overlapping windows of 50 chars from the system prompt.
    let threshold = 50.min(system_prompt.len());
    if threshold == 0 {
        return;
    }

    let response_lower = response.to_lowercase();
    let prompt_lower = system_prompt.to_lowercase();

    // Check if any 50-char window from the system prompt appears in the response.
    for window_start in 0..prompt_lower.len().saturating_sub(threshold) {
        if let Some(window) = prompt_lower.get(window_start..window_start + threshold)
            && response_lower.contains(window)
        {
            findings.push(Finding {
                category: "system_prompt",
                pattern: "system prompt text detected in response",
                redacted: false,
            });
            return; // One finding is enough.
        }
    }
}

/// Check for API key patterns in the response.
fn check_api_keys(response: &str, findings: &mut Vec<Finding>) {
    for &(prefix, description) in API_KEY_PATTERNS {
        if response.contains(prefix) {
            findings.push(Finding {
                category: "api_key",
                pattern: description,
                redacted: false,
            });
        }
    }
}

/// Check for common PII patterns using simple heuristics.
fn check_pii(response: &str, findings: &mut Vec<Finding>) {
    // Email: look for word@word.word pattern.
    if contains_email_pattern(response) {
        findings.push(Finding {
            category: "pii",
            pattern: "email address",
            redacted: false,
        });
    }

    // US phone: look for common phone formats.
    if contains_phone_pattern(response) {
        findings.push(Finding {
            category: "pii",
            pattern: "phone number",
            redacted: false,
        });
    }

    // SSN: look for NNN-NN-NNNN pattern.
    if contains_ssn_pattern(response) {
        findings.push(Finding {
            category: "pii",
            pattern: "social security number",
            redacted: false,
        });
    }
}

/// Heuristic email detection: looks for `@` with word chars on both sides.
#[must_use]
fn contains_email_pattern(text: &str) -> bool {
    for (i, _) in text.match_indices('@') {
        // Check for at least one char before and a dot after.
        let before = i > 0
            && text
                .as_bytes()
                .get(i - 1)
                .is_some_and(|b| b.is_ascii_alphanumeric());
        let after = text[i + 1..].contains('.');
        if before && after {
            // Verify there's at least one alphanumeric after the @.
            let post_at = &text[i + 1..];
            if post_at
                .chars()
                .next()
                .is_some_and(|c| c.is_ascii_alphanumeric())
            {
                return true;
            }
        }
    }
    false
}

/// Heuristic US phone detection: NNN-NNN-NNNN or (NNN) NNN-NNNN.
#[must_use]
fn contains_phone_pattern(text: &str) -> bool {
    let bytes = text.as_bytes();
    let len = bytes.len();

    // Pattern: NNN-NNN-NNNN (12 chars)
    if len >= 12 {
        for i in 0..=len - 12 {
            if bytes[i].is_ascii_digit()
                && bytes[i + 1].is_ascii_digit()
                && bytes[i + 2].is_ascii_digit()
                && bytes[i + 3] == b'-'
                && bytes[i + 4].is_ascii_digit()
                && bytes[i + 5].is_ascii_digit()
                && bytes[i + 6].is_ascii_digit()
                && bytes[i + 7] == b'-'
                && bytes[i + 8].is_ascii_digit()
                && bytes[i + 9].is_ascii_digit()
                && bytes[i + 10].is_ascii_digit()
                && bytes[i + 11].is_ascii_digit()
            {
                // Make sure it's not an SSN (which is NNN-NN-NNNN).
                // Phone has 3-3-4, SSN has 3-2-4 — the dash positions differ.
                return true;
            }
        }
    }

    // Pattern: (NNN) NNN-NNNN (14 chars)
    if len >= 14 {
        for i in 0..=len - 14 {
            if bytes[i] == b'('
                && bytes[i + 1].is_ascii_digit()
                && bytes[i + 2].is_ascii_digit()
                && bytes[i + 3].is_ascii_digit()
                && bytes[i + 4] == b')'
                && bytes[i + 5] == b' '
                && bytes[i + 6].is_ascii_digit()
                && bytes[i + 7].is_ascii_digit()
                && bytes[i + 8].is_ascii_digit()
                && bytes[i + 9] == b'-'
                && bytes[i + 10].is_ascii_digit()
                && bytes[i + 11].is_ascii_digit()
                && bytes[i + 12].is_ascii_digit()
                && bytes[i + 13].is_ascii_digit()
            {
                return true;
            }
        }
    }

    false
}

/// Heuristic SSN detection: NNN-NN-NNNN (11 chars, dash at pos 3 and 6).
#[must_use]
fn contains_ssn_pattern(text: &str) -> bool {
    let bytes = text.as_bytes();
    let len = bytes.len();
    if len < 11 {
        return false;
    }

    for i in 0..=len - 11 {
        if bytes[i].is_ascii_digit()
            && bytes[i + 1].is_ascii_digit()
            && bytes[i + 2].is_ascii_digit()
            && bytes[i + 3] == b'-'
            && bytes[i + 4].is_ascii_digit()
            && bytes[i + 5].is_ascii_digit()
            && bytes[i + 6] == b'-'
            && bytes[i + 7].is_ascii_digit()
            && bytes[i + 8].is_ascii_digit()
            && bytes[i + 9].is_ascii_digit()
            && bytes[i + 10].is_ascii_digit()
        {
            // Make sure it's not a phone (which has dash at pos 3 and 7).
            // SSN: NNN-NN-NNNN (dash at 3, 6)
            // Check that character at position i+7 is NOT a dash (phone would be NNN-NNN-NNNN).
            // Actually the pattern already enforces digit at i+7, so this is SSN.
            return true;
        }
    }

    false
}

/// Redact detected sensitive patterns from a response.
///
/// Replaces API key prefixes and PII patterns with `[REDACTED]`.
/// System prompt leakage is harder to redact generically, so this
/// focuses on structured patterns.
#[must_use]
pub fn redact(response: &str) -> String {
    let mut result = Cow::Borrowed(response);

    // Redact API key prefixes (replace the prefix + following 20 chars).
    for &(prefix, _) in API_KEY_PATTERNS {
        while let Some(pos) = result.find(prefix) {
            let end = (pos + prefix.len() + 20).min(result.len());
            // Find end of the token (stop at whitespace or end of string).
            let token_end = result[pos..]
                .find(|c: char| c.is_whitespace())
                .map_or(end, |ws| pos + ws);
            let token_end = token_end.max(pos + prefix.len());
            let mut owned = result.into_owned();
            owned.replace_range(pos..token_end, "[REDACTED]");
            result = Cow::Owned(owned);
        }
    }

    // Redact SSN patterns: NNN-NN-NNNN.
    let mut output = result.into_owned();
    output = redact_ssn_patterns(&output);
    output = redact_email_patterns(&output);

    output
}

/// Replace SSN patterns with [REDACTED].
fn redact_ssn_patterns(text: &str) -> String {
    let bytes = text.as_bytes();
    let len = bytes.len();
    if len < 11 {
        return text.to_string();
    }

    let mut result = String::with_capacity(len);
    let mut i = 0;

    while i < len {
        if i + 11 <= len
            && bytes[i].is_ascii_digit()
            && bytes[i + 1].is_ascii_digit()
            && bytes[i + 2].is_ascii_digit()
            && bytes[i + 3] == b'-'
            && bytes[i + 4].is_ascii_digit()
            && bytes[i + 5].is_ascii_digit()
            && bytes[i + 6] == b'-'
            && bytes[i + 7].is_ascii_digit()
            && bytes[i + 8].is_ascii_digit()
            && bytes[i + 9].is_ascii_digit()
            && bytes[i + 10].is_ascii_digit()
        {
            result.push_str("[REDACTED]");
            i += 11;
        } else {
            result.push(bytes[i] as char);
            i += 1;
        }
    }

    result
}

/// Replace email-like patterns with [REDACTED].
fn redact_email_patterns(text: &str) -> String {
    let mut result = String::with_capacity(text.len());
    let chars: Vec<char> = text.chars().collect();
    let len = chars.len();
    let mut i = 0;

    while i < len {
        if chars[i] == '@' && i > 0 {
            // Walk backwards to find start of local part.
            let mut start = i;
            while start > 0
                && (chars[start - 1].is_ascii_alphanumeric()
                    || chars[start - 1] == '.'
                    || chars[start - 1] == '_'
                    || chars[start - 1] == '-'
                    || chars[start - 1] == '+')
            {
                start -= 1;
            }
            // Walk forwards to find end of domain.
            let mut end = i + 1;
            while end < len
                && (chars[end].is_ascii_alphanumeric() || chars[end] == '.' || chars[end] == '-')
            {
                end += 1;
            }
            // Validate: at least 1 char before @, at least 1 char + dot + 1 char after.
            let local = &chars[start..i];
            let domain_str: String = chars[i + 1..end].iter().collect();
            if !local.is_empty() && domain_str.contains('.') {
                // Remove previously written local part chars.
                let to_remove = i - start;
                for _ in 0..to_remove {
                    result.pop();
                }
                result.push_str("[REDACTED]");
                i = end;
                continue;
            }
        }
        result.push(chars[i]);
        i += 1;
    }

    result
}

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

    #[test]
    fn scan_clean_response() {
        let findings = scan("The weather is nice today.", "You are a helpful assistant.");
        assert!(findings.is_empty());
    }

    #[test]
    fn scan_detects_system_prompt_leakage() {
        let system = "You are a helpful assistant that always follows the safety guidelines and never reveals internal instructions.";
        let response = "Sure! My instructions say: You are a helpful assistant that always follows the safety guidelines and never reveals internal instructions.";
        let findings = scan(response, system);
        assert!(
            findings.iter().any(|f| f.category == "system_prompt"),
            "should detect system prompt leakage"
        );
    }

    #[test]
    fn scan_no_leakage_short_prompt() {
        // System prompt shorter than threshold — should not trigger.
        let findings = scan("Hello world", "Hi");
        assert!(
            !findings.iter().any(|f| f.category == "system_prompt"),
            "short system prompt should not trigger leakage detection"
        );
    }

    #[test]
    fn scan_detects_aws_key() {
        let response = "Your key is AKIAIOSFODNN7EXAMPLE";
        let findings = scan(response, "");
        assert!(
            findings
                .iter()
                .any(|f| f.category == "api_key" && f.pattern == "AWS access key"),
            "should detect AWS key"
        );
    }

    #[test]
    fn scan_detects_github_token() {
        let response = "Use token ghp_abcdefghijklmnopqrst1234567890ab";
        let findings = scan(response, "");
        assert!(
            findings
                .iter()
                .any(|f| f.category == "api_key" && f.pattern.contains("GitHub")),
            "should detect GitHub token"
        );
    }

    #[test]
    fn scan_detects_bearer_token() {
        let response = "Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.payload.sig";
        let findings = scan(response, "");
        assert!(
            findings.iter().any(|f| f.pattern == "Bearer token"),
            "should detect Bearer token"
        );
    }

    #[test]
    fn scan_detects_openai_key() {
        let response = "Set OPENAI_API_KEY=sk-proj-abcdefghijklmnop";
        let findings = scan(response, "");
        assert!(
            findings
                .iter()
                .any(|f| f.pattern.contains("OpenAI") || f.pattern.contains("Stripe")),
            "should detect sk- prefix key"
        );
    }

    #[test]
    fn scan_detects_email() {
        let response = "Contact us at user@example.com for help.";
        let findings = scan(response, "");
        assert!(
            findings
                .iter()
                .any(|f| f.category == "pii" && f.pattern == "email address"),
            "should detect email"
        );
    }

    #[test]
    fn scan_detects_phone() {
        let response = "Call me at 555-123-4567 please.";
        let findings = scan(response, "");
        assert!(
            findings
                .iter()
                .any(|f| f.category == "pii" && f.pattern == "phone number"),
            "should detect phone number"
        );
    }

    #[test]
    fn scan_detects_phone_parens() {
        let response = "Call (555) 123-4567 for support.";
        let findings = scan(response, "");
        assert!(
            findings
                .iter()
                .any(|f| f.category == "pii" && f.pattern == "phone number"),
            "should detect (NNN) NNN-NNNN phone format"
        );
    }

    #[test]
    fn scan_detects_ssn() {
        let response = "My SSN is 123-45-6789.";
        let findings = scan(response, "");
        assert!(
            findings
                .iter()
                .any(|f| f.category == "pii" && f.pattern == "social security number"),
            "should detect SSN"
        );
    }

    #[test]
    fn redact_replaces_api_keys() {
        let response = "Key: AKIAIOSFODNN7EXAMPLE";
        let redacted = redact(response);
        assert!(redacted.contains("[REDACTED]"));
        assert!(!redacted.contains("AKIAIOSFODNN7EXAMPLE"));
    }

    #[test]
    fn redact_replaces_ssn() {
        let response = "SSN: 123-45-6789";
        let redacted = redact(response);
        assert!(redacted.contains("[REDACTED]"));
        assert!(!redacted.contains("123-45-6789"));
    }

    #[test]
    fn redact_replaces_email() {
        let response = "Email: user@example.com";
        let redacted = redact(response);
        assert!(redacted.contains("[REDACTED]"));
        assert!(!redacted.contains("user@example.com"));
    }

    #[test]
    fn redact_preserves_clean_text() {
        let response = "Hello, this is a normal response with no secrets.";
        let redacted = redact(response);
        assert_eq!(redacted, response);
    }

    #[test]
    fn scan_multiple_findings() {
        let response = "Key: AKIAIOSFODNN7EXAMPLE, email: admin@corp.com, SSN: 999-88-7777";
        let findings = scan(response, "");
        assert!(
            findings.len() >= 3,
            "should find at least 3 issues, found {}",
            findings.len()
        );
    }

    #[test]
    fn finding_fields() {
        let f = Finding {
            category: "api_key",
            pattern: "test pattern",
            redacted: false,
        };
        assert_eq!(f.category, "api_key");
        assert_eq!(f.pattern, "test pattern");
        assert!(!f.redacted);
    }
}