Skip to content

WebView2 on WebAssembly throws SecurityError and doesn't complete navigations to cross-origin content #25735

Closed

Description

@mikernet

Current behavior 🐛

On WebAssembly, WebView2 shows its content in an <iframe>. When the frame loads, the load handler in WebView.ts reads iframe.contentWindow.location.href to report the URL. The browser blocks that read when the frame shows a document from another origin, so the handler throws on every such load:

Uncaught SecurityError: Failed to read a named property 'href' from 'Location': Blocked a frame with origin "http://localhost:5000" from accessing a cross-origin frame.

This covers pages on another site, data: URLs (they get an opaque origin) and pages served with a Content-Security-Policy: sandbox header. We hit it in a WebAssembly app showing documents from another origin: the console shows an uncaught error for every document.

Because the handler throws before it reports the load:

  • NavigationCompleted isn't raised for the document. The only NavigationCompleted is the one ProcessNavigation raises synchronously when the navigation is requested, before NavigationStarting and before the frame loads, with about:blank.
  • CoreWebView2.Source stays about:blank, and so does WebView2.Source when the navigation was started with CoreWebView2.Navigate.
  • CoreWebView2.DocumentTitle throws a JSException (TypeError: Cannot read properties of null (reading 'title')): contentDocument is null for a cross-origin frame.

A runtime test on Skia WebAssembly (headless Chrome, master at b40e235) navigated a WebView2 to each kind of content and recorded what it reports:

Content NavigationCompleted CoreWebView2.Source afterwards DocumentTitle Console
data: URL only the early one, with about:blank about:blank throws uncaught SecurityError
Page on another origin only the early one, with about:blank about:blank throws uncaught SecurityError
Same-origin page with Content-Security-Policy: sandbox only the early one, with about:blank about:blank throws uncaught SecurityError
Same-origin page the early one, then one for the page the page's URL the page's title nothing
NavigateToString the early one, then one for about:srcdoc about:srcdoc the document's title nothing

Expected behavior 🎯

As in WinUI, NavigationCompleted is raised with IsSuccess once the cross-origin document has loaded, and Source is its URL. Nothing is logged as an error, since a cross-origin document is an expected case. DocumentTitle doesn't throw. The browser doesn't expose the title of a cross-origin document, so it can only be empty on WebAssembly.

How to reproduce it (as minimally and precisely as possible) 🔬

In a WebAssembly app:

var webView = new WebView2 { Width = 400, Height = 300 };
// Add webView to the visual tree, then:
await webView.EnsureCoreWebView2Async();

webView.NavigationCompleted += (s, e) => Console.WriteLine($"NavigationCompleted: {s.CoreWebView2.Source}");
webView.CoreWebView2.Navigate("data:text/html,Hello");

The console shows the SecurityError, and the only NavigationCompleted reports about:blank. Navigating to a page on another site (https://example.com/ from an app served on another origin) does the same.

Workaround 🛠️

Show cross-origin content in an iframe of your own instead of a WebView2: create it with BrowserHtmlElement.CreateHtmlElement("iframe"), set its src attribute with SetHtmlAttribute, and use it as the Content of a ContentControl.

Affected platforms 📱💻🖥️

WebAssembly

Uno.Sdk version (and other relevant versions) 📦

master at b40e235 (Uno 7.0 dev), and Uno.Sdk 6.8.0-dev.66 (Uno.WinUI 6.8.0-dev.222). On 6.8, the native WebAssembly renderer uses the same WebView.ts.

IDE version 🧑‍💻

Not IDE-related.

Anything else we need to know? 💬

The read is in onLoad in src/Uno.UI.Runtime.WebAssembly.Browser/ts/Runtime/WebView.ts, outside the try that guards the NewWindowRequested hooks. navigate() sets the frame's location rather than its src attribute, so src can't serve as a fallback either.

Related behavior on WebAssembly that this issue doesn't cover:

  • ProcessNavigation raises NavigationCompleted with about:blank synchronously, before NavigationStarting and before the frame loads (the early event in the table).
  • NavigationStarting for a URI navigation reports a data: URI that wraps the target URL as if it were HTML content, instead of the URL.
  • CanGoBack and CanGoForward are never updated.

The open #23754 addresses these as part of a larger change, and it also fixes this read. WebView2.Close() isn't implemented on WebAssembly either; that is #25736.

I have a fix with a runtime test ready and will open a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions