Repository navigation
fix(wasm): Complete WebView2 cross-origin navigations (backport #25737) - #26133
Closed
mergify[bot] wants to merge 3 commits into
Closed
mergify[bot] wants to merge 3 commits into
mergify[bot] wants to merge 3 commits into
Conversation
3 of 5 tasks
Contributor
|
🤖 Your WebAssembly Sample App stage site is ready! Visit it here: |
Contributor
|
🤖 Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-26133/docs/index.html |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
GitHub Issue: closes #25735
PR Type:
🐞 Bugfix
What changed? 🚀
On WebAssembly,
WebView2shows its content in an<iframe>, and theloadhandler inWebView.tsreported the loaded URL by readingiframe.contentWindow.location.href. The browser blocks that read when the frame shows a document from another origin: a page on another site, adata:URL, or a page served with aContent-Security-Policy: sandboxheader (or shown in a sandboxed frame). The handler threw an uncaughtSecurityErrorbefore it reported the load. SoNavigationCompletedwas never raised for the document,Sourcestayedabout:blank, and readingDocumentTitleafterwards threw aJSException.The
loadhandler 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 aWeakMapkeyed by the iframe, so nothing outlives the element.navigate()records the URL resolved againstdocument.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, whichUrican't parse, instead of the absolute one a readable load reports.NavigateToStringgoes through a newnavigateToStringfunction, which records the URL of asrcdocdocument, so a string shown in a sandboxed frame isn't reported with the URL of the page before it. The genericsetAttributeinterop it used before has no other caller in this PR, but stays, since feat(webview2): Improve API parity across platforms #23754 still uses it.NavigationCompletedis raised with it, as for any other load, andSourceis set to it. Nothing is logged: a cross-origin document is an expected case.SecurityErroris treated that way. Any other error in the handler still surfaces, as before.NewWindowRequestedhooks 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.getDocumentTitlereturns an empty string whencontentDocumentisnull, which is the case for a cross-origin document, instead of throwing.A
NavigateToStringpage is now reported asabout:blank, readable or not, rather than as the iframe'sabout:srcdoc. That's what WinUI reports (Microsoft's "Working with local content" says aNavigateToStringdocument "has its location set to about:blank"), and what #23754 reports too. Apps that comparedSourcewithabout:srcdocon WebAssembly should compare withabout:blank.The recorded URL also fixes
Sourceafter a reparent. When a loadedWebView2is unloaded and loaded again (moved to another parent, for example), its frame keeps showing the same document, butLoadedreported the frame'ssrcattribute, whichWebView2doesn't set, soSourcewent back toabout: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
WebView2to each kind of content:data:URLSecurityError; noNavigationCompletedfor the document;Sourcestaysabout:blank;DocumentTitlethrowsNavigationCompletedwithIsSuccess;Sourceis the data URL;DocumentTitleis emptyNavigationCompletedwithIsSuccess;Sourceis the page's URL;DocumentTitleis emptyContent-Security-Policy: sandboxNavigationCompletedwithIsSuccess;Sourceis the page's URL;DocumentTitleis emptySetVirtualHostNameToFolderMapping) in a sandboxed frameNavigationCompletedwithIsSuccess;Sourceis the same absolute URL a readable load reports (the file's URL in the app's package)NavigateToStringin a sandboxed frameNavigationCompletedwithIsSuccess;Sourceisabout:blankSecurityErrors; noNavigationCompletedNavigationCompletedfor each load; the second one reports the URL the frame was sent toNavigationCompletedwith the page's URLNavigateToStringNavigationCompletedwithabout:srcdocNavigationCompletedwithabout:blank, as in WinUIWebView2againSourcegoes back toabout:blankSourcestays the URL of the page the frame showsEach navigation also still raises an early
NavigationCompletedwithabout:blankbeforeNavigationStarting(see #25806), and adding a loadedWebView2to 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.mdnow lists them in a "Limitations" subsection of "WebAssembly support":GoBack/GoForwardon 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),Sourcecan 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.X-Frame-Options, CSPframe-ancestors) and network errors load the browser's error page, which is cross-origin, soNavigationCompletedis raised withIsSuccess(status 200) for them. The parent page can't tell a failed cross-origin load from a successful one.DocumentTitleis empty (WinUI returns the title),GoBack,GoForward,ReloadandStopthrow aJSException(SecurityError), andExecuteScriptAsyncfails 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 getNavigationCompletedfor such pages and may call them from that handler.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..../package_<hash>/<folder>/...) rather than the virtual host URL (such ashttp://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
NavigationCompletedwithabout:blankthatProcessNavigationraises beforeNavigationStarting(#25806),NavigationStarting.Urifor URI navigations, andCanGoBack/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 inWebView.tsandNativeWebView.wasm.cs(ProcessNavigation(string),OnLoaded);NativeWebView.Interop.wasm.csand the test file merge cleanly. Both reportabout:blankforNavigateToString. The reparent tests (When_Url_Read_Before_Cross_Origin_Content_Arrives_Then_Its_Url_Is_ReportedandWhen_Loaded_Again_Then_Source_Is_Kept) are tied to this PR'sOnLoadedreporting and will need rework under #23754's navigation model, whoseOnLoadedreports no load.Tests: five runtime tests in
Given_WebView2.uno.cs, Skia WebAssembly only, linked to #25735. They usedata:URLs and the SamplesApp's ownWebContentfolder, so they need no network.When_Navigated_To_Cross_Origin_Content_Then_Navigation_Completesnavigates to adata:URL, which gets an opaque origin. It checks thatNavigationCompletedis raised for it withIsSuccess, thatSourceis the data URL, thatDocumentTitleis empty, and that the browser reports noSecurityError.When_Navigated_To_Sandboxed_Mapped_Content_Then_Navigation_Completesmaps a virtual host toWebContentand navigates to itsindex.htmltwice: in a frame sandboxed withallow-same-origin(readable) and in one sandboxed without it (hidden). It checks that both loads report the same absolute URL, thatSourceis that URL, and that the browser reports noSecurityError. Both sandboxes leave scripts off, so the page'ssite.jsalert (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_Blankshows a string and checks that the load reportsabout:blank. It then sandboxes the frame, navigates it to adata:URL, shows another string, and checks that this load reportsabout:blanktoo, rather than thedata:URL. It waits for theNavigationCompletedwhoseNavigationIdis the oneNavigationStartingreported, 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_Reportedstarts a navigation to adata:URL and, in the same browser task, reads the frame's current URL asLoadeddoes 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_Keptnavigates to adata:URL, unloads theWebView2and loads it again, and checks thatSourceis still the data URL.The first two only listen for
SecurityError, and only around the navigation, so an unrelated window error (such as aResizeObserverloop warning) can't fail them.I ran all runtime tests matching
WebViewlocally (SamplesAppnet10.0-browserwasm, headless Chrome 156) on this branch, rebased onmasterat 822f75d: the seven below pass, and the otherWebView,WebView2andCoreWebView2tests are skipped on Skia WebAssembly. Each of the three commits builds on its own (Uno.UI.Runtime.WebAssembly.Browser, with its TypeScript, andUno.UI.RuntimeTests).Earlier runs, with the new tests on the code before each change:
When_Navigated_To_Cross_Origin_Content_Then_Navigation_Completesmaster: noNavigationCompletedfor the document within 10 sWhen_Navigated_To_Sandboxed_Mapped_Content_Then_Navigation_Completesmaster, by the code: noNavigationCompletedfor the hidden load (it was added with the absolute-URL fix)When_Showing_String_Then_Source_Is_Blankdata:text/html,earlier, then the readable one reportedabout:srcdocWhen_Url_Read_Before_Cross_Origin_Content_Arrives_Then_Its_Url_Is_Reportedabout:blank, the previous pageWhen_Loaded_Again_Then_Source_Is_KeptSourceisabout:blankafter loading againWhen_Collected_Then_Native_Iframe_Is_Removed(#24841)When_Unloaded_While_Referenced_Then_Native_Iframe_Is_Kept(#24841)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 ✅
Screenshots Compare Test Runresults.This is an automatic backport of pull request #25737 done by [Mergify](https://mergify.com).