Skip to content

▚▚ Python SDK: clear_cookies with a compiled regex fails or is silently ignored (flags sent as null, unsupported flags dropped, (?P<name>) groups) #3270

Open

Description

@breken-ai

BrowserContext.clear_cookies() in the Python SDK fails when it is given a compiled regular expression. Depending on the pattern, it either raises an error or silently leaves the cookie in place. Plain string filters work.

Seen on main at 901eb2764 (packages/sdk-python/src/stagehand/browser_context.py), with a real Chromium. Each case below was run 3 times with the same result, and each has a control that works.

1. Any compiled pattern without i, m or s fails

import re
await context.clear_cookies(name=re.compile("session"))

Expected: cookies whose name matches session are cleared.
Actual: the call raises an RPC error and the cookie stays.

Controls (both clear the cookie, 3 of 3):

  • clear_cookies(name="session")
  • clear_cookies(name=re.compile("session", re.I))

Cause: _cookie_filter builds CookieRegex(source=..., flags=flags or None). When no supported flag is set, flags is explicitly None. RpcClient.send serialises params with exclude_unset=True, so an explicitly set None goes over the wire as "flags": null. CookieRegexSchema in packages/protocol/schemas.ts declares flags as z.string().regex(...).optional(). That accepts a missing value but not null, so the request is rejected.

Most compiled patterns in practice have no i/m/s flag, so this is the common case.

2. Unsupported flags are dropped silently

await context.clear_cookies(name=re.compile(r" session  # cookie ", re.I | re.VERBOSE))

Expected: this either behaves like the Python pattern (VERBOSE ignores the whitespace and the comment) or is rejected.
Actual: no error is raised, and the cookie silently stays.

_cookie_filter only maps re.IGNORECASE, re.MULTILINE and re.DOTALL. Every other flag, such as re.VERBOSE or re.ASCII, is dropped without a word. The pattern is then matched literally (spaces and comment included) and matches nothing.

3. Python-only regex syntax is passed through as is

await context.clear_cookies(name=re.compile(r"(?P<kind>session)", re.I))

Expected: this works, or there is a clear error explaining that the syntax isn't supported.
Actual: "Invalid cookie filter regular expression", and the cookie stays.

The pattern source is sent unchanged to a JavaScript RegExp, and Python's (?P<name>...) group syntax is not valid there.

Suggested fix

  • Omit flags when it is empty instead of sending None, e.g. only pass flags= when it is non-empty, or serialise with exclude_none.
  • Raise a clear ValueError for Python flags that have no JavaScript equivalent (re.VERBOSE, re.ASCII, re.LOCALE, …) instead of dropping them.
  • Either translate (?P<name> to (?<name> and (?P=name) to \k<name>, or reject them with a clear message.
  • A test covering re.compile("x"), with no flags, would have caught case 1.

with love, breken.ai

Activity

  1. HhH666-ux commented on Nov 10, 2026

    @HhH666-ux

    Hi — I'd like to take this one.

    Plan: fix _cookie_filter so a compiled pattern is always sent as a valid CookieRegex — drop the explicit flags: None (leave the field unset) instead of serialising null, translate only supported regex flags and reject/annotate unsupported ones instead of silently dropping them. Regression tests for a plain compiled pattern, re.I, and an unsupported-flag pattern.

    PR to follow.

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