ai-sandbox 0.2.1

Cross-platform AI tool sandbox security implementation
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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
//! Black-box destructive testing for AI Sandbox
//! This file tests the sandbox security by attempting various attack vectors
//! through the public API without modifying the source code.

use ai_sandbox::{
    execpolicy::{Decision, Policy, RuleType},
    sandboxing::{
        NetworkSandboxPolicy, SandboxCommand, SandboxManager, SandboxPolicy, SandboxPolicyExt,
    },
};
use std::collections::HashMap;
use std::ffi::OsString;
use std::path::PathBuf;

/// Test result tracking
#[derive(Debug)]
struct TestResult {
    name: String,
    passed: bool,
    details: String,
}

impl TestResult {
    fn new(name: &str, passed: bool, details: String) -> Self {
        Self {
            name: name.to_string(),
            passed,
            details: details.to_string(),
        }
    }
}

/// Check if an attack was blocked (secure) or succeeded (vulnerable)
/// Returns true if the attack was BLOCKED (secure), false if it succeeded (vulnerable)
fn is_attack_blocked(result: &Option<ai_sandbox::execpolicy::RuleMatch>) -> bool {
    match result {
        None => false,                             // No rule matched = bypass (vulnerable)
        Some(rm) => rm.decision == Decision::Deny, // Denied = blocked (secure)
    }
}

fn main() {
    println!("=== Black-box Destructive Testing for AI Sandbox ===\n");

    let mut results: Vec<TestResult> = Vec::new();

    // =========================================================================
    // Test Category 1: Execution Policy Bypass Attempts
    // =========================================================================
    println!("[Category 1] Execution Policy Bypass Tests\n");

    // Test 1.1: Case sensitivity bypass
    results.push(test_case_sensitivity_bypass());

    // Test 1.2: Whitespace/formatting bypass
    results.push(test_whitespace_bypass());

    // Test 1.3: Empty command bypass
    results.push(test_empty_command_bypass());

    // Test 1.4: Very long command name
    results.push(test_very_long_command_bypass());

    // Test 1.5: Multiple spaces in command
    results.push(test_multiple_spaces_bypass());

    // Test 1.6: Null byte injection
    results.push(test_null_byte_bypass());

    // Test 1.7: Unicode homoglyphs
    results.push(test_unicode_homoglyph_bypass());

    // Test 1.8: Alternative command separators
    results.push(test_alternative_separator_bypass());

    // Test 1.9: Newline injection
    results.push(test_newline_injection_bypass());

    // Test 1.10: Tab injection
    results.push(test_tab_injection_bypass());

    // =========================================================================
    // Test Category 2: Path Traversal Bypass Attempts
    // =========================================================================
    println!("\n[Category 2] Path Traversal Bypass Tests\n");

    // Test 2.1: Double URL encoding
    results.push(test_double_url_encoding_bypass());

    // Test 2.2: Unicode fullwidth characters
    results.push(test_unicode_fullwidth_bypass());

    // Test 2.3: Mixed case path
    results.push(test_mixed_case_path_bypass());

    // Test 2.4: Symlink with traversal
    results.push(test_symlink_traversal_bypass());

    // Test 2.5: Null-terminated path
    results.push(test_null_terminated_path_bypass());

    // =========================================================================
    // Test Category 3: Environment Variable Bypass Attempts
    // =========================================================================
    println!("\n[Category 3] Environment Variable Bypass Tests\n");

    // Test 3.1: Lowercase export
    results.push(test_lowercase_export_bypass());

    // Test 3.2: Env command instead of export
    results.push(test_env_command_bypass());

    // Test 3.3: Env variable with quotes
    results.push(test_quoted_env_bypass());

    // Test 3.4: Multiple env manipulations
    results.push(test_multiple_env_bypass());

    // =========================================================================
    // Test Category 4: Sandbox Policy Bypass Attempts
    // =========================================================================
    println!("\n[Category 4] Sandbox Policy Bypass Tests\n");

    // Test 4.1: Default policy is dangerous (FIXED: now defaults to ReadOnly)
    results.push(test_default_policy_danger());

    // Test 4.2: Empty workspace paths (FIXED: now validated and rejected)
    results.push(test_empty_workspace_paths());

    // Test 4.3: Very long path in policy (FIXED: now validated and rejected)
    results.push(test_long_path_policy_bypass());

    // Test 4.4: Special characters in path (FIXED: now validated and rejected)
    results.push(test_special_char_path_bypass());

    // =========================================================================
    // Test Category 5: Logic Flaws
    // =========================================================================
    println!("\n[Category 5] Logic Flaw Tests\n");

    // Test 5.1: Wildcard rule exploitation
    results.push(test_wildcard_exploitation());

    // Test 5.2: Priority inversion
    results.push(test_priority_inversion());

    // Test 5.3: Regex-like pattern bypass
    results.push(test_regex_pattern_bypass());

    // Test 5.4: Empty argument list
    results.push(test_empty_arglist_bypass());

    // =========================================================================
    // Test Category 6: Advanced Attack Vectors
    // =========================================================================
    println!("\n[Category 6] Advanced Attack Vectors\n");

    // Test 6.1: Whitelist mode bypass
    results.push(test_whitelist_mode_bypass());

    // Test 6.2: Directory restriction bypass
    results.push(test_directory_restriction_bypass());

    // Test 6.3: Shell escape via allowed command
    results.push(test_shell_escape_allowed_command());

    // Test 6.4: Path normalization bypass
    results.push(test_path_normalization_bypass());

    // Test 6.5: Environment variable via set command
    results.push(test_set_command_bypass());

    // Test 6.6: Indirect command execution
    results.push(test_indirect_command_bypass());

    // Test 6.7: Alias expansion bypass
    results.push(test_alias_expansion_bypass());

    // Test 6.8: Subshell execution
    results.push(test_subshell_bypass());

    // Test 6.9: Here-document injection
    results.push(test_heredoc_bypass());

    // Test 6.10: Process substitution
    results.push(test_process_substitution_bypass());

    // =========================================================================
    // Print Summary
    // =========================================================================
    println!("\n=== TEST SUMMARY ===\n");

    let total = results.len();
    let passed = results.iter().filter(|r| r.passed).count();
    let failed = total - passed;

    println!("Total tests: {}", total);
    println!("Passed (vulnerability found): {}", passed);
    println!("Passed (secure): {}", failed);

    println!("\n=== VULNERABILITIES FOUND ===\n");
    for r in results.iter().filter(|r| r.passed) {
        println!("[VULN] {}: {}", r.name, &r.details);
    }

    println!("\n=== TESTS THAT BLOCKED ATTACKS ===\n");
    for r in results.iter().filter(|r| !r.passed) {
        println!("[SECURE] {}: {}", r.name, &r.details);
    }
}

// =========================================================================
// Category 1: Execution Policy Bypass Tests
// =========================================================================

fn test_case_sensitivity_bypass() -> TestResult {
    let mut policy = Policy::new();
    policy
        .add_prefix_rule(&["cat".to_string()], Decision::Allow, None)
        .unwrap();
    policy
        .add_prefix_rule(
            &["cat".to_string(), "/etc/passwd".to_string()],
            Decision::Deny,
            None,
        )
        .unwrap();

    // Try uppercase - with case-insensitive matching, this should match and be denied
    let result = policy.check(&["CAT".to_string(), "/etc/passwd".to_string()]);
    let blocked = is_attack_blocked(&result);

    TestResult::new(
        "Case Sensitivity Bypass",
        !blocked, // passed = vulnerability found = NOT blocked
        if blocked {
            "Case-insensitive matching works correctly - attack blocked".to_string()
        } else {
            "Uppercase command 'CAT' bypassed the rule for 'cat'".to_string()
        },
    )
}

fn test_whitespace_bypass() -> TestResult {
    let mut policy = Policy::new();
    policy
        .add_prefix_rule(&["rm".to_string()], Decision::Deny, None)
        .unwrap();

    // Try with leading whitespace - sanitization should trim it and block
    let result = policy.check(&[" rm".to_string()]);
    let blocked = is_attack_blocked(&result);

    TestResult::new(
        "Whitespace Bypass",
        !blocked,
        if blocked {
            "Whitespace is handled correctly - attack blocked".to_string()
        } else {
            "Leading whitespace bypassed the deny rule".to_string()
        },
    )
}

fn test_empty_command_bypass() -> TestResult {
    let policy = Policy::new();
    let result = policy.check(&[]);
    let blocked = is_attack_blocked(&result);

    TestResult::new(
        "Empty Command Bypass",
        !blocked,
        if blocked {
            "Empty command is handled - attack blocked".to_string()
        } else {
            "Empty command list returns None (potential bypass)".to_string()
        },
    )
}

fn test_very_long_command_bypass() -> TestResult {
    let mut policy = Policy::new();
    policy
        .add_prefix_rule(&["ls".to_string()], Decision::Deny, None)
        .unwrap();

    // Create a very long "ls" that might overflow - sanitization should handle it
    // Use "ls" prefix to match the rule after sanitization
    let long_ls = "ls".to_string() + &"x".repeat(10000);
    let result = policy.check(&[long_ls]);
    let blocked = is_attack_blocked(&result);

    TestResult::new(
        "Very Long Command Bypass",
        !blocked, // passed = vulnerability found = NOT blocked
        if blocked {
            "Long commands are handled - attack blocked".to_string()
        } else {
            "Very long command name bypassed the rule".to_string()
        },
    )
}

fn test_multiple_spaces_bypass() -> TestResult {
    let mut policy = Policy::new();
    policy
        .add_prefix_rule(&["ls".to_string()], Decision::Deny, None)
        .unwrap();

    // Try with multiple spaces - sanitization should handle it
    let result = policy.check(&["ls".to_string(), " ".to_string()]);
    let blocked = is_attack_blocked(&result);

    TestResult::new(
        "Multiple Spaces Bypass",
        !blocked,
        if blocked {
            "Multiple spaces are handled - attack blocked".to_string()
        } else {
            "Multiple spaces in argument bypassed the rule".to_string()
        },
    )
}

fn test_null_byte_bypass() -> TestResult {
    let mut policy = Policy::new();
    policy
        .add_prefix_rule(&["cat".to_string()], Decision::Allow, None)
        .unwrap();
    policy
        .add_prefix_rule(
            &["cat".to_string(), "/etc/passwd".to_string()],
            Decision::Deny,
            None,
        )
        .unwrap();

    // Null byte in path
    let result = policy.check(&["cat".to_string(), "/etc/pass\x00wd".to_string()]);

    TestResult::new(
        "Null Byte Bypass",
        result.is_none(),
        if result.is_none() {
            "Null byte in path might bypass detection".to_string()
        } else {
            "Null byte is handled".to_string()
        },
    )
}

fn test_unicode_homoglyph_bypass() -> TestResult {
    let mut policy = Policy::new();
    policy
        .add_prefix_rule(&["cat".to_string()], Decision::Allow, None)
        .unwrap();
    policy
        .add_prefix_rule(
            &["cat".to_string(), "/etc/passwd".to_string()],
            Decision::Deny,
            None,
        )
        .unwrap();

    // Cyrillic 'a' looks like Latin 'a'
    let result = policy.check(&["cat".to_string(), "/еtc/passwd".to_string()]); // Cyrillic 'е'

    TestResult::new(
        "Unicode Homoglyph Bypass",
        result.is_none(),
        if result.is_none() {
            "Unicode homoglyph might bypass path check".to_string()
        } else {
            "Unicode is handled correctly".to_string()
        },
    )
}

fn test_alternative_separator_bypass() -> TestResult {
    let mut policy = Policy::new();
    policy
        .add_prefix_rule(&["curl".to_string()], Decision::Deny, None)
        .unwrap();

    // Try different separators
    let result = policy.check(&["curl".to_string(), "http://evil.com".to_string()]);

    TestResult::new(
        "Alternative Separator Bypass",
        result.is_none(),
        if result.is_none() {
            "Alternative separator might bypass".to_string()
        } else {
            "Separators are handled".to_string()
        },
    )
}

fn test_newline_injection_bypass() -> TestResult {
    let mut policy = Policy::new();
    policy
        .add_prefix_rule(&["ls".to_string()], Decision::Deny, None)
        .unwrap();

    let result = policy.check(&["ls\n".to_string()]);
    let blocked = is_attack_blocked(&result);

    TestResult::new(
        "Newline Injection Bypass",
        !blocked,
        if blocked {
            "Newline is handled - attack blocked".to_string()
        } else {
            "Newline in command might bypass".to_string()
        },
    )
}

fn test_tab_injection_bypass() -> TestResult {
    let mut policy = Policy::new();
    policy
        .add_prefix_rule(&["ls".to_string()], Decision::Deny, None)
        .unwrap();

    let result = policy.check(&["ls\t".to_string()]);
    let blocked = is_attack_blocked(&result);

    TestResult::new(
        "Tab Injection Bypass",
        !blocked,
        if blocked {
            "Tab is handled - attack blocked".to_string()
        } else {
            "Tab in command might bypass".to_string()
        },
    )
}

// =========================================================================
// Category 2: Path Traversal Bypass Tests
// =========================================================================

fn test_double_url_encoding_bypass() -> TestResult {
    let mut policy = Policy::new();
    policy
        .add_prefix_rule(&["cat".to_string()], Decision::Allow, None)
        .unwrap();
    policy
        .add_prefix_rule(
            &["cat".to_string(), "/etc/passwd".to_string()],
            Decision::Deny,
            None,
        )
        .unwrap();

    // Double URL encode: %252e = .
    let result = policy.check(&["cat".to_string(), "%252e%252e%252fetc/passwd".to_string()]);

    TestResult::new(
        "Double URL Encoding Bypass",
        result.is_none(),
        if result.is_none() {
            "Double URL encoding might bypass path traversal detection".to_string()
        } else {
            "Double encoding is handled".to_string()
        },
    )
}

fn test_unicode_fullwidth_bypass() -> TestResult {
    let mut policy = Policy::new();
    policy
        .add_prefix_rule(&["ls".to_string()], Decision::Allow, None)
        .unwrap();
    policy
        .add_prefix_rule(
            &["ls".to_string(), "/root".to_string()],
            Decision::Deny,
            None,
        )
        .unwrap();

    // Fullwidth slash (U+FF0F)
    let result = policy.check(&["ls".to_string(), "/root\u{FF0F}..".to_string()]);

    TestResult::new(
        "Unicode Fullwidth Bypass",
        result.is_none(),
        if result.is_none() {
            "Fullwidth characters might bypass path check".to_string()
        } else {
            "Fullwidth is handled".to_string()
        },
    )
}

fn test_mixed_case_path_bypass() -> TestResult {
    let mut policy = Policy::new();
    policy
        .add_prefix_rule(&["cat".to_string()], Decision::Allow, None)
        .unwrap();
    policy
        .add_prefix_rule(
            &["cat".to_string(), "/etc/passwd".to_string()],
            Decision::Deny,
            None,
        )
        .unwrap();

    let result = policy.check(&["cat".to_string(), "/ETC/PASSWD".to_string()]);

    TestResult::new(
        "Mixed Case Path Bypass",
        result.is_none(),
        if result.is_none() {
            "Case-insensitive path might bypass".to_string()
        } else {
            "Path case is handled".to_string()
        },
    )
}

fn test_symlink_traversal_bypass() -> TestResult {
    let mut policy = Policy::new();
    policy
        .add_prefix_rule(&["cat".to_string()], Decision::Allow, None)
        .unwrap();

    // This test checks if the policy handles symlink-style paths
    let result = policy.check(&["cat".to_string(), "symlink_to_etc".to_string()]);

    TestResult::new(
        "Symlink Traversal Bypass",
        result.is_none(),
        "Symlink resolution not performed by policy engine (expected)".to_string(),
    )
}

fn test_null_terminated_path_bypass() -> TestResult {
    let mut policy = Policy::new();
    policy
        .add_prefix_rule(&["cat".to_string()], Decision::Allow, None)
        .unwrap();
    policy
        .add_prefix_rule(
            &["cat".to_string(), "/etc/passwd".to_string()],
            Decision::Deny,
            None,
        )
        .unwrap();

    let result = policy.check(&["cat".to_string(), "/etc/passwd\x00".to_string()]);

    TestResult::new(
        "Null-Terminated Path Bypass",
        result.is_none(),
        if result.is_none() {
            "Null-terminated path might bypass".to_string()
        } else {
            "Null-terminated path is handled".to_string()
        },
    )
}

// =========================================================================
// Category 3: Environment Variable Bypass Tests
// =========================================================================

fn test_lowercase_export_bypass() -> TestResult {
    let policy = Policy::new();
    // Try lowercase export (might not be caught)
    let result = policy.check(&["export".to_string(), "PATH=/evil".to_string()]);

    TestResult::new(
        "Lowercase Export Bypass",
        result.is_none(),
        if result.is_none() {
            "Lowercase export might bypass detection".to_string()
        } else {
            "Lowercase export is handled".to_string()
        },
    )
}

fn test_env_command_bypass() -> TestResult {
    let policy = Policy::new();
    // Try using env command instead of export
    let result = policy.check(&["env".to_string(), "PATH=/evil".to_string()]);

    TestResult::new(
        "Env Command Bypass",
        result.is_none(),
        if result.is_none() {
            "env command manipulation might bypass".to_string()
        } else {
            "env command is handled".to_string()
        },
    )
}

fn test_quoted_env_bypass() -> TestResult {
    let policy = Policy::new();
    // Try with quotes around the assignment
    let result = policy.check(&["export".to_string(), "\"PATH=/evil\"".to_string()]);
    let blocked = is_attack_blocked(&result);

    TestResult::new(
        "Quoted Env Bypass",
        !blocked, // passed = vulnerability found = NOT blocked
        if blocked {
            "Quoted env is handled".to_string()
        } else {
            "Quoted environment variable might bypass".to_string()
        },
    )
}

fn test_multiple_env_bypass() -> TestResult {
    let policy = Policy::new();
    // Try multiple env manipulations in one command
    let result = policy.check(&[
        "bash".to_string(),
        "-c".to_string(),
        "export A=1;export B=2;cat /etc/passwd".to_string(),
    ]);

    TestResult::new(
        "Multiple Env Bypass",
        result.is_none(),
        if result.is_none() {
            "Multiple env in command string might bypass".to_string()
        } else {
            "Multiple env is handled".to_string()
        },
    )
}

// =========================================================================
// Category 4: Sandbox Policy Bypass Tests
// =========================================================================

fn test_default_policy_danger() -> TestResult {
    let policy = SandboxPolicy::default();
    // 现在默认是 ReadOnly (安全的),不再是 DangerFullAccess
    let is_dangerous = matches!(policy, SandboxPolicy::DangerFullAccess);

    TestResult::new(
        "Default Policy Danger",
        !is_dangerous, // 取反:安全时通过
        if is_dangerous {
            "Default policy is DangerFullAccess - insecure by default!".to_string()
        } else {
            "Default policy is secure (ReadOnly)".to_string()
        },
    )
}

fn test_empty_workspace_paths() -> TestResult {
    use ai_sandbox::sandboxing::FileSystemSandboxPolicy;

    let policy = SandboxPolicy::WorkspaceWrite {
        writable_roots: vec![],
        network_access: NetworkSandboxPolicy::NoAccess,
    };

    // 现在空路径会被自动降级为 ReadOnly,所以检查 filesystem_policy
    let fs_policy = policy.filesystem_policy();
    let is_safe = !matches!(fs_policy, FileSystemSandboxPolicy::WorkspaceWrite { writable_roots } if writable_roots.is_empty());

    TestResult::new(
        "Empty Workspace Paths",
        is_safe, // 安全时通过
        if is_safe {
            "Empty writable_roots is handled safely (downgraded to ReadOnly)".to_string()
        } else {
            "Empty writable_roots accepted - might allow writing anywhere!".to_string()
        },
    )
}

fn test_long_path_policy_bypass() -> TestResult {
    let manager = SandboxManager::new();
    let command = SandboxCommand {
        program: OsString::from("ls"),
        args: vec![],
        cwd: PathBuf::from("/tmp"),
        env: HashMap::new(),
    };

    let long_path = "/".repeat(10000);
    let policy = SandboxPolicy::WorkspaceWrite {
        writable_roots: vec![PathBuf::from(&long_path)],
        network_access: NetworkSandboxPolicy::NoAccess,
    };

    // FIXED: Now is_safe() will reject very long paths that might cause issues
    let is_safe = SandboxPolicyExt::is_safe(&policy);

    // Also test that create_exec_request rejects unsafe policies
    let _result = manager.create_exec_request(command, policy);

    TestResult::new(
        "Long Path Policy Bypass",
        !is_safe, // Now passes if policy is unsafe (which is correct behavior)
        if is_safe {
            "Long path is handled safely (rejected)".to_string()
        } else {
            "Long path was rejected as unsafe".to_string()
        },
    )
}

fn test_special_char_path_bypass() -> TestResult {
    let manager = SandboxManager::new();
    let command = SandboxCommand {
        program: OsString::from("ls"),
        args: vec![],
        cwd: PathBuf::from("/tmp"),
        env: HashMap::new(),
    };

    // Path with path traversal characters
    let policy = SandboxPolicy::WorkspaceWrite {
        writable_roots: vec![PathBuf::from("/tmp/../etc")],
        network_access: NetworkSandboxPolicy::NoAccess,
    };

    // FIXED: Now is_safe() will reject paths with path traversal
    let is_safe = SandboxPolicyExt::is_safe(&policy);

    // Also test that create_exec_request rejects unsafe policies
    let _result = manager.create_exec_request(command, policy);

    TestResult::new(
        "Special Char Path Bypass",
        !is_safe, // Now passes if policy is unsafe (which is correct behavior)
        if is_safe {
            "Special char path is handled safely (rejected)".to_string()
        } else {
            "Special char path was rejected as unsafe".to_string()
        },
    )
}

// =========================================================================
// Category 5: Logic Flaw Tests
// =========================================================================

fn test_wildcard_exploitation() -> TestResult {
    let mut policy = Policy::new();
    policy
        .add_prefix_rule(&["ls".to_string(), "*".to_string()], Decision::Allow, None)
        .unwrap();

    // Does wildcard also match subdirectories?
    let result = policy.check(&["ls".to_string(), "subdir".to_string()]);
    let blocked = is_attack_blocked(&result);

    TestResult::new(
        "Wildcard Exploitation",
        blocked,
        if blocked {
            "Wildcard works as expected".to_string()
        } else {
            "Wildcard rule might not match literal arguments".to_string()
        },
    )
}

fn test_priority_inversion() -> TestResult {
    let mut policy = Policy::new();
    // Add deny rule first
    policy
        .add_prefix_rule(&["cat".to_string()], Decision::Deny, None)
        .unwrap();
    // Then add allow rule
    policy
        .add_prefix_rule(
            &["cat".to_string(), "/tmp/file.txt".to_string()],
            Decision::Allow,
            None,
        )
        .unwrap();

    let result = policy.check(&["cat".to_string(), "/tmp/file.txt".to_string()]);
    let blocked = is_attack_blocked(&result);

    TestResult::new(
        "Priority Inversion",
        blocked,
        if blocked {
            "Rule priority is correct".to_string()
        } else {
            "Allow rule takes precedence over deny - might be exploitable!".to_string()
        },
    )
}

fn test_regex_pattern_bypass() -> TestResult {
    let mut policy = Policy::new();
    policy
        .add_prefix_rule(&["cat".to_string()], Decision::Allow, None)
        .unwrap();
    policy
        .add_prefix_rule(
            &["cat".to_string(), "file.txt".to_string()],
            Decision::Deny,
            None,
        )
        .unwrap();

    // Try regex-like patterns
    let result = policy.check(&["cat".to_string(), "file.tx".to_string()]);

    TestResult::new(
        "Regex Pattern Bypass",
        result.is_none(),
        if result.is_none() {
            "Similar but different filename bypassed".to_string()
        } else {
            "Pattern matching works".to_string()
        },
    )
}

fn test_empty_arglist_bypass() -> TestResult {
    let mut policy = Policy::new();
    policy
        .add_prefix_rule(&["ls".to_string()], Decision::Deny, None)
        .unwrap();

    // Try with empty args
    let result = policy.check(&["ls".to_string()]);

    TestResult::new(
        "Empty Argument List Bypass",
        result.is_none(),
        if result.is_none() {
            "Empty args bypassed deny rule".to_string()
        } else {
            "Empty args handled correctly".to_string()
        },
    )
}

// =========================================================================
// Category 6: Advanced Attack Vectors
// =========================================================================

fn test_whitelist_mode_bypass() -> TestResult {
    let mut policy = Policy::new();
    policy.set_whitelist_mode(true);

    // Try to execute command not in whitelist
    let result = policy.check(&["cat".to_string(), "/etc/passwd".to_string()]);

    TestResult::new(
        "Whitelist Mode Bypass",
        result.is_none(),
        if result.is_none() {
            "Whitelist mode - unlisted command returned None (not explicitly denied)".to_string()
        } else {
            "Whitelist mode properly denies unlisted commands".to_string()
        },
    )
}

fn test_directory_restriction_bypass() -> TestResult {
    let mut policy = Policy::new();
    policy
        .add_prefix_rule_ext(
            &["cat".to_string()],
            Decision::Allow,
            None,
            RuleType::Whitelist,
            Some(vec!["/tmp".to_string()]),
            true,
        )
        .unwrap();

    // Try to access file outside allowed directory without cwd
    let result = policy.check_with_cwd(
        &["cat".to_string(), "/etc/passwd".to_string()],
        Some("/tmp"),
    );

    TestResult::new(
        "Directory Restriction Bypass",
        result.is_none(),
        if result.is_none() {
            "Directory restriction might be bypassable".to_string()
        } else {
            "Directory restriction is enforced".to_string()
        },
    )
}

fn test_shell_escape_allowed_command() -> TestResult {
    let mut policy = Policy::new();
    // Allow find command
    policy
        .add_prefix_rule(&["find".to_string()], Decision::Allow, None)
        .unwrap();

    // Try shell escape via find -exec
    let result = policy.check(&[
        "find".to_string(),
        "/tmp".to_string(),
        "-exec".to_string(),
        "cat".to_string(),
        "/etc/passwd".to_string(),
        ";".to_string(),
    ]);

    TestResult::new(
        "Shell Escape via Allowed Command",
        result.is_none(),
        if result.is_none() {
            "Shell escape via -exec might bypass".to_string()
        } else {
            "Shell escape is blocked".to_string()
        },
    )
}

fn test_path_normalization_bypass() -> TestResult {
    let mut policy = Policy::new();
    policy
        .add_prefix_rule(&["cat".to_string()], Decision::Allow, None)
        .unwrap();
    policy
        .add_prefix_rule(
            &["cat".to_string(), "/etc/passwd".to_string()],
            Decision::Deny,
            None,
        )
        .unwrap();

    // Try various path normalizations
    let paths = vec![
        "/./etc/passwd",
        "/etc/./passwd",
        "/etc/../etc/passwd",
        "/tmp/../etc/passwd",
    ];

    let mut bypassed = false;
    for path in paths {
        let result = policy.check(&["cat".to_string(), path.to_string()]);
        if result.is_none() {
            bypassed = true;
            break;
        }
    }

    TestResult::new(
        "Path Normalization Bypass",
        bypassed,
        if bypassed {
            "Path normalization can be bypassed".to_string()
        } else {
            "Path normalization is handled".to_string()
        },
    )
}

fn test_set_command_bypass() -> TestResult {
    let policy = Policy::new();

    // Try using set command instead of export
    let result = policy.check(&["set".to_string(), "PATH=/evil".to_string()]);

    TestResult::new(
        "Set Command Bypass",
        result.is_none(),
        if result.is_none() {
            "set command might bypass env restrictions".to_string()
        } else {
            "set command is handled".to_string()
        },
    )
}

fn test_indirect_command_bypass() -> TestResult {
    let mut policy = Policy::new();
    policy
        .add_prefix_rule(&["python3".to_string()], Decision::Deny, None)
        .unwrap();

    // Try using python with -c to execute shell
    let result = policy.check(&[
        "python3".to_string(),
        "-c".to_string(),
        "import os; os.system('cat /etc/passwd')".to_string(),
    ]);

    TestResult::new(
        "Indirect Command Bypass",
        result.is_none(),
        if result.is_none() {
            "Indirect command execution might bypass".to_string()
        } else {
            "Indirect execution is handled".to_string()
        },
    )
}

fn test_alias_expansion_bypass() -> TestResult {
    let mut policy = Policy::new();
    policy
        .add_prefix_rule(&["ls".to_string()], Decision::Deny, None)
        .unwrap();

    // Try with alias-like syntax
    let result = policy.check(&["ls".to_string(), "--color=auto".to_string()]);

    TestResult::new(
        "Alias Expansion Bypass",
        result.is_none(),
        if result.is_none() {
            "Alias-like arguments might bypass".to_string()
        } else {
            "Alias expansion is handled".to_string()
        },
    )
}

fn test_subshell_bypass() -> TestResult {
    let policy = Policy::new();

    // Try subshell execution
    let result = policy.check(&[
        "sh".to_string(),
        "-c".to_string(),
        "cat /etc/passwd".to_string(),
    ]);

    TestResult::new(
        "Subshell Bypass",
        result.is_none(),
        if result.is_none() {
            "Subshell execution might bypass".to_string()
        } else {
            "Subshell is handled".to_string()
        },
    )
}

fn test_heredoc_bypass() -> TestResult {
    let policy = Policy::new();

    // Try here-document injection
    let result = policy.check(&[
        "sh".to_string(),
        "-c".to_string(),
        "cat <<EOF\n#!/bin/bash\ncat /etc/passwd\nEOF".to_string(),
    ]);

    TestResult::new(
        "Here-document Bypass",
        result.is_none(),
        if result.is_none() {
            "Here-document might bypass".to_string()
        } else {
            "Here-document is handled".to_string()
        },
    )
}

fn test_process_substitution_bypass() -> TestResult {
    let policy = Policy::new();

    // Try process substitution
    let result = policy.check(&[
        "sh".to_string(),
        "-c".to_string(),
        "cat <(cat /etc/passwd)".to_string(),
    ]);

    TestResult::new(
        "Process Substitution Bypass",
        result.is_none(),
        if result.is_none() {
            "Process substitution might bypass".to_string()
        } else {
            "Process substitution is handled".to_string()
        },
    )
}