kindly-guard-server 0.11.14

KindlyGuard MCP server - Enterprise-grade security for AI model interactions
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
// Copyright 2025 Kindly Software Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Security property-based tests for KindlyGuard
//!
//! These tests verify that critical security properties hold across all implementations
//! using property-based testing with proptest. The tests ensure:
//!
//! 1. No injection bypasses are possible
//! 2. All malicious inputs are neutralized
//! 3. Neutralized content is always safe
//! 4. Security properties hold for random/fuzzy inputs
//! 5. Security guarantees are maintained for all implementations
//! 6. Adversarial test cases don't break security

use anyhow::Result;
use kindly_guard_server::{
    config::Config,
    neutralizer::{NeutralizeAction, ThreatNeutralizer},
    scanner::{Location, SecurityScanner, Severity, Threat, ThreatType},
    ScannerConfig,
};
use proptest::prelude::*;
use std::sync::Arc;
use tokio::runtime::Runtime;

// Helper to create all neutralizer implementations
fn get_all_neutralizers() -> Vec<Arc<dyn ThreatNeutralizer>> {
    let mut neutralizers = vec![];

    // Create neutralizer with default config
    // The implementation (standard vs enhanced) is determined by feature flags
    let config = Config::default();
    neutralizers.push(kindly_guard_server::neutralizer::create_neutralizer(
        &config.neutralization,
        None, // No rate limiter for tests
    ));

    neutralizers
}

// Helper to create scanner
fn create_test_scanner(enhanced: bool) -> Result<Arc<SecurityScanner>> {
    let config = ScannerConfig {
        unicode_detection: true,
        injection_detection: true,
        path_traversal_detection: true,
        custom_patterns: None,
        max_scan_depth: 10,
        enable_event_buffer: enhanced,
        xss_detection: Some(true),
        enhanced_mode: Some(enhanced),
        crypto_detection: true,
        max_content_size: 5 * 1024 * 1024, // 5MB
        max_input_size: None,
    };
    Ok(Arc::new(SecurityScanner::new(config)?))
}

// Property composer for generating adversarial inputs
prop_compose! {
    fn adversarial_input()(
        base in prop::string::string_regex("[a-zA-Z0-9 ]{0,100}").unwrap(),
        injection_type in prop::sample::select(vec![
            "sql", "command", "ldap", "xpath", "nosql", "template"
        ]),
        evasion_technique in prop::sample::select(vec![
            "none", "encoding", "case_variation", "whitespace", "comments", "unicode"
        ]),
        payload_position in prop::sample::select(vec!["prefix", "suffix", "middle", "wrapped"]),
    ) -> String {
        let payload = match injection_type {
            "sql" => "' OR '1'='1' --",
            "command" => "; cat /etc/passwd",
            "ldap" => ")(cn=*)",
            "xpath" => "' or '1'='1",
            "nosql" => "{'$ne': null}",
            "template" => "{{7*7}}",
            _ => "UNION SELECT * FROM users",
        };

        let evaded_payload = match evasion_technique {
            "encoding" => percent_encode(payload),
            "case_variation" => randomize_case(payload),
            "whitespace" => add_whitespace(payload),
            "comments" => add_sql_comments(payload),
            "unicode" => add_unicode_tricks(payload),
            _ => payload.to_string(),
        };

        match payload_position {
            "prefix" => format!("{}{}", evaded_payload, base),
            "suffix" => format!("{}{}", base, evaded_payload),
            "middle" => {
                let mid = base.len() / 2;
                format!("{}{}{}", &base[..mid], evaded_payload, &base[mid..])
            }
            "wrapped" => format!("{}{}{}", base, evaded_payload, base),
            _ => evaded_payload,
        }
    }
}

// Helper functions for evasion techniques
fn percent_encode(s: &str) -> String {
    s.chars()
        .map(|c| {
            if c.is_alphanumeric() {
                c.to_string()
            } else {
                format!("%{:02X}", c as u8)
            }
        })
        .collect()
}

fn randomize_case(s: &str) -> String {
    s.chars()
        .enumerate()
        .map(|(i, c)| {
            if i % 2 == 0 {
                c.to_uppercase().to_string()
            } else {
                c.to_lowercase().to_string()
            }
        })
        .collect()
}

fn add_whitespace(s: &str) -> String {
    s.chars()
        .map(|c| {
            if c == ' ' {
                "  ".to_string() // Double spaces
            } else {
                c.to_string()
            }
        })
        .collect()
}

fn add_sql_comments(s: &str) -> String {
    s.replace(" OR ", " /*comment*/ OR /*comment*/ ")
        .replace("SELECT", "SEL/**/ECT")
}

fn add_unicode_tricks(s: &str) -> String {
    // Add zero-width spaces and other unicode tricks
    s.chars()
        .flat_map(|c| {
            if c == ' ' {
                vec!['\u{200B}', c] // Zero-width space before regular space
            } else {
                vec![c]
            }
        })
        .collect()
}

// Property composer for unicode attack strings
prop_compose! {
    fn unicode_attack_string()(
        base in prop::string::string_regex("[a-zA-Z0-9]{0,50}").unwrap(),
        attack_type in prop::sample::select(vec![
            "homograph", "bidi", "invisible", "normalization", "mixed"
        ]),
        complexity in 1usize..5,
    ) -> String {
        let mut result = base.clone();

        for _ in 0..complexity {
            match attack_type {
                "homograph" => {
                    // Replace ASCII with lookalikes
                    result = result
                        .replace('a', "а") // Cyrillic
                        .replace('o', "о") // Cyrillic
                        .replace('e', "е"); // Cyrillic
                }
                "bidi" => {
                    // Add bidi override characters
                    result = format!("\u{202E}{}\u{202C}", result);
                }
                "invisible" => {
                    // Insert zero-width characters
                    let pos = result.len() / 2;
                    result.insert(pos, '\u{200B}'); // Zero-width space
                    result.insert(pos, '\u{200C}'); // Zero-width non-joiner
                }
                "normalization" => {
                    // Add combining characters
                    result.push('\u{0301}'); // Combining acute accent
                }
                "mixed" => {
                    // Combine multiple techniques
                    result = format!("\u{202E}{}а\u{200B}о\u{202C}", result);
                }
                _ => {}
            }
        }

        result
    }
}

// Main property tests
proptest! {
    #[test]
    fn prop_no_injection_bypass(
        input in adversarial_input(),
    ) {
        let runtime = Runtime::new().unwrap();

        runtime.block_on(async {
            for neutralizer in get_all_neutralizers() {
                // First scan for threats
                let scanner = create_test_scanner(false).unwrap();
                let threats = scanner.scan_text(&input).unwrap_or_default();

                // If threats detected, ensure they're neutralized
                if !threats.is_empty() {
                    for threat in &threats {
                        let result = neutralizer.neutralize(threat, &input).await.unwrap();

                        // Verify threat was handled
                        prop_assert!(
                            result.action_taken != NeutralizeAction::NoAction,
                            "Threat not neutralized: {:?} in input: {:?}",
                            threat.threat_type,
                            input
                        );

                        // If content was sanitized, verify it's safe
                        if let Some(sanitized) = &result.sanitized_content {
                            // Re-scan sanitized content
                            let new_threats = scanner.scan_text(sanitized).unwrap_or_default();

                            // Should not contain the same threat type
                            prop_assert!(
                                !new_threats.iter().any(|t| t.threat_type == threat.threat_type),
                                "Sanitized content still contains threat: {:?}",
                                threat.threat_type
                            );
                        }
                    }
                }
            }
            Ok::<(), TestCaseError>(())
        }).unwrap();
    }

    #[test]
    fn prop_all_malicious_inputs_neutralized(
        inputs in prop::collection::vec(adversarial_input(), 1..10),
    ) {
        let runtime = Runtime::new().unwrap();

        runtime.block_on(async {
            for neutralizer in get_all_neutralizers() {
                for input in &inputs {
                    let scanner = create_test_scanner(false).unwrap();
                    let threats = scanner.scan_text(input).unwrap_or_default();

                    if !threats.is_empty() {
                        // Batch neutralize all threats
                        let batch_result = neutralizer.batch_neutralize(&threats, input).await.unwrap();

                        // Verify all threats were addressed
                        prop_assert_eq!(
                            batch_result.individual_results.len(),
                            threats.len(),
                            "Not all threats were processed"
                        );

                        // Verify final content is safe
                        let final_threats = scanner.scan_text(&batch_result.final_content).unwrap_or_default();
                        prop_assert!(
                            final_threats.is_empty() ||
                            final_threats.iter().all(|t| t.severity == Severity::Low),
                            "High severity threats remain in final content"
                        );
                    }
                }
            }
            Ok::<(), TestCaseError>(())
        }).unwrap();
    }

    #[test]
    fn prop_neutralized_content_always_safe(
        base_content in prop::string::string_regex(".{0,1000}").unwrap(),
        threat_count in 1usize..5,
    ) {
        let runtime = Runtime::new().unwrap();

        runtime.block_on(async {
            for neutralizer in get_all_neutralizers() {
                // Create synthetic threats
                let threats: Vec<Threat> = (0..threat_count)
                    .map(|i| Threat {
                        threat_type: match i % 3 {
                            0 => ThreatType::SqlInjection,
                            1 => ThreatType::CommandInjection,
                            _ => ThreatType::CrossSiteScripting,
                        },
                        severity: Severity::High,
                        location: Location::Text {
                            offset: i * 10,
                            length: 5,
                        },
                        description: format!("Test threat {}", i),
                        remediation: None,
                    })
                    .collect();

                // Neutralize threats
                let result = neutralizer.batch_neutralize(&threats, &base_content).await.unwrap();

                // Verify properties of neutralized content
                prop_assert!(
                    !result.final_content.is_empty() || base_content.is_empty(),
                    "Content was completely removed"
                );

                // Ensure no high-severity threats remain
                let scanner = create_test_scanner(false).unwrap();
                let remaining_threats = scanner.scan_text(&result.final_content).unwrap_or_default();

                prop_assert!(
                    remaining_threats.iter().all(|t| t.severity != Severity::Critical),
                    "Critical threats remain after neutralization"
                );
            }
            Ok::<(), TestCaseError>(())
        }).unwrap();
    }

    #[test]
    fn prop_unicode_attacks_neutralized(
        input in unicode_attack_string(),
    ) {
        let runtime = Runtime::new().unwrap();

        runtime.block_on(async {
            for neutralizer in get_all_neutralizers() {
                let scanner = create_test_scanner(false).unwrap();
                let threats = scanner.scan_text(&input).unwrap_or_default();

                // Unicode attacks should be detected
                let unicode_threats: Vec<_> = threats.iter()
                    .filter(|t| matches!(
                        t.threat_type,
                        ThreatType::UnicodeInvisible |
                        ThreatType::UnicodeBiDi |
                        ThreatType::UnicodeHomograph |
                        ThreatType::UnicodeControl
                    ))
                    .collect();

                if !unicode_threats.is_empty() {
                    for threat in unicode_threats {
                        let result = neutralizer.neutralize(threat, &input).await.unwrap();

                        prop_assert!(
                            result.action_taken != NeutralizeAction::NoAction,
                            "Unicode threat not neutralized: {:?}",
                            threat.threat_type
                        );

                        // Verify sanitized content doesn't contain the threat
                        if let Some(sanitized) = &result.sanitized_content {
                            let new_threats = scanner.scan_text(sanitized).unwrap_or_default();
                            prop_assert!(
                                !new_threats.iter().any(|t|
                                    matches!(t.threat_type,
                                        ThreatType::UnicodeInvisible |
                                        ThreatType::UnicodeBiDi |
                                        ThreatType::UnicodeHomograph |
                                        ThreatType::UnicodeControl
                                    )
                                ),
                                "Unicode threat persists after neutralization"
                            );
                        }
                    }
                }
            }
            Ok::<(), TestCaseError>(())
        }).unwrap();
    }

    #[test]
    fn prop_neutralizer_deterministic(
        input in prop::string::string_regex(".{0,500}").unwrap(),
        threat_type in prop::sample::select(vec![
            ThreatType::SqlInjection,
            ThreatType::CommandInjection,
            ThreatType::CrossSiteScripting,
            ThreatType::PathTraversal,
        ]),
    ) {
        let runtime = Runtime::new().unwrap();

        runtime.block_on(async {
            for neutralizer in get_all_neutralizers() {
                let threat = Threat {
                    threat_type: threat_type.clone(),
                    severity: Severity::High,
                    location: Location::Text { offset: 0, length: input.len() },
                    description: "Test threat".to_string(),
                    remediation: None,
                };

                // Run neutralization multiple times
                let result1 = neutralizer.neutralize(&threat, &input).await.unwrap();
                let result2 = neutralizer.neutralize(&threat, &input).await.unwrap();

                // Results should be identical
                prop_assert_eq!(
                    result1.action_taken,
                    result2.action_taken,
                    "Non-deterministic action"
                );

                prop_assert_eq!(
                    result1.sanitized_content,
                    result2.sanitized_content,
                    "Non-deterministic sanitization"
                );
            }
            Ok::<(), TestCaseError>(())
        }).unwrap();
    }

    #[test]
    fn prop_extreme_inputs_handled_safely(
        size_mb in 1usize..10,
        pattern in prop::sample::select(vec!["a", "😀", "\u{200B}", "SELECT", "<script>"]),
    ) {
        let runtime = Runtime::new().unwrap();

        runtime.block_on(async {
            // Create large input
            let input: String = pattern.repeat(size_mb * 1024 * 1024 / pattern.len());

            for neutralizer in get_all_neutralizers() {
                // Should handle large inputs without panic or OOM
                let threat = Threat {
                    threat_type: ThreatType::Custom("Generic".to_string()),
                    severity: Severity::Low,
                    location: Location::Text { offset: 0, length: 100 },
                    description: "Large input test".to_string(),
                    remediation: None,
                };

                let result = neutralizer.neutralize(&threat, &input).await;
                prop_assert!(result.is_ok(), "Failed to handle large input");
            }
            Ok::<(), TestCaseError>(())
        }).unwrap();
    }

    #[test]
    fn prop_nested_threats_properly_handled(
        depth in 1usize..5,
        base in prop::string::string_regex("[a-zA-Z0-9]{10,20}").unwrap(),
    ) {
        let runtime = Runtime::new().unwrap();

        runtime.block_on(async {
            // Create nested payload
            let mut payload = base.clone();
            for _ in 0..depth {
                payload = format!("' OR '{}'='{}", payload, payload);
            }

            for neutralizer in get_all_neutralizers() {
                let scanner = create_test_scanner(false).unwrap();
                let threats = scanner.scan_text(&payload).unwrap_or_default();

                if !threats.is_empty() {
                    let result = neutralizer.batch_neutralize(&threats, &payload).await.unwrap();

                    // Verify nested threats are neutralized
                    let final_threats = scanner.scan_text(&result.final_content).unwrap_or_default();
                    prop_assert!(
                        final_threats.len() < threats.len(),
                        "Neutralization didn't reduce threat count"
                    );
                }
            }
            Ok::<(), TestCaseError>(())
        }).unwrap();
    }

    #[test]
    fn prop_polyglot_payloads_neutralized(
        components in prop::collection::vec(
            prop::sample::select(vec![
                "<script>alert(1)</script>",
                "' OR '1'='1",
                "{{7*7}}",
                "${7*7}",
                "=cmd|'/c calc'!A1",
                "../../../etc/passwd",
            ]),
            2..5
        ),
    ) {
        let runtime = Runtime::new().unwrap();

        runtime.block_on(async {
            // Create polyglot payload
            let polyglot = components.join("");

            for neutralizer in get_all_neutralizers() {
                let scanner = create_test_scanner(false).unwrap();
                let threats = scanner.scan_text(&polyglot).unwrap_or_default();

                // Should detect multiple threat types
                prop_assert!(
                    threats.len() >= 2,
                    "Polyglot payload should trigger multiple detections"
                );

                // All should be neutralized
                let result = neutralizer.batch_neutralize(&threats, &polyglot).await.unwrap();

                // Final content should be significantly different
                prop_assert!(
                    result.final_content != polyglot,
                    "Polyglot payload unchanged after neutralization"
                );

                // And safe
                let final_threats = scanner.scan_text(&result.final_content).unwrap_or_default();
                prop_assert!(
                    final_threats.iter().all(|t| t.severity != Severity::High),
                    "High severity threats remain in polyglot neutralization"
                );
            }
            Ok::<(), TestCaseError>(())
        }).unwrap();
    }

    #[test]
    fn prop_capabilities_match_behavior(
        threat_types in prop::collection::vec(
            prop::sample::select(vec![
                ThreatType::SqlInjection,
                ThreatType::CommandInjection,
                ThreatType::CrossSiteScripting,
                ThreatType::PathTraversal,
                ThreatType::UnicodeInvisible,
            ]),
            1..10
        ),
    ) {
        let runtime = Runtime::new().unwrap();

        runtime.block_on(async {
            for neutralizer in get_all_neutralizers() {
                let capabilities = neutralizer.get_capabilities();

                for threat_type in &threat_types {
                    let can_handle = neutralizer.can_neutralize(threat_type);

                    // Create a threat of this type
                    let threat = Threat {
                        threat_type: threat_type.clone(),
                        severity: Severity::High,
                        location: Location::Text { offset: 0, length: 10 },
                        description: "Test".to_string(),
                        remediation: None,
                    };

                    let result = neutralizer.neutralize(&threat, "test content").await;

                    if can_handle {
                        prop_assert!(
                            result.is_ok(),
                            "Neutralizer claims to handle {:?} but failed",
                            threat_type
                        );
                    }

                    // Verify capabilities are consistent
                    prop_assert!(
                        capabilities.real_time || capabilities.batch_mode,
                        "Neutralizer must support at least one mode"
                    );
                }
            }
            Ok::<(), TestCaseError>(())
        }).unwrap();
    }
}

// Additional security invariant tests
#[cfg(test)]
mod security_invariants {
    use super::*;

    #[test]
    fn test_no_content_amplification() {
        let runtime = Runtime::new().unwrap();

        runtime.block_on(async {
            for neutralizer in get_all_neutralizers() {
                let test_inputs = vec![
                    "normal text",
                    "text with <script>alert(1)</script>",
                    "SQL: ' OR '1'='1",
                    "Path: ../../../etc/passwd",
                ];

                for input in test_inputs {
                    let threat = Threat {
                        threat_type: ThreatType::Custom("Generic".to_string()),
                        severity: Severity::High,
                        location: Location::Text {
                            offset: 0,
                            length: input.len(),
                        },
                        description: "Test".to_string(),
                        remediation: None,
                    };

                    let result = neutralizer.neutralize(&threat, input).await.unwrap();

                    if let Some(sanitized) = &result.sanitized_content {
                        // Sanitized content should not be significantly larger
                        assert!(
                            sanitized.len() <= input.len() * 2,
                            "Content amplification detected: {} -> {}",
                            input.len(),
                            sanitized.len()
                        );
                    }
                }
            }
        });
    }

    #[test]
    fn test_no_infinite_loops() {
        let runtime = Runtime::new().unwrap();

        runtime.block_on(async {
            for neutralizer in get_all_neutralizers() {
                // Create pathological input that might cause loops
                let circular_ref = "{{{{{{{{{{";
                let recursive_payload = "';exec(';exec(';exec(';exec(';exec(";

                for input in [circular_ref, recursive_payload] {
                    let threat = Threat {
                        threat_type: ThreatType::Custom("Generic".to_string()),
                        severity: Severity::High,
                        location: Location::Text {
                            offset: 0,
                            length: input.len(),
                        },
                        description: "Loop test".to_string(),
                        remediation: None,
                    };

                    // Should complete in reasonable time
                    let result = tokio::time::timeout(
                        std::time::Duration::from_secs(5),
                        neutralizer.neutralize(&threat, input),
                    )
                    .await;

                    assert!(
                        result.is_ok(),
                        "Neutralization timed out - possible infinite loop"
                    );
                }
            }
        });
    }

    #[test]
    fn test_memory_safety() {
        let runtime = Runtime::new().unwrap();

        runtime.block_on(async {
            for neutralizer in get_all_neutralizers() {
                // Test with inputs that might cause memory issues
                let null_bytes = "test\0\0\0content";
                let high_unicode = "test\u{10FFFF}content";
                let mixed_encoding = "test\u{202E}\u{200B}\u{202C}content";

                for input in [null_bytes, high_unicode, mixed_encoding] {
                    let threat = Threat {
                        threat_type: ThreatType::Custom("Generic".to_string()),
                        severity: Severity::High,
                        location: Location::Text {
                            offset: 0,
                            length: input.len(),
                        },
                        description: "Memory safety test".to_string(),
                        remediation: None,
                    };

                    // Should not cause memory errors
                    let result = neutralizer.neutralize(&threat, input).await;
                    assert!(result.is_ok(), "Memory safety issue detected");
                }
            }
        });
    }
}