Repository navigation
fix(web): validate browser.setCookies cookies before the browser sees them #2159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
2e2ff31
7422ee0
5dbadf3
079ad54
c3129f8
ffcb393
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@e2e-dev/web': patch | ||
| --- | ||
|
|
||
| `browser.setCookies` checks each cookie against the `Cookie` type before the browser sees it, and refuses one it does not allow with `INVALID_ARGUMENT` naming the field. |
| Original file line number | Diff line number | Diff line change | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,88 @@ | |||||||||||||||||
| /** | |||||||||||||||||
| * The cookies `browser.setCookies` takes, checked before the URL rule or the | |||||||||||||||||
| * browser sees them. Tests run as JavaScript, so the `Cookie` type alone is no | |||||||||||||||||
| * guard, and a cookie the browser would refuse is the test's mistake, not the | |||||||||||||||||
| * engine's. | |||||||||||||||||
| */ | |||||||||||||||||
|
|
|||||||||||||||||
| import { domainToUnicode } from 'node:url'; | |||||||||||||||||
| import { rejectUnknownKeys, TestError } from '@e2e-dev/core/engine'; | |||||||||||||||||
| import type { Cookie } from './browser.ts'; | |||||||||||||||||
| import { describe } from './support.ts'; | |||||||||||||||||
|
|
|||||||||||||||||
| const COOKIE_KEYS = ['name', 'value', 'url', 'domain', 'path', 'expires', 'httpOnly', 'secure', 'sameSite'] as const; | |||||||||||||||||
| const SAME_SITE = ['Strict', 'Lax', 'None'] as const; | |||||||||||||||||
| /** The latest `expires` Playwright takes: the end of the year 9999, in seconds. */ | |||||||||||||||||
| const MAX_EXPIRES = 253_402_300_799; | |||||||||||||||||
|
|
|||||||||||||||||
| /** Throws `INVALID_ARGUMENT` naming the first field the `Cookie` type does not allow. */ | |||||||||||||||||
| export function validateCookies(cookies: unknown): asserts cookies is readonly Cookie[] { | |||||||||||||||||
| if (!Array.isArray(cookies)) { | |||||||||||||||||
| throw new TestError('INVALID_ARGUMENT', `browser.setCookies takes an array of cookies, got ${describe(cookies)}`); | |||||||||||||||||
| } | |||||||||||||||||
| for (const [index, cookie] of cookies.entries()) { | |||||||||||||||||
| const label = `browser.setCookies cookies[${index}]`; | |||||||||||||||||
| if (typeof cookie !== 'object' || cookie === null || Array.isArray(cookie)) { | |||||||||||||||||
| throw new TestError('INVALID_ARGUMENT', `${label} must be an object, got ${describe(cookie)}`); | |||||||||||||||||
| } | |||||||||||||||||
| rejectUnknownKeys(label, cookie, COOKIE_KEYS, 'INVALID_ARGUMENT'); | |||||||||||||||||
| const problem = cookieProblem(cookie); | |||||||||||||||||
| if (problem !== undefined) throw new TestError('INVALID_ARGUMENT', `${label} ${problem}`); | |||||||||||||||||
| } | |||||||||||||||||
| } | |||||||||||||||||
|
|
|||||||||||||||||
| function cookieProblem(cookie: object): string | undefined { | |||||||||||||||||
| const field = (key: (typeof COOKIE_KEYS)[number]): unknown => Reflect.get(cookie, key); | |||||||||||||||||
| for (const key of ['name', 'value'] as const) { | |||||||||||||||||
| if (typeof field(key) !== 'string') return `${key} must be a string, got ${describe(field(key))}`; | |||||||||||||||||
| } | |||||||||||||||||
| for (const key of ['url', 'path'] as const) { | |||||||||||||||||
| if (field(key) !== undefined && typeof field(key) !== 'string') return `${key} must be a string, got ${describe(field(key))}`; | |||||||||||||||||
| } | |||||||||||||||||
| const domain = field('domain'); | |||||||||||||||||
| if (domain !== undefined && (typeof domain !== 'string' || !isHostName(domain))) { | |||||||||||||||||
| return `domain must be a host name like "example.com" or ".example.com", got ${describe(domain)}`; | |||||||||||||||||
| } | |||||||||||||||||
| if (field('url') !== undefined && (domain !== undefined || field('path') !== undefined)) { | |||||||||||||||||
| return 'takes url, or domain with an optional path, not both'; | |||||||||||||||||
| } | |||||||||||||||||
| if (field('url') === undefined && domain === undefined) return 'needs a url, or a domain with an optional path'; | |||||||||||||||||
| const expires = field('expires'); | |||||||||||||||||
| if (expires !== undefined && !isExpiry(expires)) { | |||||||||||||||||
| return `expires must be a Unix timestamp in seconds (not milliseconds), got ${describe(expires)}`; | |||||||||||||||||
| } | |||||||||||||||||
| for (const key of ['httpOnly', 'secure'] as const) { | |||||||||||||||||
| if (field(key) !== undefined && typeof field(key) !== 'boolean') return `${key} must be a boolean, got ${describe(field(key))}`; | |||||||||||||||||
| } | |||||||||||||||||
| const sameSite = field('sameSite'); | |||||||||||||||||
| if (sameSite !== undefined && !(SAME_SITE as readonly unknown[]).includes(sameSite)) { | |||||||||||||||||
| return `sameSite must be "Strict", "Lax", or "None", got ${describe(sameSite)}`; | |||||||||||||||||
| } | |||||||||||||||||
| return undefined; | |||||||||||||||||
| } | |||||||||||||||||
|
|
|||||||||||||||||
| /** Seconds from 0 through the end of the year 9999, or the `-1` Playwright reads as a session cookie. */ | |||||||||||||||||
| function isExpiry(expires: unknown): boolean { | |||||||||||||||||
| return typeof expires === 'number' && (expires === -1 || (expires >= 0 && expires <= MAX_EXPIRES)); | |||||||||||||||||
|
kunalgoesbyken marked this conversation as resolved.
Resolved
|
|||||||||||||||||
| } | |||||||||||||||||
|
|
|||||||||||||||||
| /** | |||||||||||||||||
| * Whether `domain`, less a leading dot, parses back as only that host in the | |||||||||||||||||
| * origin `setCookies` checks it under, changed at most in case or to punycode, | |||||||||||||||||
| * with no empty label but a trailing dot's. A scheme, port, path, or | |||||||||||||||||
| * whitespace would be dropped, and a host the parser | |||||||||||||||||
| * rewrites (`1.2.3` to `1.2.0.3`, `%41.com` to `a.com`) would put the URL rule | |||||||||||||||||
| * on another host than the cookie's. | |||||||||||||||||
| */ | |||||||||||||||||
| function isHostName(domain: string): boolean { | |||||||||||||||||
| const host = domain.replace(/^\./, ''); | |||||||||||||||||
| if (!URL.canParse(`http://${host}`)) return false; | |||||||||||||||||
| const url = new URL(`http://${host}`); | |||||||||||||||||
| const written = host.toLowerCase(); | |||||||||||||||||
| const labels = url.hostname.replace(/\.$/, '').split('.'); | |||||||||||||||||
| return ( | |||||||||||||||||
| url.href === `http://${url.hostname}/` && | |||||||||||||||||
| !labels.includes('') && | |||||||||||||||||
| (url.hostname === written || domainToUnicode(url.hostname) === written) | |||||||||||||||||
| ); | |||||||||||||||||
| } | |||||||||||||||||
|
Comment on lines
+69
to
+86
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3 · Non-blocking · The domain check refuses host spellings that main stored correctly, and its comment gives a wrong reason Optional. This does not block the merge.
The last two rows are not consistent: the head accepts a trailing dot on a DNS name but refuses one on an IPv4 address. The The doc comment (lines 69-75) says that a rewritten host "would put the URL rule on another host than the cookie's". The URL rule in These inputs are rare, and the error message is clear, so the impact is small. |
|||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| /** | ||
| * `browser.setCookies` against a real browser context: every shape the | ||
| * `Cookie` type allows lands as written, and anything else is | ||
| * `INVALID_ARGUMENT` naming the field, before the browser is asked, so a | ||
| * test's cookie mistake is not reported as an engine failure. | ||
| */ | ||
|
|
||
| import { mkdtempSync, rmSync } from 'node:fs'; | ||
| import { createServer, type Server } from 'node:http'; | ||
| import type { AddressInfo } from 'node:net'; | ||
| import { tmpdir } from 'node:os'; | ||
| import path from 'node:path'; | ||
| import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest'; | ||
| import type { EngineFixtureContext } from '@e2e-dev/core/engine'; | ||
| import { PlaywrightSurface } from '../../src/surface.ts'; | ||
| import { createBrowserFixture, type Browser, type Cookie } from '../../src/browser.ts'; | ||
| import { ignoreTrace, noSecrets } from '../helpers/secrets.ts'; | ||
|
|
||
| /** Answers every path with an empty page. */ | ||
| function startServer(): Promise<{ server: Server; port: number }> { | ||
| const server = createServer((_request, response) => { | ||
| response.writeHead(200, { 'content-type': 'text/html' }); | ||
| response.end('<!doctype html><title>Cookies</title>'); | ||
| }); | ||
| return new Promise((resolve) => { | ||
| server.listen(0, '127.0.0.1', () => resolve({ server, port: (server.address() as AddressInfo).port })); | ||
| }); | ||
| } | ||
|
|
||
| describe('browser.setCookies', () => { | ||
| const surface = new PlaywrightSurface({}); | ||
| const projectRoot = mkdtempSync(path.join(tmpdir(), 'e2e-set-cookies-')); | ||
| const signal = new AbortController().signal; | ||
| let server: Server; | ||
| let origin: string; | ||
| let browser: Browser; | ||
|
|
||
| beforeAll(async () => { | ||
| const started = await startServer(); | ||
| server = started.server; | ||
| origin = `http://127.0.0.1:${started.port}`; | ||
| await surface.init({ runId: 'cookies', targetName: 'web', projectRoot, app: { site: '127.0.0.1' }, | ||
| env: {}, headed: false, workerSlot: 0, signal, log: () => undefined }); | ||
| }); | ||
|
|
||
| beforeEach(async () => { | ||
| await surface.startAttempt({ attemptId: 'cookies', artifactsDir: projectRoot, signal, resolveSecret: noSecrets, ...ignoreTrace }); | ||
| const page = await surface.ensurePage(); | ||
| await page.goto(`${origin}/`); | ||
| browser = createBrowserFixture(surface, { | ||
| app: { site: '127.0.0.1', resolveUrl: (url: string) => new URL(url, `${origin}/`).href }, | ||
| operation: (timeoutMs = 5_000) => ({ signal, timeoutMs, runId: 'cookies', attemptId: 'cookies', origin: 'test' }), | ||
| expectable: (target: object) => target, | ||
| fixture: (_name: string, target: object) => target, | ||
| } as unknown as EngineFixtureContext); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await surface.endAttempt({ signal, timeoutMs: 5_000 }); | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| await surface.dispose({ signal, timeoutMs: 5_000 }); | ||
| await new Promise<void>((resolve) => server.close(() => resolve())); | ||
| rmSync(projectRoot, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| it('sets every shape the Cookie type allows as written', async () => { | ||
| const tomorrow = Math.floor(Date.now() / 1000) + 86_400; | ||
| await browser.setCookies([ | ||
| { name: 'session', value: 'root', domain: '127.0.0.1' }, | ||
| { name: 'admin', value: '', domain: '127.0.0.1', path: '/admin', httpOnly: true, secure: false, sameSite: 'Strict' }, | ||
| { name: 'theme', value: 'dark', url: `${origin}/settings`, expires: tomorrow, sameSite: 'Lax' }, | ||
| { name: 'lang', value: 'en', url: '/' }, | ||
| ]); | ||
| const cookies = (await browser.cookies()).map(({ name, value, path: at, httpOnly, sameSite, expires }) => ({ name, value, at, httpOnly, sameSite, expires })); | ||
| expect(cookies.toSorted((a, b) => a.name.localeCompare(b.name))).toEqual([ | ||
| { name: 'admin', value: '', at: '/admin', httpOnly: true, sameSite: 'Strict', expires: undefined }, | ||
| { name: 'lang', value: 'en', at: '/', httpOnly: false, sameSite: 'Lax', expires: undefined }, | ||
| { name: 'session', value: 'root', at: '/', httpOnly: false, sameSite: 'Lax', expires: undefined }, | ||
| { name: 'theme', value: 'dark', at: '/', httpOnly: false, sameSite: 'Lax', expires: tomorrow }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('takes a domain with a leading dot', async () => { | ||
| await browser.setCookies([{ name: 'session', value: 'root', domain: '.127.0.0.1' }]); | ||
| expect((await browser.cookies()).map(({ name, domain }) => ({ name, domain }))).toEqual([{ name: 'session', domain: '127.0.0.1' }]); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ['an uppercase host', 'EXAMPLE.test', 'example.test'], | ||
| ['a punycode host', 'xn--bcher-kva.example', 'xn--bcher-kva.example'], | ||
| ['a unicode host', 'bücher.example', 'xn--bcher-kva.example'], | ||
| ['an IPv6 host', '[::1]', '[::1]'], | ||
| ['a host with a trailing dot', 'example.test.', 'example.test.'], | ||
| ])('takes %s as the host it names', async (_name, domain, stored) => { | ||
| await browser.setCookies([{ name: 'session', value: 'root', domain }]); | ||
| expect((await browser.cookies()).map((cookie) => cookie.domain)).toEqual([stored]); | ||
| }); | ||
|
|
||
| it('takes -1 as a session cookie and the end of the year 9999 as the latest expiry', async () => { | ||
| await browser.setCookies([ | ||
| { name: 'session', value: 'root', domain: '127.0.0.1', expires: -1 }, | ||
| { name: 'theme', value: 'dark', domain: '127.0.0.1', expires: 253_402_300_799 }, | ||
| ]); | ||
| const cookies = (await browser.cookies()).map(({ name, expires }) => ({ name, persistent: expires !== undefined })); | ||
| expect(cookies.toSorted((a, b) => a.name.localeCompare(b.name))).toEqual([ | ||
| { name: 'session', persistent: false }, | ||
| { name: 'theme', persistent: true }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('takes back the cookies browser.cookies returns', async () => { | ||
| await browser.setCookies([ | ||
| { name: 'session', value: 'root', domain: '127.0.0.1', httpOnly: true, sameSite: 'Strict' }, | ||
| { name: 'theme', value: 'dark', url: '/settings', expires: Math.floor(Date.now() / 1000) + 86_400 }, | ||
| ]); | ||
| const saved = await browser.cookies(); | ||
| await browser.clearCookies(); | ||
| await browser.setCookies(saved); | ||
| expect(await browser.cookies()).toEqual(saved); | ||
| }); | ||
|
|
||
| const base = { name: 'session', value: 'root', domain: '127.0.0.1' }; | ||
|
|
||
| it.each<[string, unknown, string]>([ | ||
| ['a single cookie, not an array', base, 'browser.setCookies takes an array of cookies, got an object'], | ||
| ['a null entry', [null], 'browser.setCookies cookies[0] must be an object, got null'], | ||
| ['an array entry', [['session', 'root']], 'cookies[0] must be an object, got an array'], | ||
| ['no url and no domain', [{ name: 'session', value: 'root' }], 'cookies[0] needs a url, or a domain with an optional path'], | ||
| ['url and domain', [{ ...base, url: '/' }], 'cookies[0] takes url, or domain with an optional path, not both'], | ||
| ['url and path', [{ name: 'session', value: 'root', url: '/', path: '/admin' }], 'cookies[0] takes url, or domain with an optional path, not both'], | ||
| ['an empty domain', [{ ...base, domain: '' }], 'cookies[0] domain must be a host name like "example.com" or ".example.com", got ""'], | ||
| ['a domain with a scheme', [{ ...base, domain: 'https://127.0.0.1' }], 'cookies[0] domain must be a host name like "example.com" or ".example.com", got "https://127.0.0.1"'], | ||
| ['a domain with a path', [{ ...base, domain: '127.0.0.1/app' }], 'cookies[0] domain must be a host name like "example.com" or ".example.com", got "127.0.0.1/app"'], | ||
| ['a domain with a port', [{ ...base, domain: '127.0.0.1:4399' }], 'cookies[0] domain must be a host name like "example.com" or ".example.com", got "127.0.0.1:4399"'], | ||
| ['a domain with a space', [{ ...base, domain: ' 127.0.0.1' }], 'cookies[0] domain must be a host name like "example.com" or ".example.com", got " 127.0.0.1"'], | ||
| ['a short IPv4 domain the parser fills in', [{ ...base, domain: '1.2.3' }], 'cookies[0] domain must be a host name like "example.com" or ".example.com", got "1.2.3"'], | ||
| ['a percent-encoded domain', [{ ...base, domain: '%41.com' }], 'cookies[0] domain must be a host name like "example.com" or ".example.com", got "%41.com"'], | ||
| ['a domain with an empty label', [{ ...base, domain: 'a..example.com' }], 'cookies[0] domain must be a host name like "example.com" or ".example.com", got "a..example.com"'], | ||
| ['a domain with two leading dots', [{ ...base, domain: '..example.com' }], 'cookies[0] domain must be a host name like "example.com" or ".example.com", got "..example.com"'], | ||
| ['a missing name', [{ value: 'root', domain: '127.0.0.1' }], 'cookies[0] name must be a string, got undefined'], | ||
| ['a number value', [{ ...base, value: 5 }], 'cookies[0] value must be a string, got 5'], | ||
| ['a string expires', [{ ...base, expires: '1999999999' }], 'cookies[0] expires must be a Unix timestamp in seconds (not milliseconds), got "1999999999"'], | ||
| ['a NaN expires', [{ ...base, expires: Number.NaN }], 'cookies[0] expires must be a Unix timestamp in seconds (not milliseconds), got NaN'], | ||
| ['an expires in milliseconds', [{ ...base, expires: 1_767_225_600_000 }], 'cookies[0] expires must be a Unix timestamp in seconds (not milliseconds), got 1767225600000'], | ||
| ['a negative expires', [{ ...base, expires: -5 }], 'cookies[0] expires must be a Unix timestamp in seconds (not milliseconds), got -5'], | ||
| ['an expires past the year 9999', [{ ...base, expires: 253_402_300_800 }], 'cookies[0] expires must be a Unix timestamp in seconds (not milliseconds), got 253402300800'], | ||
| ['a string httpOnly', [{ ...base, httpOnly: 'true' }], 'cookies[0] httpOnly must be a boolean, got "true"'], | ||
| ['a lowercase sameSite', [{ ...base, sameSite: 'strict' }], 'cookies[0] sameSite must be "Strict", "Lax", or "None", got "strict"'], | ||
| ['an unknown key', [{ ...base, maxAge: 60 }], 'cookies[0] has unknown key "maxAge"; expected one of name, value, url, domain, path, expires, httpOnly, secure, sameSite'], | ||
| ['a misspelled key', [{ ...base, samesite: 'Lax' }], 'cookies[0] has unknown key "samesite"; did you mean "sameSite"?'], | ||
| ['a hole', Object.assign([base], { 2: base }), 'browser.setCookies cookies[1] must be an object, got undefined'], | ||
| ['a bad second cookie', [base, { ...base, sameSite: 'lax' }], 'browser.setCookies cookies[1] sameSite'], | ||
| ])('refuses %s with INVALID_ARGUMENT, setting nothing', async (_name, cookies, message) => { | ||
| expect(() => browser.setCookies(cookies as readonly Cookie[])).toThrow( | ||
| expect.objectContaining({ code: 'INVALID_ARGUMENT', message: expect.stringContaining(message) }), | ||
| ); | ||
| expect(await browser.cookies()).toEqual([]); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P3 · Non-blocking · Some cookies that the browser refuses still fail as ENGINE_FAILURE
Optional. This does not block the merge.
The header comment (lines 2-5) says that a cookie the browser would refuse is the test's mistake, not the engine's. A domain cookie with a
paththat is a string but not a valid path passes this check and reaches Playwright. Main and this head behave the same:path: ''givesENGINE_FAILURE: setCookies failed: browserContext.addCookies: Cookie should have a url or a domain/path pair. The message is also wrong, because the cookie has a domain.path: 'account'givesENGINE_FAILURE: ... Protocol error (Storage.setCookies): Invalid cookie fields.sameSite: 'None'withoutsecure: trueresolves, but Chromium does not store the cookie, and there is no error.The
Cookietype allows these values, so they are outside the scope of the docs row. They are in the scope of the header comment.