Skip to content

[Bug]: clearCookies({ name: "" }) unintentionally clears all cookies instead of only nameless cookies #43112

Closed

Description

@alok-108

Version

main @ b630e71 (1.64.0-next); also affects 1.63.0

Steps to reproduce

import { test, expect } from '@playwright/test';

test('clearCookies({ name: "" }) should only clear unnamed cookies', async ({ context }) => {
  // A nameless cookie (RFC 6265 §4.1.1, e.g. from Set-Cookie: =nameless; Path=/)
  // and a regular named cookie
  await context.addCookies([
    { name: '', value: 'nameless', domain: 'example.com', path: '/' },
    { name: 'session', value: 'secret', domain: 'example.com', path: '/' },
  ]);

  // Attempt to delete only the nameless cookie
  await context.clearCookies({ name: '' });

  const cookies = await context.cookies();
  expect(cookies.map(c => c.name)).toEqual(['session']);
});

Run with npx playwright test.

(The exact same issue also reproduces with APIRequestContext.clearCookies({ name: '' })).

Expected behavior

Only the cookie with name: "" is deleted. The named cookie ('session') remains in the context.

Actual behavior

All cookies in the context are deleted (cookies is []):

  Error: expect(received).toEqual(expected) // deep equality

  - Expected  - 3
  + Received  + 1

  - Array [
  -   "session",
  - ]
  + Array []

Additional context

In packages/playwright-core/src/server/network.ts:

export function cookieMatchesClearFilter(cookie: channels.NetworkCookie, options: ClearCookiesOptions): boolean {
  const matches = (prop: 'name' | 'domain' | 'path', value: string | RegExp | undefined) => {
    if (!value)
      return true;
    if (value instanceof RegExp) {
      value.lastIndex = 0;
      return value.test(cookie[prop]);
    }
    return cookie[prop] === value;
  };
  return matches('name', options.name) && matches('domain', options.domain) && matches('path', options.path);
}

Because matches uses if (!value) return true;, passing empty string "" (name: "") is treated as falsy and returns true for all cookies regardless of cookie.name.

Since hasClearCookiesFilter({ name: '' }) is true (options.name !== undefined), Playwright enters the filtering path instead of doClearCookies(), but because cookieMatchesClearFilter returns true for every cookie, all cookies are expired and deleted.

The check should be if (value === undefined) return true; (the same applies to domain and path).

Environment

Not environment-specific: reproduces across Chromium, Firefox, and WebKit on Windows, macOS, and Linux.
No browser launch needed when reproduced via APIRequestContext or unit test.

Activity

  1. alok-108 commented on Oct 4, 2026

    @alok-108
    ContributorAuthor

    Thanks for addressing #43112 so quickly, Lazizbek Ergashev (@lazerg)! Looks great to me.

  2. github-actions commented on Oct 5, 2026

    @github-actions
    Contributor

    Hi, I'm the Playwright bot and I took a first look.

    Reproduced on 1.64.0-next at a18dade00769, but the result is browser-specific and #43114 is only a partial fix. APIRequestContext.clearCookies({ name: '' }) matches the report exactly and removes both cookies. The same BrowserContext repro does not preserve the named cookie on any engine, but fails differently:

    • Chromium: throws Protocol error (Storage.setCookies): Invalid cookie fields.
    • Firefox: removes session and leaves the nameless cookie.
    • WebKit: removes both cookies.

    Applying #43114's value === undefined change makes the API-request and WebKit cases pass. Chromium still throws the protocol error, while Firefox leaves both cookies in place. The predicate change is correct, but it does not fully fix BrowserContext.clearCookies({ name: '' }).

    Minimal repro
    import { contextTest as test, expect } from '../config/browserTest';
    
    test('should remove an unnamed cookie 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']);
    });
    What I ran

    Triaged on Linux by the Playwright bot - agent run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions