Skip to content

Commit

Permalink
fix: custom protocol on Windows, change scheme on Android, closes #7801
Browse files Browse the repository at this point in the history
… (#7808)
  • Loading branch information
lucasfernog committed Sep 11, 2023
1 parent 822ba2b commit d5074af
Show file tree
Hide file tree
Showing 28 changed files with 56 additions and 58 deletions.
8 changes: 8 additions & 0 deletions .changes/custom-protocol-http-android.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"tauri": patch:breaking
"@tauri-apps/api": patch:breaking
"@tauri-apps/cli": patch:breaking
"tauri-cli": patch:breaking
---

The custom protocol on Android now uses the `http` scheme instead of `https`.
6 changes: 6 additions & 0 deletions .changes/fix-windows-custom-protocol.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri": patch:bug
"tauri-runtime-wry": patch:bug
---

Fixes custom protocol not working on Windows.
2 changes: 1 addition & 1 deletion core/tauri-runtime-wry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ rust-version = { workspace = true }
features = [ "dox" ]

[dependencies]
wry = { version = "0.32", default-features = false, features = [ "tao", "file-drop", "protocol" ] }
wry = { version = "0.33", default-features = false, features = [ "tao", "file-drop", "protocol" ] }
tauri-runtime = { version = "1.0.0-alpha.1", path = "../tauri-runtime" }
tauri-utils = { version = "2.0.0-alpha.7", path = "../tauri-utils" }
uuid = { version = "1", features = [ "v4" ] }
Expand Down
8 changes: 2 additions & 6 deletions core/tauri/scripts/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@

window.__TAURI__.convertFileSrc = function convertFileSrc(filePath, protocol = 'asset') {
const path = encodeURIComponent(filePath)
return osName === 'windows'
return osName === 'windows' || osName === 'android'
? `http://${protocol}.localhost/${path}`
: (
osName === 'android'
? `https://${protocol}.localhost/${path}`
: `${protocol}://localhost/${path}`
)
: `${protocol}://localhost/${path}`
}

window.__TAURI__.transformCallback = function transformCallback(
Expand Down
5 changes: 2 additions & 3 deletions core/tauri/src/ipc/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ fn handle_ipc_message<R: Runtime>(message: String, manager: &WindowManager<R>, l
if !(cfg!(target_os = "macos") || cfg!(target_os = "ios"))
&& matches!(v, JsonValue::Object(_) | JsonValue::Array(_))
{
let _ = Channel::from_ipc(window.clone(), callback).send(v);
let _ = Channel::from_ipc(window, callback).send(v);
} else {
responder_eval(
&window,
Expand All @@ -261,8 +261,7 @@ fn handle_ipc_message<R: Runtime>(message: String, manager: &WindowManager<R>, l
error,
);
} else {
let _ =
Channel::from_ipc(window.clone(), callback).send(InvokeBody::Raw(v.clone()));
let _ = Channel::from_ipc(window, callback).send(InvokeBody::Raw(v.clone()));
}
}
InvokeResponse::Err(e) => responder_eval(
Expand Down
14 changes: 4 additions & 10 deletions core/tauri/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,10 +506,8 @@ impl<R: Runtime> WindowManager<R> {
}

pub(crate) fn protocol_url(&self) -> Cow<'_, Url> {
if cfg!(windows) {
if cfg!(windows) || cfg!(target_os = "android") {
Cow::Owned(Url::parse("http://tauri.localhost").unwrap())
} else if cfg!(target_os = "android") {
Cow::Owned(Url::parse("https://tauri.localhost").unwrap())
} else {
Cow::Owned(Url::parse("tauri://localhost").unwrap())
}
Expand Down Expand Up @@ -614,13 +612,11 @@ impl<R: Runtime> WindowManager<R> {
let window_url = Url::parse(&pending.url).unwrap();
let window_origin = if window_url.scheme() == "data" {
"null".into()
} else if cfg!(windows) && window_url.scheme() != "http" && window_url.scheme() != "https" {
format!("http://{}.localhost", window_url.scheme())
} else if cfg!(target_os = "android")
} else if (cfg!(windows) || cfg!(target_os = "android"))
&& window_url.scheme() != "http"
&& window_url.scheme() != "https"
{
format!("https://{}.localhost", window_url.scheme())
format!("http://{}.localhost", window_url.scheme())
} else {
format!(
"{}://{}{}",
Expand Down Expand Up @@ -883,10 +879,8 @@ mod test {
{
assert_eq!(
manager.get_url().to_string(),
if cfg!(windows) {
if cfg!(windows) || cfg!(target_os = "android") {
"http://tauri.localhost/"
} else if cfg!(target_os = "android") {
"https://tauri.localhost/"
} else {
"tauri://localhost"
}
Expand Down
4 changes: 1 addition & 3 deletions core/tauri/src/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,8 @@ pub(crate) struct PatternJavascript {

#[allow(dead_code)]
pub(crate) fn format_real_schema(schema: &str) -> String {
if cfg!(windows) {
if cfg!(windows) || cfg!(target_os = "android") {
format!("http://{schema}.{ISOLATION_IFRAME_SRC_DOMAIN}")
} else if cfg!(target_os = "android") {
format!("https://{schema}.{ISOLATION_IFRAME_SRC_DOMAIN}")
} else {
format!("{schema}://{ISOLATION_IFRAME_SRC_DOMAIN}")
}
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/test/fixture/src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
],
"security": {
"csp": "default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self'; connect-src ipc: http://ipc.localhost https://ipc.localhost"
"csp": "default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self'; connect-src ipc: http://ipc.localhost"
}
}
}
6 changes: 3 additions & 3 deletions examples/api/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/api/src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@
"security": {
"csp": {
"default-src": "'self' customprotocol: asset:",
"connect-src": "ipc: http://ipc.localhost https://ipc.localhost",
"connect-src": "ipc: http://ipc.localhost",
"font-src": [
"https://fonts.gstatic.com"
],
"img-src": "'self' asset: http://asset.localhost https://asset.localhost blob: data:",
"img-src": "'self' asset: http://asset.localhost blob: data:",
"style-src": "'unsafe-inline' 'self' https://fonts.googleapis.com"
},
"freezePrototype": true,
Expand Down
2 changes: 1 addition & 1 deletion examples/commands/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
}
],
"security": {
"csp": "default-src 'self'; connect-src ipc: http://ipc.localhost https://ipc.localhost"
"csp": "default-src 'self'; connect-src ipc: http://ipc.localhost"
}
}
}
2 changes: 1 addition & 1 deletion examples/helloworld/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
}
],
"security": {
"csp": "default-src 'self'; connect-src ipc: http://ipc.localhost https://ipc.localhost"
"csp": "default-src 'self'; connect-src ipc: http://ipc.localhost"
}
}
}
2 changes: 1 addition & 1 deletion examples/isolation/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
}
],
"security": {
"csp": "default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self' img-src: 'self'; connect-src ipc: http://ipc.localhost https://ipc.localhost"
"csp": "default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self' img-src: 'self'; connect-src ipc: http://ipc.localhost"
}
}
}
2 changes: 1 addition & 1 deletion examples/multiwindow/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
}
],
"security": {
"csp": "default-src 'self'; connect-src ipc: http://ipc.localhost https://ipc.localhost"
"csp": "default-src 'self'; connect-src ipc: http://ipc.localhost"
}
}
}
2 changes: 1 addition & 1 deletion examples/navigation/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
}
],
"security": {
"csp": "default-src 'self'; connect-src ipc: http://ipc.localhost https://ipc.localhost"
"csp": "default-src 'self'; connect-src ipc: http://ipc.localhost"
}
}
}
2 changes: 1 addition & 1 deletion examples/parent-window/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"category": "DeveloperTool"
},
"security": {
"csp": "default-src 'self'; connect-src ipc: http://ipc.localhost https://ipc.localhost"
"csp": "default-src 'self'; connect-src ipc: http://ipc.localhost"
}
}
}
2 changes: 1 addition & 1 deletion examples/resources/src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
}
],
"security": {
"csp": "default-src 'self'; connect-src ipc: http://ipc.localhost https://ipc.localhost"
"csp": "default-src 'self'; connect-src ipc: http://ipc.localhost"
}
}
}
2 changes: 1 addition & 1 deletion examples/splashscreen/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
}
],
"security": {
"csp": "default-src 'self'; connect-src ipc: http://ipc.localhost https://ipc.localhost"
"csp": "default-src 'self'; connect-src ipc: http://ipc.localhost"
}
}
}
2 changes: 1 addition & 1 deletion examples/state/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
}
],
"security": {
"csp": "default-src 'self'; connect-src ipc: http://ipc.localhost https://ipc.localhost"
"csp": "default-src 'self'; connect-src ipc: http://ipc.localhost"
}
}
}
2 changes: 1 addition & 1 deletion examples/streaming/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
}
],
"security": {
"csp": "default-src 'self' ipc:; media-src stream: http://stream.localhost https://stream.localhost asset: http://asset.localhost https://asset.localhost",
"csp": "default-src 'self' ipc:; media-src stream: http://stream.localhost asset: http://asset.localhost",
"assetProtocol": {
"scope": [
"**/test_video.mp4"
Expand Down
2 changes: 1 addition & 1 deletion examples/tauri-dynamic-lib/src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
}
],
"security": {
"csp": "default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self'; connect-src ipc: http://ipc.localhost https://ipc.localhost"
"csp": "default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self'; connect-src ipc: http://ipc.localhost"
}
}
}
2 changes: 1 addition & 1 deletion tooling/api/docs/js-api.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions tooling/api/src/tauri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ async function invoke<T>(

/**
* Convert a device file path to an URL that can be loaded by the webview.
* Note that `asset:`, `http://asset.localhost` and `https://asset.localhost` must be added to [`tauri.security.csp`](https://tauri.app/v1/api/config/#securityconfig.csp) in `tauri.conf.json`.
* Example CSP value: `"csp": "default-src 'self' ipc: http://ipc.localhost https://ipc.localhost; img-src 'self' asset: http://asset.localhost https://asset.localhost"` to use the asset protocol on image sources.
* Note that `asset:` and `http://asset.localhost` must be added to [`tauri.security.csp`](https://tauri.app/v1/api/config/#securityconfig.csp) in `tauri.conf.json`.
* Example CSP value: `"csp": "default-src 'self' ipc: http://ipc.localhost; img-src 'self' asset: http://asset.localhost"` to use the asset protocol on image sources.
*
* Additionally, `asset` must be added to [`tauri.allowlist.protocol`](https://tauri.app/v1/api/config/#allowlistconfig.protocol)
* in `tauri.conf.json` and its access scope must be defined on the `assetScope` array on the same `protocol` object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
}
],
"security": {
"csp": "default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self'; connect-src ipc: http://ipc.localhost https://ipc.localhost"
"csp": "default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self'; connect-src ipc: http://ipc.localhost"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
}
],
"security": {
"csp": "default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self'; connect-src ipc: http://ipc.localhost https://ipc.localhost"
"csp": "default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self'; connect-src ipc: http://ipc.localhost"
}
}
}
2 changes: 1 addition & 1 deletion tooling/bench/tests/helloworld/src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
}
],
"security": {
"csp": "default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self'; connect-src ipc: http://ipc.localhost https://ipc.localhost"
"csp": "default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self'; connect-src ipc: http://ipc.localhost"
}
}
}
19 changes: 8 additions & 11 deletions tooling/cli/src/migrate/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,21 @@ fn process_security(security: &mut Map<String, Value>) -> Result<()> {
match &mut csp {
tauri_utils_v1::config::Csp::Policy(csp) => {
if csp.contains("connect-src") {
*csp = csp.replace(
"connect-src",
"connect-src ipc: http://ipc.localhost https://ipc.localhost",
);
*csp = csp.replace("connect-src", "connect-src ipc: http://ipc.localhost");
} else {
*csp = format!("{csp}; connect-src ipc: http://ipc.localhost https://ipc.localhost");
*csp = format!("{csp}; connect-src ipc: http://ipc.localhost");
}
}
tauri_utils_v1::config::Csp::DirectiveMap(csp) => {
if let Some(connect_src) = csp.get_mut("connect-src") {
if !connect_src.contains("ipc: http://ipc.localhost https://ipc.localhost") {
connect_src.push("ipc: http://ipc.localhost https://ipc.localhost");
if !connect_src.contains("ipc: http://ipc.localhost") {
connect_src.push("ipc: http://ipc.localhost");
}
} else {
csp.insert(
"connect-src".into(),
tauri_utils_v1::config::CspDirectiveSources::List(vec![
"ipc: http://ipc.localhost https://ipc.localhost".to_string(),
"ipc: http://ipc.localhost".to_string()
]),
);
}
Expand Down Expand Up @@ -334,7 +331,7 @@ mod test {
assert_eq!(
migrated["tauri"]["security"]["csp"],
format!(
"{}; connect-src ipc: http://ipc.localhost https://ipc.localhost",
"{}; connect-src ipc: http://ipc.localhost",
original["tauri"]["security"]["csp"].as_str().unwrap()
)
);
Expand All @@ -361,7 +358,7 @@ mod test {
assert!(migrated["tauri"]["security"]["csp"]["connect-src"]
.as_array()
.expect("connect-src isn't an array")
.contains(&"ipc: http://ipc.localhost https://ipc.localhost".into()));
.contains(&"ipc: http://ipc.localhost".into()));
}

#[test]
Expand All @@ -388,7 +385,7 @@ mod test {
.as_str()
.expect("connect-src isn't a string"),
format!(
"{} ipc: http://ipc.localhost https://ipc.localhost",
"{} ipc: http://ipc.localhost",
original["tauri"]["security"]["csp"]["connect-src"]
.as_str()
.unwrap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
}
],
"security": {
"csp": "default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self' img-src: 'self'; connect-src ipc: http://ipc.localhost https://ipc.localhost"
"csp": "default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self' img-src: 'self'; connect-src ipc: http://ipc.localhost"
}
}
}

0 comments on commit d5074af

Please sign in to comment.