fix(web): validate browser.setCookies cookies before the browser sees them
What
browser.setCookieschecks every cookie against theCookietype before the URL rule or the browser sees it, and refuses the first bad one withINVALID_ARGUMENTnaming the index and the field. nothing is set when any cookie is refused.- the docs already said it "validates every cookie, then sets them", this makes that true.
Why
- on main the cookies go to playwright unchecked:
sameSite: 'lax'isENGINE_FAILURE ... cookies[0].sameSite: expected one of (Strict|Lax|None), exit 3- no
urland nodomainisCannot read properties of undefined (reading 'replace') urlplusdomainpluspathpasses, butdomainandpathare dropped, so a session cookie lands on a different scope than the one written and the test runs logged out
- tests can be plain js and fixture data isn't typed, so the
Cookietype alone doesn't catch these.
How it works
- new
packages/web/src/cookies.tsvalidateCookies: an array with no holes, of objects with only theCookiekeys (unknown keys go through the sharedrejectUnknownKeys, sosamesitesays did you meansameSite);name/valuestrings;url/pathstrings;domaina bare host that parses back as itself in thehttp://<domain>origin the URL rule checks, changed at most in case or to punycode and with no empty label, so a scheme, port, path, space,a..b.comor a host the parser rewrites (1.2.3,%41.com) is refused while[::1],.example.com, punycode and unicode hosts still work;urlordomain(+ optionalpath) but not both, matching the type's union;expireswhole Unix seconds from 0 through the end of 9999 (what playwright takes), or the-1playwright reads as a session cookie, so a millisecondDate.now() + ...is refused;httpOnly/securebooleans;sameSiteone ofStrict,Lax,None. packages/web/src/browser.tssetCookiescalls it first, so a refusal isINVALID_ARGUMENT(categorytest) and not wrapped bysurface.guardintoENGINE_FAILURE.browser.cookies()only returns keys the type allows, sosetCookies(await browser.cookies())still works. there's a test for that round trip.docs/reference/web.mdx: a throws row undersetCookies.
Issue
Fixes #2154
Verified
Ran it locally: yes
the repro on published e2e 0.24.0:
| main | this PR | |
|---|---|---|
sameSite: 'lax' | ENGINE_FAILURE, exit 3 | INVALID_ARGUMENT: browser.setCookies cookies[0] sameSite must be "Strict", "Lax", or "None", got "lax" |
no url, no domain | ERROR: Cannot read properties of undefined (reading 'replace') | INVALID_ARGUMENT: ... cookies[0] needs a url, or a domain with an optional path |
url + domain + path | passes, stored as domain 127.0.0.1 path / | INVALID_ARGUMENT: ... cookies[0] takes url, or domain with an optional path, not both |
domain: 'https://127.0.0.1' | passes, stored as domain https | INVALID_ARGUMENT: ... cookies[0] domain must be a host name like "example.com" or ".example.com" |
expires: Date.now() + 86_400_000 | ENGINE_FAILURE: ... Cookie should have a valid expires, exit 3 | INVALID_ARGUMENT: ... cookies[0] expires must be a Unix timestamp in seconds (not milliseconds), got ... |
tests/integration/set-cookies.test.ts runs a real browser: every valid shape the type allows is stored as written, a leading-dot, uppercase, punycode, unicode and IPv6 domain, expires: -1 and the year 9999 limit, the cookies() round trip, and 26 refused inputs (not an array, null / array entries, a hole, no target, url+domain, url+path, an empty domain or one with a scheme / path / port / space or one the parser rewrites or with an empty label, missing name, number value, string / NaN / millisecond / negative / past-9999 expires, string httpOnly, lowercase sameSite, unknown and misspelled keys, a bad second cookie), each checking no cookie was set. the refused ones fail on main. mutants (no -1, exclusive max, no lower or upper bound, no rewrite check, no punycode allowance) each fail at least one test.
pnpm --filter @e2e-dev/web test: the failures are the same as on main for me on Windows (install.test.ts, the SIGTERM cli test, and provider-downloads which is timing and fails on main too). pnpm check passes.
Checklist
- Changeset added (
pnpm changeset) for any change to a published package, or the change is testbed, docs, or CI only. - Breaking change: title carries
!, the changeset body names what breaks and what replaces it, and the deprecation warning landed a window earlier (stability policy in CONTRIBUTING.md). - Docs updated in the same PR: the
docs/**/*.mdxpage for the behavior, andskills/e2e/if the skill describes it. - Contract change: emitted
.d.tsreviewed,tests/types/sdk-types.tsupdated; wire change edits the schema and both fixtures. - Engine contract change (
e2e/engine): changesets fore2e,@e2e-dev/web, and@e2e-dev/mobile.