Skip to content

Commit 1730b1a

Browse files
authored
feat(core): enable CORS on the tauri protocol (#3750)
1 parent 3d11ac6 commit 1730b1a

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

.changes/tauri-protocol-cors.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Set the `Access-Control-Allow-Origin` header on the `tauri` protocol response with the initial webview URL as value.

core/tauri/src/manager.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -471,10 +471,27 @@ impl<R: Runtime> WindowManager<R> {
471471
});
472472
}
473473

474+
let window_url = Url::parse(&pending.url).unwrap();
475+
let window_origin =
476+
if cfg!(windows) && window_url.scheme() != "http" && window_url.scheme() != "https" {
477+
format!("https://{}.localhost", window_url.scheme())
478+
} else {
479+
format!(
480+
"{}://{}{}",
481+
window_url.scheme(),
482+
window_url.host().unwrap(),
483+
if let Some(port) = window_url.port() {
484+
format!(":{}", port)
485+
} else {
486+
"".into()
487+
}
488+
)
489+
};
490+
474491
if !registered_scheme_protocols.contains(&"tauri".into()) {
475492
pending.register_uri_scheme_protocol(
476493
"tauri",
477-
self.prepare_uri_scheme_protocol(web_resource_request_handler),
494+
self.prepare_uri_scheme_protocol(&window_origin, web_resource_request_handler),
478495
);
479496
registered_scheme_protocols.push("tauri".into());
480497
}
@@ -484,22 +501,6 @@ impl<R: Runtime> WindowManager<R> {
484501
use tokio::io::{AsyncReadExt, AsyncSeekExt};
485502
use url::Position;
486503
let asset_scope = self.state().get::<crate::Scopes>().asset_protocol.clone();
487-
let window_url = Url::parse(&pending.url).unwrap();
488-
let window_origin =
489-
if cfg!(windows) && window_url.scheme() != "http" && window_url.scheme() != "https" {
490-
format!("https://{}.localhost", window_url.scheme())
491-
} else {
492-
format!(
493-
"{}://{}{}",
494-
window_url.scheme(),
495-
window_url.host().unwrap(),
496-
if let Some(port) = window_url.port() {
497-
format!(":{}", port)
498-
} else {
499-
"".into()
500-
}
501-
)
502-
};
503504
pending.register_uri_scheme_protocol("asset", move |request| {
504505
let parsed_path = Url::parse(request.uri())?;
505506
let filtered_path = &parsed_path[..Position::AfterPath];
@@ -796,12 +797,14 @@ impl<R: Runtime> WindowManager<R> {
796797
#[allow(clippy::type_complexity)]
797798
fn prepare_uri_scheme_protocol(
798799
&self,
800+
window_origin: &str,
799801
web_resource_request_handler: Option<
800802
Box<dyn Fn(&HttpRequest, &mut HttpResponse) + Send + Sync>,
801803
>,
802804
) -> Box<dyn Fn(&HttpRequest) -> Result<HttpResponse, Box<dyn std::error::Error>> + Send + Sync>
803805
{
804806
let manager = self.clone();
807+
let window_origin = window_origin.to_string();
805808
Box::new(move |request| {
806809
let path = request
807810
.uri()
@@ -812,7 +815,9 @@ impl<R: Runtime> WindowManager<R> {
812815
.to_string()
813816
.replace("tauri://localhost", "");
814817
let asset = manager.get_asset(path)?;
815-
let mut builder = HttpResponseBuilder::new().mimetype(&asset.mime_type);
818+
let mut builder = HttpResponseBuilder::new()
819+
.header("Access-Control-Allow-Origin", &window_origin)
820+
.mimetype(&asset.mime_type);
816821
if let Some(csp) = &asset.csp_header {
817822
builder = builder.header("Content-Security-Policy", csp);
818823
}

0 commit comments

Comments
 (0)