Skip to content

Commit 166b337

Browse files
committed
Add test
Signed-off-by: Ryan Levick <[email protected]>
1 parent 4821970 commit 166b337

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

crates/factor-outbound-http/tests/factor_test.rs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct TestFactors {
2121

2222
#[tokio::test]
2323
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?;
2525
let mut wasi_http = OutboundHttpFactor::get_wasi_http_impl(&mut state).unwrap();
2626

2727
// [100::] is an IPv6 "black hole", which should always fail
@@ -39,7 +39,7 @@ async fn allowed_host_is_allowed() -> anyhow::Result<()> {
3939

4040
#[tokio::test]
4141
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?;
4343
let origin = SelfRequestOrigin::from_uri(&Uri::from_static("http://[100::1]"))?;
4444
state.http.set_self_request_origin(origin);
4545

@@ -58,7 +58,7 @@ async fn self_request_smoke_test() -> anyhow::Result<()> {
5858

5959
#[tokio::test]
6060
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?;
6262
let mut wasi_http = OutboundHttpFactor::get_wasi_http_impl(&mut state).unwrap();
6363

6464
let req = Request::get("https://denied.test").body(Default::default())?;
@@ -71,13 +71,47 @@ async fn disallowed_host_fails() -> anyhow::Result<()> {
7171
Ok(())
7272
}
7373

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+
74107
async fn test_instance_state(
75108
allowed_outbound_hosts: &str,
109+
allow_private_ips: bool,
76110
) -> anyhow::Result<TestFactorsInstanceState> {
77111
let factors = TestFactors {
78112
variables: VariablesFactor::default(),
79113
networking: OutboundNetworkingFactor::new(),
80-
http: OutboundHttpFactor::default(),
114+
http: OutboundHttpFactor::new(allow_private_ips),
81115
};
82116
let env = TestEnvironment::new(factors).extend_manifest(toml! {
83117
[component.test-component]

examples/spin-timer/Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)