Skip to content

fix(wasm): Complete WebView2 cross-origin navigations (backport #25737) - #26133

Closed
mergify[bot] wants to merge 3 commits into
release/stable/7.0from
mergify/bp/release/stable/7.0/pr-25737
Closed

mergify[bot] wants to merge 3 commits into
release/stable/7.0from
mergify/bp/release/stable/7.0/pr-25737

Conversation

@mergify

@mergify mergify Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

GitHub Issue: closes #25735

PR Type:

🐞 Bugfix

What changed? 🚀

On WebAssembly, WebView2 shows its content in an <iframe>, and the load handler in WebView.ts reported the loaded URL by reading iframe.contentWindow.location.href. The browser blocks that read when the frame shows a document from another origin: a page on another site, a data: URL, or a page served with a Content-Security-Policy: sandbox header (or shown in a sandboxed frame). The handler threw an uncaught SecurityError before it reported the load. So NavigationCompleted was never raised for the document, Source stayed about:blank, and reading DocumentTitle afterwards threw a JSException.

The load handler now catches the failed read and reports the last URL known for the frame:

  • navigate() records the URL it sends the frame to, and every readable load records the frame's actual URL. They are kept in a WeakMap keyed by the iframe, so nothing outlives the element.
  • navigate() records the URL resolved against document.baseURI, as the browser resolves it when it navigates the frame. A virtual host mapping (SetVirtualHostNameToFolderMapping) navigates to a path relative to the app (./package_<hash>/WebContent/...), so without this a hidden virtual-host page would have been reported with a relative URL, which Uri can't parse, instead of the absolute one a readable load reports.
  • NavigateToString goes through a new navigateToString function, which records the URL of a srcdoc document, so a string shown in a sandboxed frame isn't reported with the URL of the page before it. The generic setAttribute interop it used before has no other caller in this PR, but stays, since feat(webview2): Improve API parity across platforms #23754 still uses it.
  • For a cross-origin load, the handler reports the recorded URL. NavigationCompleted is raised with it, as for any other load, and Source is set to it. Nothing is logged: a cross-origin document is an expected case.
  • Only the browser's SecurityError is treated that way. Any other error in the handler still surfaces, as before.
  • It also skips the NewWindowRequested hooks for such a document. They can't be attached to a cross-origin window, and trying would only log a console warning for every load.
  • getDocumentTitle returns an empty string when contentDocument is null, which is the case for a cross-origin document, instead of throwing.

A NavigateToString page is now reported as about:blank, readable or not, rather than as the iframe's about:srcdoc. That's what WinUI reports (Microsoft's "Working with local content" says a NavigateToString document "has its location set to about:blank"), and what #23754 reports too. Apps that compared Source with about:srcdoc on WebAssembly should compare with about:blank.

The recorded URL also fixes Source after a reparent. When a loaded WebView2 is unloaded and loaded again (moved to another parent, for example), its frame keeps showing the same document, but Loaded reported the frame's src attribute, which WebView2 doesn't set, so Source went back to about:blank. It now reports the frame's current URL, or the recorded one when the browser hides it. Reading the current URL there doesn't change the recorded one, so a cross-origin page that arrives after the control is added back is still reported with its own URL.

navigate() still sets the frame's location as before, so history behavior doesn't change.

A runtime test on Skia WebAssembly (headless Chrome) navigated a WebView2 to each kind of content:

Content Before After
data: URL uncaught SecurityError; no NavigationCompleted for the document; Source stays about:blank; DocumentTitle throws NavigationCompleted with IsSuccess; Source is the data URL; DocumentTitle is empty
Page on another origin same as above NavigationCompleted with IsSuccess; Source is the page's URL; DocumentTitle is empty
Same-origin page with Content-Security-Policy: sandbox same as above NavigationCompleted with IsSuccess; Source is the page's URL; DocumentTitle is empty
Virtual-host page (SetVirtualHostNameToFolderMapping) in a sandboxed frame same as above NavigationCompleted with IsSuccess; Source is the same absolute URL a readable load reports (the file's URL in the app's package)
NavigateToString in a sandboxed frame same as above NavigationCompleted with IsSuccess; Source is about:blank
A cross-origin page that navigates itself two uncaught SecurityErrors; no NavigationCompleted NavigationCompleted for each load; the second one reports the URL the frame was sent to
Same-origin page NavigationCompleted with the page's URL unchanged
NavigateToString NavigationCompleted with about:srcdoc NavigationCompleted with about:blank, as in WinUI
Unloading and loading the WebView2 again Source goes back to about:blank Source stays the URL of the page the frame shows

Each navigation also still raises an early NavigationCompleted with about:blank before NavigationStarting (see #25806), and adding a loaded WebView2 to the tree again still raises one too, which WinUI doesn't; this PR doesn't change either, and the table shows the one raised when the frame loads.

Limits. The browser hides a cross-origin document from the page that shows it, so some things can't be reported or done on WebAssembly. WebView.md now lists them in a "Limitations" subsection of "WebAssembly support":

  • After going back or forward to a page from another origin (with GoBack/GoForward on a same-origin page, or the browser's Back button, since iframe navigations are in the page's session history), or after a page moves to a page from another origin by itself (through a link, a script or a server redirect), Source can be an earlier URL: the page before it or, after a redirect, the URL before the redirect. The new URL can't be read from the parent page. Navigations to pages of the app's own origin are reported correctly.
  • Pages that refuse to be framed (X-Frame-Options, CSP frame-ancestors) and network errors load the browser's error page, which is cross-origin, so NavigationCompleted is raised with IsSuccess (status 200) for them. The parent page can't tell a failed cross-origin load from a successful one.
  • For cross-origin pages, DocumentTitle is empty (WinUI returns the title), GoBack, GoForward, Reload and Stop throw a JSException (SecurityError), and ExecuteScriptAsync fails with it, since they use members of the frame's window that the browser keeps from other origins. These calls already failed before this PR, but apps now get NavigationCompleted for such pages and may call them from that handler.
  • Pages from another origin never raise NewWindowRequested, since its hooks can't be attached to their window, so the app can't handle the new windows their links and scripts open.
  • Pages shown through a virtual host mapping report the URL of the file in the app's package (.../package_<hash>/<folder>/...) rather than the virtual host URL (such as http://UnoNativeAssets/index.html) as WinUI does. That predates this PR.

#23754 also fixes this read, as part of a larger change. That change also covers the early NavigationCompleted with about:blank that ProcessNavigation raises before NavigationStarting (#25806), NavigationStarting.Uri for URI navigations, and CanGoBack/CanGoForward. This PR is the minimal fix for the cross-origin read, independent of #23754, so either can go in first. The second to merge gets textual conflicts in WebView.ts and NativeWebView.wasm.cs (ProcessNavigation(string), OnLoaded); NativeWebView.Interop.wasm.cs and the test file merge cleanly. Both report about:blank for NavigateToString. The reparent tests (When_Url_Read_Before_Cross_Origin_Content_Arrives_Then_Its_Url_Is_Reported and When_Loaded_Again_Then_Source_Is_Kept) are tied to this PR's OnLoaded reporting and will need rework under #23754's navigation model, whose OnLoaded reports no load.

Tests: five runtime tests in Given_WebView2.uno.cs, Skia WebAssembly only, linked to #25735. They use data: URLs and the SamplesApp's own WebContent folder, so they need no network.

  • When_Navigated_To_Cross_Origin_Content_Then_Navigation_Completes navigates to a data: URL, which gets an opaque origin. It checks that NavigationCompleted is raised for it with IsSuccess, that Source is the data URL, that DocumentTitle is empty, and that the browser reports no SecurityError.
  • When_Navigated_To_Sandboxed_Mapped_Content_Then_Navigation_Completes maps a virtual host to WebContent and navigates to its index.html twice: in a frame sandboxed with allow-same-origin (readable) and in one sandboxed without it (hidden). It checks that both loads report the same absolute URL, that Source is that URL, and that the browser reports no SecurityError. Both sandboxes leave scripts off, so the page's site.js alert (WebView test page (WebContent/js/site.js) shows an error alert in Chrome and Edge #25807) doesn't run.
  • When_Showing_String_Then_Source_Is_Blank shows a string and checks that the load reports about:blank. It then sandboxes the frame, navigates it to a data: URL, shows another string, and checks that this load reports about:blank too, rather than the data: URL. It waits for the NavigationCompleted whose NavigationId is the one NavigationStarting reported, which skips the early one (WebView2 on WebAssembly raises NavigationCompleted before NavigationStarting, so each navigation completes twice #25806) and keeps working once that event is gone.
  • When_Url_Read_Before_Cross_Origin_Content_Arrives_Then_Its_Url_Is_Reported starts a navigation to a data: URL and, in the same browser task, reads the frame's current URL as Loaded does when the control is added back before the document arrives. It checks that the document is still reported with its own URL.
  • When_Loaded_Again_Then_Source_Is_Kept navigates to a data: URL, unloads the WebView2 and loads it again, and checks that Source is still the data URL.

The first two only listen for SecurityError, and only around the navigation, so an unrelated window error (such as a ResizeObserver loop warning) can't fail them.

I ran all runtime tests matching WebView locally (SamplesApp net10.0-browserwasm, headless Chrome 156) on this branch, rebased on master at 822f75d: the seven below pass, and the other WebView, WebView2 and CoreWebView2 tests are skipped on Skia WebAssembly. Each of the three commits builds on its own (Uno.UI.Runtime.WebAssembly.Browser, with its TypeScript, and Uno.UI.RuntimeTests).

Earlier runs, with the new tests on the code before each change:

Test Before the change After
When_Navigated_To_Cross_Origin_Content_Then_Navigation_Completes fails on master: no NavigationCompleted for the document within 10 s passes
When_Navigated_To_Sandboxed_Mapped_Content_Then_Navigation_Completes fails on master, by the code: no NavigationCompleted for the hidden load (it was added with the absolute-URL fix) passes
When_Showing_String_Then_Source_Is_Blank fails: first the sandboxed load reported data:text/html,earlier, then the readable one reported about:srcdoc passes
When_Url_Read_Before_Cross_Origin_Content_Arrives_Then_Its_Url_Is_Reported fails: the document was reported as about:blank, the previous page passes
When_Loaded_Again_Then_Source_Is_Kept fails: Source is about:blank after loading again passes
When_Collected_Then_Native_Iframe_Is_Removed (#24841) passes passes
When_Unloaded_While_Referenced_Then_Native_Iframe_Is_Kept (#24841) passes passes

The rows of the content table above come from these tests and from a scratch test, not part of this PR, that also navigated to pages served by a local server on another port.

PR Checklist ✅


This is an automatic backport of pull request #25737 done by [Mergify](https://mergify.com).

@github-actions github-actions Bot added platform/wasm 🌐 Categorizes an issue or PR as relevant to the WebAssembly platform kind/documentation labels Nov 6, 2026
@unodevops

Copy link
Copy Markdown
Contributor

🤖 Your WebAssembly Sample App stage site is ready! Visit it here:

@unodevops

Copy link
Copy Markdown
Contributor

@MartinZikmund
MartinZikmund deleted the mergify/bp/release/stable/7.0/pr-25737 branch November 12, 2026 10:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/documentation platform/wasm 🌐 Categorizes an issue or PR as relevant to the WebAssembly platform

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants