Skip to content

Commit 11a1529

Browse files
authored
feat(core): set CORS headers on protocol errors (#8419)
* feat(core): set CORS headers on protocol errors This ensures the frontend can read the error message instead of just showing a CORS error * fix statuscode
1 parent ea03ade commit 11a1529

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

.changes/fix-protocol-response.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch:enhance
3+
---
4+
5+
Include CORS header on custom protocol response errors to ensure frontend can read the error message.

core/tauri/src/protocol/asset.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub fn get(scope: scope::fs::Scope, window_origin: String) -> UriSchemeProtocolH
1919
http::Response::builder()
2020
.status(http::StatusCode::BAD_REQUEST)
2121
.header(CONTENT_TYPE, mime::TEXT_PLAIN.essence_str())
22+
.header("Access-Control-Allow-Origin", &window_origin)
2223
.body(e.to_string().as_bytes().to_vec())
2324
.unwrap(),
2425
),
@@ -36,24 +37,18 @@ fn get_response(
3637
.decode_utf8_lossy()
3738
.to_string();
3839

40+
let mut resp = Response::builder().header("Access-Control-Allow-Origin", window_origin);
41+
3942
if let Err(e) = SafePathBuf::new(path.clone().into()) {
4043
debug_eprintln!("asset protocol path \"{}\" is not valid: {}", path, e);
41-
return Response::builder()
42-
.status(403)
43-
.body(Vec::new().into())
44-
.map_err(Into::into);
44+
return resp.status(403).body(Vec::new().into()).map_err(Into::into);
4545
}
4646

4747
if !scope.is_allowed(&path) {
4848
debug_eprintln!("asset protocol not configured to allow the path: {}", path);
49-
return Response::builder()
50-
.status(403)
51-
.body(Vec::new().into())
52-
.map_err(Into::into);
49+
return resp.status(403).body(Vec::new().into()).map_err(Into::into);
5350
}
5451

55-
let mut resp = Response::builder().header("Access-Control-Allow-Origin", window_origin);
56-
5752
let (mut file, len, mime_type, read_bytes) = crate::async_runtime::safe_block_on(async move {
5853
let mut file = File::open(&path).await?;
5954

core/tauri/src/protocol/tauri.rs

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ pub fn get<R: Runtime>(
5757
HttpResponse::builder()
5858
.status(StatusCode::BAD_REQUEST)
5959
.header(CONTENT_TYPE, mime::TEXT_PLAIN.essence_str())
60+
.header("Access-Control-Allow-Origin", &window_origin)
6061
.body(e.to_string().as_bytes().to_vec())
6162
.unwrap(),
6263
),

0 commit comments

Comments
 (0)