From 55044cfe6d6b433165ec7b635dd2571f1a82037a Mon Sep 17 00:00:00 2001 From: FabianLars Date: Thu, 27 Jul 2023 11:53:41 +0200 Subject: [PATCH 1/8] feat(windows): Add option to use `http` scheme for custom protocols --- .changes/windows-http.md | 5 +++++ src/webview/mod.rs | 15 +++++++++++++++ src/webview/webview2/mod.rs | 17 +++++++++++------ 3 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 .changes/windows-http.md diff --git a/.changes/windows-http.md b/.changes/windows-http.md new file mode 100644 index 000000000..abd8bac04 --- /dev/null +++ b/.changes/windows-http.md @@ -0,0 +1,5 @@ +--- +"wry": "minor" +--- + +Add `WebViewBuilderExtWindows::with_http_scheme` to be able to choose between `http` and `https` for custom protocols on Windows. diff --git a/src/webview/mod.rs b/src/webview/mod.rs index ededc87cf..6922a0ef2 100644 --- a/src/webview/mod.rs +++ b/src/webview/mod.rs @@ -277,6 +277,7 @@ pub(crate) struct PlatformSpecificWebViewAttributes { additional_browser_args: Option, browser_accelerator_keys: bool, theme: Option, + http_scheme: bool, } #[cfg(windows)] impl Default for PlatformSpecificWebViewAttributes { @@ -285,6 +286,7 @@ impl Default for PlatformSpecificWebViewAttributes { additional_browser_args: None, browser_accelerator_keys: true, // This is WebView2's default behavior theme: None, + http_scheme: false, } } } @@ -695,6 +697,14 @@ pub trait WebViewBuilderExtWindows { /// /// Defaults to [`Theme::Auto`] which will follow the OS defaults. fn with_theme(self, theme: Theme) -> Self; + + /// Determines whether the custom protocols should use `http://.localhost` instead of `http://.localhost`. + /// + /// Using a `http` scheme will prevent mixed content warnings when trying to fetch `http` endpoints + /// and is therefore less secure but will match the behavior of the `://localhost` protocols on macOS and Linux. + /// + /// The default value is `false`. + fn with_http_scheme(self, enabled: bool) -> Self; } #[cfg(windows)] @@ -713,6 +723,11 @@ impl WebViewBuilderExtWindows for WebViewBuilder<'_> { self.platform_specific.theme = Some(theme); self } + + fn with_http_scheme(mut self, enabled: bool) -> Self { + self.platform_specific.http_scheme = enabled; + self + } } #[cfg(target_os = "android")] diff --git a/src/webview/webview2/mod.rs b/src/webview/webview2/mod.rs index e73ee5cbe..b089ed508 100644 --- a/src/webview/webview2/mod.rs +++ b/src/webview/webview2/mod.rs @@ -537,15 +537,20 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_ } } + let scheme = if pl_attrs.http_scheme { + "http" + } else { + "https" + }; let mut custom_protocol_names = HashSet::new(); if !attributes.custom_protocols.is_empty() { for (name, _) in &attributes.custom_protocols { - // WebView2 doesn't support non-standard protocols yet, so we have to use this workaround + // WebView2 supports non-standard protocols only on Windows 10+, so we have to use this workaround // See https://github.com/MicrosoftEdge/WebView2Feedback/issues/73 custom_protocol_names.insert(name.clone()); unsafe { webview.AddWebResourceRequestedFilter( - PCWSTR::from_raw(encode_wide(format!("https://{}.*", name)).as_ptr()), + PCWSTR::from_raw(encode_wide(format!("{scheme}://{name}.*")).as_ptr()), COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL, ) } @@ -616,11 +621,11 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_ if let Some(custom_protocol) = custom_protocols .iter() - .find(|(name, _)| uri.starts_with(&format!("https://{}.", name))) + .find(|(name, _)| uri.starts_with(&format!("{scheme}://{name}."))) { // Undo the protocol workaround when giving path to resolver let path = uri.replace( - &format!("https://{}.", custom_protocol.0), + &format!("{scheme}://{}.", custom_protocol.0), &format!("{}://", custom_protocol.0), ); @@ -741,11 +746,11 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_ let mut url_string = String::from(url.as_str()); let name = url.scheme(); if custom_protocol_names.contains(name) { - // WebView2 doesn't support non-standard protocols yet, so we have to use this workaround + // WebView2 supports non-standard protocols only on Windows 10+, so we have to use this workaround // See https://github.com/MicrosoftEdge/WebView2Feedback/issues/73 url_string = url .as_str() - .replace(&format!("{}://", name), &format!("https://{}.", name)) + .replace(&format!("{}://", name), &format!("{scheme}://{name}.")) } if let Some(headers) = attributes.headers { From 891a1298d9a2df727c9945dac70ac12058e0e924 Mon Sep 17 00:00:00 2001 From: Fabian-Lars Date: Thu, 27 Jul 2023 12:46:30 +0200 Subject: [PATCH 2/8] Update src/webview/mod.rs Co-authored-by: Amr Bashir --- src/webview/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/webview/mod.rs b/src/webview/mod.rs index 6922a0ef2..31b41a93e 100644 --- a/src/webview/mod.rs +++ b/src/webview/mod.rs @@ -698,7 +698,7 @@ pub trait WebViewBuilderExtWindows { /// Defaults to [`Theme::Auto`] which will follow the OS defaults. fn with_theme(self, theme: Theme) -> Self; - /// Determines whether the custom protocols should use `http://.localhost` instead of `http://.localhost`. + /// Determines whether the custom protocols should use `http://.localhost` instead of the default `https://.localhost`. /// /// Using a `http` scheme will prevent mixed content warnings when trying to fetch `http` endpoints /// and is therefore less secure but will match the behavior of the `://localhost` protocols on macOS and Linux. From 71e590cf437122cb8596dfd04b4d28314a9067a6 Mon Sep 17 00:00:00 2001 From: FabianLars Date: Thu, 27 Jul 2023 13:01:41 +0200 Subject: [PATCH 3/8] enabled by default --- .changes/windows-http.md | 2 +- src/webview/mod.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.changes/windows-http.md b/.changes/windows-http.md index abd8bac04..9ae317447 100644 --- a/.changes/windows-http.md +++ b/.changes/windows-http.md @@ -2,4 +2,4 @@ "wry": "minor" --- -Add `WebViewBuilderExtWindows::with_http_scheme` to be able to choose between `http` and `https` for custom protocols on Windows. +Add `WebViewBuilderExtWindows::with_http_scheme` to be able to choose between `http` and `https` for custom protocols on Windows. **Wry now uses `http` by default!** diff --git a/src/webview/mod.rs b/src/webview/mod.rs index 31b41a93e..cdad499cf 100644 --- a/src/webview/mod.rs +++ b/src/webview/mod.rs @@ -286,7 +286,7 @@ impl Default for PlatformSpecificWebViewAttributes { additional_browser_args: None, browser_accelerator_keys: true, // This is WebView2's default behavior theme: None, - http_scheme: false, + http_scheme: true, // To match macOS & Linux behavior in the context of mixed content. } } } @@ -700,10 +700,10 @@ pub trait WebViewBuilderExtWindows { /// Determines whether the custom protocols should use `http://.localhost` instead of the default `https://.localhost`. /// - /// Using a `http` scheme will prevent mixed content warnings when trying to fetch `http` endpoints + /// Using a `http` scheme will allow mixed content warnings when trying to fetch `http` endpoints /// and is therefore less secure but will match the behavior of the `://localhost` protocols on macOS and Linux. /// - /// The default value is `false`. + /// The default value is `true`. fn with_http_scheme(self, enabled: bool) -> Self; } From 51fc911ace7db6fec16b77b60623126ddadaf447 Mon Sep 17 00:00:00 2001 From: FabianLars Date: Thu, 27 Jul 2023 13:02:59 +0200 Subject: [PATCH 4/8] docs: remove "warnings" --- src/webview/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/webview/mod.rs b/src/webview/mod.rs index cdad499cf..97a8c23c1 100644 --- a/src/webview/mod.rs +++ b/src/webview/mod.rs @@ -700,7 +700,7 @@ pub trait WebViewBuilderExtWindows { /// Determines whether the custom protocols should use `http://.localhost` instead of the default `https://.localhost`. /// - /// Using a `http` scheme will allow mixed content warnings when trying to fetch `http` endpoints + /// Using a `http` scheme will allow mixed content when trying to fetch `http` endpoints /// and is therefore less secure but will match the behavior of the `://localhost` protocols on macOS and Linux. /// /// The default value is `true`. From 3f1d5c95a098f9a3c915d052ee1f254a6924e9e4 Mon Sep 17 00:00:00 2001 From: FabianLars Date: Thu, 27 Jul 2023 13:04:18 +0200 Subject: [PATCH 5/8] wording --- src/webview/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/webview/mod.rs b/src/webview/mod.rs index 97a8c23c1..ca0d82d3a 100644 --- a/src/webview/mod.rs +++ b/src/webview/mod.rs @@ -698,12 +698,12 @@ pub trait WebViewBuilderExtWindows { /// Defaults to [`Theme::Auto`] which will follow the OS defaults. fn with_theme(self, theme: Theme) -> Self; - /// Determines whether the custom protocols should use `http://.localhost` instead of the default `https://.localhost`. + /// Determines whether the custom protocols should use `http://.localhost` or `https://.localhost`. /// /// Using a `http` scheme will allow mixed content when trying to fetch `http` endpoints - /// and is therefore less secure but will match the behavior of the `://localhost` protocols on macOS and Linux. + /// and is therefore less secure but will match the behavior of the `://localhost` protocols used on macOS and Linux. /// - /// The default value is `true`. + /// The default value is `true`, meaning it will use `http`. fn with_http_scheme(self, enabled: bool) -> Self; } From 550adc5b4ac26b8e32c33a6b8f657e76cb3ffedc Mon Sep 17 00:00:00 2001 From: FabianLars Date: Thu, 27 Jul 2023 13:58:01 +0200 Subject: [PATCH 6/8] split change files --- .changes/windows-http.md | 2 +- .changes/windows-with-https-scheme.md | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 .changes/windows-with-https-scheme.md diff --git a/.changes/windows-http.md b/.changes/windows-http.md index 9ae317447..402381bc7 100644 --- a/.changes/windows-http.md +++ b/.changes/windows-http.md @@ -2,4 +2,4 @@ "wry": "minor" --- -Add `WebViewBuilderExtWindows::with_http_scheme` to be able to choose between `http` and `https` for custom protocols on Windows. **Wry now uses `http` by default!** +**Breaking change** Wry now defaults to `http://.localhost/` for custom protocols on Windows. diff --git a/.changes/windows-with-https-scheme.md b/.changes/windows-with-https-scheme.md new file mode 100644 index 000000000..0565f6e95 --- /dev/null +++ b/.changes/windows-with-https-scheme.md @@ -0,0 +1,5 @@ +--- +"wry": "minor" +--- + +Add `WebViewBuilderExtWindows::with_https_scheme` to be able to choose between `http` and `https` for custom protocols on Windows. From 4371e5b412fae47fb4fb537fb8a07b316b31e493 Mon Sep 17 00:00:00 2001 From: FabianLars Date: Thu, 27 Jul 2023 14:00:19 +0200 Subject: [PATCH 7/8] http_scheme -> https_scheme --- src/webview/mod.rs | 14 +++++++------- src/webview/webview2/mod.rs | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/webview/mod.rs b/src/webview/mod.rs index ca0d82d3a..ec0e68d9a 100644 --- a/src/webview/mod.rs +++ b/src/webview/mod.rs @@ -277,7 +277,7 @@ pub(crate) struct PlatformSpecificWebViewAttributes { additional_browser_args: Option, browser_accelerator_keys: bool, theme: Option, - http_scheme: bool, + https_scheme: bool, } #[cfg(windows)] impl Default for PlatformSpecificWebViewAttributes { @@ -286,7 +286,7 @@ impl Default for PlatformSpecificWebViewAttributes { additional_browser_args: None, browser_accelerator_keys: true, // This is WebView2's default behavior theme: None, - http_scheme: true, // To match macOS & Linux behavior in the context of mixed content. + https_scheme: false, // To match macOS & Linux behavior in the context of mixed content. } } } @@ -698,13 +698,13 @@ pub trait WebViewBuilderExtWindows { /// Defaults to [`Theme::Auto`] which will follow the OS defaults. fn with_theme(self, theme: Theme) -> Self; - /// Determines whether the custom protocols should use `http://.localhost` or `https://.localhost`. + /// Determines whether the custom protocols should use `https://.localhost` instead of the default `http://.localhost`. /// /// Using a `http` scheme will allow mixed content when trying to fetch `http` endpoints /// and is therefore less secure but will match the behavior of the `://localhost` protocols used on macOS and Linux. /// - /// The default value is `true`, meaning it will use `http`. - fn with_http_scheme(self, enabled: bool) -> Self; + /// The default value is `false`. + fn with_https_scheme(self, enabled: bool) -> Self; } #[cfg(windows)] @@ -724,8 +724,8 @@ impl WebViewBuilderExtWindows for WebViewBuilder<'_> { self } - fn with_http_scheme(mut self, enabled: bool) -> Self { - self.platform_specific.http_scheme = enabled; + fn with_https_scheme(mut self, enabled: bool) -> Self { + self.platform_specific.https_scheme = enabled; self } } diff --git a/src/webview/webview2/mod.rs b/src/webview/webview2/mod.rs index b089ed508..1e53a8a89 100644 --- a/src/webview/webview2/mod.rs +++ b/src/webview/webview2/mod.rs @@ -537,7 +537,7 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_ } } - let scheme = if pl_attrs.http_scheme { + let scheme = if pl_attrs.https_scheme { "http" } else { "https" From 69d4409499f190264ac36d9bcf8cbdf131ef0b7c Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Tue, 15 Aug 2023 02:09:57 +0300 Subject: [PATCH 8/8] fix logic --- src/webview/webview2/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/webview/webview2/mod.rs b/src/webview/webview2/mod.rs index 1e53a8a89..9781cb963 100644 --- a/src/webview/webview2/mod.rs +++ b/src/webview/webview2/mod.rs @@ -538,9 +538,9 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_ } let scheme = if pl_attrs.https_scheme { - "http" - } else { "https" + } else { + "http" }; let mut custom_protocol_names = HashSet::new(); if !attributes.custom_protocols.is_empty() {