From fecb514e77760f3263b6c59366482de17d58cbb9 Mon Sep 17 00:00:00 2001 From: Raphael Fakhri <153192858+RaphaelFakhri@users.noreply.github.com> Date: Sat, 24 Oct 2026 16:51:49 +0000 Subject: [PATCH] fix(cookies): clear host-only copies of overwritten chunks when domain is set --- src/cookies.spec.ts | 159 ++++++++++++++++++++++++++++++++++++++++++++ src/cookies.ts | 26 ++++++-- 2 files changed, 181 insertions(+), 4 deletions(-) diff --git a/src/cookies.spec.ts b/src/cookies.spec.ts index 70598d9..656d54d 100644 --- a/src/cookies.spec.ts +++ b/src/cookies.spec.ts @@ -1766,6 +1766,35 @@ describe("host-only also-clear when cookieOptions.domain is set", () => { expect(store.get("remove-key")?.domain).toBe(".example.com"); expect(store.get("remove-key.0")?.domain).toBe(".example.com"); }); + + it("keeps the domain-scoped set after a same-name host-only clear", async () => { + const store = new Map< + string, + { value: string; options: CookieOptions } + >(); + + const { storage } = createStorageFromOptions( + { + cookieEncoding: "raw", + cookieOptions: { domain: ".example.com" }, + cookies: { + getAll: async () => [{ name: "storage-key", value: "old" }], + setAll: async (setCookies) => { + // model @edge-runtime/cookies: last write for a name wins + setCookies.forEach(({ name, value, options }) => + store.set(name, { value, options }), + ); + }, + }, + }, + false, + ); + + await storage.setItem("storage-key", "new"); + + expect(store.get("storage-key")?.value).toBe("new"); + expect(store.get("storage-key")?.options.domain).toBe(".example.com"); + }); }); describe("browser client setItem chunk-cleanup", () => { @@ -1809,10 +1838,95 @@ describe("host-only also-clear when cookieOptions.domain is set", () => { // Order: host-only removes, then with-domain removes, then the set. expect(setAllCalls).toEqual([ { name: "storage-key.4", value: "", options: hostOnlyRemove }, + { name: "storage-key", value: "", options: hostOnlyRemove }, { name: "storage-key.4", value: "", options: withDomainRemove }, { name: "storage-key", value: "new-value", options: withDomainSet }, ]); }); + + it("clears host-only copies of the chunks it overwrites when domain is configured", async () => { + // After a host-only -> `.example.com` migration the browser holds both + // copies under the same names. The older host-only one is listed first + // in the Cookie header and shadows the new value until it is cleared. + const setAllCalls: { + name: string; + value: string; + options: CookieOptions; + }[] = []; + + const { storage } = createStorageFromOptions( + { + cookieEncoding: "raw", + cookieOptions: { domain: ".example.com" }, + cookies: { + getAll: async () => [ + { name: "storage-key.0", value: "old-chunk-0" }, + { name: "storage-key.1", value: "old-chunk-1" }, + ], + setAll: async (setCookies) => { + setAllCalls.push(...setCookies); + }, + }, + }, + false, + ); + + const value = "a".repeat(MAX_CHUNK_SIZE + 10); + await storage.setItem("storage-key", value); + + const hostOnlyRemove = { ...DEFAULT_COOKIE_OPTIONS, maxAge: 0 }; + const withDomainSet = { + ...DEFAULT_COOKIE_OPTIONS, + domain: ".example.com", + }; + + expect(setAllCalls).toEqual([ + { name: "storage-key.0", value: "", options: hostOnlyRemove }, + { name: "storage-key.1", value: "", options: hostOnlyRemove }, + { + name: "storage-key.0", + value: value.substring(0, MAX_CHUNK_SIZE), + options: withDomainSet, + }, + { + name: "storage-key.1", + value: value.substring(MAX_CHUNK_SIZE), + options: withDomainSet, + }, + ]); + }); + + it("does not emit host-only removes for new cookies when domain is configured", async () => { + const setAllCalls: { + name: string; + value: string; + options: CookieOptions; + }[] = []; + + const { storage } = createStorageFromOptions( + { + cookieEncoding: "raw", + cookieOptions: { domain: ".example.com" }, + cookies: { + getAll: async () => [], + setAll: async (setCookies) => { + setAllCalls.push(...setCookies); + }, + }, + }, + false, + ); + + await storage.setItem("storage-key", "new-value"); + + expect(setAllCalls).toEqual([ + { + name: "storage-key", + value: "new-value", + options: { ...DEFAULT_COOKIE_OPTIONS, domain: ".example.com" }, + }, + ]); + }); }); describe("applyServerStorage", () => { @@ -1865,6 +1979,51 @@ describe("host-only also-clear when cookieOptions.domain is set", () => { ]); }); + it("clears host-only copies of the chunks it overwrites when domain is configured", async () => { + const setAllCalls: { + name: string; + value: string; + options: CookieOptions; + }[] = []; + + const { storage, getAll, setAll, setItems, removedItems } = + createStorageFromOptions( + { + cookieEncoding: "raw", + cookieOptions: { domain: ".example.com" }, + cookies: { + getAll: async () => [{ name: "set-key", value: "old" }], + setAll: async (setCookies) => { + setAllCalls.push(...setCookies); + }, + }, + }, + true, + ); + + await storage.setItem("set-key", "new"); + await applyServerStorage( + { getAll, setAll, setItems, removedItems }, + { + cookieEncoding: "raw", + cookieOptions: { domain: ".example.com" }, + }, + ); + + expect(setAllCalls).toEqual([ + { + name: "set-key", + value: "", + options: { ...DEFAULT_COOKIE_OPTIONS, maxAge: 0 }, + }, + { + name: "set-key", + value: "new", + options: { ...DEFAULT_COOKIE_OPTIONS, domain: ".example.com" }, + }, + ]); + }); + it("does not emit host-only removes when no domain is configured (baseline)", async () => { const setAllCalls: { name: string; diff --git a/src/cookies.ts b/src/cookies.ts index c105c2e..f1f8c64 100644 --- a/src/cookies.ts +++ b/src/cookies.ts @@ -317,6 +317,11 @@ export function createStorageFromOptions( // See removeItem below for the host-only also-clear rationale. // Same logic applies here: when overwriting an existing session, // stale chunks at a previous scope must be cleared at that scope. + // This includes the chunks being written now: a host-only cookie + // with the same name is a different cookie, and as the older one + // it is listed first in the Cookie header and shadows the new + // value. The host-only clears come before the sets, so cookie + // stores keyed by name only keep the set. const hostOnlyRemoveOptions = removeCookieOptions.domain ? (() => { const { domain: _domain, ...rest } = removeCookieOptions; @@ -326,7 +331,12 @@ export function createStorageFromOptions( const allToSet = [ ...(hostOnlyRemoveOptions - ? [...removeCookies].map((name) => ({ + ? [ + ...removeCookies, + ...setCookies + .map(({ name }) => name) + .filter((name) => cookieNames.includes(name)), + ].map((name) => ({ name, value: "", options: hostOnlyRemoveOptions, @@ -629,9 +639,17 @@ export async function applyServerStorage( delete (setCookieOptions as any).name; // See removeItem in createStorageFromOptions for the host-only also-clear - // rationale. Same logic on the server-side response path. + // rationale. Same logic on the server-side response path, including the + // existing chunks being overwritten (see setItem in + // createStorageFromOptions). + const hostOnlyRemoveNames = [ + ...removeCookiesToWrite, + ...setCookiesToWrite + .map(({ name }) => name) + .filter((name) => currentByName.has(name)), + ]; const hostOnlyRemoveOptions = - removeCookieOptions.domain && removeCookiesToWrite.length > 0 + removeCookieOptions.domain && hostOnlyRemoveNames.length > 0 ? (() => { const { domain: _domain, ...rest } = removeCookieOptions; return rest; @@ -645,7 +663,7 @@ export async function applyServerStorage( await setAll( [ ...(hostOnlyRemoveOptions - ? removeCookiesToWrite.map((name) => ({ + ? hostOnlyRemoveNames.map((name) => ({ name, value: "", options: hostOnlyRemoveOptions,