Skip to content

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

Closed

Description

@houko

Context

NSGo's web views keep a persistent website data store that every web view in the app shares. The app has no way to read that store or clear it.

  • On Linux, one WebKitGTK network session serves every web view of an app ("one set of cookies and storage", internal/platform/linux/web.go:304-306). NSGo fetches its cookie manager only to switch it to persistent SQLite storage (internal/platform/linux/web.go:320-322). The bindings stop at webkit_network_session_get_cookie_manager and webkit_cookie_manager_set_persistent_storage (internal/platform/linux/web.go:27-28, 95-96).
  • On Apple, each WKWebView is built from a plain WKWebViewConfiguration (internal/platform/apple/web.go:249-256), so it uses WKWebsiteDataStore.defaultDataStore. Nothing reaches its httpCookieStore or calls removeDataOfTypes:.
  • On Windows, every web view shares one WebView2 profile folder (internal/platform/windows/web.go:120, 124-131). NSGo never queries ICoreWebView2CookieManager or the profile's ClearBrowsingData.
  • Ref offers Reload, GoBack, GoForward, StopLoading, PostMessage, InjectJavaScript, SendInput and Evaluate (core/webview.go:216-279), and the driver's commands are the same set (internal/driver/webview.go:142-154). None of them reads cookies or stored data.

The only workaround today is Ref.Evaluate("document.cookie", …) (core/webview.go:279), and it falls short in three ways. It can't see HttpOnly cookies, which are the session cookies a sign-in sets. It doesn't return domain, path, expiry or the secure flag. And it only works while a page from that site is loaded.

#33 doesn't cover this. Its "request headers and cookies a load carries" row (from docs/roadmap.md:27) is about what the app adds to a request. Incognito (Draft PR #76) keeps one view's data in memory. Neither one lets an app read what the persistent store holds, or clear it.

Why it matters for real apps

Kando is a video player with a built-in browser. A user signs in to a video site in the app's web view and then saves a video from that site. yt-dlp does the download. It needs the site's cookies in Netscape format, HttpOnly session cookies included, or members-only and age-gated videos fail. On MyGo, Kando lists the store for the video's registrable domain (mygo.Cookies.List(mygo.CookieFilter{Domain: …})). It does this from the download pipeline, so the browser may already be on a different page (state_browse.go:94-106, 419-442). Kando also has a "Clear sign-in data…" command that empties the store through mygo.App.ClearBrowsingData (state_browse.go:99).

On NSGo, neither works on any of the three desktops. The only fallback is yt-dlp's --cookies-from-browser, which reads another browser's cookies, not the sign-in the user just completed in the app. On macOS it also needs Full Disk Access to read Safari's cookies. Other apps hit the same wall: any app that signs in through a web view and then calls the same site from Go (an API client, a sync engine, a downloader), and any app that offers "sign out" or "clear browsing data".

Minimal repro: after signing in to a site that sets an HttpOnly session cookie, the app can't see that cookie.

func Browser(cx *core.Scope) core.Node {
	web := &core.Ref{}
	return core.View{Children: []core.Node{
		core.WebView{Ref: web, Source: core.WebSrc("https://github.com/login")},
		ui.Button{Text: "Read cookies", OnPress: func() {
			web.Evaluate("document.cookie", func(json string, err error) {
				// After signing in: user_session / __Host-user_session_same_site
				// (HttpOnly) are missing, and there is no domain, path or expiry.
				log.Println(json, err)
			})
		}},
	}}
}

Proposal

Every web view in the app shares the store, so this belongs on Scope as an asynchronous service, like ReadClipboard or PickFiles (core/services.go:128, 456), not on a Ref. It should work whether or not a web view is mounted at the time.

// WebCookie is a cookie in the app's web views' persistent store.
type WebCookie struct {
	Name, Value  string
	Domain, Path string
	Expires      time.Time // zero for a session cookie
	Secure       bool
	HTTPOnly     bool
	SameSite     string // "", "Lax", "Strict" or "None"
}

// WebCookieFilter picks cookies: those a request to URL would send, or those of Domain and every host under it. The zero filter picks all.
type WebCookieFilter struct {
	URL    string
	Domain string
}

// WebCookies lists the cookies of the app's web views, HttpOnly ones included, and calls done on the engine's goroutine.
func (cx *Scope) WebCookies(f WebCookieFilter, done func(cookies []WebCookie, err error))

// ClearWebsiteData removes everything the app's web views stored: cookies, local storage, IndexedDB, service workers and caches.
func (cx *Scope) ClearWebsiteData(done func(err error))

Backends:

  • Apple: list with WKWebsiteDataStore.defaultDataStore.httpCookieStore getAllCookies: and filter in Go. Clear with removeDataOfTypes:allWebsiteDataTypes modifiedSince:distantPast completionHandler:.
  • Windows: list with GetCookies(uri) on ICoreWebView2_2.CookieManager (an empty URI lists all). Clear with ICoreWebView2Profile2.ClearBrowsingDataAll. WebView2 only exposes these from a live ICoreWebView2, so when no web view is mounted the backend can keep a hidden one in the shared profile.
  • Linux: list with webkit_cookie_manager_get_all_cookies / webkit_cookie_manager_get_cookies on the session's existing cookie manager. Clear with webkit_website_data_manager_clear(…, WEBKIT_WEBSITE_DATA_ALL, 0, …) on the manager from webkit_network_session_get_website_data_manager.
  • Android: CookieManager.getCookie(url) returns only names and values. Either say so in the docs (fill Domain from the URL and leave the other fields empty) or fail Domain filters with an Unavailable error. Clear with CookieManager.removeAllCookies plus WebStorage.deleteAllData.
  • Web, tvOS, watchOS: fail with core.Unavailable (core/errors.go:53), as errNoInstaller does (core/services.go:725).

Both calls act on the persistent store only. Incognito views (#33 / #76) keep their data in memory, so they are out of scope, and the docs should say that.

Acceptance

  • On macOS, Windows and Linux, a page in a core.WebView sets a cookie with Set-Cookie: s=1; HttpOnly; Secure; Path=/; Domain=example.test, and WebCookies returns it with every field filled in.
  • A Domain: "example.test" filter returns cookies for example.test and www.example.test but not for other.test. A URL filter returns exactly the cookies a request to that URL would send.
  • WebCookies still returns the cookie after the web view that set it has been unmounted, and after the app restarts (the persistent store).
  • After ClearWebsiteData, WebCookies returns nothing, and a reloaded page sees empty localStorage and IndexedDB.
  • done runs on the engine's goroutine. On an unsupported target, both calls fail with an Unavailable error instead of returning an empty list.
  • The gallery's WebView demo lists and clears cookies, and docs/architecture/native-views.md records the per-target matrix.

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