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
// 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.
use kindly_guard_server::{
    config::Config,
    neutralizer::{NeutralizeAction, NeutralizeResult, ThreatNeutralizer},
    scanner::{SecurityScanner, Severity, Threat, ThreatType},
};
use serde::{Deserialize, Serialize};
use std::{
    collections::HashMap,
    fs,
    path::PathBuf,
    sync::Arc,
    time::{Duration, Instant},
};
use tokio::runtime::Runtime;

/// Performance baseline data for a specific operation
#[derive(Debug, Clone, Serialize, Deserialize)]
struct PerformanceBaseline {
    operation: String,
    implementation: String,
    mean_duration_ns: u64,
    std_deviation_ns: u64,
    samples: usize,
    timestamp: chrono::DateTime<chrono::Utc>,
    rust_version: String,
    os: String,
}

/// Performance measurement result
#[derive(Debug)]
struct PerformanceMeasurement {
    durations: Vec<Duration>,
    mean: Duration,
    std_dev: Duration,
    min: Duration,
    max: Duration,
    p50: Duration,
    p95: Duration,
    p99: Duration,
}

impl PerformanceMeasurement {
    fn from_durations(mut durations: Vec<Duration>) -> Self {
        durations.sort_unstable();

        let sum: Duration = durations.iter().sum();
        let mean = sum / durations.len() as u32;

        let variance: f64 = durations
            .iter()
            .map(|d| {
                let diff = d.as_nanos() as f64 - mean.as_nanos() as f64;
                diff * diff
            })
            .sum::<f64>()
            / durations.len() as f64;

        let std_dev = Duration::from_nanos(variance.sqrt() as u64);

        let p50_idx = durations.len() / 2;
        let p95_idx = (durations.len() as f64 * 0.95) as usize;
        let p99_idx = (durations.len() as f64 * 0.99) as usize;

        Self {
            mean,
            std_dev,
            min: durations[0],
            max: durations[durations.len() - 1],
            p50: durations[p50_idx],
            p95: durations[p95_idx.min(durations.len() - 1)],
            p99: durations[p99_idx.min(durations.len() - 1)],
            durations,
        }
    }

    fn to_baseline(&self, operation: &str, implementation: &str) -> PerformanceBaseline {
        PerformanceBaseline {
            operation: operation.to_string(),
            implementation: implementation.to_string(),
            mean_duration_ns: self.mean.as_nanos() as u64,
            std_deviation_ns: self.std_dev.as_nanos() as u64,
            samples: self.durations.len(),
            timestamp: chrono::Utc::now(),
            rust_version: env!("RUSTC_VERSION").to_string(),
            os: std::env::consts::OS.to_string(),
        }
    }
}

/// Performance regression detector
struct RegressionDetector {
    baseline_path: PathBuf,
    regression_threshold: f64, // Default 20% (0.2)
}

impl RegressionDetector {
    fn new() -> Self {
        Self {
            baseline_path: PathBuf::from("tests/performance_baselines.json"),
            regression_threshold: 0.2,
        }
    }

    fn load_baselines(&self) -> HashMap<String, PerformanceBaseline> {
        if let Ok(data) = fs::read_to_string(&self.baseline_path) {
            serde_json::from_str(&data).unwrap_or_default()
        } else {
            HashMap::new()
        }
    }

    fn save_baselines(&self, baselines: &HashMap<String, PerformanceBaseline>) {
        if let Ok(data) = serde_json::to_string_pretty(baselines) {
            let _ = fs::create_dir_all(self.baseline_path.parent().unwrap());
            let _ = fs::write(&self.baseline_path, data);
        }
    }

    fn detect_regression(
        &self,
        current: &PerformanceMeasurement,
        baseline: &PerformanceBaseline,
    ) -> Option<RegressionReport> {
        let current_mean = current.mean.as_nanos() as f64;
        let baseline_mean = baseline.mean_duration_ns as f64;

        // Calculate percentage change
        let change = (current_mean - baseline_mean) / baseline_mean;

        // Account for standard deviation in comparison
        let combined_std_dev =
            (current.std_dev.as_nanos() as f64 + baseline.std_deviation_ns as f64) / 2.0;
        let significant_change = (current_mean - baseline_mean).abs() > combined_std_dev * 2.0;

        if change > self.regression_threshold && significant_change {
            Some(RegressionReport {
                operation: baseline.operation.clone(),
                implementation: baseline.implementation.clone(),
                baseline_mean: Duration::from_nanos(baseline.mean_duration_ns),
                current_mean: current.mean,
                percentage_change: change * 100.0,
                is_regression: true,
                details: format!(
                    "Performance regression detected: {:.1}% slower than baseline",
                    change * 100.0
                ),
            })
        } else if change < -self.regression_threshold && significant_change {
            Some(RegressionReport {
                operation: baseline.operation.clone(),
                implementation: baseline.implementation.clone(),
                baseline_mean: Duration::from_nanos(baseline.mean_duration_ns),
                current_mean: current.mean,
                percentage_change: change * 100.0,
                is_regression: false,
                details: format!(
                    "Performance improvement detected: {:.1}% faster than baseline",
                    -change * 100.0
                ),
            })
        } else {
            None
        }
    }
}

/// Regression analysis report
#[derive(Debug)]
struct RegressionReport {
    operation: String,
    implementation: String,
    baseline_mean: Duration,
    current_mean: Duration,
    percentage_change: f64,
    is_regression: bool,
    details: String,
}

/// Test data generators
mod test_data {
    use super::*;

    pub fn generate_threats(count: usize) -> Vec<Threat> {
        (0..count)
            .map(|i| Threat {
                threat_type: match i % 5 {
                    0 => ThreatType::SqlInjection,
                    1 => ThreatType::CommandInjection,
                    2 => ThreatType::XssAttempt,
                    3 => ThreatType::PathTraversal,
                    _ => ThreatType::UnicodeExploit,
                },
                severity: match i % 3 {
                    0 => Severity::Low,
                    1 => Severity::Medium,
                    _ => Severity::High,
                },
                location: kindly_guard_server::scanner::Location::Text {
                    offset: i * 10,
                    length: 10,
                },
                description: format!("Test threat {}", i),
                remediation: None,
            })
            .collect()
    }

    pub fn generate_content_samples(count: usize) -> Vec<String> {
        (0..count)
            .map(|i| match i % 5 {
                0 => format!("SELECT * FROM users WHERE id = '{}'", i),
                1 => format!("rm -rf /tmp/test{} && echo done", i),
                2 => format!("<script>alert('{}')</script>", i),
                3 => format!("../../etc/passwd{}", i),
                _ => format!("Normal content with unicode: test{} \u{202E}", i),
            })
            .collect()
    }

    pub fn generate_large_content(size_kb: usize) -> String {
        let base = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. ";
        let repeat_count = (size_kb * 1024) / base.len();
        base.repeat(repeat_count)
    }
}

/// Performance benchmark runner
struct BenchmarkRunner {
    runtime: Runtime,
    warmup_iterations: usize,
    measurement_iterations: usize,
}

impl BenchmarkRunner {
    fn new() -> Self {
        Self {
            runtime: Runtime::new().unwrap(),
            warmup_iterations: 100,
            measurement_iterations: 1000,
        }
    }

    fn measure_neutralization<N: ThreatNeutralizer + Send + Sync + 'static>(
        &self,
        neutralizer: Arc<N>,
        threats: &[Threat],
        content: &str,
    ) -> PerformanceMeasurement {
        // Warmup
        for threat in threats.iter().cycle().take(self.warmup_iterations) {
            let _ = self
                .runtime
                .block_on(neutralizer.neutralize(threat, content));
        }

        // Measurement
        let mut durations = Vec::with_capacity(self.measurement_iterations);

        for threat in threats.iter().cycle().take(self.measurement_iterations) {
            let neutralizer = neutralizer.clone();
            let threat = threat.clone();
            let content = content.to_string();

            let start = Instant::now();
            let _ = self
                .runtime
                .block_on(async move { neutralizer.neutralize(&threat, &content).await });
            durations.push(start.elapsed());
        }

        PerformanceMeasurement::from_durations(durations)
    }

    fn measure_scanning<S: SecurityScanner + Send + Sync + 'static>(
        &self,
        scanner: Arc<S>,
        content_samples: &[String],
    ) -> PerformanceMeasurement {
        // Warmup
        for content in content_samples.iter().cycle().take(self.warmup_iterations) {
            let _ = scanner.scan_text(content);
        }

        // Measurement
        let mut durations = Vec::with_capacity(self.measurement_iterations);

        for content in content_samples
            .iter()
            .cycle()
            .take(self.measurement_iterations)
        {
            let scanner = scanner.clone();
            let content = content.clone();

            let start = Instant::now();
            let _ = scanner.scan_text(&content);
            durations.push(start.elapsed());
        }

        PerformanceMeasurement::from_durations(durations)
    }

    fn measure_batch_operation<F, T>(
        &self,
        operation: F,
        batch_size: usize,
    ) -> PerformanceMeasurement
    where
        F: Fn() -> T + Clone + Send + 'static,
        T: Send + 'static,
    {
        // Warmup
        for _ in 0..self.warmup_iterations {
            for _ in 0..batch_size {
                operation();
            }
        }

        // Measurement
        let mut durations = Vec::with_capacity(self.measurement_iterations);

        for _ in 0..self.measurement_iterations {
            let start = Instant::now();
            for _ in 0..batch_size {
                operation();
            }
            durations.push(start.elapsed() / batch_size as u32);
        }

        PerformanceMeasurement::from_durations(durations)
    }
}

// Test modules
#[cfg(test)]
mod tests {
    use super::*;
    use kindly_guard_server::resilience::{create_circuit_breaker, create_retry_strategy};

    fn create_test_config() -> Config {
        Config::default()
    }

    fn create_standard_neutralizer() -> Arc<dyn ThreatNeutralizer> {
        let config = create_test_config();
        Arc::new(kindly_guard_server::standard_impl::StandardNeutralizer::new())
    }

    #[cfg(feature = "enhanced")]
    fn create_enhanced_neutralizer() -> Arc<dyn ThreatNeutralizer> {
        let config = create_test_config();
        Arc::new(kindly_guard_server::enhanced_impl::EnhancedNeutralizer::new(config))
    }

    fn create_standard_scanner() -> Arc<dyn SecurityScanner> {
        Arc::new(kindly_guard_server::scanner::create_scanner())
    }

    #[test]
    fn test_neutralization_performance_baseline() {
        let runner = BenchmarkRunner::new();
        let detector = RegressionDetector::new();
        let mut baselines = detector.load_baselines();

        let neutralizer = create_standard_neutralizer();
        let threats = test_data::generate_threats(10);
        let content = "Test content with potential threats";

        let measurement = runner.measure_neutralization(neutralizer, &threats, content);

        println!("Standard Neutralization Performance:");
        println!("  Mean: {:?}", measurement.mean);
        println!("  Std Dev: {:?}", measurement.std_dev);
        println!("  P50: {:?}", measurement.p50);
        println!("  P95: {:?}", measurement.p95);
        println!("  P99: {:?}", measurement.p99);

        let key = "neutralization_standard".to_string();
        if let Some(baseline) = baselines.get(&key) {
            if let Some(report) = detector.detect_regression(&measurement, baseline) {
                if report.is_regression {
                    panic!("{}", report.details);
                } else {
                    println!("{}", report.details);
                }
            }
        } else {
            // Save as new baseline
            baselines.insert(key, measurement.to_baseline("neutralization", "standard"));
            detector.save_baselines(&baselines);
        }
    }

    #[cfg(feature = "enhanced")]
    #[test]
    fn test_enhanced_neutralization_performance() {
        let runner = BenchmarkRunner::new();
        let detector = RegressionDetector::new();
        let mut baselines = detector.load_baselines();

        let neutralizer = create_enhanced_neutralizer();
        let threats = test_data::generate_threats(10);
        let content = "Test content with potential threats";

        let measurement = runner.measure_neutralization(neutralizer, &threats, content);

        println!("Enhanced Neutralization Performance:");
        println!("  Mean: {:?}", measurement.mean);
        println!("  Std Dev: {:?}", measurement.std_dev);
        println!("  P50: {:?}", measurement.p50);
        println!("  P95: {:?}", measurement.p95);
        println!("  P99: {:?}", measurement.p99);

        let key = "neutralization_enhanced".to_string();
        if let Some(baseline) = baselines.get(&key) {
            if let Some(report) = detector.detect_regression(&measurement, baseline) {
                if report.is_regression {
                    panic!("{}", report.details);
                } else {
                    println!("{}", report.details);
                }
            }
        } else {
            baselines.insert(key, measurement.to_baseline("neutralization", "enhanced"));
            detector.save_baselines(&baselines);
        }
    }

    #[test]
    fn test_scanning_performance_baseline() {
        let runner = BenchmarkRunner::new();
        let detector = RegressionDetector::new();
        let mut baselines = detector.load_baselines();

        let scanner = create_standard_scanner();
        let content_samples = test_data::generate_content_samples(50);

        let measurement = runner.measure_scanning(scanner, &content_samples);

        println!("Scanning Performance:");
        println!("  Mean: {:?}", measurement.mean);
        println!("  Std Dev: {:?}", measurement.std_dev);
        println!("  P50: {:?}", measurement.p50);
        println!("  P95: {:?}", measurement.p95);
        println!("  P99: {:?}", measurement.p99);

        let key = "scanning_standard".to_string();
        if let Some(baseline) = baselines.get(&key) {
            if let Some(report) = detector.detect_regression(&measurement, baseline) {
                if report.is_regression {
                    panic!("{}", report.details);
                } else {
                    println!("{}", report.details);
                }
            }
        } else {
            baselines.insert(key, measurement.to_baseline("scanning", "standard"));
            detector.save_baselines(&baselines);
        }
    }

    #[test]
    fn test_large_content_performance() {
        let runner = BenchmarkRunner::new();
        let detector = RegressionDetector::new();
        let mut baselines = detector.load_baselines();

        let scanner = create_standard_scanner();
        let large_content = test_data::generate_large_content(100); // 100KB

        let measurement = runner.measure_batch_operation(|| scanner.scan_text(&large_content), 1);

        println!("Large Content Scanning Performance:");
        println!("  Mean: {:?}", measurement.mean);
        println!("  P99: {:?}", measurement.p99);

        let key = "large_content_scanning".to_string();
        if let Some(baseline) = baselines.get(&key) {
            if let Some(report) = detector.detect_regression(&measurement, baseline) {
                if report.is_regression {
                    panic!("{}", report.details);
                }
            }
        } else {
            baselines.insert(key, measurement.to_baseline("large_content", "standard"));
            detector.save_baselines(&baselines);
        }
    }

    #[test]
    fn test_batch_neutralization_performance() {
        let runner = BenchmarkRunner::new();
        let detector = RegressionDetector::new();
        let mut baselines = detector.load_baselines();

        let neutralizer = create_standard_neutralizer();
        let threats = test_data::generate_threats(100);
        let contents = test_data::generate_content_samples(100);

        let runtime = Runtime::new().unwrap();
        let measurement = runner.measure_batch_operation(
            move || {
                let neutralizer = neutralizer.clone();
                let threat = &threats[0];
                let content = &contents[0];
                runtime.block_on(async { neutralizer.neutralize(threat, content).await })
            },
            10, // Batch size
        );

        println!("Batch Neutralization Performance:");
        println!("  Mean per operation: {:?}", measurement.mean);
        println!("  P99 per operation: {:?}", measurement.p99);

        let key = "batch_neutralization".to_string();
        if let Some(baseline) = baselines.get(&key) {
            if let Some(report) = detector.detect_regression(&measurement, baseline) {
                if report.is_regression && report.percentage_change > 25.0 {
                    panic!("Significant regression: {}", report.details);
                }
            }
        } else {
            baselines.insert(
                key,
                measurement.to_baseline("batch_neutralization", "standard"),
            );
            detector.save_baselines(&baselines);
        }
    }

    #[test]
    fn test_statistical_significance() {
        // Test that our statistical analysis properly reduces false positives
        let detector = RegressionDetector::new();

        // Create baseline with known values
        let baseline = PerformanceBaseline {
            operation: "test_op".to_string(),
            implementation: "test".to_string(),
            mean_duration_ns: 1_000_000, // 1ms
            std_deviation_ns: 50_000,    // 50us
            samples: 1000,
            timestamp: chrono::Utc::now(),
            rust_version: "test".to_string(),
            os: "test".to_string(),
        };

        // Test within normal variation (should not trigger regression)
        let normal_variation = PerformanceMeasurement {
            durations: vec![Duration::from_micros(1050); 100],
            mean: Duration::from_micros(1050),
            std_dev: Duration::from_micros(50),
            min: Duration::from_micros(1000),
            max: Duration::from_micros(1100),
            p50: Duration::from_micros(1050),
            p95: Duration::from_micros(1090),
            p99: Duration::from_micros(1095),
        };

        assert!(detector
            .detect_regression(&normal_variation, &baseline)
            .is_none());

        // Test significant regression (should trigger)
        let regression = PerformanceMeasurement {
            durations: vec![Duration::from_micros(1300); 100],
            mean: Duration::from_micros(1300),
            std_dev: Duration::from_micros(50),
            min: Duration::from_micros(1250),
            max: Duration::from_micros(1350),
            p50: Duration::from_micros(1300),
            p95: Duration::from_micros(1340),
            p99: Duration::from_micros(1345),
        };

        let report = detector.detect_regression(&regression, &baseline).unwrap();
        assert!(report.is_regression);
        assert!(report.percentage_change > 20.0);
    }

    #[test]
    fn test_performance_report_generation() {
        let runner = BenchmarkRunner::new();
        let scanner = create_standard_scanner();
        let content_samples = test_data::generate_content_samples(10);

        let measurement = runner.measure_scanning(scanner, &content_samples);

        // Generate detailed performance report
        let report = format!(
            r#"Performance Report
==================
Operation: Text Scanning
Implementation: Standard
Samples: {}

Timing Statistics:
  Mean:     {:?}
  Std Dev:  {:?}
  Min:      {:?}
  Max:      {:?}
  
Percentiles:
  P50:      {:?}
  P95:      {:?}
  P99:      {:?}

Throughput:
  Ops/sec:  {:.2}
"#,
            measurement.durations.len(),
            measurement.mean,
            measurement.std_dev,
            measurement.min,
            measurement.max,
            measurement.p50,
            measurement.p95,
            measurement.p99,
            1_000_000_000.0 / measurement.mean.as_nanos() as f64
        );

        println!("{}", report);

        // Ensure report contains expected data
        assert!(report.contains("Performance Report"));
        assert!(report.contains("Ops/sec"));
    }
}

// Integration with CI/CD
#[cfg(test)]
mod ci_integration {
    use super::*;

    #[test]
    #[ignore] // Run with: cargo test --ignored -- --nocapture
    fn generate_ci_performance_report() {
        let runner = BenchmarkRunner::new();
        let detector = RegressionDetector::new();
        let baselines = detector.load_baselines();

        let mut has_regression = false;
        let mut report = String::from("CI Performance Report\n=====================\n\n");

        // Test all critical operations
        let operations = vec![
            ("neutralization", "Threat Neutralization"),
            ("scanning", "Content Scanning"),
            ("batch_ops", "Batch Operations"),
        ];

        for (key, name) in operations {
            report.push_str(&format!("{}\n", name));
            report.push_str(&format!("{}\n", "-".repeat(name.len())));

            if let Some(baseline) = baselines.get(&format!("{}_standard", key)) {
                report.push_str(&format!(
                    "Baseline: {:?}{:?})\n",
                    Duration::from_nanos(baseline.mean_duration_ns),
                    Duration::from_nanos(baseline.std_deviation_ns)
                ));

                // Would run actual measurement here in CI
                report.push_str("Current: [Would measure in CI]\n");
                report.push_str("Status: [Would compare in CI]\n");
            } else {
                report.push_str("No baseline established\n");
            }

            report.push_str("\n");
        }

        // Output for CI systems
        println!("{}", report);

        if has_regression {
            panic!("Performance regressions detected!");
        }
    }
}