Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 159 additions & 0 deletions src/cookies.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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;
Expand Down
26 changes: 22 additions & 4 deletions src/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -645,7 +663,7 @@ export async function applyServerStorage(
await setAll(
[
...(hostOnlyRemoveOptions
? removeCookiesToWrite.map((name) => ({
? hostOnlyRemoveNames.map((name) => ({
name,
value: "",
options: hostOnlyRemoveOptions,
Expand Down