Skip to content

Commit

Permalink
Merge pull request #3003 from itowlson/enable-js-fetch-self-requests
Browse files Browse the repository at this point in the history
Allow `self.alt` host for self-requests
  • Loading branch information
itowlson authored Feb 18, 2025
2 parents b70f85e + aa47c0d commit 7b40001
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
7 changes: 6 additions & 1 deletion crates/factor-outbound-http/src/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,12 @@ async fn send_request_impl(
let host = request.uri().host().unwrap_or_default();
let tls_client_config = component_tls_configs.get_client_config(host).clone();

if request.uri().authority().is_some() {
let is_self_request = request
.uri()
.authority()
.is_some_and(|a| a.host() == "self.alt");

if request.uri().authority().is_some() && !is_self_request {
// Absolute URI
let is_allowed = outbound_allowed_hosts
.check_url(&request.uri().to_string(), "https")
Expand Down
2 changes: 1 addition & 1 deletion crates/factor-outbound-networking/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl HostConfig {
return Ok(Self::Any);
}

if host == "self" {
if host == "self" || host == "self.alt" {
return Ok(Self::ToSelf);
}

Expand Down
5 changes: 5 additions & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,11 @@ mod integration_tests {
Request::new(Method::Get, "/outbound-allowed"),
Response::new_with_body(200, "Hello, Fermyon!\n"),
)?;
assert_spin_request(
spin,
Request::new(Method::Get, "/outbound-allowed-alt"),
Response::new_with_body(200, "Hello, Fermyon!\n"),
)?;

assert_spin_request(
spin,
Expand Down
15 changes: 15 additions & 0 deletions tests/test-components/components/outbound-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,29 @@ use spin_sdk::{
/// Send an HTTP request and return the response.
#[http_component]
async fn send_outbound(_req: Request) -> Result<impl IntoResponse> {
// Test self-request via relative URL
let mut res: http::Response<String> = spin_sdk::http::send(
http::Request::builder()
.method("GET")
.uri("/hello")
.body(())?,
)
.await?;

// Test self-request via self.alt
let res_alt: http::Response<String> = spin_sdk::http::send(
http::Request::builder()
.method("GET")
.uri("http://self.alt/hello")
.body(())?,
)
.await?;

assert_eq!(res.body(), res_alt.body());
assert_eq!(res.status(), res_alt.status());

res.headers_mut()
.insert("spin-component", "outbound-http-component".try_into()?);

Ok(res)
}
7 changes: 7 additions & 0 deletions tests/testcases/outbound-http-to-same-app/spin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ allowed_http_hosts = ["self"]
[component.trigger]
route = "/outbound-allowed/..."

[[component]]
id = "outbound-http-allowed-alt"
source = "%{source=outbound-http}"
allowed_http_hosts = ["self.alt"]
[component.trigger]
route = "/outbound-allowed-alt/..."

[[component]]
id = "outbound-http-not-allowed"
source = "%{source=outbound-http}"
Expand Down

0 comments on commit 7b40001

Please sign in to comment.