Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(cookies): clearCookies({ name: '' }) should only remove nameless cookies (#43129)

Pavel Feldman25cb5954f27c
diff --git a/packages/playwright-core/src/server/browserContext.ts b/packages/playwright-core/src/server/browserContext.ts
index c430b3d02b..e43f2348a6 100644
--- a/packages/playwright-core/src/server/browserContext.ts
+++ b/packages/playwright-core/src/server/browserContext.ts
@@ -322,9 +322,9 @@ export abstract class BrowserContext<EM extends EventMap = EventMap> extends Sdk
if (!cookiesToExpire.length)
return;
+ // Keep the original value: browsers reject or ignore cookies with both empty name and value.
await this.addCookies(cookiesToExpire.map(cookie => ({
...cookie,
- value: '',
expires: 0,
})));
}
diff --git a/packages/playwright-core/src/server/network.ts b/packages/playwright-core/src/server/network.ts
index 20bea1ec43..1d68e2b5b8 100644
--- a/packages/playwright-core/src/server/network.ts
+++ b/packages/playwright-core/src/server/network.ts
@@ -127,7 +127,7 @@ export function hasClearCookiesFilter(options: ClearCookiesOptions): boolean {
export function cookieMatchesClearFilter(cookie: channels.NetworkCookie, options: ClearCookiesOptions): boolean {
const matches = (prop: 'name' | 'domain' | 'path', value: string | RegExp | undefined) => {
- if (!value)
+ if (value === undefined)
return true;
if (value instanceof RegExp) {
value.lastIndex = 0;
diff --git a/tests/library/browsercontext-clearcookies.spec.ts b/tests/library/browsercontext-clearcookies.spec.ts
index bcb17a7ea3..72cd834ec6 100644
--- a/tests/library/browsercontext-clearcookies.spec.ts
+++ b/tests/library/browsercontext-clearcookies.spec.ts
@@ -239,3 +239,14 @@ it('should remove partitioned cookies by name', {
]);
await context.close();
});
+
+it('should remove nameless cookies by empty name', {
+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43112' },
+}, async ({ context }) => {
+ await context.addCookies([
+ { name: '', value: 'nameless', domain: 'example.com', path: '/' },
+ { name: 'session', value: 'secret', domain: 'example.com', path: '/' },
+ ]);
+ await context.clearCookies({ name: '' });
+ expect((await context.cookies()).map(cookie => cookie.name)).toEqual(['session']);
+});
diff --git a/tests/library/global-fetch-cookie.spec.ts b/tests/library/global-fetch-cookie.spec.ts
index 149dc48a47..9fad71ad11 100644
--- a/tests/library/global-fetch-cookie.spec.ts
+++ b/tests/library/global-fetch-cookie.spec.ts
@@ -140,6 +140,17 @@ it('clearCookies should filter by name, domain and path', async ({ request }) =>
expect(await values()).toEqual([]);
});
+it('clearCookies should remove nameless cookies by empty name', {
+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43112' },
+}, async ({ request }) => {
+ await request.addCookies([
+ { name: '', value: 'nameless', domain: 'one.com', path: '/' },
+ { name: 'session', value: 'secret', domain: 'one.com', path: '/' },
+ ]);
+ await request.clearCookies({ name: '' });
+ expect((await request.cookies()).map(c => c.name)).toEqual(['session']);
+});
+
it('should filter outgoing cookies by path', async ({ request, server }) => {
server.setRoute('/setcookie.html', (req, res) => {
res.setHeader('Set-Cookie', ['a=v; path=/input/subfolder', 'b=v; path=/input', 'c=v;']);