Skip to content

Commit

Permalink
fix: do not throw from fetch when response has invalid cookie (micros…
Browse files Browse the repository at this point in the history
…oft#27192)

Cookie value is limited by 4096 characters in the browsers. If
setCookies failed we try setting each cookie individually just in case
only some of them are bad.

Fixes microsoft#27165
  • Loading branch information
yury-s authored and Germandrummer92 committed Oct 27, 2023
1 parent 3f1fb37 commit ba57363
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/playwright-core/src/server/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,15 @@ export abstract class APIRequestContext extends SdkObject {
progress.log(` ${name}: ${value}`);

const cookies = this._parseSetCookieHeader(response.url || url.toString(), response.headers['set-cookie']) ;
if (cookies.length)
await this._addCookies(cookies);
if (cookies.length) {
try {
await this._addCookies(cookies);
} catch (e) {
// Cookie value is limited by 4096 characters in the browsers. If setCookies failed,
// we try setting each cookie individually just in case only some of them are bad.
await Promise.all(cookies.map(c => this._addCookies([c]).catch(() => {})));
}
}

if (redirectStatus.includes(response.statusCode!) && options.maxRedirects >= 0) {
if (!options.maxRedirects) {
Expand Down
10 changes: 10 additions & 0 deletions tests/library/browsercontext-fetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,16 @@ it('should set domain=localhost cookie', async ({ context, server, browserName,
expect(cookie.value).toBe('val');
});

it('fetch should not throw on long set-cookie value', async ({ context, server }) => {
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/27165' });
server.setRoute('/empty.html', (req, res) => {
res.setHeader('Set-Cookie', [`foo=${'a'.repeat(4100)}; path=/;`, `bar=val`]);
res.end();
});
await context.request.get(server.EMPTY_PAGE, { timeout: 5000 });
const cookies = await context.cookies();
expect(cookies.map(c => c.name)).toContain('bar');
});

it('should support set-cookie with SameSite and without Secure attribute over HTTP', async ({ page, server, browserName, isWindows }) => {
for (const value of ['None', 'Lax', 'Strict']) {
Expand Down

0 comments on commit ba57363

Please sign in to comment.