Repository navigation
Description
Version
1.56.1 (verified against main at v1.60.0 — implementation unchanged)
Steps to reproduce
browserContext.clearCookies(options) is documented and named as a targeted operation, but its implementation in packages/playwright-core/src/server/browserContext.ts:311-332 (permalink) is wipe-and-readd:
async clearCookies(options) {
const currentCookies = await this._cookies();
await this.doClearCookies(); // wipes EVERY cookie
const cookiesToReadd = currentCookies.filter(/* exclude matches */);
await this.addCookies(cookiesToReadd); // re-adds non-matching
}This is observable from the page via the cookieStore.change API. Minimal repro test (no external repo needed):
import { test, expect } from "@playwright/test";
test("clearCookies({name}) should not transiently delete non-matching cookies", async ({
page,
context,
}) => {
await page.goto("https://example.com");
await context.addCookies([
{
name: "keep_me",
value: "1",
url: "https://example.com",
sameSite: "Lax",
},
{
name: "delete_me",
value: "1",
url: "https://example.com",
sameSite: "Lax",
},
]);
const events: { changed: string[]; deleted: string[] }[] = [];
await page.exposeFunction("onCookieChange", (ev: (typeof events)[number]) => {
events.push(ev);
});
await page.evaluate(() => {
// @ts-expect-error cookieStore is a global in modern browsers
cookieStore.addEventListener("change", (ev) => {
// @ts-expect-error
(window as any).onCookieChange({
// @ts-expect-error
changed: ev.changed.map((c) => c.name),
// @ts-expect-error
deleted: ev.deleted.map((c) => c.name),
});
});
});
await context.clearCookies({ name: "delete_me" });
// Wait a tick so all change events flush
await page.waitForTimeout(200);
// Expected: a single deletion event for `delete_me`, nothing about `keep_me`
const keepMeWasDeleted = events.some((e) => e.deleted.includes("keep_me"));
expect(
keepMeWasDeleted,
"keep_me should never appear in a deletion event",
).toBe(false);
});Expected behavior
context.clearCookies({ name: 'delete_me' }) should delete only the delete_me cookie. The page should observe a single cookieStore.change event with deleted: [{ name: 'delete_me' }]. No other cookies should appear in any deleted array.
Actual behavior
keep_me is briefly deleted (and observable as such via cookieStore.change) before being re-added. Apps that subscribe to cookie changes — including useSyncExternalStore-style auth state machines — see a transient state where keep_me is absent.
Additional context
This surfaced as a flaky e2e test that occasionally bounced through /auth/login when only the auth token was supposed to be cleared. A separate session-cookie was being briefly deleted by the wipe step, observed by a React auth state machine subscribing to cookieStore.change, which fired a route-guard redirect before the readd completed. ~22ms transient window observed in CI logs.
Historical context
- [Feature] - Delete a certain cookie instead of clear all cookies #742 (Jan 2020) — original feature request to delete a single cookie
- [Feature] Cookie Removal Api #10143 (Nov 2021) — re-request, explicitly describing the user-land workaround (read all → clear → re-add)
- chore: move filter params into the clearCookies #30111 (Mar 2024) — moved filter params into the
clearCookiesoptions bag (current API shape)
The implementation simply hoisted the user-land workaround into the library. That race window wasn't widely observable through any web API at the time, but cookieStore is now Baseline 2025 across all modern engines — and apps that subscribe to cookie changes will react to the transient deletions.
Proposed fix
The wipe-and-readd is only required when the underlying browser protocol doesn't support filtered deletion. But even without protocol changes, the same end state can be achieved by expiring the matching cookies in place — addCookies with expires: 0 deletes targeted cookies without touching others:
async clearCookies(options) {
const hasFilter = options.name || options.domain || options.path;
if (!hasFilter) {
return this.doClearCookies();
}
const currentCookies = await this._cookies();
const toExpire = currentCookies.filter(
(cookie) =>
matches(cookie, 'name', options.name) &&
matches(cookie, 'domain', options.domain) &&
matches(cookie, 'path', options.path),
);
if (!toExpire.length) return;
await this.addCookies(
toExpire.map((c) => ({
name: c.name,
value: '',
domain: c.domain,
path: c.path,
expires: 0, // past = delete
})),
);
}~10-line diff, no per-browser code touched, preserves the no-filter wipe path for backward compat. Happy to open a PR if maintainers signal interest in this shape.
Activity