You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
▚▚ Python SDK: clear_cookies with a compiled regex fails or is silently ignored (flags sent as null, unsupported flags dropped, (?P<name>) groups) #3270
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.
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.
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
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.
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.
Activity
HhH666-ux commentedon Nov 10, 2026
Hi — I'd like to take this one.
Plan: fix
_cookie_filterso a compiled pattern is always sent as a validCookieRegex— drop the explicitflags: None(leave the field unset) instead of serialisingnull, 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.