Skip to content

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

Merged
MartinZikmund merged 3 commits into
unoplatform:masterfrom
mikernet:dev/mikernet/wasm-webview-cross-origin
Nov 6, 2026
Merged

MartinZikmund merged 3 commits into
unoplatform:masterfrom
mikernet:dev/mikernet/wasm-webview-cross-origin

Conversation

@mikernet

@mikernet mikernet commented Oct 27, 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 ✅

Copilot AI balanced review requested due to automatic review settings October 27, 2026 07:34
@github-actions github-actions Bot added the platform/wasm 🌐 Categorizes an issue or PR as relevant to the WebAssembly platform label Oct 27, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Relative fallback URLs from virtual-host mappings can still cause managed navigation completion to throw.

1 open issue
What changed in this PR

This PR addresses #25735 by allowing WebAssembly WebView2 to report completed loads when browser origin restrictions prevent reading the iframe’s URL or title. It keeps the broader API-parity work in #23754 separate.

Changes:

  • Tracks the last known iframe URL as a fallback for cross-origin loads.
  • Returns an empty title and skips inaccessible new-window hooks for cross-origin documents.
  • Adds a WebAssembly regression test using an opaque-origin data: URL.
File Description
src/​Uno.UI.RuntimeTests/​Microsoft/​UI/​Xaml/​Controls/​Given_WebView2.uno.cs Tests cross-origin completion, source updates, title access, and browser errors.
src/​Uno.UI.Runtime.WebAssembly.Browser/​ts/​Runtime/​WebView.ts Adds URL fallback tracking and handles restricted document access.

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 27, 2026 10:26

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The focused fix addresses the reported failure and previously flagged relative-URL issue, with targeted regression coverage and no unresolved findings.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@mikernet
mikernet force-pushed the dev/mikernet/wasm-webview-cross-origin branch from 0cb1c8f to fe06745 Compare October 29, 2026 16:13
Copilot AI balanced review requested due to automatic review settings October 29, 2026 16:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The review identifies no active issues in the WebAssembly WebView2 navigation changes.

No open issues 🎉

Review effort: Balanced

@unodevops

Copy link
Copy Markdown
Contributor

@MartinZikmund

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 2 pipeline(s).

@unodevops

Copy link
Copy Markdown
Contributor

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

@nventive-devops

Copy link
Copy Markdown
Contributor

The build 242773 found UI Test snapshots differences: skia-linux-screenshots: 2458, skia-windows-screenshots: 2458

Details
  • skia-linux-screenshots: 2458 changed over 2492

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AndroidWindowInsets.png
    • AnimatedVisualsTestbedPage.png-dark
    • AppBarButtonWithIconTest.png-dark
    • AppBar_KeyBoard.png-dark
    • ApplicationViewSizing.png-dark
    • ApplicationViewSpanningRectsPage.png
    • AppWindowClosing.png
    • AppWindowPositionAndSize.png
    • Arrange_Performance01.png-dark
    • Arrange_Performance01.png
    • AutoBorderStretchwithleftmargin.png-dark
    • AutoBorderStretchwithleftmargin.png
    • AutomationProperties_Landmark.png-dark
    • AutomationProperties_Landmark.png
    • AutoSizedPathCentered.png
    • AutoSizedTopLeft.png-dark
    • AutoSuggestBox_Keyboard.png
    • AutoSuggestBox_Reason.png-dark
    • BasicEnability.png-dark
    • BasicEnability.png
  • skia-windows-screenshots: 2458 changed over 2492

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AnimatedBackVisualSourceSamplePage.png
    • AnimatedVisualsTestbedPage.png-dark
    • AppBarButtonWithIconTest.png
    • AppBarToggleButtonTest.png
    • ApplicationViewMode.png-dark
    • ApplicationViewSpanningRectsPage.png-dark
    • AppWindowClosing.png-dark
    • AppWindowClosing.png
    • AppWindowPresenters.png
    • AppWindowTitleBar Properties.png
    • ArcSegment.png
    • Attributed_text_FontSize_Changing.png
    • AuthenticationBroker_Cancel.png
    • AutoBorderStretchwithbottommargin.png
    • AutoBorderStretchwithrightmargin.png-dark
    • AutoBorderStretchwithtopmargin.png
    • AutomationProperties_Landmark.png
    • AutomationProperties_Relations.png-dark
    • AutomationProperties_Name.png-dark
    • AutomationProperties_Relations.png

@unodevops

Copy link
Copy Markdown
Contributor

@unodevops

Copy link
Copy Markdown
Contributor

⚠️⚠️ The build 242773 has failed on Uno.UI - CI.

@MartinZikmund MartinZikmund added the 7.0 PRs only. For issues, use the Release field. label Nov 4, 2026
@MartinZikmund

Copy link
Copy Markdown
Member

@Mergifyio backport release/stable/7.0

@mergify

mergify Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

backport release/stable/7.0

✅ Backports have been created

Details

@MartinZikmund
MartinZikmund merged commit 22929cb into unoplatform:master Nov 6, 2026
48 of 50 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

7.0 PRs only. For issues, use the Release field. 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.

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

5 participants