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
// 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.
//! Comprehensive Multi-Protocol Security Tests for KindlyGuard (Standalone Version)
//!
//! This test suite validates security across all supported protocols:
//! - HTTP API endpoint security
//! - HTTPS proxy interception
//! - WebSocket connection security
//! - stdio mode security
//! - Cross-protocol attack scenarios
//!
//! This is a standalone version that doesn't depend on the main library compilation.

use std::sync::Arc;
use std::time::Duration;

use anyhow::Result;
use axum::{
    body::Body,
    extract::{ws::WebSocket, WebSocketUpgrade},
    http::{Request, StatusCode},
    response::Response,
    routing::{get, post},
    Router,
};
use serde_json::{json, Value};
use tokio::net::TcpListener;
use tokio::sync::Mutex;
use tower::ServiceBuilder;
use tower_http::cors::CorsLayer;

// Test constants
const TEST_PORT: u16 = 8899;
const OVERSIZED_PAYLOAD_MB: usize = 100;
const MAX_WEBSOCKET_FRAME_SIZE: usize = 10 * 1024 * 1024; // 10MB

// Minimal threat type for testing
#[derive(Debug, Clone, PartialEq)]
enum ThreatType {
    UnicodeInvisible { position: usize },
    InjectionAttempt { pattern: String },
    UnicodeBiDi,
    XssAttempt,
}

// Minimal threat struct
#[derive(Debug, Clone)]
struct Threat {
    threat_type: ThreatType,
    severity: String,
    location: String,
}

// Minimal security scanner for testing
struct SecurityScanner;

impl SecurityScanner {
    fn new() -> Self {
        SecurityScanner
    }

    fn scan_text(&self, text: &str) -> Result<Vec<Threat>> {
        let mut threats = Vec::new();

        // Check for unicode attacks
        if text.contains('\u{202E}') || text.contains('\u{202D}') {
            threats.push(Threat {
                threat_type: ThreatType::UnicodeBiDi,
                severity: "high".to_string(),
                location: "text".to_string(),
            });
        }

        // Check for injection patterns
        if text.contains("'; DROP TABLE") || text.contains("$(") || text.contains("`") {
            threats.push(Threat {
                threat_type: ThreatType::InjectionAttempt {
                    pattern: "sql/command injection".to_string(),
                },
                severity: "critical".to_string(),
                location: "text".to_string(),
            });
        }

        // Check for XSS patterns
        if text.contains("<script>") || text.contains("javascript:") {
            threats.push(Threat {
                threat_type: ThreatType::XssAttempt,
                severity: "high".to_string(),
                location: "text".to_string(),
            });
        }

        // Check for null bytes
        if text.contains('\u{0000}') {
            threats.push(Threat {
                threat_type: ThreatType::UnicodeInvisible { position: 0 },
                severity: "medium".to_string(),
                location: "text".to_string(),
            });
        }

        Ok(threats)
    }

    fn scan_json(&self, json: &Value) -> Result<Vec<Threat>> {
        let text = serde_json::to_string(json)?;
        self.scan_text(&text)
    }
}

/// HTTP API Endpoint Security Tests
#[cfg(test)]
mod http_api_security {
    use super::*;
    use proptest::prelude::*;

    #[tokio::test]
    async fn test_http_fuzzing_resistance() -> Result<()> {
        let server = create_test_http_server().await?;

        // Generate various malformed requests
        let fuzz_payloads = vec![
            // Malformed JSON
            r#"{"incomplete": "#,
            r#"{"nested": {"level": {"too": {"deep": {"for": {"safety": null}}}}}}"#,
            r#"{"unicode": "\u0000\u0001\u0002"}"#,
            r#"{"bidi": "Hello\u{202E}World"}"#,
            // Invalid content types
            "not json at all",
            "<xml>also not json</xml>",
            "\x00\x01\x02\x03binary data",
            // Injection attempts
            r#"{"sql": "'; DROP TABLE users; --"}"#,
            r#"{"xss": "<script>alert('xss')</script>"}"#,
            r#"{"cmd": "$(rm -rf /)"}"#,
        ];

        for payload in fuzz_payloads {
            let response = send_http_request(&server, "/api/scan", payload).await?;

            // Server should handle gracefully, not crash
            assert!(
                response.status() == StatusCode::BAD_REQUEST
                    || response.status() == StatusCode::UNPROCESSABLE_ENTITY
                    || response.status() == StatusCode::FORBIDDEN,
                "Unexpected status for payload: {}",
                payload
            );
        }

        Ok(())
    }

    #[tokio::test]
    async fn test_oversized_payload_rejection() -> Result<()> {
        let server = create_test_http_server().await?;

        // Create oversized payload
        let large_string = "x".repeat(OVERSIZED_PAYLOAD_MB * 1024 * 1024);
        let payload = json!({
            "content": large_string
        });

        let response =
            send_http_request(&server, "/api/scan", &serde_json::to_string(&payload)?).await?;

        // Should reject oversized payloads
        assert_eq!(response.status(), StatusCode::PAYLOAD_TOO_LARGE);

        Ok(())
    }

    #[tokio::test]
    async fn test_http_header_injection() -> Result<()> {
        let _server = create_test_http_server().await?;

        // Test various header injection attempts
        let malicious_headers = vec![
            ("X-Forwarded-For", "127.0.0.1\r\nX-Admin: true"),
            ("User-Agent", "Mozilla/5.0\r\nX-Privilege: root"),
            (
                "Content-Type",
                "application/json\r\nX-Bypass-Security: true",
            ),
        ];

        for (header_name, header_value) in malicious_headers {
            let client = reqwest::Client::new();
            let response = client
                .post(format!("http://localhost:{}/api/scan", TEST_PORT))
                .header(header_name, header_value)
                .body(r#"{"test": "data"}"#)
                .send()
                .await?;

            // Should sanitize or reject malicious headers
            assert_ne!(
                response.status(),
                StatusCode::OK,
                "Accepted malicious header: {} = {}",
                header_name,
                header_value
            );
        }

        Ok(())
    }

    #[tokio::test]
    async fn test_http_method_validation() -> Result<()> {
        let _server = create_test_http_server().await?;

        // Test non-allowed methods
        let methods = vec!["PUT", "DELETE", "PATCH", "HEAD", "OPTIONS", "TRACE"];

        for method in methods {
            let client = reqwest::Client::new();
            let response = client
                .request(
                    method.parse()?,
                    format!("http://localhost:{}/api/scan", TEST_PORT),
                )
                .send()
                .await?;

            assert_eq!(
                response.status(),
                StatusCode::METHOD_NOT_ALLOWED,
                "Allowed unexpected method: {}",
                method
            );
        }

        Ok(())
    }

    proptest! {
        #[test]
        fn prop_test_json_fuzzing(s in "\\PC*") {
            let rt = tokio::runtime::Runtime::new().unwrap();
            rt.block_on(async {
                let server = create_test_http_server().await.unwrap();
                let _response = send_http_request(&server, "/api/scan", &s).await;
                // Should not panic, regardless of input
            });
        }
    }
}

/// WebSocket Connection Security Tests  
#[cfg(test)]
mod websocket_security {
    use super::*;
    use futures_util::{SinkExt, StreamExt};
    use tokio_tungstenite::{connect_async, tungstenite::Message};

    #[tokio::test]
    async fn test_websocket_message_tampering() -> Result<()> {
        let ws_server = create_test_websocket_server().await?;

        // Test connection
        let url = format!("ws://localhost:{}/ws", TEST_PORT + 20);
        let (mut ws, _) = connect_async(url).await?;

        // Test various message tampering scenarios
        let tampered_messages = vec![
            // Oversized frame
            Message::Binary(vec![0u8; MAX_WEBSOCKET_FRAME_SIZE + 1]),
            // Malformed JSON
            Message::Text("{broken json".to_string()),
            // Control frame injection
            Message::Text(r#"{"type":"data","content":"test\x00\x01\x02"}"#.to_string()),
            // Unicode direction override
            Message::Text(r#"{"content":"Hello\u{202E}World"}"#.to_string()),
        ];

        for msg in tampered_messages {
            let _ = ws.send(msg).await;

            if let Some(Ok(response)) = ws.next().await {
                // Should reject or sanitize tampered messages
                match response {
                    Message::Text(text) => {
                        assert!(
                            text.contains("error") || text.contains("invalid"),
                            "WebSocket accepted tampered message"
                        );
                    },
                    Message::Close(_) => {
                        // Connection closed is acceptable
                        break;
                    },
                    _ => {},
                }
            }
        }

        Ok(())
    }

    #[tokio::test]
    async fn test_websocket_dos_protection() -> Result<()> {
        let ws_server = create_test_websocket_server().await?;

        // Attempt rapid-fire connections
        let mut handles = vec![];

        for i in 0..50 {
            let server = ws_server.clone();
            let handle = tokio::spawn(async move {
                let url = format!("ws://localhost:{}/ws", TEST_PORT + 20);
                match connect_async(url).await {
                    Ok((mut ws, _)) => {
                        let _ = ws.send(Message::Text(format!("spam {}", i))).await;
                        let _ = ws.close(None).await;
                    },
                    Err(_) => {
                        // Connection refused is acceptable under DoS
                    },
                }
            });
            handles.push(handle);
        }

        // Wait for all attempts
        for handle in handles {
            let _ = handle.await;
        }

        // Server should still be responsive to legitimate connection
        tokio::time::sleep(Duration::from_millis(100)).await;

        let url = format!("ws://localhost:{}/ws", TEST_PORT + 20);
        let result = tokio::time::timeout(Duration::from_secs(5), connect_async(url)).await;

        assert!(
            result.is_ok(),
            "WebSocket server unresponsive after DoS attempt"
        );

        Ok(())
    }
}

/// Protocol-Specific Injection Attack Tests
#[cfg(test)]
mod protocol_injection_attacks {
    use super::*;

    #[tokio::test]
    async fn test_stdio_command_injection() -> Result<()> {
        let scanner = SecurityScanner::new();

        // Test stdio-specific injection patterns
        let stdio_attacks = vec![
            // Shell command injection
            r#"{"method": "scan", "params": {"text": "$(cat /etc/passwd)"}}"#,
            r#"{"method": "scan", "params": {"text": "`rm -rf /`"}}"#,
            r#"{"method": "scan", "params": {"text": "text; echo 'pwned'"}}"#,
            // Path traversal
            r#"{"method": "readFile", "params": {"path": "../../../etc/passwd"}}"#,
            r#"{"method": "readFile", "params": {"path": "/etc/passwd"}}"#,
            // Process injection
            r#"{"method": "exec", "params": {"cmd": "bash", "args": ["-c", "evil"]}}"#,
        ];

        for attack in stdio_attacks {
            let threats = scanner.scan_text(attack)?;

            assert!(
                !threats.is_empty(),
                "Failed to detect stdio injection: {}",
                attack
            );
        }

        Ok(())
    }

    #[tokio::test]
    async fn test_multipart_form_injection() -> Result<()> {
        let _server = create_test_http_server().await?;

        // Multipart form with embedded attacks
        let boundary = "----WebKitFormBoundary7MA4YWxkTrZu0gW";
        let malicious_multipart = format!(
            "--{}\r\n\
            Content-Disposition: form-data; name=\"file\"; filename=\"test.txt\r\n\r\n\
            normal content\r\n\
            --{}\r\n\
            Content-Disposition: form-data; name=\"description\"\r\n\r\n\
            <script>alert('xss')</script>\r\n\
            --{}\r\n\
            Content-Disposition: form-data; name=\"../../../etc/passwd\"\r\n\r\n\
            attempted path traversal\r\n\
            --{}--",
            boundary, boundary, boundary, boundary
        );

        let client = reqwest::Client::new();
        let response = client
            .post(format!("http://localhost:{}/api/upload", TEST_PORT))
            .header(
                "Content-Type",
                format!("multipart/form-data; boundary={}", boundary),
            )
            .body(malicious_multipart)
            .send()
            .await?;

        assert_eq!(
            response.status(),
            StatusCode::BAD_REQUEST,
            "Failed to detect multipart injection"
        );

        Ok(())
    }
}

/// Cross-Protocol Attack Scenario Tests
#[cfg(test)]
mod cross_protocol_attacks {
    use super::*;

    #[tokio::test]
    async fn test_protocol_smuggling() -> Result<()> {
        let _server = create_test_http_server().await?;

        // Attempt to smuggle WebSocket frames in HTTP body
        let smuggled_payload = vec![
            0x81, 0x85, // WebSocket frame header
            0x37, 0xfa, 0x21, 0x3d, // Masking key
            0x7f, 0x9f, 0x4d, 0x51, 0x58, // Masked payload
        ];

        let client = reqwest::Client::new();
        let response = client
            .post(format!("http://localhost:{}/api/scan", TEST_PORT))
            .header("Content-Type", "application/octet-stream")
            .body(smuggled_payload)
            .send()
            .await?;

        assert_ne!(
            response.status(),
            StatusCode::OK,
            "Server processed smuggled WebSocket frame"
        );

        Ok(())
    }

    #[tokio::test]
    async fn test_timing_attack_protection() -> Result<()> {
        let scanner = SecurityScanner::new();

        // Test timing consistency for different inputs
        let medium_input = "a".repeat(100);
        let long_input = "a".repeat(1000);
        let test_inputs = vec![
            ("short", "abc"),
            ("medium", medium_input.as_str()),
            ("long", long_input.as_str()),
            ("with_threat", "normal text <script>alert('xss')</script>"),
        ];

        let mut timings = vec![];

        for (name, input) in test_inputs {
            let start = std::time::Instant::now();

            let _ = scanner.scan_text(input);

            let duration = start.elapsed();
            timings.push((name, duration));
        }

        // Timing should be relatively consistent
        let max_duration = timings.iter().map(|(_, d)| d).max().unwrap();
        let min_duration = timings.iter().map(|(_, d)| d).min().unwrap();

        let variance = max_duration.as_micros() as f64 / min_duration.as_micros() as f64;

        // Allow up to 10x variance (generous for testing)
        assert!(
            variance < 10.0,
            "Excessive timing variance detected: {:.2}x",
            variance
        );

        Ok(())
    }
}

// Helper functions implementation
async fn create_test_http_server() -> Result<Arc<TestServer>> {
    let scanner = Arc::new(SecurityScanner::new());

    let app = Router::new()
        .route("/api/scan", post(scan_handler))
        .route("/api/upload", post(upload_handler))
        .layer(
            ServiceBuilder::new()
                .layer(CorsLayer::permissive())
                .into_inner(),
        )
        .with_state(scanner.clone());

    let listener = TcpListener::bind(format!("127.0.0.1:{}", TEST_PORT)).await?;

    let server = Arc::new(TestServer {
        address: listener.local_addr()?,
        scanner,
    });

    tokio::spawn(async move {
        axum::serve(listener, app).await.unwrap();
    });

    // Give server time to start
    tokio::time::sleep(Duration::from_millis(100)).await;

    Ok(server)
}

async fn create_test_websocket_server() -> Result<Arc<TestWebSocketServer>> {
    let app = Router::new().route("/ws", get(websocket_handler));

    let listener = TcpListener::bind(format!("127.0.0.1:{}", TEST_PORT + 20)).await?;

    let server = Arc::new(TestWebSocketServer {
        port: TEST_PORT + 20,
        connections: Arc::new(Mutex::new(vec![])),
    });

    tokio::spawn(async move {
        axum::serve(listener, app).await.unwrap();
    });

    tokio::time::sleep(Duration::from_millis(100)).await;

    Ok(server)
}

async fn scan_handler(
    axum::extract::State(scanner): axum::extract::State<Arc<SecurityScanner>>,
    body: String,
) -> Result<Response<Body>, StatusCode> {
    // Check payload size
    if body.len() > 10 * 1024 * 1024 {
        return Err(StatusCode::PAYLOAD_TOO_LARGE);
    }

    // Validate JSON
    let json_result: Result<Value, _> = serde_json::from_str(&body);

    match json_result {
        Ok(json) => {
            // Scan for threats
            let threats = scanner
                .scan_json(&json)
                .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;

            if !threats.is_empty() {
                return Err(StatusCode::FORBIDDEN);
            }

            Ok(Response::new(Body::from(
                json!({"status": "clean"}).to_string(),
            )))
        },
        Err(_) => Err(StatusCode::BAD_REQUEST),
    }
}

async fn upload_handler(body: String) -> Result<Response<Body>, StatusCode> {
    // Basic multipart validation
    if body.contains("<script>") || body.contains("../") {
        return Err(StatusCode::BAD_REQUEST);
    }

    Ok(Response::new(Body::from(
        json!({"status": "uploaded"}).to_string(),
    )))
}

async fn websocket_handler(ws: WebSocketUpgrade) -> Response<Body> {
    ws.on_upgrade(handle_websocket)
}

async fn handle_websocket(mut socket: WebSocket) {
    let scanner = SecurityScanner::new();

    while let Some(msg) = socket.recv().await {
        match msg {
            Ok(axum::extract::ws::Message::Text(text)) => {
                // Scan text for threats
                if let Ok(threats) = scanner.scan_text(&text) {
                    if !threats.is_empty() {
                        let _ = socket
                            .send(axum::extract::ws::Message::Text(
                                json!({"error": "threat detected"}).to_string(),
                            ))
                            .await;
                        break;
                    }
                }

                // Echo back for testing
                let _ = socket.send(axum::extract::ws::Message::Text(text)).await;
            },
            Ok(axum::extract::ws::Message::Binary(data)) => {
                if data.len() > MAX_WEBSOCKET_FRAME_SIZE {
                    let _ = socket
                        .send(axum::extract::ws::Message::Text(
                            json!({"error": "frame too large"}).to_string(),
                        ))
                        .await;
                    break;
                }
            },
            Ok(axum::extract::ws::Message::Close(_)) => break,
            Err(_) => break,
            _ => {},
        }
    }
}

async fn send_http_request(
    _server: &Arc<TestServer>,
    path: &str,
    body: &str,
) -> Result<reqwest::Response> {
    let client = reqwest::Client::new();

    Ok(client
        .post(format!("http://localhost:{}{}", TEST_PORT, path))
        .header("Content-Type", "application/json")
        .body(body.to_string())
        .send()
        .await?)
}

// Test server structures
struct TestServer {
    address: std::net::SocketAddr,
    scanner: Arc<SecurityScanner>,
}

struct TestWebSocketServer {
    port: u16,
    connections: Arc<Mutex<Vec<String>>>,
}