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
This commit is contained in:
@@ -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')
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user