From a20165a43db63046d2aedd126ac768a14289d635 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Sun, 2 Aug 2026 18:53:22 -0700 Subject: [PATCH] fix(browser): import __Host- cookies host-only so Chromium keeps them (#12166) * fix(browser): import __Host- cookies host-only so Chromium keeps them The cookie file/JSON import path passed a Domain attribute for every cookie. Chromium rejects any __Host--prefixed cookie that carries a Domain (the prefix requires host-only, path=/, Secure), so file import silently dropped session cookies like GitHub's __Host-user_session_same_site and users stayed logged out after importing. Mirror the browser-native import path, which already shapes __Host- cookies host-only: omit domain and force path=/ when the name is __Host--prefixed. Add a regression test covering both a __Host- cookie and a normal domain cookie in one import. * test(browser): cover __Host- cookie payload constraints * test(browser): preserve ordinary cookie paths --- .../browser/browser-cookie-import.test.ts | 29 +++++++++++++++++++ src/main/browser/browser-cookie-import.ts | 6 ++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/main/browser/browser-cookie-import.test.ts b/src/main/browser/browser-cookie-import.test.ts index 55fae27c9..657215a6f 100644 --- a/src/main/browser/browser-cookie-import.test.ts +++ b/src/main/browser/browser-cookie-import.test.ts @@ -223,6 +223,35 @@ describe('importCookiesFromFile', () => { expect(firstCall.sameSite).toBe('lax') }) + it('sets __Host- cookies host-only so Chromium does not reject them', async () => { + const filePath = writeCookieFile([ + { + domain: 'github.com', + name: '__Host-user_session_same_site', + value: 'sess', + path: '/account', + secure: true, + httpOnly: true, + sameSite: 'lax' + }, + { domain: '.github.com', name: '_gh_sess', value: 'abc', path: '/settings', secure: true } + ]) + + const result = await importCookiesFromFile(filePath, 'persist:test') + expect(result.ok).toBe(true) + + const hostCall = cookiesSetMock.mock.calls + .map((c) => c[0]) + .find((c) => c.name === '__Host-user_session_same_site') + // __Host- prefix requires no Domain attribute and path=/, or Chromium drops it. + expect(hostCall).not.toHaveProperty('domain') + expect(hostCall.path).toBe('/') + + const normalCall = cookiesSetMock.mock.calls.map((c) => c[0]).find((c) => c.name === '_gh_sess') + expect(normalCall.domain).toBe('.github.com') + expect(normalCall.path).toBe('/settings') + }) + it('rejects non-JSON files', async () => { const filePath = join(tmpDir, 'bad.json') writeFileSync(filePath, 'not json at all') diff --git a/src/main/browser/browser-cookie-import.ts b/src/main/browser/browser-cookie-import.ts index 22b891cd2..23bdd2487 100644 --- a/src/main/browser/browser-cookie-import.ts +++ b/src/main/browser/browser-cookie-import.ts @@ -567,12 +567,14 @@ async function importValidatedCookies( for (const cookie of cookies) { try { + // Why: Chromium rejects __Host- cookies unless they omit domain and use path=/. + const isHostPrefixed = cookie.name.startsWith('__Host-') await targetSession.cookies.set({ url: cookie.url, name: cookie.name, value: stripNonPrintable(cookie.value), - domain: cookie.domain, - path: cookie.path, + ...(isHostPrefixed ? {} : { domain: cookie.domain }), + path: isHostPrefixed ? '/' : cookie.path, secure: cookie.secure, httpOnly: cookie.httpOnly, sameSite: cookie.sameSite,