Skip to content

Commit 3cf38ab

Browse files
Fix WebView2 file: URL security origin warning (#944)
Replace `file://` navigation with `SetVirtualHostNameToFolderMapping` using the `bravo.example` virtual host (RFC 6761 reserved TLD). This eliminates the Chromium security warning "file: URLs are treated as unique security origins" that appeared in the WebView2 console at startup.
1 parent 6a32776 commit 3cf38ab

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

src/Infrastructure/AppWindow.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,12 @@ private async void InitializeWebViewAsync()
127127
/* ICoreWebView2 */ WebView.CoreWebView2.PermissionRequested += OnWebViewPermissionRequested;
128128
/* ICoreWebView2_10 */ WebView2Helper.TryAndIgnoreUnsupportedError(() => WebView.CoreWebView2.BasicAuthenticationRequested += OnWebViewBasicAuthenticationRequested);
129129

130-
/* ICoreWebView2 */ await WebView.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync(WindowExternalWebMessageCallbackScript);
131-
/* ICoreWebView2 */ WebView.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.All);
132-
WebView.Source = new Uri(Path.Combine(Environment.CurrentDirectory, "wwwroot\\index.html"));
133-
// /* ICoreWebView2_3 */ WebView.CoreWebView2.SetVirtualHostNameToFolderMapping("bravo.example", "wwwroot", CoreWebView2HostResourceAccessKind.Allow);
134-
//WebView.CoreWebView2.Navigate("https://bravo.example/index.html"); // For '.example' see rfc6761
130+
/* ICoreWebView2 */ await WebView.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync(WindowExternalWebMessageCallbackScript);
131+
/* ICoreWebView2 */ WebView.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.All);
132+
// Use a virtual host name to serve local content over HTTPS, avoiding 'file:' URL security origin restrictions.
133+
// The '.example' TLD is reserved by RFC 6761 and guaranteed never to be registered, preventing domain collisions.
134+
/* ICoreWebView2_3 */ WebView.CoreWebView2.SetVirtualHostNameToFolderMapping("bravo.example", "wwwroot", CoreWebView2HostResourceAccessKind.Allow);
135+
/* ICoreWebView2 */ WebView.CoreWebView2.Navigate("https://bravo.example/index.html");
135136

136137
// Allow users to open the DevTools for troubleshooting; this is only available in non-stable releases
137138
if (!AppEnvironment.IsStableRelease && CommonHelper.IsKeyDown(System.Windows.Forms.Keys.ShiftKey))

0 commit comments

Comments
 (0)