Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix: do not throw from fetch when response has invalid cookie (#27192)

Yury Semikhatsky88038f1b00af
diff --git a/packages/playwright-core/src/server/fetch.ts b/packages/playwright-core/src/server/fetch.ts
index e2c92b2b4b..f198984101 100644
--- a/packages/playwright-core/src/server/fetch.ts
+++ b/packages/playwright-core/src/server/fetch.ts
@@ -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) {
diff --git a/tests/library/browsercontext-fetch.spec.ts b/tests/library/browsercontext-fetch.spec.ts
index de0da3a118..742d4a7f99 100644
--- a/tests/library/browsercontext-fetch.spec.ts
+++ b/tests/library/browsercontext-fetch.spec.ts
@@ -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']) {