Open softworkz opened 12 months ago
Any update on this? Thanks
@softworkz Can you please check if corewebview2customschemeregistration works for your case to serve the content.
Also, you mentioned trying SetVirtualHostNameToFolderMapping
and handling the WRR event by providing the response. Is there an issue using that approach?
@softworkz Can you please check if corewebview2customschemeregistration works for your case to serve the content.
For working with CustomSchemeRegistrations, you need to set those at the CoreWebView2EnvironmentOptions.CustomSchemeRegistrations property when creating an environment.
The link above points to the .NET API, in the WinRT API that property is missing. Even when it existed, it wouldn't be possible to use it, because neither the UWP/WinUI2 nor the WinUI3 Webview2 element does allow you to create the environment yourself, so there's not way to use custom scheme registrations with UWP and WinUI3.
Please see my related proposal on this subject.
Nonetheless I was still able to try this, because I have done a custom WebView2 element for WinUI3 (a C# port of the official XAML control with extra capabilities), and so I can say:
No, that doesn't work. For a custom scheme, you can set TreatAsSecure
to true or false and the result is the same as whether using http or https:
Also, you mentioned trying
SetVirtualHostNameToFolderMapping
and handling the WRR event by providing the response. Is there an issue using that approach?
I don't have an issue using that approach per se, but it doesn't help with the situation either. With that approach, you have the choice to use http or http, with the same outcome as above (see "true/https" and "false/http").
Background
We are trying to port and established application to WebView2. Its core is implemented in HTML/JS, surrounded by platform-specific implementations in native code. The html/js code is installed with the application and it never runs any code or displays any html views from external sources. In the context of this application, we need to load data from "insecure origins" on the private network via http while the application needs to run in a "secure context".
Why a Secure Context for the Running Code?
Most new APIs are available in a secure context only, and we need to access quite a few of them.
Why only HTTP Connections?
The application interacts with devices (mini-servers) on the local network via REST API and loads images or videos from those devices (which are developed by us as well). These are often fully private and not accessible from outside, which means that they don't have any SSL certs and can only be accessed via http (not https). Since this is targeting end users rather than enterprises, it has to work in a plug'n'play manner and it cannot be expected from users to set up a local PKI/DNS infrastructure.
How do other Platforms Compare?
Android WebView
The app code is running from file: URLs and this context can be declared to be treated secure with a WebView option.
Electron (Windows, Linux, Mac)
The app code is running from file: URLs and Electron provides a way to lift restrictions for this context.
iOS - WebKit WebView
I don't know how we do it there, but there's a way.
UWP/WinJS
There are no mixed content restrictions IIRC
Hosted App (for browser access and PWAs)
This a pretty unfortunate topic. At the moment, we are forced to serve it over http to allow users to access their devices, and we are participating in the Chrome Origin Trial to lift restrictions due to https://wicg.github.io/private-network-access/. We will implement the "Permission Prompt" mechanism as soon as available (https://github.com/WICG/private-network-access/blob/main/permission_prompt/explainer.md).
Please note that this is acceptable for in-browser access, but it is not a viable option for an installed application using a WebView control.
Problem
Executing a fetch request against a REST API fails and gets blocked with the following error message:
What we have tried so far
Serving application files
Using
file://
URLsAs there appears to be no way (like the other WebView implementations are providing) to treat this as secure, I have skipped trying this.
Using SetVirtualHostNameToFolderMapping
One way I've tried is using urls like
https://appassets/
with this:Using the WebResourceRequested event
The primary way we're using is via the WebResourceRequested event with the same kind of urls:
https://appassets/
For the WebResourceResponse I've tried returning https headers like this:
Command Line Flags
Via Environment Variable
I tried an abundance of different flags in different combinations, all set via WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS environment variable. Also, I verified that those flags are really being set on the msedgewebview2.exe processes.
Note: What does work is
--unsafely-treat-insecure-origin-as-secure=http://1.2.3.4
but the IP addresses are not known up-front and the setting doesn't support wildcard for IP addresses (as per docs and per trying).Via CoreWebView2Environment
Somewhere it was stated that
disable-web-security
wouldn't work via environment variable. As I also have a custom WebView2 control (re-implemented in C# from the WinUI2 C++ code) which allows usingCoreWebView2Environment
to create aCoreWebView2
, I have also tried setting flags viaCoreWebView2EnvironmentOptions.AdditionalBrowserArguments
Policies
I tried setting the following policies
AutomaticHttpsDefault 0 InsecurePrivateNetworkRequestsAllowed 1
Distribution Modes
I tried with the evergreen runtime 119.0.2151.58 and the canary version 120.0.2210.4
I tried with fixed version distribution: 119.0.2151.58