eryx 0.4.7

A Python sandbox with async callbacks powered by WebAssembly
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
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
//! Security tests for socket bypass attempts.
//!
//! These tests verify that Python code cannot bypass the eryx socket shim
//! to access raw WASI sockets or other low-level networking primitives.
//!
//! Think like an attacker: what are all the ways to get network access in Python?
#![allow(clippy::unwrap_used, clippy::expect_used)]

#[cfg(not(feature = "embedded"))]
use std::path::PathBuf;

use eryx::{NetConfig, Sandbox};

/// Helper to run adversarial Python code and check it doesn't succeed
async fn run_adversarial_test(code: &str, test_name: &str) -> (bool, String) {
    let sandbox = sandbox_builder()
        .with_network(NetConfig::default()) // Default blocks localhost/private
        .build()
        .expect("Failed to build sandbox");

    let result = sandbox.execute(code).await;
    match result {
        Ok(output) => {
            let stdout = output.stdout;
            let has_security_issue =
                stdout.contains("SECURITY ISSUE") || stdout.contains("BYPASS SUCCESSFUL");
            if has_security_issue {
                eprintln!("SECURITY ISSUE in {}: {}", test_name, stdout);
            }
            (!has_security_issue, stdout)
        }
        Err(e) => {
            // Execution error is generally safe (sandbox blocked something)
            (true, format!("Execution error (safe): {}", e))
        }
    }
}

#[allow(dead_code)]
/// Helper to run adversarial test with verbose output (for debugging)
async fn run_adversarial_test_verbose(code: &str, test_name: &str) -> (bool, String) {
    let sandbox = sandbox_builder()
        .with_network(NetConfig::default())
        .build()
        .expect("Failed to build sandbox");

    let result = sandbox.execute(code).await;
    match result {
        Ok(output) => {
            let stdout = output.stdout;
            let has_security_issue =
                stdout.contains("SECURITY ISSUE") || stdout.contains("BYPASS SUCCESSFUL");
            println!("=== {} ===\n{}", test_name, stdout);
            if !output.stderr.is_empty() {
                println!("stderr: {}", output.stderr);
            }
            (!has_security_issue, stdout)
        }
        Err(e) => {
            println!("=== {} ===\nExecution error (safe): {}", test_name, e);
            (true, format!("Execution error (safe): {}", e))
        }
    }
}

// =============================================================================
// Helper Functions
// =============================================================================

#[cfg(not(feature = "embedded"))]
fn runtime_wasm_path() -> PathBuf {
    let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".to_string());
    PathBuf::from(manifest_dir)
        .parent()
        .unwrap_or_else(|| std::path::Path::new("."))
        .join("eryx-runtime")
        .join("runtime.wasm")
}

#[cfg(not(feature = "embedded"))]
fn python_stdlib_path() -> PathBuf {
    if let Ok(path) = std::env::var("ERYX_PYTHON_STDLIB") {
        let path = PathBuf::from(path);
        if path.exists() {
            return path;
        }
    }

    let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".to_string());
    PathBuf::from(manifest_dir)
        .parent()
        .unwrap_or_else(|| std::path::Path::new("."))
        .join("eryx-wasm-runtime")
        .join("tests")
        .join("python-stdlib")
}

fn sandbox_builder() -> eryx::SandboxBuilder<eryx::state::Has, eryx::state::Has> {
    #[cfg(feature = "embedded")]
    {
        Sandbox::embedded()
    }

    #[cfg(not(feature = "embedded"))]
    {
        let stdlib_path = python_stdlib_path();
        Sandbox::builder()
            .with_wasm_file(runtime_wasm_path())
            .with_python_stdlib(&stdlib_path)
    }
}

// =============================================================================
// Socket Bypass Attempt Tests
// =============================================================================

/// Test that _socket module is replaced with eryx shim
#[tokio::test]
async fn test_socket_module_is_shimmed() {
    let sandbox = sandbox_builder()
        .with_network(NetConfig::default())
        .build()
        .expect("Failed to build sandbox");

    let result = sandbox
        .execute(
            r#"
import socket
import _socket

# Check that both modules are the same (our shim)
print(f"socket is _socket: {socket is _socket}")

# Check for eryx-specific attributes that wouldn't be in real _socket
has_eryx_markers = hasattr(socket, '__doc__') and 'eryx' in (socket.__doc__ or '').lower()
print(f"Has eryx markers: {has_eryx_markers}")

# The real _socket has SocketType as the main class
# Our shim uses 'socket' as the class name
socket_class_name = socket.socket.__name__
print(f"Socket class name: {socket_class_name}")
"#,
        )
        .await;

    assert!(result.is_ok(), "Should execute: {:?}", result);
    let output = result.unwrap();
    assert!(
        output.stdout.contains("socket is _socket: True"),
        "Both modules should be the same shim: {}",
        output.stdout
    );
}

/// Test that importing _socket directly doesn't give access to real sockets
#[tokio::test]
async fn test_direct_socket_import_is_shimmed() {
    let sandbox = sandbox_builder()
        .with_network(NetConfig::default())
        .build()
        .expect("Failed to build sandbox");

    let result = sandbox
        .execute(
            r#"
# Try to import _socket directly (the C extension in normal Python)
import _socket

# In real CPython, _socket.socket would be a type with specific C-level attributes
# Check if this has the behavior of our shim vs real _socket

# Real _socket.socket has these C-level methods that our shim doesn't perfectly replicate
# But more importantly, check if we can actually make connections that bypass NetConfig

sock = _socket.socket(_socket.AF_INET, _socket.SOCK_STREAM)
print(f"Created socket via _socket: {type(sock)}")

# The socket should still go through our shim's connect which uses eryx:net
print("_socket import test passed - it's our shim")
"#,
        )
        .await;

    assert!(result.is_ok(), "Should execute: {:?}", result);
    let output = result.unwrap();
    assert!(
        output.stdout.contains("_socket import test passed"),
        "Should use shimmed _socket: {}",
        output.stdout
    );
}

/// Test that ctypes cannot be used to call libc socket functions
#[tokio::test]
async fn test_ctypes_socket_bypass_blocked() {
    let sandbox = sandbox_builder()
        .with_network(NetConfig::default())
        .build()
        .expect("Failed to build sandbox");

    let result = sandbox
        .execute(
            r#"
try:
    import ctypes

    # Try to load libc and call socket() directly
    try:
        libc = ctypes.CDLL("libc.so.6")
        print("SECURITY ISSUE: Loaded libc.so.6")
    except OSError as e:
        print(f"EXPECTED: Cannot load libc: {e}")

    try:
        libc = ctypes.CDLL(None)  # Try default library
        socket_func = libc.socket
        print("SECURITY ISSUE: Got socket function from default lib")
    except (OSError, AttributeError) as e:
        print(f"EXPECTED: Cannot get socket from default lib: {e}")

except ImportError as e:
    print(f"EXPECTED: ctypes not available: {e}")
except Exception as e:
    print(f"EXPECTED: ctypes blocked: {type(e).__name__}: {e}")
"#,
        )
        .await;

    assert!(result.is_ok(), "Should execute: {:?}", result);
    let output = result.unwrap();
    assert!(
        output.stdout.contains("EXPECTED") && !output.stdout.contains("SECURITY ISSUE"),
        "ctypes socket bypass should be blocked: {}",
        output.stdout
    );
}

/// Test that os.open cannot be used to open /dev/tcp or similar
#[tokio::test]
async fn test_dev_tcp_bypass_blocked() {
    let sandbox = sandbox_builder()
        .with_network(NetConfig::default())
        .build()
        .expect("Failed to build sandbox");

    let result = sandbox
        .execute(
            r#"
import os

# Try bash-style /dev/tcp bypass (doesn't exist in real filesystem but test anyway)
try:
    fd = os.open("/dev/tcp/127.0.0.1/80", os.O_RDWR)
    print("SECURITY ISSUE: Opened /dev/tcp")
    os.close(fd)
except (OSError, FileNotFoundError, PermissionError) as e:
    print(f"EXPECTED: Cannot open /dev/tcp: {e}")
except Exception as e:
    print(f"EXPECTED: /dev/tcp blocked: {type(e).__name__}: {e}")

# Also try /dev/udp
try:
    fd = os.open("/dev/udp/127.0.0.1/53", os.O_RDWR)
    print("SECURITY ISSUE: Opened /dev/udp")
    os.close(fd)
except (OSError, FileNotFoundError, PermissionError) as e:
    print(f"EXPECTED: Cannot open /dev/udp: {e}")
except Exception as e:
    print(f"EXPECTED: /dev/udp blocked: {type(e).__name__}: {e}")
"#,
        )
        .await;

    assert!(result.is_ok(), "Should execute: {:?}", result);
    let output = result.unwrap();
    assert!(
        output.stdout.contains("EXPECTED") && !output.stdout.contains("SECURITY ISSUE"),
        "/dev/tcp bypass should be blocked: {}",
        output.stdout
    );
}

/// Test that subprocess cannot be used to make network connections
#[tokio::test]
async fn test_subprocess_network_bypass_blocked() {
    let sandbox = sandbox_builder()
        .with_network(NetConfig::default())
        .build()
        .expect("Failed to build sandbox");

    let result = sandbox
        .execute(
            r#"
try:
    import subprocess

    # Try to use curl/wget via subprocess
    try:
        result = subprocess.run(["curl", "http://example.com"], capture_output=True, timeout=5)
        print(f"SECURITY ISSUE: curl executed with return code {result.returncode}")
    except FileNotFoundError:
        print("EXPECTED: curl not found")
    except Exception as e:
        print(f"EXPECTED: subprocess curl blocked: {type(e).__name__}: {e}")

except ImportError as e:
    print(f"EXPECTED: subprocess not available: {e}")
except Exception as e:
    print(f"EXPECTED: subprocess blocked: {type(e).__name__}: {e}")
"#,
        )
        .await;

    assert!(result.is_ok(), "Should execute: {:?}", result);
    let output = result.unwrap();
    assert!(
        output.stdout.contains("EXPECTED") && !output.stdout.contains("SECURITY ISSUE"),
        "subprocess network bypass should be blocked: {}",
        output.stdout
    );
}

/// Test that the shimmed socket still respects NetConfig policies
#[tokio::test]
async fn test_shimmed_socket_respects_netconfig() {
    let sandbox = sandbox_builder()
        .with_network(NetConfig::default()) // Default blocks localhost
        .build()
        .expect("Failed to build sandbox");

    let result = sandbox
        .execute(
            r#"
import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(5)

try:
    # This should be blocked by NetConfig even through the shim
    sock.connect(("127.0.0.1", 80))
    print("SECURITY ISSUE: Connected to localhost despite NetConfig blocking it")
except OSError as e:
    error_str = str(e).lower()
    if "not permitted" in error_str or "blocked" in error_str or "refused" in error_str:
        print(f"EXPECTED: Connection blocked by policy: {e}")
    else:
        # Connection refused is also acceptable (means we tried but port isn't open)
        print(f"EXPECTED: Connection failed: {e}")
except Exception as e:
    print(f"EXPECTED: Connection blocked: {type(e).__name__}: {e}")
finally:
    sock.close()
"#,
        )
        .await;

    assert!(result.is_ok(), "Should execute: {:?}", result);
    let output = result.unwrap();
    assert!(
        output.stdout.contains("EXPECTED") && !output.stdout.contains("SECURITY ISSUE"),
        "Shimmed socket should respect NetConfig: {}",
        output.stdout
    );
}

/// Test attempting to access WASI sockets directly via the component model
/// This tests whether wasi:sockets is actually importable from the guest
#[tokio::test]
async fn test_wasi_sockets_not_importable() {
    let sandbox = sandbox_builder()
        .with_network(NetConfig::permissive())
        .build()
        .expect("Failed to build sandbox");

    let result = sandbox
        .execute(
            r#"
# The sandbox world only imports eryx:net/tcp and eryx:net/tls
# It should NOT have wasi:sockets available

# Try to find any WASI socket-related modules
import sys

wasi_modules = [name for name in sys.modules.keys() if 'wasi' in name.lower()]
print(f"WASI-related modules: {wasi_modules}")

# Check if there's any way to import wasi_snapshot_preview1 socket functions
# In WASI preview 1, these would be sock_accept, sock_recv, sock_send, etc.
# But they shouldn't be exposed to Python in our sandbox

# The only network access should be through _eryx_async
try:
    import _eryx_async
    print(f"_eryx_async available: {dir(_eryx_async)}")
    print("Network access is properly channeled through eryx")
except ImportError:
    print("_eryx_async not available")

print("WASI socket isolation test completed")
"#,
        )
        .await;

    assert!(result.is_ok(), "Should execute: {:?}", result);
    let output = result.unwrap();
    assert!(
        output
            .stdout
            .contains("Network access is properly channeled through eryx")
            || output
                .stdout
                .contains("WASI socket isolation test completed"),
        "Should show proper network channeling: {}",
        output.stdout
    );
}

/// Test that os module network functions are not available or are shimmed
#[tokio::test]
async fn test_os_network_functions_blocked() {
    let sandbox = sandbox_builder()
        .with_network(NetConfig::default())
        .build()
        .expect("Failed to build sandbox");

    let result = sandbox
        .execute(
            r#"
import os

# Check for low-level socket-related os functions that might bypass our shim
dangerous_funcs = ['socket', 'socketpair', 'bind', 'listen', 'accept', 'connect']
available = []
for func in dangerous_funcs:
    if hasattr(os, func):
        available.append(func)

if available:
    print(f"WARNING: os module has socket functions: {available}")
    # Try to use them
    for func in available:
        try:
            f = getattr(os, func)
            print(f"SECURITY CONCERN: os.{func} is accessible")
        except Exception as e:
            print(f"os.{func} blocked: {e}")
else:
    print("EXPECTED: os module does not expose socket functions")
"#,
        )
        .await;

    assert!(result.is_ok(), "Should execute: {:?}", result);
    let output = result.unwrap();
    // Note: This test documents current behavior - os module shouldn't have raw socket functions
    // but if it does, they should go through WASI which we don't import
    println!("os network functions output: {}", output.stdout);
}

/// Test that multiprocessing cannot be used to spawn processes with network access
#[tokio::test]
async fn test_multiprocessing_blocked() {
    let sandbox = sandbox_builder()
        .with_network(NetConfig::default())
        .build()
        .expect("Failed to build sandbox");

    let result = sandbox
        .execute(
            r#"
try:
    import multiprocessing
    print(f"multiprocessing imported: {multiprocessing}")

    # Try to create a process
    try:
        p = multiprocessing.Process(target=lambda: None)
        p.start()
        print("SECURITY ISSUE: Created subprocess via multiprocessing")
    except Exception as e:
        print(f"EXPECTED: multiprocessing.Process blocked: {type(e).__name__}: {e}")

except ImportError as e:
    print(f"EXPECTED: multiprocessing not available: {e}")
except Exception as e:
    print(f"EXPECTED: multiprocessing blocked: {type(e).__name__}: {e}")
"#,
        )
        .await;

    assert!(result.is_ok(), "Should execute: {:?}", result);
    let output = result.unwrap();
    assert!(
        output.stdout.contains("EXPECTED") && !output.stdout.contains("SECURITY ISSUE"),
        "multiprocessing should be blocked: {}",
        output.stdout
    );
}

// =============================================================================
// Adversarial Attack Tests - Think Like a Hacker!
// =============================================================================

/// Attack: Use importlib to get the "real" socket module before it was replaced
#[tokio::test]
async fn test_attack_importlib_reload_socket() {
    let (safe, output) = run_adversarial_test(
        r#"
import sys
import importlib

# Attack: Try to reload socket to get original implementation
try:
    # Remove the shimmed module
    if 'socket' in sys.modules:
        del sys.modules['socket']
    if '_socket' in sys.modules:
        del sys.modules['_socket']

    # Try to reimport - will this get the real one?
    import socket

    # Check if we got the shim or the real module
    if 'eryx' in str(socket.__doc__).lower():
        print("EXPECTED: Got eryx shim after reload")
    else:
        # Try to connect to localhost to verify
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.settimeout(1)
        try:
            sock.connect(("127.0.0.1", 22))  # SSH port
            print("BYPASS SUCCESSFUL: Connected via reloaded socket!")
        except Exception as e:
            if "not permitted" in str(e).lower():
                print("EXPECTED: Still blocked by policy")
            else:
                print(f"Connection failed (might still be shim): {e}")
        finally:
            sock.close()
except Exception as e:
    print(f"EXPECTED: Reload attack failed: {type(e).__name__}: {e}")
"#,
        "importlib_reload_socket",
    )
    .await;
    assert!(
        safe,
        "importlib reload attack should not bypass shim: {}",
        output
    );
}

/// Attack: Use importlib.util to load socket from a specific path
#[tokio::test]
async fn test_attack_importlib_spec_socket() {
    let (safe, output) = run_adversarial_test(
        r#"
import importlib.util
import sys

# Attack: Try to find and load the "real" _socket C extension
try:
    # Find where _socket would normally be
    spec = importlib.util.find_spec('_socket')
    if spec is None:
        print("EXPECTED: _socket spec not found (no real C extension)")
    else:
        print(f"Found _socket spec: {spec.origin}")
        # Try to load it
        module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(module)

        # Check if it's the real deal
        if hasattr(module, 'socket') and 'eryx' not in str(getattr(module, '__doc__', '')).lower():
            sock = module.socket(module.AF_INET, module.SOCK_STREAM)
            sock.settimeout(1)
            try:
                sock.connect(("127.0.0.1", 22))
                print("BYPASS SUCCESSFUL: Connected via importlib.util loaded socket!")
            except Exception as e:
                print(f"Connection failed: {e}")
            finally:
                sock.close()
        else:
            print("EXPECTED: Got shim via importlib.util")
except Exception as e:
    print(f"EXPECTED: importlib.util attack failed: {type(e).__name__}: {e}")
"#,
        "importlib_spec_socket",
    )
    .await;
    assert!(
        safe,
        "importlib.util attack should not bypass shim: {}",
        output
    );
}

/// Attack: Access __builtins__ to find original open/socket
#[tokio::test]
async fn test_attack_builtins_manipulation() {
    let (safe, output) = run_adversarial_test(
        r#"
import builtins

# Attack: Check if there's a hidden socket in builtins
try:
    # Look for anything socket-related in builtins
    socket_stuff = [name for name in dir(builtins) if 'socket' in name.lower()]
    if socket_stuff:
        print(f"WARNING: Found socket in builtins: {socket_stuff}")
        for name in socket_stuff:
            obj = getattr(builtins, name)
            print(f"SECURITY ISSUE: builtins.{name} = {obj}")
    else:
        print("EXPECTED: No socket in builtins")

    # Also check __builtins__ in different contexts
    import __main__
    if hasattr(__main__, '__builtins__'):
        b = __main__.__builtins__
        if isinstance(b, dict):
            socket_in_b = [k for k in b.keys() if 'socket' in k.lower()]
        else:
            socket_in_b = [name for name in dir(b) if 'socket' in name.lower()]
        if socket_in_b:
            print(f"WARNING: Found socket in __main__.__builtins__: {socket_in_b}")
        else:
            print("EXPECTED: No socket in __main__.__builtins__")
except Exception as e:
    print(f"EXPECTED: builtins attack failed: {type(e).__name__}: {e}")
"#,
        "builtins_manipulation",
    )
    .await;
    assert!(
        safe,
        "builtins manipulation should not expose socket: {}",
        output
    );
}

/// Attack: Use exec/eval with crafted globals to bypass module replacement
#[tokio::test]
async fn test_attack_exec_with_custom_globals() {
    let (safe, output) = run_adversarial_test(
        r#"
import sys

# Attack: Try exec with empty globals to avoid module cache
try:
    code = '''
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1)
try:
    sock.connect(("127.0.0.1", 22))
    result = "BYPASS SUCCESSFUL"
except Exception as e:
    result = f"blocked: {e}"
finally:
    sock.close()
'''

    # Try with fresh globals
    fresh_globals = {'__builtins__': __builtins__}
    exec(code, fresh_globals)

    if "BYPASS" in fresh_globals.get('result', ''):
        print(fresh_globals['result'])
    else:
        print(f"EXPECTED: exec with fresh globals still blocked: {fresh_globals.get('result', 'no result')}")

except Exception as e:
    print(f"EXPECTED: exec attack failed: {type(e).__name__}: {e}")
"#,
        "exec_custom_globals",
    )
    .await;
    assert!(
        safe,
        "exec with custom globals should not bypass: {}",
        output
    );
}

/// Attack: Use __import__ directly with different parameters
#[tokio::test]
async fn test_attack_dunder_import() {
    let (safe, output) = run_adversarial_test(
        r#"
import sys

# Attack: Use __import__ with various tricks
try:
    # Clear module cache first
    for mod in list(sys.modules.keys()):
        if 'socket' in mod:
            del sys.modules[mod]

    # Try __import__ with fromlist
    socket_mod = __import__('socket', globals(), locals(), ['socket'], 0)

    # Check if it's the shim
    if 'eryx' in str(getattr(socket_mod, '__doc__', '')).lower():
        print("EXPECTED: __import__ returned eryx shim")
    else:
        sock = socket_mod.socket(socket_mod.AF_INET, socket_mod.SOCK_STREAM)
        sock.settimeout(1)
        try:
            sock.connect(("127.0.0.1", 22))
            print("BYPASS SUCCESSFUL: __import__ bypassed shim!")
        except Exception as e:
            if "not permitted" in str(e).lower():
                print("EXPECTED: __import__ socket still enforces policy")
            else:
                print(f"Connection failed: {e}")
        finally:
            sock.close()
except Exception as e:
    print(f"EXPECTED: __import__ attack failed: {type(e).__name__}: {e}")
"#,
        "dunder_import",
    )
    .await;
    assert!(safe, "__import__ attack should not bypass shim: {}", output);
}

/// Attack: Try to access _io module for low-level file operations
#[tokio::test]
async fn test_attack_io_module_bypass() {
    let (safe, output) = run_adversarial_test(
        r#"
# Attack: Use _io module to bypass any file operation shims
try:
    import _io

    # Try to open /etc/passwd (should be blocked by WASI sandbox)
    try:
        f = _io.open('/etc/passwd', 'r')
        content = f.read(100)
        f.close()
        print(f"SECURITY ISSUE: Read /etc/passwd via _io: {content[:50]}")
    except (FileNotFoundError, PermissionError, OSError) as e:
        print(f"EXPECTED: _io.open blocked: {e}")

    # Try to open /proc/net/tcp (Linux network connections)
    try:
        f = _io.open('/proc/net/tcp', 'r')
        content = f.read(100)
        f.close()
        print(f"SECURITY ISSUE: Read /proc/net/tcp via _io: {content[:50]}")
    except (FileNotFoundError, PermissionError, OSError) as e:
        print(f"EXPECTED: _io.open /proc blocked: {e}")

except ImportError as e:
    print(f"EXPECTED: _io not available: {e}")
except Exception as e:
    print(f"EXPECTED: _io attack failed: {type(e).__name__}: {e}")
"#,
        "io_module_bypass",
    )
    .await;
    assert!(safe, "_io module should not bypass sandbox: {}", output);
}

/// Attack: Try to find socket via sys.modules manipulation
#[tokio::test]
async fn test_attack_sys_modules_manipulation() {
    let (safe, output) = run_adversarial_test(
        r#"
import sys

# Attack: Look for any socket-related modules that might have been loaded
socket_modules = [(name, mod) for name, mod in sys.modules.items()
                  if mod is not None and 'socket' in name.lower()]

print(f"Socket-related modules in sys.modules: {[name for name, _ in socket_modules]}")

for name, mod in socket_modules:
    # Check each module for socket capabilities
    if hasattr(mod, 'socket') and callable(getattr(mod, 'socket', None)):
        print(f"Module {name} has socket function")
        # Check if it's the shim
        doc = str(getattr(mod, '__doc__', ''))
        if 'eryx' in doc.lower():
            print(f"EXPECTED: {name} is eryx shim")
        else:
            print(f"WARNING: {name} might be real socket module")
            # Try to use it
            try:
                sock_class = getattr(mod, 'socket')
                sock = sock_class(2, 1)  # AF_INET, SOCK_STREAM
                sock.settimeout(1)
                sock.connect(("127.0.0.1", 22))
                print(f"BYPASS SUCCESSFUL via {name}!")
                sock.close()
            except Exception as e:
                print(f"Connection via {name} failed: {e}")
"#,
        "sys_modules_manipulation",
    )
    .await;
    assert!(
        safe,
        "sys.modules manipulation should not bypass: {}",
        output
    );
}

/// Attack: Try to use code objects to bypass restrictions
#[tokio::test]
async fn test_attack_code_object_manipulation() {
    let (safe, output) = run_adversarial_test(
        r#"
import types

# Attack: Create a code object that imports socket in a fresh namespace
try:
    code = compile('''
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(1)
try:
    s.connect(("127.0.0.1", 22))
    RESULT = "BYPASS SUCCESSFUL"
except Exception as e:
    RESULT = f"blocked: {e}"
finally:
    s.close()
''', '<attack>', 'exec')

    # Execute in a minimal namespace
    namespace = {}
    exec(code, namespace)

    result = namespace.get('RESULT', 'no result')
    if 'BYPASS' in result:
        print(result)
    else:
        print(f"EXPECTED: Code object attack blocked: {result}")

except Exception as e:
    print(f"EXPECTED: Code object attack failed: {type(e).__name__}: {e}")
"#,
        "code_object_manipulation",
    )
    .await;
    assert!(
        safe,
        "Code object manipulation should not bypass: {}",
        output
    );
}

/// Attack: Look for any C extension modules that might have socket access
#[tokio::test]
async fn test_attack_find_c_extensions() {
    let (safe, output) = run_adversarial_test(
        r#"
import sys
import importlib

# Attack: Find all C extension modules (they might have raw socket access)
c_extensions = []
for name, mod in sys.modules.items():
    if mod is None:
        continue
    # C extensions typically have __file__ ending in .so or no __file__
    file = getattr(mod, '__file__', None)
    if file and (file.endswith('.so') or file.endswith('.pyd')):
        c_extensions.append((name, file))
    elif not hasattr(mod, '__file__') and hasattr(mod, '__loader__'):
        # Built-in modules
        loader = getattr(mod, '__loader__', None)
        if loader and 'BuiltinImporter' in str(type(loader)):
            c_extensions.append((name, '<builtin>'))

print(f"Found {len(c_extensions)} C extensions/builtins")

# Check each for socket capabilities
dangerous = []
for name, path in c_extensions:
    mod = sys.modules.get(name)
    if mod:
        # Look for socket-related attributes
        attrs = dir(mod)
        socket_attrs = [a for a in attrs if 'sock' in a.lower() or 'connect' in a.lower() or 'bind' in a.lower()]
        if socket_attrs:
            dangerous.append((name, socket_attrs))

if dangerous:
    print(f"WARNING: Modules with socket-like attributes: {dangerous}")
    for name, attrs in dangerous:
        print(f"  {name}: {attrs}")
else:
    print("EXPECTED: No C extensions with exposed socket functions")
"#,
        "find_c_extensions",
    )
    .await;
    assert!(
        safe,
        "C extension search should not find bypass: {}",
        output
    );
}

/// Attack: Try to use asyncio internals to bypass socket shim
#[tokio::test]
async fn test_attack_asyncio_internals() {
    let (safe, output) = run_adversarial_test(
        r#"
import asyncio

# Attack: asyncio has its own socket handling - can we bypass via it?
try:
    # Look at asyncio's selector module
    import selectors
    print(f"selectors module: {selectors}")

    # asyncio.open_connection might use different socket path
    async def try_connect():
        try:
            reader, writer = await asyncio.wait_for(
                asyncio.open_connection('127.0.0.1', 22),
                timeout=2.0
            )
            print("BYPASS SUCCESSFUL: asyncio.open_connection worked!")
            writer.close()
            await writer.wait_closed()
        except asyncio.TimeoutError:
            print("Connection timed out")
        except OSError as e:
            if "not permitted" in str(e).lower():
                print(f"EXPECTED: asyncio.open_connection blocked by policy: {e}")
            else:
                print(f"Connection failed (might still be blocked): {e}")
        except Exception as e:
            print(f"EXPECTED: asyncio attack blocked: {type(e).__name__}: {e}")

    asyncio.run(try_connect())

except Exception as e:
    print(f"EXPECTED: asyncio internals attack failed: {type(e).__name__}: {e}")
"#,
        "asyncio_internals",
    )
    .await;
    assert!(safe, "asyncio internals should not bypass shim: {}", output);
}

/// Diagnostic: What socket module do we actually get after reload?
#[tokio::test]
async fn test_diagnostic_socket_after_reload() {
    let (safe, output) = run_adversarial_test(
        r#"
import sys

# First, check what socket module we have before manipulation
import socket as original_socket
print(f"Original socket module: {original_socket}")
print(f"Original socket.__name__: {original_socket.__name__}")
print(f"Original socket.__file__: {getattr(original_socket, '__file__', 'NO FILE')}")
print(f"Original socket.__doc__[:100]: {str(original_socket.__doc__)[:100]}")
print(f"Original socket.socket: {original_socket.socket}")

# Now delete and reimport
del sys.modules['socket']
del sys.modules['_socket']

import socket as reloaded_socket
print(f"\nReloaded socket module: {reloaded_socket}")
print(f"Reloaded socket.__name__: {reloaded_socket.__name__}")
print(f"Reloaded socket.__file__: {getattr(reloaded_socket, '__file__', 'NO FILE')}")
print(f"Reloaded socket.__doc__[:100]: {str(reloaded_socket.__doc__)[:100]}")
print(f"Reloaded socket.socket: {reloaded_socket.socket}")

# Are they the same?
print(f"\nSame module? {original_socket is reloaded_socket}")
print(f"Same socket class? {original_socket.socket is reloaded_socket.socket}")

# Try to create a socket and see what type it is
sock = reloaded_socket.socket(reloaded_socket.AF_INET, reloaded_socket.SOCK_STREAM)
print(f"\nSocket instance type: {type(sock)}")
print(f"Socket instance module: {type(sock).__module__}")

# Check the connect method
print(f"connect method: {sock.connect}")

sock.close()
"#,
        "diagnostic_socket_after_reload",
    )
    .await;
    // This is diagnostic, just print results
    println!("Diagnostic complete");
    assert!(
        safe,
        "Diagnostic should not show security issues: {}",
        output
    );
}

/// CRITICAL: Test that bypassing shim to real socket is blocked by WASI
/// This verifies defense in depth - even if shim is bypassed, WASI blocks it
#[tokio::test]
async fn test_critical_real_socket_blocked_by_wasi() {
    let (safe, output) = run_adversarial_test(
        r#"
import sys

# Bypass the shim by reimporting real socket module
del sys.modules['socket']
del sys.modules['_socket']

# This gets the REAL socket module from /python-stdlib/socket.py
import socket

print(f"Got real socket from: {getattr(socket, '__file__', 'unknown')}")
print(f"This is the real CPython socket module, not eryx shim")

# Create a real socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(5)

# Try to connect to an external server (should be blocked by WASI, not our policy)
# Using a well-known IP to avoid DNS issues
try:
    sock.connect(("8.8.8.8", 53))  # Google DNS
    data = sock.recv(100)
    print(f"BYPASS SUCCESSFUL: Connected to 8.8.8.8:53 with real socket! Got {len(data)} bytes")
except PermissionError as e:
    print(f"EXPECTED (WASI blocks): PermissionError: {e}")
except OSError as e:
    # Check if this is our shim's error or a WASI error
    if "not-permitted" in str(e).lower() and "blocked" in str(e).lower():
        print(f"WARNING: Got shim error, but we should have real socket: {e}")
    else:
        print(f"EXPECTED (WASI blocks): OSError: {e}")
except Exception as e:
    print(f"EXPECTED (blocked): {type(e).__name__}: {e}")
finally:
    sock.close()

# Also try localhost to be thorough
sock2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock2.settimeout(2)
try:
    sock2.connect(("127.0.0.1", 22))
    print("BYPASS SUCCESSFUL: Connected to localhost with real socket!")
except Exception as e:
    print(f"Localhost also blocked: {type(e).__name__}: {e}")
finally:
    sock2.close()
"#,
        "critical_real_socket_blocked_by_wasi",
    )
    .await;
    assert!(safe, "Real socket should be blocked by WASI: {}", output);
}

/// Attack: Try urllib and http.client which use socket internally
#[tokio::test]
async fn test_attack_urllib_http_client() {
    let (safe, output) = run_adversarial_test(
        r#"
# Attack: urllib/http.client are high-level but use socket internally
try:
    import urllib.request

    # This should use socket internally - does it bypass our shim?
    try:
        response = urllib.request.urlopen('http://127.0.0.1:80/', timeout=2)
        print(f"BYPASS SUCCESSFUL: urllib connected to localhost!")
        response.close()
    except urllib.error.URLError as e:
        if "not permitted" in str(e).lower() or "blocked" in str(e).lower():
            print(f"EXPECTED: urllib blocked by policy: {e}")
        else:
            print(f"urllib failed (might be blocked): {e}")
    except Exception as e:
        print(f"urllib blocked: {type(e).__name__}: {e}")

except ImportError as e:
    print(f"urllib not available: {e}")
except Exception as e:
    print(f"EXPECTED: urllib attack failed: {type(e).__name__}: {e}")

try:
    import http.client

    try:
        conn = http.client.HTTPConnection('127.0.0.1', 80, timeout=2)
        conn.request('GET', '/')
        response = conn.getresponse()
        print(f"BYPASS SUCCESSFUL: http.client connected to localhost!")
        conn.close()
    except OSError as e:
        if "not permitted" in str(e).lower():
            print(f"EXPECTED: http.client blocked by policy: {e}")
        else:
            print(f"http.client failed: {e}")
    except Exception as e:
        print(f"EXPECTED: http.client blocked: {type(e).__name__}: {e}")

except ImportError as e:
    print(f"http.client not available: {e}")
except Exception as e:
    print(f"EXPECTED: http.client attack failed: {type(e).__name__}: {e}")
"#,
        "urllib_http_client",
    )
    .await;
    assert!(
        safe,
        "urllib/http.client should respect shim policies: {}",
        output
    );
}