Repository navigation
feat(core): set web view cookies, HttpOnly ones included - #401
Merged
Merged
Conversation
houko
force-pushed
the
feat/set-web-cookies
branch
from
November 16, 2026 14:40
f340dba to
8a1dce9
Compare
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
force-pushed
the
feat/set-web-cookies
branch
from
November 16, 2026 14:44
8a1dce9 to
03c0e9f
Compare
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.
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 callsdoneon 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.core.WebCookie(Name,Value,Domain,Path,Expires,Secure,HTTPOnly,SameSite) andcore.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.Domainwith a leading dot is a domain cookie, without one a host-only cookie, as a Set-CookieDomainattribute makes it; a zeroExpiresis a session cookie.SameSiteis an enum of NSGo's own rather thanhttp.SameSite, as apps must not linknet/http(internal/moduletest/linked_test.go).=,;,,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/,SameSiteNone withoutSecure, and__Secure-/__Host-names without their prefix's attributes. An empty list answers at once.ItemWebCookieswith aWebCookiesRequest(the cookies and the app schemes the calling scope serves), answered byEventWebCookiesthrough the tree's request waits; both are appended so Android's mirrored numbers do not move.driver.NoWebCookieStoreanswers where there is no store, which core turns intoUnavailable.AccessWebViewwith the new methodAccessCookiesfor each cookie's URL (gatenow passes a rule's method).tsx/pkggrants 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.WebView.Incognitoalready documents), so there they do.Backends
NSHTTPCookie(Foundation's exportedNSHTTPCookie*keys, plus CFNetwork'sHttpOnlykey, which Foundation exports no constant for) set inWKWebsiteDataStore.defaultDataStore.httpCookieStorewithsetCookie:completionHandler:;doneruns 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.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 schemesServeSchemeserves 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.AddOrUpdateCookieapplies after it returns, so the request answers onceGetCookies("")lists the cookies, and fails after 5 seconds; a watchdog answers a request whose web view closed in the middle.webkit_cookie_manager_add_cookiewith a libsoup 3SoupCookie(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 incookies.sqlite. It needs no web view.msgCookies/msgCookiesSet(JavaCOOKIES/COOKIES_SET: 20 to Java, after the keystore'sSECRET, 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.cookiescallsCookieManager.setCookie(url, value, callback)on the UI thread, thenflush(), and answers with the surface and count.classes.dexandclasses.jarare rebuilt withruntime/build.sh; rebuilding frommain's sources with the same toolchain reproducesmain's artifacts byte for byte, so the diff is this change only.Unavailable, as a page reaches no frame's cookie store and none of them has WebKit's.App.WebCookiesmodels the store (a later cookie of the same name, domain and path replaces one), andApp.SetWebCookiescan refuse a call.Targets
core,base,ui,tsx, thensgotool)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
httptestserver's pages. The server must receive the first two from the web view and none from the Incognito one, the page'sdocument.cookiemust 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 for127.0.0.1over HTTP, as no test server can beexample.comover HTTPS, soSecureis checked only in the unit tests and the NSHTTPCookie test.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.xvfb-run):TestSetWebCookiesReachesThePagesLoadedAfterin./internal/platform/linuxpassed; withwebkit_cookie_manager_add_cookieskipped, it fails.go tool nsgo check -target macos_arm64,macos_x64,windows_arm64,windows_x64,linux_arm64,linux_x64,android_arm64,web(on Ubuntu, ascheck.ymldoes) and-target ios,ios_simulator,tvos,tvos_simulator,watchos,watchos_simulator(on the Mac).go tool nsgo teston Ubuntu underxvfb-run, astest.ymldoes: 2372 passed, 3 failed, 25 skipped.internal/platform/linuxTestShadersReadImagesandTestTrayServesItsItemAndMenuOverDBusfailed under the full run's load (GL context, a GTK step timing out) and passed when rerun alone;base/terminal/internal/ptyTestClose("the process outlived its terminal") fails the same way onmainon that machine. None of them touch this change.core(TestSetWebCookies*: the store, replacement, refusals, platform errors, Access; the gate tests),internal/driver(TestWebCookieSetCookieAndURL),tsx/pkg(cookiesis never granted),internal/platform/android(TestJavaMessagesMatchTheBackendholds the new message numbers to Java's).TestSetWebCookiesReachesThePagesLoadedAfterandTestHoldsWebCookiesSkipsWhatTheStoreDropsin./internal/platform/windowsare written in the style ofTestWebPagesCaptureWhatMediaAllowsbut not run: no Windows machine was available, and CI runs no Windows tests. The WebView2 code is only vetted for amd64, arm64 and 386.