fix(web): validate browser.setCookies cookies before the browser sees them

Merged
KU
1d ago
+255-0

What

  • browser.setCookies checks every cookie against the Cookie type before the URL rule or the browser sees it, and refuses the first bad one with INVALID_ARGUMENT naming 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' is ENGINE_FAILURE ... cookies[0].sameSite: expected one of (Strict|Lax|None), exit 3
    • no url and no domain is Cannot read properties of undefined (reading 'replace')
    • url plus domain plus path passes, but domain and path are 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 Cookie type alone doesn't catch these.

How it works

  1. new packages/web/src/cookies.ts validateCookies: an array with no holes, of objects with only the Cookie keys (unknown keys go through the shared rejectUnknownKeys, so samesite says did you mean sameSite); name / value strings; url / path strings; domain a bare host that parses back as itself in the http://<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.com or a host the parser rewrites (1.2.3, %41.com) is refused while [::1], .example.com, punycode and unicode hosts still work; url or domain (+ optional path) but not both, matching the type's union; expires whole Unix seconds from 0 through the end of 9999 (what playwright takes), or the -1 playwright reads as a session cookie, so a millisecond Date.now() + ... is refused; httpOnly / secure booleans; sameSite one of Strict, Lax, None.
  2. packages/web/src/browser.ts setCookies calls it first, so a refusal is INVALID_ARGUMENT (category test) and not wrapped by surface.guard into ENGINE_FAILURE.
  3. browser.cookies() only returns keys the type allows, so setCookies(await browser.cookies()) still works. there's a test for that round trip.
  4. docs/reference/web.mdx: a throws row under setCookies.

Issue

Fixes #2154

Verified

Ran it locally: yes

the repro on published e2e 0.24.0:

mainthis PR
sameSite: 'lax'ENGINE_FAILURE, exit 3INVALID_ARGUMENT: browser.setCookies cookies[0] sameSite must be "Strict", "Lax", or "None", got "lax"
no url, no domainERROR: Cannot read properties of undefined (reading 'replace')INVALID_ARGUMENT: ... cookies[0] needs a url, or a domain with an optional path
url + domain + pathpasses, 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 httpsINVALID_ARGUMENT: ... cookies[0] domain must be a host name like "example.com" or ".example.com"
expires: Date.now() + 86_400_000ENGINE_FAILURE: ... Cookie should have a valid expires, exit 3INVALID_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/**/*.mdx page for the behavior, and skills/e2e/ if the skill describes it.
  • Contract change: emitted .d.ts reviewed, tests/types/sdk-types.ts updated; wire change edits the schema and both fixtures.
  • Engine contract change (e2e/engine): changesets for e2e, @e2e-dev/web, and @e2e-dev/mobile.