Skip to content

feat(core): set web view cookies, HttpOnly ones included - #401

Merged
houko merged 1 commit into
mainfrom
feat/set-web-cookies
Nov 16, 2026
Merged

houko merged 1 commit into
mainfrom
feat/set-web-cookies

Conversation

@houko

@houko houko commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes #387.

Scope.SetWebCookies(cookies []WebCookie, done func(error)) adds or replaces cookies, HttpOnly ones included, in the persistent store every web view of the app shares, and calls done on the engine's goroutine once the store holds them, so a page loaded after it sends them. An app can carry the sign-in the user has in their own browser into its web views this way (Kando's Browse page, yldm-tech/Kando#167). It works whether or not a web view exists.

  • API: core.WebCookie (Name, Value, Domain, Path, Expires, Secure, HTTPOnly, SameSite) and core.WebSameSite (WebSameSiteDefault, Lax, Strict, None), aliases of the driver's types, so [Feature] Web view cookie store: list cookies (including HttpOnly) and clear website data #112's listing can return the same type. Domain with a leading dot is a domain cookie, without one a host-only cookie, as a Set-Cookie Domain attribute makes it; a zero Expires is a session cookie. SameSite is an enum of NSGo's own rather than http.SameSite, as apps must not link net/http (internal/moduletest/linked_test.go).
  • Checks before the platform: core refuses, before any cookie is written, a cookie no platform takes: no name, a name with =, ;, , or spaces, a value with ;, a domain that is no host (a scheme, a port, a leading dot on an IP address), a path not starting with /, SameSite None without Secure, and __Secure- / __Host- names without their prefix's attributes. An empty list answers at once.
  • Protocol: one request item, ItemWebCookies with a WebCookiesRequest (the cookies and the app schemes the calling scope serves), answered by EventWebCookies through the tree's request waits; both are appended so Android's mirrored numbers do not move. driver.NoWebCookieStore answers where there is no store, which core turns into Unavailable.
  • Access: held code needs AccessWebView with the new method AccessCookies for each cookie's URL (gate now passes a rule's method). tsx/pkg grants it to no package: writing the store all of the host app's web views share reaches past a package's own pages. TSX gets no binding.
  • Incognito: Incognito web views keep their own stores on Apple, Windows and Linux and do not see these cookies. Android has one cookie store per app (as WebView.Incognito already documents), so there they do.

Backends

  • macOS, iOS: each cookie becomes an NSHTTPCookie (Foundation's exported NSHTTPCookie* keys, plus CFNetwork's HttpOnly key, which Foundation exports no constant for) set in WKWebsiteDataStore.defaultDataStore.httpCookieStore with setCookie:completionHandler:; done runs once every completion handler did. SameSite None leaves the policy out, as Foundation has no constant for it. tvOS has no WebKit and answers unavailable. Foundation caps an expiry at 400 days, as Chromium does.
  • Windows: WebView2 reaches a profile's cookies only through a web view (ICoreWebView2_2::get_CookieManager, slot 66; CreateCookie, AddOrUpdateCookie, GetCookies, slots checked against MyGo's WebView2 bindings, which were checked against WebView2.h). A request uses a web view of the window that shows pages of the app's profile. While there is none, the window makes a hidden composition controller in the profile's environment, starting the environment if needed, with the schemes ServeScheme serves to the calling scope, as a first web view would, so setting cookies before the first web view does not leave its app schemes unregistered. The hidden view stays until a web view of the profile shows pages (then it closes from a posted message, not inside a WebView2 handler) or the window closes, because WebView2's processes, and the session cookies they hold, end with the profile's last web view. AddOrUpdateCookie applies after it returns, so the request answers once GetCookies("") lists the cookies, and fails after 5 seconds; a watchdog answers a request whose web view closed in the middle.
  • Linux: webkit_cookie_manager_add_cookie with a libsoup 3 SoupCookie (soup_cookie_new, set_secure, set_http_only, set_same_site_policy; a max age of -1 for a session cookie) on the app's existing network session, whose cookies are in cookies.sqlite. It needs no web view.
  • Android: new runtime messages msgCookies / msgCookiesSet (Java COOKIES / COOKIES_SET: 20 to Java, after the keystore's SECRET, and 21 back, after the sensors' SENSOR_REPORT) rather than a batch item, carrying each cookie as a URL it goes to and its Set-Cookie value. NsgoView.cookies calls CookieManager.setCookie(url, value, callback) on the UI thread, then flush(), and answers with the surface and count. classes.dex and classes.jar are rebuilt with runtime/build.sh; rebuilding from main's sources with the same toolchain reproduces main's artifacts byte for byte, so the diff is this change only.
  • Web, watchOS, mini programs: answer Unavailable, as a page reaches no frame's cookie store and none of them has WebKit's.
  • nsgotest: App.WebCookies models the store (a later cookie of the same name, domain and path replaces one), and App.SetWebCookies can refuse a call.

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

None.

Verification

Native tests set an HttpOnly session cookie, an HttpOnly cookie with an expiry and a cookie of another path before any web view exists, then show a web view and an Incognito one of a local httptest server's pages. The server must receive the first two from the web view and none from the Incognito one, the page's document.cookie must not show the HttpOnly ones, and a second run of the app with the same website data must still send the cookie with an expiry and not the session one. The cookies are for 127.0.0.1 over HTTP, as no test server can be example.com over HTTPS, so Secure is checked only in the unit tests and the NSHTTPCookie test.

  • macOS (MacBook Pro, arm64): go tool nsgo test ./internal/platform/macos (TestSetWebCookiesReachesThePagesLoadedAfter) and ./internal/platform/apple (TestHTTPCookieCarriesEveryAttribute: name, value, domain, path, Secure, HttpOnly, session, SameSite and expiry read back from the NSHTTPCookie). With the cookie store write removed, the macOS test fails.
  • Linux (Ubuntu 24.04 x86_64, WebKitGTK 6.0, under xvfb-run): TestSetWebCookiesReachesThePagesLoadedAfter in ./internal/platform/linux passed; with webkit_cookie_manager_add_cookie skipped, it fails.
  • go tool nsgo check -target macos_arm64,macos_x64,windows_arm64,windows_x64,linux_arm64,linux_x64,android_arm64,web (on Ubuntu, as check.yml does) and -target ios,ios_simulator,tvos,tvos_simulator,watchos,watchos_simulator (on the Mac).
  • go tool nsgo test on Ubuntu under xvfb-run, as test.yml does: 2372 passed, 3 failed, 25 skipped. internal/platform/linux TestShadersReadImages and TestTrayServesItsItemAndMenuOverDBus failed under the full run's load (GL context, a GTK step timing out) and passed when rerun alone; base/terminal/internal/pty TestClose ("the process outlived its terminal") fails the same way on main on that machine. None of them touch this change.
  • Unit tests: core (TestSetWebCookies*: the store, replacement, refusals, platform errors, Access; the gate tests), internal/driver (TestWebCookieSetCookieAndURL), tsx/pkg (cookies is never granted), internal/platform/android (TestJavaMessagesMatchTheBackend holds the new message numbers to Java's).
  • Windows: TestSetWebCookiesReachesThePagesLoadedAfter and TestHoldsWebCookiesSkipsWhatTheStoreDrops in ./internal/platform/windows are written in the style of TestWebPagesCaptureWhatMediaAllows but not run: no Windows machine was available, and CI runs no Windows tests. The WebView2 code is only vetted for amd64, arm64 and 386.
  • iOS, Android: not run on a simulator or device. CI's Linux job does not install WebKitGTK, so the Linux native test skips there too.

@github-actions github-actions Bot added size/XL enhancement New feature or request documentation Improvements or additions to documentation macOS iOS watchOS Windows Linux Web Android Mini-program core tsx conflict Conflicts with the base branch; resolve it first and removed enhancement New feature or request labels Nov 16, 2026
@houko
houko force-pushed the feat/set-web-cookies branch from f340dba to 8a1dce9 Compare November 16, 2026 14:40
@github-actions github-actions Bot added the enhancement New feature or request label Nov 16, 2026
Scope.SetWebCookies adds or replaces cookies in the persistent store every web view of the app shares and answers once the store holds them, so a page loaded after it sends them. Apple uses WKHTTPCookieStore, Linux WebKitGTK's cookie manager, Android CookieManager over new runtime messages, and Windows the profile's ICoreWebView2CookieManager through a page web view or a hidden one. The web, tvOS, watchOS and mini programs answer unavailable.

Fixes #387.
@houko
houko force-pushed the feat/set-web-cookies branch from 8a1dce9 to 03c0e9f Compare November 16, 2026 14:44
@github-actions github-actions Bot removed the conflict Conflicts with the base branch; resolve it first label Nov 16, 2026
@houko
houko merged commit ddbc12f into main Nov 16, 2026
15 of 17 checks passed
@houko
houko deleted the feat/set-web-cookies branch November 16, 2026 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Web view cookie store: set cookies (including HttpOnly), so an in-app browser can carry a sign-in

1 participant