fix: do not throw from fetch when response has invalid cookie (#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 https://github.com/microsoft/playwright/issues/27165
This commit is contained in:
Yury Semikhatsky 2023-09-19 16:18:16 -07:00 committed by GitHub
parent bb4268d165
commit 88038f1b00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 2 deletions

View File

@ -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) {

View File

@ -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']) {