@@ -21,7 +21,7 @@ struct TestFactors {
21
21
22
22
#[ tokio:: test]
23
23
async fn allowed_host_is_allowed ( ) -> anyhow:: Result < ( ) > {
24
- let mut state = test_instance_state ( "https://*" ) . await ?;
24
+ let mut state = test_instance_state ( "https://*" , true ) . await ?;
25
25
let mut wasi_http = OutboundHttpFactor :: get_wasi_http_impl ( & mut state) . unwrap ( ) ;
26
26
27
27
// [100::] is an IPv6 "black hole", which should always fail
@@ -39,7 +39,7 @@ async fn allowed_host_is_allowed() -> anyhow::Result<()> {
39
39
40
40
#[ tokio:: test]
41
41
async fn self_request_smoke_test ( ) -> anyhow:: Result < ( ) > {
42
- let mut state = test_instance_state ( "http://self" ) . await ?;
42
+ let mut state = test_instance_state ( "http://self" , true ) . await ?;
43
43
let origin = SelfRequestOrigin :: from_uri ( & Uri :: from_static ( "http://[100::1]" ) ) ?;
44
44
state. http . set_self_request_origin ( origin) ;
45
45
@@ -58,7 +58,7 @@ async fn self_request_smoke_test() -> anyhow::Result<()> {
58
58
59
59
#[ tokio:: test]
60
60
async fn disallowed_host_fails ( ) -> anyhow:: Result < ( ) > {
61
- let mut state = test_instance_state ( "https://allowed.test" ) . await ?;
61
+ let mut state = test_instance_state ( "https://allowed.test" , true ) . await ?;
62
62
let mut wasi_http = OutboundHttpFactor :: get_wasi_http_impl ( & mut state) . unwrap ( ) ;
63
63
64
64
let req = Request :: get ( "https://denied.test" ) . body ( Default :: default ( ) ) ?;
@@ -71,13 +71,47 @@ async fn disallowed_host_fails() -> anyhow::Result<()> {
71
71
Ok ( ( ) )
72
72
}
73
73
74
+ #[ tokio:: test]
75
+ async fn disallowed_private_ips_fails ( ) -> anyhow:: Result < ( ) > {
76
+ async fn run_test ( allow_private_ips : bool ) -> anyhow:: Result < ( ) > {
77
+ let mut state = test_instance_state ( "http://*" , allow_private_ips) . await ?;
78
+ let mut wasi_http = OutboundHttpFactor :: get_wasi_http_impl ( & mut state) . unwrap ( ) ;
79
+ let req = Request :: get ( "http://localhost" ) . body ( Default :: default ( ) ) ?;
80
+ let mut future_resp = wasi_http. send_request ( req, test_request_config ( ) ) ?;
81
+ future_resp. ready ( ) . await ;
82
+ match future_resp. unwrap_ready ( ) . unwrap ( ) {
83
+ // If we don't allow private IPs, we should not get a response
84
+ Ok ( _) if !allow_private_ips => bail ! ( "expected Err, got Ok" ) ,
85
+ // Otherwise, it's fine if the request happens to succeed
86
+ Ok ( _) => { }
87
+ // If private IPs are disallowed, we should get an error saying the destination is prohibited
88
+ Err ( err) if !allow_private_ips => {
89
+ assert ! ( matches!( err, ErrorCode :: DestinationIpProhibited ) )
90
+ }
91
+ // Otherwise, we should get some non-DestinationIpProhibited error
92
+ Err ( err) => {
93
+ assert ! ( !matches!( err, ErrorCode :: DestinationIpProhibited ) )
94
+ }
95
+ } ;
96
+ Ok ( ( ) )
97
+ }
98
+
99
+ // Test with private IPs allowed
100
+ run_test ( true ) . await ?;
101
+ // Test with private IPs disallowed
102
+ run_test ( false ) . await ?;
103
+
104
+ Ok ( ( ) )
105
+ }
106
+
74
107
async fn test_instance_state (
75
108
allowed_outbound_hosts : & str ,
109
+ allow_private_ips : bool ,
76
110
) -> anyhow:: Result < TestFactorsInstanceState > {
77
111
let factors = TestFactors {
78
112
variables : VariablesFactor :: default ( ) ,
79
113
networking : OutboundNetworkingFactor :: new ( ) ,
80
- http : OutboundHttpFactor :: default ( ) ,
114
+ http : OutboundHttpFactor :: new ( allow_private_ips ) ,
81
115
} ;
82
116
let env = TestEnvironment :: new ( factors) . extend_manifest ( toml ! {
83
117
[ component. test-component]
0 commit comments