Skip to content

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

Closed

Description

@houko

Context

An app with an in-app browser often wants its pages signed in as the user already is in their own browser (an app that downloads videos the user can watch, for example: members-only or age-restricted ones need the sign-in). The app can read the user's browser's cookies itself (yt-dlp's --cookies-from-browser, or a cookie-reading library), but it cannot put them into its web views: NSGo has no way to write to the web views' cookie store.

  • Apple: each WKWebView uses WKWebsiteDataStore.defaultDataStore (internal/platform/apple/web.go, the WKWebViewConfiguration it builds); nothing reaches httpCookieStore (setCookie:completionHandler:).
  • Windows: every web view shares one WebView2 profile (internal/platform/windows/web.go:120-131); ICoreWebView2CookieManager (CreateCookie, AddOrUpdateCookie) is never queried.
  • Linux: the WebKitGTK network session's cookie manager is fetched only to set persistent storage (internal/platform/linux/web.go, webkit_cookie_manager_set_persistent_storage); webkit_cookie_manager_add_cookie is not bound.
  • Android: android.webkit.CookieManager.setCookie is not reached.
  • Ref.InjectJavaScript / document.cookie cannot help: the cookies that carry a sign-in are HttpOnly (YouTube's SID, __Secure-3PSID, Bilibili's SESSDATA), which page scripts can neither read nor write.

#112 asks for listing and clearing the store; this is the other half, writing to it.

Why it matters

Without it, an in-app browser always starts signed out, and the user has to sign in again inside it, which some sites refuse in an embedded web view (Google's sign-in rejects many of them). Kando (an app built on NSGo) wants its Browse page to carry the sign-in the user has in Chrome, Edge, Firefox or Safari.

Proposal

A function on the web views' shared store, async because every platform's call is:

// WebCookie is a cookie of the web views' store.
type WebCookie struct {
	Name, Value, Domain, Path string
	Expires                   time.Time // zero: a session cookie
	Secure, HTTPOnly          bool
	SameSite                  http.SameSite
}

// SetWebCookies adds or replaces cookies in the store every web view of the app shares (not an Incognito view's), and calls done on the engine's goroutine.
func SetWebCookies(cx *Scope, cookies []WebCookie, done func(error))

With #112's listing on the same type, WebCookie would serve both. Apple: WKHTTPCookieStore setCookie:completionHandler: per cookie (NSHTTPCookie from properties, including HttpOnly and SameSite). Windows: ICoreWebView2CookieManager.CreateCookie + AddOrUpdateCookie. Linux: webkit_cookie_manager_add_cookie with a SoupCookie. Android: CookieManager.setCookie with the attribute string, then flush.

Acceptance

  • After SetWebCookies with an HttpOnly, Secure cookie for .example.com, a web view loading https://example.com/ sends it (checked by a test server reading the request's Cookie header) on macOS, iOS, Windows, Linux and Android.
  • A cookie with a zero Expires lasts the session; one with an Expires survives a restart of the app (the store is persistent).
  • An Incognito web view does not see the cookies.
  • Writing works before any web view has been made (the store exists on its own).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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