Skip to content

feat(core): list web view cookies and clear website data - #892

Merged
houko merged 4 commits into
mainfrom
feat/webview-cookies
Nov 19, 2026
Merged

houko merged 4 commits into
mainfrom
feat/webview-cookies

Conversation

@houko

@houko houko commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

Summary

Adds Scope.WebCookies and Scope.ClearWebsiteData, the listing and clearing half of the web views' persistent cookie store that SetWebCookies (#387) started.

  • cx.WebCookies(core.WebCookieFilter{URL: …} | {Domain: …} | {}, done) lists the store's cookies, HttpOnly ones included, with every WebCookie field, whether or not a web view is mounted. A URL filter picks what a request to that URL would send (RFC 6265 domain and path match, Secure only for https, expired dropped); a Domain filter picks the domain and every host under it; the zero filter picks all. Results come sorted by host, path and name.
  • cx.ClearWebsiteData(done) removes the store's cookies, local and session storage, IndexedDB, service workers and caches.
  • Both reuse ItemWebCookies / EventWebCookies: WebCookiesRequest gains an Op (set, list, clear) and a Filter, and a list answers its cookies in the event's Data, so the backends that answer "unavailable" (web, tvOS, watchOS, mini programs) need no change.
  • Core rejects a filter with both a URL and a domain, or a non-http(s) URL, before the platform sees it, and holds code an Access holds to AccessWebView + AccessCookies for the filter's URL, https://<domain>/, or "" for the whole store and for clearing (no TSX capability grants it, as for SetWebCookies).
  • nsgotest lists from App.WebCookies, empties it on clear and counts clears in App.WebsiteDataClears.
Target WebCookies ClearWebsiteData
macOS, iOS WKHTTPCookieStore getAllCookies:, filtered in Go; Foundation has no SameSite None, which reads as the default removeDataOfTypes:allWebsiteDataTypes modifiedSince:distantPast
Windows ICoreWebView2CookieManager::GetCookies(""), filtered in Go ICoreWebView2Profile2::ClearBrowsingDataAll via ICoreWebView2_13::get_Profile
Linux webkit_cookie_manager_get_all_cookies (bound only where WebKitGTK ≥ 2.42 has it), filtered in Go webkit_website_data_manager_clear(WEBKIT_WEBSITE_DATA_ALL, 0)
Android CookieManager.getCookie(url): names and values only, Domain = the URL's host; a Domain or zero filter answers Unavailable removeAllCookies + flush + WebStorage.deleteAllData (the HTTP cache stays)
Web, tvOS, watchOS, mini programs Unavailable Unavailable

Two fixes found on the way, both in unreleased #387 code:

  • Linux: SetWebCookies turned Expires into a max age counted from now, ending the cookie up to a second early; it now sets the exact expiry, so a cookie lists back as it was written.
  • Windows: a cookie request that picked a page web view the same batch deletes never got an answer (WebView2 calls no handler once the controller closes) and failed after six seconds. A request now restarts on another web view or the hidden one when the one it uses closes first.

The gallery's WebView demo gets a Cookies card that sets an HttpOnly cookie for nsgo.dev, lists the site's cookies and clears the store.

Fixes #112

Targets

  • Shared code (core, base, ui, tsx, the nsgo tool)
  • macOS
  • iOS
  • tvOS
  • watchOS
  • Android
  • Windows
  • Linux
  • Web
  • Mini programs
  • Docs or website only

Screenshots

The gallery's WebView demo on macOS (-target macos, 1200×800), before and after "Set a cookie" was pressed:

Before After
Before: WebView demo without the Cookies card After: WebView demo with the Cookies card listing an HttpOnly cookie

Verification

  • go tool nsgo check -target macos,windows,linux,android,web,ios,tvos,watchos (every target vetted)
  • go tool nsgo test: full headless suite on Linux, 3006 passed; the only failures were two Android Java-fixture tests that need javac on that host, unrelated. On macOS: core, internal/driver, nsgotest, internal/platform/apple, internal/platform/android, examples/gallery, tsx/pkg, internal/moduletest, cmd/nsgo/harmony.
  • go tool nsgo test -target macos ./internal/platform/macos -run WebCookies: new TestWebCookiesListsAndClearWebsiteDataClears. A local server's page sets an HttpOnly cookie, writes localStorage and makes an IndexedDB database, next to SetWebCookies cookies for .example.test, www.example.test and other.test. After the web view unmounts, a URL filter lists the page's cookie and a domain filter lists the two example.test ones, with every field. After a restart with no web view mounted, the same lists come back; then ClearWebsiteData leaves the store empty, and a page loaded afterwards sends no Cookie, reads localStorage as null and gets a freshly created database. A mutation that skips the clear makes the test fail.
  • The same test on Linux (internal/platform/linux, WebKitGTK 2.52 under Xvfb).
  • The same test on Windows (internal/platform/windows, WebView2, interactive session) passes, with TestSetWebCookiesReachesThePagesLoadedAfter and TestHoldsWebCookiesSkipsWhatTheStoreDrops.
  • iOS simulator (iPhone 18 Pro, iOS 27.2) and an Android 15 arm64 emulator: an ad-hoc nsgotest target test set, listed by URL and domain, cleared and listed the store with no page. On Android the URL list came back as names/values only and the domain list as Unavailable, as documented.
  • New unit tests: driver.FilterWebCookies (domain/path/Secure/expiry matching), core's filter validation, Access gating and clearing, Apple's NSHTTPCookie round trip, and Android's message encoding and Cookie-header parsing, with a check that its op constants match Protocol.java.

Not verified: real HTTPS sign-ins on devices (tracked in the roadmap row), and a tvOS or web run of the unavailable path (covered only by the shared driver.NoWebCookies answer and core's mapping).

@github-actions github-actions Bot added enhancement New feature or request size/XL documentation Improvements or additions to documentation macOS iOS Windows Linux Android core conflict Conflicts with the base branch; resolve it first and removed enhancement New feature or request labels Nov 19, 2026
@houko
houko force-pushed the feat/webview-cookies branch from 21c4c4a to 90a4625 Compare November 19, 2026 15:33
@github-actions github-actions Bot added tsx enhancement New feature or request and removed conflict Conflicts with the base branch; resolve it first labels Nov 19, 2026
@houko
houko force-pushed the feat/webview-cookies branch 2 times, most recently from 3e1dea6 to 9b96067 Compare November 19, 2026 18:13
@houko

houko commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator Author

Review status: not merging yet. The code review is done and fixes are pushed, but the new macOS native test fails on CI every time, and the latest run looks like a real platform bug, not a flaky test.

Pushed on top of the rebase onto main:

  • Rebuilt classes.dex/classes.jar from the merged Java sources. main had changed the runtime too, so the binary conflict could not just take either side.
  • Windows: ClearWebsiteData now waits up to a minute before reporting the request lost. Before, it shared the 6 s cookie timeout, so a slow ClearBrowsingDataAll on a large profile could report an error even though the data was cleared.
  • Updated the AccessCookies and TSX Allows comments to cover listing and clearing, and added a TSX capability test for the whole-store resource ("", cookies).
  • TestWebCookiesListsAndClearWebsiteDataClears (macOS): the second run now has the page set its cookie again before it clears.

Checks that passed: TSX plugins cannot reach WebCookies/ClearWebsiteData, because Capabilities.Allows returns false for cookies whatever the resource is. Clearing only touches the app's own store on every platform: the app's WKWebsiteDataStore.defaultDataStore, the WebView2 profile under %LOCALAPPDATA%\<app id>\WebView2, the WebKitGTK network session under $XDG_DATA_HOME/<app id>/webkit, and Android's per-app CookieManager/WebStorage. WEBKIT_WEBSITE_DATA_ALL matches upstream's 6.0 header ((1 << 13) - 1).

The blocker. On GitHub's macos-15-intel runner the second process of the test sees nothing that the first process stored. It passes on a recent macOS over SSH.

  • Runs 44569751051 (twice): the second process's WebCookies{URL} did not list the cookie the page set in the first process.
  • Run 44585461493, after the test change: the second process's first call, WebCookies{Domain: "example.test"}, came back with no cookies. It runs before any web view exists in that process. Later in the same process, the page's freshly set cookie listed fine.

So on macOS 15 there are two possible causes:

  • WKHTTPCookieStore getAllCookies: with no WKWebView alive yet does not see the persisted cookies.
  • Or cookies written just before quitting are never saved to disk.

TestSetWebCookiesReachesThePagesLoadedAfter does see a persisted cookie after a restart. But it loads pages before quitting, and its second run reads the cookie through a page load, not through getAllCookies:. Either way, the documented promise that listing works "whether or not a web view is shown" does not hold on macOS 15. A likely fix is to keep a hidden WKWebView on the default store alive while a request runs with no web view shown, as Windows already does. That needs to be confirmed on macOS 15 before this merges. HarmonyOS also never answers ItemWebCookies, so done is never called there. That gap already existed for SetWebCookies (#387) and is out of scope here.

houko and others added 4 commits November 20, 2026 07:21
Scope.WebCookies lists the cookies of the persistent store every web view of the app shares, HttpOnly ones and every field included, picked by a URL a request would send them to or by a domain and the hosts under it, and Scope.ClearWebsiteData removes the store's cookies, storage, service workers and caches; both work with no web view shown. They ride ItemWebCookies, whose request now carries an op. Apple lists with WKHTTPCookieStore getAllCookies and clears with removeDataOfTypes, Windows with the profile's GetCookies and ClearBrowsingDataAll through a page web view or the hidden one, restarting a request whose web view closes first, Linux with WebKitGTK's get_all_cookies and its website data manager, and Android with CookieManager.getCookie by URL alone, names and values, and removeAllCookies with WebStorage.deleteAllData. The web, tvOS, watchOS and mini programs answer unavailable. Linux now keeps a cookie SetWebCookies writes until the second its Expires names. The gallery's WebView demo sets, lists and clears cookies.

Fixes #112.
ClearBrowsingDataAll deletes the profile's caches from disk, which can outlast the six seconds a cookie request waits before calling itself lost, so a clear that succeeded could answer an error. A clear now waits a minute. Access's and the TSX capabilities' comments now say listing and clearing the store are what no capability grants, with a test for the whole-store resource.
On the hosted macOS 15 runners WebKit ends the first run's process before it writes the cookie the page set last, so the second run listed none and failed. The second run now lists the example.test cookies SetWebCookies wrote before any web view shows, has the page set its cookie and storage again, lists the page's cookie and then clears, so what ClearWebsiteData must remove is there in that run.
WebKit before 313745@main (https://webkit.org/b/315310), macOS 15's among it, answers WKHTTPCookieStore getAllCookies: with no cookies while the data store's network process has not started, and nothing starts it before the app's first web view. So WebCookies before any web view showed listed none of the cookies an earlier run kept on disk. WebCookies now fetches the store's cookie records first, which starts the process on a persistent store. SetWebCookies and ClearWebsiteData needed nothing: setCookie: and removeDataOfTypes: start it themselves.

The macOS test goes back to listing the first run's cookies, the page's and SetWebCookies', and clearing them in a second run with no web view, and logs what the store lists before anything else.
@houko

houko commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator Author

Reviewed. Root cause: WebKit before 313745@main (https://webkit.org/b/315310), including macOS 15's, answers getAllCookies: with nothing while the data store's network process has not started, and nothing starts it before the first web view; WebCookies now fetches the store's cookie records first, which launches it (setCookie: and removeDataOfTypes: already launch it, so SetWebCookies/ClearWebsiteData were unaffected), and the test is back to its strict form, listing and clearing the first run's cookies in a second run with no web view, which passes on the macos-15 runner (run 44628040832).

@houko
houko merged commit 568b222 into main Nov 19, 2026
20 checks passed
@houko
houko deleted the feat/webview-cookies branch November 19, 2026 23:02
@houko houko self-assigned this Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Android core documentation Improvements or additions to documentation enhancement New feature or request iOS Linux macOS size/XL tsx Windows

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Web view cookie store: list cookies (including HttpOnly) and clear website data

1 participant