llm-config-security 0.5.0

Security hardening and validation for LLM Config Manager with input validation, rate limiting, and threat protection
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
//! Input validation and sanitization

use crate::errors::{SecurityError, SecurityResult};
use regex::Regex;
use std::sync::OnceLock;

/// SQL injection patterns
static SQL_INJECTION_PATTERNS: OnceLock<Vec<Regex>> = OnceLock::new();

/// XSS patterns
static XSS_PATTERNS: OnceLock<Vec<Regex>> = OnceLock::new();

/// Path traversal patterns
static PATH_TRAVERSAL_PATTERNS: OnceLock<Vec<Regex>> = OnceLock::new();

/// Command injection patterns
static COMMAND_INJECTION_PATTERNS: OnceLock<Vec<Regex>> = OnceLock::new();

/// LDAP injection patterns
static LDAP_INJECTION_PATTERNS: OnceLock<Vec<Regex>> = OnceLock::new();

/// Initialize security patterns
fn init_patterns() {
    SQL_INJECTION_PATTERNS.get_or_init(|| {
        vec![
            Regex::new(r"(?i)(\bunion\b.*\bselect\b)").unwrap(),
            Regex::new(r"(?i)(\bdrop\b.*\btable\b)").unwrap(),
            Regex::new(r"(?i)(\binsert\b.*\binto\b)").unwrap(),
            Regex::new(r"(?i)(\bdelete\b.*\bfrom\b)").unwrap(),
            Regex::new(r"(?i)(\bupdate\b.*\bset\b)").unwrap(),
            Regex::new(r"(?i)(;.*(--)|(#))").unwrap(),
            Regex::new(r"(?i)('|(--)|;|/\*|\*/|@@|@)").unwrap(),
            Regex::new(r"(?i)\bexec(\s|\+)+(s|x)p\w+").unwrap(),
        ]
    });

    XSS_PATTERNS.get_or_init(|| {
        vec![
            Regex::new(r"(?i)<script[^>]*>.*?</script>").unwrap(),
            Regex::new(r"(?i)javascript:").unwrap(),
            Regex::new(r"(?i)on\w+\s*=").unwrap(),
            Regex::new(r"(?i)<iframe").unwrap(),
            Regex::new(r"(?i)<embed").unwrap(),
            Regex::new(r"(?i)<object").unwrap(),
            Regex::new(r"(?i)eval\(").unwrap(),
            Regex::new(r"(?i)expression\(").unwrap(),
        ]
    });

    PATH_TRAVERSAL_PATTERNS.get_or_init(|| {
        vec![
            Regex::new(r"\.\./").unwrap(),
            Regex::new(r"\.\./").unwrap(),
            Regex::new(r"%2e%2e/").unwrap(),
            Regex::new(r"%2e%2e\\").unwrap(),
            Regex::new(r"\.\.\\").unwrap(),
        ]
    });

    COMMAND_INJECTION_PATTERNS.get_or_init(|| {
        vec![
            Regex::new(r"[;&|`$\n]").unwrap(),
            Regex::new(r"\$\(.*\)").unwrap(),
            Regex::new(r"`.*`").unwrap(),
        ]
    });

    LDAP_INJECTION_PATTERNS.get_or_init(|| {
        vec![
            Regex::new(r"[*()\\]").unwrap(),
            Regex::new(r"\x00").unwrap(),
        ]
    });
}

/// Configuration for input sanitization
#[derive(Debug, Clone)]
pub struct SanitizationConfig {
    /// Maximum input length
    pub max_length: usize,
    /// Allow special characters
    pub allow_special_chars: bool,
    /// Allow HTML
    pub allow_html: bool,
    /// Trim whitespace
    pub trim_whitespace: bool,
}

impl Default for SanitizationConfig {
    fn default() -> Self {
        Self {
            max_length: 1000,
            allow_special_chars: false,
            allow_html: false,
            trim_whitespace: true,
        }
    }
}

/// Input validator for security
pub struct InputValidator {
    config: SanitizationConfig,
}

impl InputValidator {
    /// Create a new input validator
    pub fn new(config: SanitizationConfig) -> Self {
        init_patterns();
        Self { config }
    }

    /// Create with default configuration
    pub fn default() -> Self {
        Self::new(SanitizationConfig::default())
    }

    /// Validate and sanitize input
    pub fn validate(&self, input: &str) -> SecurityResult<String> {
        // Check length
        if input.len() > self.config.max_length {
            return Err(SecurityError::ValidationError(format!(
                "Input exceeds maximum length of {} characters",
                self.config.max_length
            )));
        }

        // Detect SQL injection
        if self.detect_sql_injection(input) {
            return Err(SecurityError::SqlInjectionAttempt);
        }

        // Detect XSS
        if self.detect_xss(input) {
            return Err(SecurityError::XssAttempt);
        }

        // Detect path traversal
        if self.detect_path_traversal(input) {
            return Err(SecurityError::PathTraversalAttempt);
        }

        // Detect command injection
        if self.detect_command_injection(input) {
            return Err(SecurityError::CommandInjectionAttempt);
        }

        // Sanitize
        let sanitized = self.sanitize(input);

        Ok(sanitized)
    }

    /// Detect SQL injection attempts
    fn detect_sql_injection(&self, input: &str) -> bool {
        SQL_INJECTION_PATTERNS
            .get()
            .unwrap()
            .iter()
            .any(|pattern| pattern.is_match(input))
    }

    /// Detect XSS attempts
    fn detect_xss(&self, input: &str) -> bool {
        if !self.config.allow_html {
            XSS_PATTERNS
                .get()
                .unwrap()
                .iter()
                .any(|pattern| pattern.is_match(input))
        } else {
            false
        }
    }

    /// Detect path traversal attempts
    fn detect_path_traversal(&self, input: &str) -> bool {
        PATH_TRAVERSAL_PATTERNS
            .get()
            .unwrap()
            .iter()
            .any(|pattern| pattern.is_match(input))
    }

    /// Detect command injection attempts
    fn detect_command_injection(&self, input: &str) -> bool {
        COMMAND_INJECTION_PATTERNS
            .get()
            .unwrap()
            .iter()
            .any(|pattern| pattern.is_match(input))
    }

    /// Detect LDAP injection attempts
    fn detect_ldap_injection(&self, input: &str) -> bool {
        LDAP_INJECTION_PATTERNS
            .get()
            .unwrap()
            .iter()
            .any(|pattern| pattern.is_match(input))
    }

    /// Sanitize input
    fn sanitize(&self, input: &str) -> String {
        let mut sanitized = input.to_string();

        // Trim whitespace
        if self.config.trim_whitespace {
            sanitized = sanitized.trim().to_string();
        }

        // Remove null bytes
        sanitized = sanitized.replace('\0', "");

        // HTML encode if not allowing HTML
        if !self.config.allow_html {
            sanitized = html_escape(&sanitized);
        }

        // Remove control characters
        sanitized = sanitized
            .chars()
            .filter(|c| !c.is_control() || *c == '\n' || *c == '\r' || *c == '\t')
            .collect();

        sanitized
    }

    /// Validate an email address
    pub fn validate_email(&self, email: &str) -> SecurityResult<String> {
        let email_regex = Regex::new(
            r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"
        ).unwrap();

        if !email_regex.is_match(email) {
            return Err(SecurityError::ValidationError(
                "Invalid email format".to_string(),
            ));
        }

        Ok(email.to_lowercase())
    }

    /// Validate a username
    pub fn validate_username(&self, username: &str) -> SecurityResult<String> {
        // Username: alphanumeric, underscore, hyphen, 3-30 characters
        let username_regex = Regex::new(r"^[a-zA-Z0-9_-]{3,30}$").unwrap();

        if !username_regex.is_match(username) {
            return Err(SecurityError::ValidationError(
                "Username must be 3-30 alphanumeric characters, underscore, or hyphen".to_string(),
            ));
        }

        Ok(username.to_string())
    }

    /// Validate a configuration key
    pub fn validate_config_key(&self, key: &str) -> SecurityResult<String> {
        // Config key: alphanumeric, underscore, hyphen, dot, slash
        let key_regex = Regex::new(r"^[a-zA-Z0-9_\-./]{1,200}$").unwrap();

        if !key_regex.is_match(key) {
            return Err(SecurityError::ValidationError(
                "Invalid configuration key format".to_string(),
            ));
        }

        // Additional checks
        if self.detect_path_traversal(key) {
            return Err(SecurityError::PathTraversalAttempt);
        }

        Ok(key.to_string())
    }

    /// Validate a URL
    pub fn validate_url(&self, url: &str) -> SecurityResult<String> {
        // Basic URL validation
        let url_regex = Regex::new(r"^https?://[^\s/$.?#].[^\s]*$").unwrap();

        if !url_regex.is_match(url) {
            return Err(SecurityError::ValidationError(
                "Invalid URL format".to_string(),
            ));
        }

        // Check for suspicious patterns
        if self.detect_xss(url) {
            return Err(SecurityError::XssAttempt);
        }

        Ok(url.to_string())
    }

    /// Validate JSON input
    pub fn validate_json(&self, json: &str) -> SecurityResult<serde_json::Value> {
        // Check for suspicious patterns before parsing
        if self.detect_xss(json) {
            return Err(SecurityError::XssAttempt);
        }

        // Parse JSON
        serde_json::from_str(json)
            .map_err(|e| SecurityError::ValidationError(format!("Invalid JSON: {}", e)))
    }
}

/// HTML escape special characters
fn html_escape(input: &str) -> String {
    input
        .replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
        .replace('\'', "&#x27;")
        .replace('/', "&#x2F;")
}

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

    #[test]
    fn test_sql_injection_detection() {
        let validator = InputValidator::default();

        // Should detect SQL injection
        assert!(validator
            .validate("' OR '1'='1")
            .is_err_and(|e| matches!(e, SecurityError::SqlInjectionAttempt)));

        assert!(validator
            .validate("'; DROP TABLE users; --")
            .is_err_and(|e| matches!(e, SecurityError::SqlInjectionAttempt)));

        // Should allow normal input
        assert!(validator.validate("normal text").is_ok());
    }

    #[test]
    fn test_xss_detection() {
        let validator = InputValidator::default();

        // Should detect XSS
        assert!(validator
            .validate("<script>alert('XSS')</script>")
            .is_err_and(|e| matches!(e, SecurityError::XssAttempt)));

        assert!(validator
            .validate("javascript:alert('XSS')")
            .is_err_and(|e| matches!(e, SecurityError::XssAttempt)));

        // Should allow normal input
        assert!(validator.validate("normal text").is_ok());
    }

    #[test]
    fn test_path_traversal_detection() {
        let validator = InputValidator::default();

        // Should detect path traversal
        assert!(validator
            .validate("../../etc/passwd")
            .is_err_and(|e| matches!(e, SecurityError::PathTraversalAttempt)));

        // Should allow normal paths
        assert!(validator.validate("normal/path").is_ok());
    }

    #[test]
    fn test_command_injection_detection() {
        let validator = InputValidator::default();

        // Should detect command injection
        assert!(validator
            .validate("test; rm -rf /")
            .is_err_and(|e| matches!(e, SecurityError::CommandInjectionAttempt)));

        assert!(validator
            .validate("$(malicious)")
            .is_err_and(|e| matches!(e, SecurityError::CommandInjectionAttempt)));

        // Should allow normal input
        assert!(validator.validate("normal text").is_ok());
    }

    #[test]
    fn test_email_validation() {
        let validator = InputValidator::default();

        assert!(validator.validate_email("user@example.com").is_ok());
        assert!(validator.validate_email("invalid.email").is_err());
        assert!(validator.validate_email("@example.com").is_err());
    }

    #[test]
    fn test_username_validation() {
        let validator = InputValidator::default();

        assert!(validator.validate_username("user123").is_ok());
        assert!(validator.validate_username("user-name_123").is_ok());
        assert!(validator.validate_username("ab").is_err()); // Too short
        assert!(validator.validate_username("user@name").is_err()); // Invalid char
    }

    #[test]
    fn test_config_key_validation() {
        let validator = InputValidator::default();

        assert!(validator.validate_config_key("app/config/key").is_ok());
        assert!(validator.validate_config_key("app.config.key").is_ok());
        assert!(validator.validate_config_key("../etc/passwd").is_err());
    }

    #[test]
    fn test_url_validation() {
        let validator = InputValidator::default();

        assert!(validator.validate_url("https://example.com").is_ok());
        assert!(validator.validate_url("http://example.com/path").is_ok());
        assert!(validator.validate_url("invalid-url").is_err());
        assert!(validator
            .validate_url("javascript:alert('XSS')")
            .is_err());
    }

    #[test]
    fn test_json_validation() {
        let validator = InputValidator::default();

        assert!(validator.validate_json(r#"{"key": "value"}"#).is_ok());
        assert!(validator.validate_json("invalid json").is_err());
        assert!(validator
            .validate_json(r#"{"xss": "<script>alert('XSS')</script>"}"#)
            .is_err());
    }

    #[test]
    fn test_sanitization() {
        let validator = InputValidator::default();

        let result = validator.validate("  test input  ").unwrap();
        assert_eq!(result, "test input");

        let result = validator.validate("test<script>").unwrap();
        assert!(result.contains("&lt;script&gt;"));
    }

    #[test]
    fn test_length_validation() {
        let config = SanitizationConfig {
            max_length: 10,
            ..Default::default()
        };
        let validator = InputValidator::new(config);

        assert!(validator.validate("short").is_ok());
        assert!(validator.validate("this is way too long").is_err());
    }
}