Skip to content

Commit e9a5248

Browse files
committed
Don't trap when outbound http request is not allowed
Signed-off-by: Ryan Levick <[email protected]>
1 parent 152409c commit e9a5248

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

crates/factor-outbound-http/src/wasi.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ async fn send_request_impl(
112112
let is_relative_url = request.uri().authority().is_none();
113113
if is_relative_url {
114114
if !allowed_hosts.allows_relative_url(&["http", "https"]) {
115-
return handle_not_allowed(request.uri(), true);
115+
return Ok(handle_not_allowed(request.uri(), true));
116116
}
117117

118118
let origin = request
@@ -131,7 +131,7 @@ async fn send_request_impl(
131131
let outbound_url = OutboundUrl::parse(request.uri().to_string(), "https")
132132
.map_err(|_| ErrorCode::HttpRequestUriInvalid)?;
133133
if !allowed_hosts.allows(&outbound_url) {
134-
return handle_not_allowed(request.uri(), false);
134+
return Ok(handle_not_allowed(request.uri(), false));
135135
}
136136
}
137137

@@ -147,11 +147,8 @@ async fn send_request_impl(
147147
}
148148

149149
// TODO(factors): Move to some callback on spin-factor-outbound-networking (?)
150-
fn handle_not_allowed(
151-
uri: &Uri,
152-
is_relative: bool,
153-
) -> anyhow::Result<Result<IncomingResponse, ErrorCode>> {
154-
tracing::error!("Destination not allowed: {uri}");
150+
fn handle_not_allowed(uri: &Uri, is_relative: bool) -> Result<IncomingResponse, ErrorCode> {
151+
tracing::error!("Destination not allowed!: {uri}");
155152
let allowed_host_example = if is_relative {
156153
terminal::warn!("A component tried to make a HTTP request to the same component but it does not have permission.");
157154
"http://self".to_string()
@@ -165,7 +162,7 @@ fn handle_not_allowed(
165162
host
166163
};
167164
eprintln!("To allow requests, add 'allowed_outbound_hosts = [\"{allowed_host_example}\"]' to the manifest component section.");
168-
Err(ErrorCode::HttpRequestDenied.into())
165+
Err(ErrorCode::HttpRequestDenied)
169166
}
170167

171168
/// This is a fork of wasmtime_wasi_http::default_send_request_handler function

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ async fn disallowed_host_fails() -> anyhow::Result<()> {
6565
let req = Request::get("https://denied.test").body(Default::default())?;
6666
let mut future_resp = wasi_http.send_request(req, test_request_config())?;
6767
future_resp.ready().await;
68-
match future_resp.unwrap_ready() {
68+
match future_resp.unwrap_ready().unwrap() {
6969
Ok(_) => bail!("expected Err, got Ok"),
70-
Err(err) => assert!(matches!(err.downcast()?, ErrorCode::HttpRequestDenied)),
70+
Err(err) => assert!(matches!(err, ErrorCode::HttpRequestDenied)),
7171
};
7272
Ok(())
7373
}

0 commit comments

Comments
 (0)