Skip to content

Commit 7b40001

Browse files
authored
Merge pull request #3003 from itowlson/enable-js-fetch-self-requests
Allow `self.alt` host for self-requests
2 parents b70f85e + aa47c0d commit 7b40001

File tree

5 files changed

+34
-2
lines changed

5 files changed

+34
-2
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,12 @@ async fn send_request_impl(
152152
let host = request.uri().host().unwrap_or_default();
153153
let tls_client_config = component_tls_configs.get_client_config(host).clone();
154154

155-
if request.uri().authority().is_some() {
155+
let is_self_request = request
156+
.uri()
157+
.authority()
158+
.is_some_and(|a| a.host() == "self.alt");
159+
160+
if request.uri().authority().is_some() && !is_self_request {
156161
// Absolute URI
157162
let is_allowed = outbound_allowed_hosts
158163
.check_url(&request.uri().to_string(), "https")

crates/factor-outbound-networking/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ impl HostConfig {
200200
return Ok(Self::Any);
201201
}
202202

203-
if host == "self" {
203+
if host == "self" || host == "self.alt" {
204204
return Ok(Self::ToSelf);
205205
}
206206

tests/integration.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,11 @@ mod integration_tests {
372372
Request::new(Method::Get, "/outbound-allowed"),
373373
Response::new_with_body(200, "Hello, Fermyon!\n"),
374374
)?;
375+
assert_spin_request(
376+
spin,
377+
Request::new(Method::Get, "/outbound-allowed-alt"),
378+
Response::new_with_body(200, "Hello, Fermyon!\n"),
379+
)?;
375380

376381
assert_spin_request(
377382
spin,

tests/test-components/components/outbound-http/src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,29 @@ use spin_sdk::{
77
/// Send an HTTP request and return the response.
88
#[http_component]
99
async fn send_outbound(_req: Request) -> Result<impl IntoResponse> {
10+
// Test self-request via relative URL
1011
let mut res: http::Response<String> = spin_sdk::http::send(
1112
http::Request::builder()
1213
.method("GET")
1314
.uri("/hello")
1415
.body(())?,
1516
)
1617
.await?;
18+
19+
// Test self-request via self.alt
20+
let res_alt: http::Response<String> = spin_sdk::http::send(
21+
http::Request::builder()
22+
.method("GET")
23+
.uri("http://self.alt/hello")
24+
.body(())?,
25+
)
26+
.await?;
27+
28+
assert_eq!(res.body(), res_alt.body());
29+
assert_eq!(res.status(), res_alt.status());
30+
1731
res.headers_mut()
1832
.insert("spin-component", "outbound-http-component".try_into()?);
33+
1934
Ok(res)
2035
}

tests/testcases/outbound-http-to-same-app/spin.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ allowed_http_hosts = ["self"]
1818
[component.trigger]
1919
route = "/outbound-allowed/..."
2020

21+
[[component]]
22+
id = "outbound-http-allowed-alt"
23+
source = "%{source=outbound-http}"
24+
allowed_http_hosts = ["self.alt"]
25+
[component.trigger]
26+
route = "/outbound-allowed-alt/..."
27+
2128
[[component]]
2229
id = "outbound-http-not-allowed"
2330
source = "%{source=outbound-http}"

0 commit comments

Comments
 (0)