path-security 0.2.0

Comprehensive path validation and sanitization library with 85%+ attack vector coverage
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
# User Guide - Path Security

## Overview

This comprehensive user guide provides detailed instructions for using the Path Security module effectively.

## Getting Started

### Installation

```bash
# Add to Cargo.toml
[dependencies]
path-security = "0.1.0"

# Or install via cargo
cargo add path-security
```

### Basic Usage

```rust
use path_security::{PathValidator, ValidationResult};

fn main() {
    let validator = PathValidator::new()
        .with_traversal_detection(true)
        .with_encoding_detection(true)
        .with_unicode_detection(true);

    let path = "/safe/path/to/file.txt";
    match validator.validate_path(path) {
        Ok(validated_path) => {
            println!("✅ Path is valid: {}", validated_path);
        }
        Err(error) => {
            eprintln!("❌ Path validation failed: {}", error);
        }
    }
}
```

## Path Validation

### Basic Path Validation

```rust
use path_security::{PathValidator, ValidationResult};

let validator = PathValidator::new()
    .with_traversal_detection(true)
    .with_encoding_detection(true)
    .with_unicode_detection(true);

// Validate a safe path
let safe_path = "/safe/path/to/file.txt";
match validator.validate_path(safe_path) {
    Ok(validated_path) => {
        println!("✅ Path is valid: {}", validated_path);
    }
    Err(error) => {
        eprintln!("❌ Path validation failed: {}", error);
    }
}

// Validate a potentially dangerous path
let dangerous_path = "../../../etc/passwd";
match validator.validate_path(dangerous_path) {
    Ok(validated_path) => {
        println!("✅ Path is valid: {}", validated_path);
    }
    Err(error) => {
        println!("❌ Path validation failed: {}", error);
    }
}
```

### Advanced Path Validation

```rust
use path_security::{PathValidator, AdvancedValidationConfig};

let advanced_config = AdvancedValidationConfig::new()
    .with_traversal_detection(true)
    .with_encoding_detection(true)
    .with_unicode_detection(true)
    .with_project_name_validation(true)
    .with_filename_validation(true)
    .with_cross_platform_validation(true)
    .with_windows_specific_validation(true)
    .with_unix_specific_validation(true);

let validator = PathValidator::new()
    .with_advanced_validation_config(advanced_config);

// Validate various types of paths
let paths = vec![
    "/safe/path/to/file.txt",
    "C:\\Windows\\System32\\file.txt",
    "/usr/local/bin/script.sh",
    "../../../etc/passwd",
    "%2e%2e%2f%2e%2e%2f%2e%2e%2fetc%2fpasswd",
    "..\u002f..\u002f..\u002fetc\u002fpasswd",
];

for path in paths {
    match validator.validate_path(path) {
        Ok(validated_path) => {
            println!("✅ Path is valid: {}", validated_path);
        }
        Err(error) => {
            println!("❌ Path validation failed: {}", error);
        }
    }
}
```

## Project Name Validation

### Basic Project Name Validation

```rust
use path_security::{PathValidator, ProjectNameValidationConfig};

let project_config = ProjectNameValidationConfig::new()
    .with_traversal_detection(true)
    .with_encoding_detection(true)
    .with_unicode_detection(true)
    .with_reserved_name_detection(true)
    .with_special_character_detection(true);

let validator = PathValidator::new()
    .with_project_name_validation_config(project_config);

// Validate a safe project name
let safe_name = "my-safe-project";
match validator.validate_project_name(safe_name) {
    Ok(validated_name) => {
        println!("✅ Project name is valid: {}", validated_name);
    }
    Err(error) => {
        eprintln!("❌ Project name validation failed: {}", error);
    }
}

// Validate a potentially dangerous project name
let dangerous_name = "../../../etc/passwd";
match validator.validate_project_name(dangerous_name) {
    Ok(validated_name) => {
        println!("✅ Project name is valid: {}", validated_name);
    }
    Err(error) => {
        println!("❌ Project name validation failed: {}", error);
    }
}
```

### Advanced Project Name Validation

```rust
use path_security::{PathValidator, AdvancedProjectNameValidationConfig};

let advanced_project_config = AdvancedProjectNameValidationConfig::new()
    .with_traversal_detection(true)
    .with_encoding_detection(true)
    .with_unicode_detection(true)
    .with_reserved_name_detection(true)
    .with_special_character_detection(true)
    .with_windows_reserved_names(true)
    .with_unix_reserved_names(true)
    .with_system_reserved_names(true);

let validator = PathValidator::new()
    .with_advanced_project_name_validation_config(advanced_project_config);

// Validate various types of project names
let project_names = vec![
    "my-safe-project",
    "project-with-dashes",
    "project_with_underscores",
    "project.with.dots",
    "../../../etc/passwd",
    "CON",
    "PRN",
    "AUX",
    "NUL",
];

for name in project_names {
    match validator.validate_project_name(name) {
        Ok(validated_name) => {
            println!("✅ Project name is valid: {}", validated_name);
        }
        Err(error) => {
            println!("❌ Project name validation failed: {}", error);
        }
    }
}
```

## Filename Validation

### Basic Filename Validation

```rust
use path_security::{PathValidator, FilenameValidationConfig};

let filename_config = FilenameValidationConfig::new()
    .with_traversal_detection(true)
    .with_encoding_detection(true)
    .with_unicode_detection(true)
    .with_special_character_detection(true)
    .with_reserved_name_detection(true);

let validator = PathValidator::new()
    .with_filename_validation_config(filename_config);

// Validate a safe filename
let safe_filename = "safe-file.txt";
match validator.validate_filename(safe_filename) {
    Ok(validated_filename) => {
        println!("✅ Filename is valid: {}", validated_filename);
    }
    Err(error) => {
        eprintln!("❌ Filename validation failed: {}", error);
    }
}

// Validate a potentially dangerous filename
let dangerous_filename = "../../../etc/passwd";
match validator.validate_filename(dangerous_filename) {
    Ok(validated_filename) => {
        println!("✅ Filename is valid: {}", validated_filename);
    }
    Err(error) => {
        println!("❌ Filename validation failed: {}", error);
    }
}
```

### Advanced Filename Validation

```rust
use path_security::{PathValidator, AdvancedFilenameValidationConfig};

let advanced_filename_config = AdvancedFilenameValidationConfig::new()
    .with_traversal_detection(true)
    .with_encoding_detection(true)
    .with_unicode_detection(true)
    .with_special_character_detection(true)
    .with_reserved_name_detection(true)
    .with_windows_special_characters(true)
    .with_unix_special_characters(true)
    .with_unicode_special_characters(true);

let validator = PathValidator::new()
    .with_advanced_filename_validation_config(advanced_filename_config);

// Validate various types of filenames
let filenames = vec![
    "safe-file.txt",
    "file_with_underscores.txt",
    "file.with.dots.txt",
    "file-with-dashes.txt",
    "../../../etc/passwd",
    "file<name>.txt",
    "file>name>.txt",
    "file|name>.txt",
    "file:name>.txt",
    "file\"name>.txt",
    "file*name>.txt",
    "file?name>.txt",
    "file\\name>.txt",
    "file/name>.txt",
];

for filename in filenames {
    match validator.validate_filename(filename) {
        Ok(validated_filename) => {
            println!("✅ Filename is valid: {}", validated_filename);
        }
        Err(error) => {
            println!("❌ Filename validation failed: {}", error);
        }
    }
}
```

## Security Configuration

### Comprehensive Security Configuration

```rust
use path_security::{PathValidator, ComprehensiveSecurityConfig};

let security_config = ComprehensiveSecurityConfig::new()
    .with_traversal_detection(true)
    .with_encoding_detection(true)
    .with_unicode_detection(true)
    .with_project_name_validation(true)
    .with_filename_validation(true)
    .with_cross_platform_validation(true)
    .with_windows_specific_validation(true)
    .with_unix_specific_validation(true)
    .with_macos_specific_validation(true)
    .with_security_monitoring(true)
    .with_threat_monitoring(true)
    .with_anomaly_monitoring(true)
    .with_incident_monitoring(true);

let validator = PathValidator::new()
    .with_comprehensive_security_config(security_config);

println!("Path Security configured with comprehensive security settings");
```

### Threat-Specific Security Configuration

```rust
use path_security::{PathValidator, ThreatSpecificSecurityConfig};

let threat_config = ThreatSpecificSecurityConfig::new()
    .with_directory_traversal_protection(true)
    .with_encoding_attack_protection(true)
    .with_unicode_attack_protection(true)
    .with_project_name_attack_protection(true)
    .with_filename_attack_protection(true)
    .with_cross_platform_attack_protection(true)
    .with_windows_attack_protection(true)
    .with_unix_attack_protection(true)
    .with_macos_attack_protection(true);

let validator = PathValidator::new()
    .with_threat_specific_security_config(threat_config);

println!("Path Security configured with threat-specific security settings");
```

## Performance Configuration

### Performance Optimization

```rust
use path_security::{PathValidator, PerformanceOptimizationConfig};

let performance_config = PerformanceOptimizationConfig::new()
    .with_caching_enabled(true)
    .with_parallel_processing_enabled(true)
    .with_lazy_evaluation_enabled(true)
    .with_memory_optimization_enabled(true)
    .with_cpu_optimization_enabled(true)
    .with_io_optimization_enabled(true)
    .with_network_optimization_enabled(true);

let validator = PathValidator::new()
    .with_performance_optimization_config(performance_config);

println!("Path Security configured with performance optimization");
```

### Resource Management

```rust
use path_security::{PathValidator, ResourceManagementConfig};

let resource_config = ResourceManagementConfig::new()
    .with_memory_limit(1024 * 1024 * 1024) // 1GB
    .with_cpu_limit(80) // 80%
    .with_io_limit(1000) // 1000 IOPS
    .with_network_limit(100 * 1024 * 1024) // 100MB/s
    .with_memory_optimization(true)
    .with_cpu_optimization(true)
    .with_io_optimization(true)
    .with_network_optimization(true);

let validator = PathValidator::new()
    .with_resource_management_config(resource_config);

println!("Path Security configured with resource management");
```

## Monitoring Configuration

### Comprehensive Monitoring

```rust
use path_security::{PathValidator, ComprehensiveMonitoringConfig};

let monitoring_config = ComprehensiveMonitoringConfig::new()
    .with_security_monitoring(true)
    .with_performance_monitoring(true)
    .with_error_monitoring(true)
    .with_threat_monitoring(true)
    .with_anomaly_monitoring(true)
    .with_incident_monitoring(true)
    .with_forensic_monitoring(true)
    .with_audit_monitoring(true);

let validator = PathValidator::new()
    .with_comprehensive_monitoring_config(monitoring_config);

println!("Path Security configured with comprehensive monitoring");
```

### Real-time Monitoring

```rust
use path_security::{PathValidator, RealTimeMonitoringConfig};

let real_time_config = RealTimeMonitoringConfig::new()
    .with_immediate_monitoring(true)
    .with_feedback_mechanism(true)
    .with_monitoring_reporting(true)
    .with_monitoring_logging(true)
    .with_monitoring_alerting(true)
    .with_monitoring_analysis(true);

let validator = PathValidator::new()
    .with_real_time_monitoring_config(real_time_config);

println!("Path Security configured with real-time monitoring");
```

## Error Handling

### Basic Error Handling

```rust
use path_security::{PathValidator, ValidationResult};

let validator = PathValidator::new()
    .with_traversal_detection(true)
    .with_encoding_detection(true)
    .with_unicode_detection(true);

let path = "../../../etc/passwd";
match validator.validate_path(path) {
    Ok(validated_path) => {
        println!("✅ Path is valid: {}", validated_path);
    }
    Err(error) => {
        match error {
            ValidationResult::TraversalDetected => {
                println!("❌ Directory traversal detected");
            }
            ValidationResult::EncodingAttackDetected => {
                println!("❌ Encoding attack detected");
            }
            ValidationResult::UnicodeAttackDetected => {
                println!("❌ Unicode attack detected");
            }
            ValidationResult::InvalidCharacters => {
                println!("❌ Invalid characters detected");
            }
            ValidationResult::PathTooLong => {
                println!("❌ Path too long");
            }
            _ => {
                println!("❌ Validation failed: {}", error);
            }
        }
    }
}
```

### Advanced Error Handling

```rust
use path_security::{PathValidator, AdvancedErrorHandler, ValidationResult};

let error_handler = AdvancedErrorHandler::new()
    .with_graceful_degradation(true)
    .with_error_recovery(true)
    .with_error_logging(true)
    .with_error_reporting(true)
    .with_error_analysis(true)
    .with_error_alerting(true);

let validator = PathValidator::new()
    .with_advanced_error_handler(error_handler);

let path = "../../../etc/passwd";
match validator.validate_path(path) {
    Ok(validated_path) => {
        println!("✅ Path is valid: {}", validated_path);
    }
    Err(error) => {
        // Handle different types of errors
        match error {
            ValidationResult::TraversalDetected => {
                println!("❌ Directory traversal detected - blocking access");
                log_security_event("Directory traversal attempt blocked", error);
                alert_security_team("Potential security threat detected");
            }
            ValidationResult::EncodingAttackDetected => {
                println!("❌ Encoding attack detected - blocking access");
                log_security_event("Encoding attack attempt blocked", error);
                alert_security_team("Potential security threat detected");
            }
            ValidationResult::UnicodeAttackDetected => {
                println!("❌ Unicode attack detected - blocking access");
                log_security_event("Unicode attack attempt blocked", error);
                alert_security_team("Potential security threat detected");
            }
            ValidationResult::InvalidCharacters => {
                println!("❌ Invalid characters detected - sanitizing path");
                log_security_event("Invalid characters detected", error);
                sanitize_path(path);
            }
            ValidationResult::PathTooLong => {
                println!("❌ Path too long - truncating path");
                log_security_event("Path too long", error);
                truncate_path(path);
            }
            _ => {
                println!("❌ Validation failed: {}", error);
                log_security_event("Path validation failed", error);
            }
        }
    }
}
```

## Testing

### Unit Testing

```rust
use path_security::{PathValidator, ValidationResult};

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

    #[test]
    fn test_safe_path_validation() {
        let validator = PathValidator::new()
            .with_traversal_detection(true)
            .with_encoding_detection(true)
            .with_unicode_detection(true);

        let safe_path = "/safe/path/to/file.txt";
        assert!(validator.validate_path(safe_path).is_ok());
    }

    #[test]
    fn test_dangerous_path_validation() {
        let validator = PathValidator::new()
            .with_traversal_detection(true)
            .with_encoding_detection(true)
            .with_unicode_detection(true);

        let dangerous_path = "../../../etc/passwd";
        assert!(validator.validate_path(dangerous_path).is_err());
    }

    #[test]
    fn test_encoding_attack_detection() {
        let validator = PathValidator::new()
            .with_traversal_detection(true)
            .with_encoding_detection(true)
            .with_unicode_detection(true);

        let encoding_attack = "%2e%2e%2f%2e%2e%2f%2e%2e%2fetc%2fpasswd";
        assert!(validator.validate_path(encoding_attack).is_err());
    }

    #[test]
    fn test_unicode_attack_detection() {
        let validator = PathValidator::new()
            .with_traversal_detection(true)
            .with_encoding_detection(true)
            .with_unicode_detection(true);

        let unicode_attack = "..\u002f..\u002f..\u002fetc\u002fpasswd";
        assert!(validator.validate_path(unicode_attack).is_err());
    }
}
```

### Integration Testing

```rust
use path_security::{PathValidator, IntegrationTestConfig};

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

    #[test]
    fn test_web_application_integration() {
        let web_config = IntegrationTestConfig::new()
            .with_web_application_testing(true)
            .with_api_testing(true)
            .with_file_upload_testing(true);

        let validator = PathValidator::new()
            .with_integration_test_config(web_config);

        // Test web application scenarios
        let web_paths = vec![
            "/uploads/user-file.txt",
            "/static/css/style.css",
            "/api/v1/data.json",
        ];

        for path in web_paths {
            assert!(validator.validate_path(path).is_ok());
        }
    }

    #[test]
    fn test_file_system_integration() {
        let fs_config = IntegrationTestConfig::new()
            .with_file_system_testing(true)
            .with_directory_testing(true)
            .with_file_access_testing(true);

        let validator = PathValidator::new()
            .with_integration_test_config(fs_config);

        // Test file system scenarios
        let fs_paths = vec![
            "/var/log/application.log",
            "/tmp/temporary-file.txt",
            "/home/user/documents/file.pdf",
        ];

        for path in fs_paths {
            assert!(validator.validate_path(path).is_ok());
        }
    }
}
```

## Best Practices

### Security Best Practices

1. **Always validate paths**: Never trust user input
2. **Use comprehensive validation**: Enable all security features
3. **Monitor security events**: Set up comprehensive monitoring
4. **Handle errors gracefully**: Implement proper error handling
5. **Keep security updated**: Regularly update security measures

### Performance Best Practices

1. **Enable caching**: Use caching for improved performance
2. **Optimize resources**: Configure resource limits appropriately
3. **Monitor performance**: Set up performance monitoring
4. **Test performance**: Regularly test performance characteristics
5. **Optimize algorithms**: Use efficient algorithms and data structures

### Configuration Best Practices

1. **Use secure defaults**: Always use secure default configurations
2. **Customize for your needs**: Configure security for your specific use case
3. **Test configurations**: Test your security configurations
4. **Monitor configurations**: Monitor configuration effectiveness
5. **Update configurations**: Regularly update security configurations

## Troubleshooting

### Common Issues

1. **Path validation failures**: Check path format and characters
2. **Performance issues**: Optimize configuration and resources
3. **Security events**: Monitor and analyze security events
4. **Configuration issues**: Verify configuration settings
5. **Integration issues**: Test integration with your application

### Debugging

```rust
use path_security::{PathValidator, DebugConfig};

let debug_config = DebugConfig::new()
    .with_debug_logging(true)
    .with_verbose_output(true)
    .with_error_details(true)
    .with_validation_trace(true)
    .with_performance_trace(true)
    .with_security_trace(true);

let validator = PathValidator::new()
    .with_debug_config(debug_config);

// Debug path validation
let path = "../../../etc/passwd";
match validator.validate_path(path) {
    Ok(validated_path) => {
        println!("✅ Path is valid: {}", validated_path);
    }
    Err(error) => {
        println!("❌ Path validation failed: {}", error);
        // Debug information will be logged
    }
}
```

## Conclusion

This user guide provides comprehensive instructions for using the Path Security module effectively. By following these guidelines, you can ensure that your applications are protected against path traversal attacks and other security vulnerabilities.

Key takeaways:

1. **Comprehensive Security**: Use all available security features
2. **Performance Optimization**: Configure for optimal performance
3. **Monitoring**: Set up comprehensive monitoring
4. **Error Handling**: Implement proper error handling
5. **Testing**: Test your security implementation
6. **Best Practices**: Follow security and performance best practices
7. **Troubleshooting**: Know how to debug and resolve issues