fix(server): handle = in cookie values correctly (#11613)

This commit is contained in:
hackerman 2022-01-26 20:27:43 +01:00 committed by GitHub
parent 5a061c528c
commit 872a4be752
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 1 deletions

View File

@ -517,7 +517,21 @@ function toHeadersArray(rawHeaders: string[]): types.HeadersArray {
const redirectStatus = [301, 302, 303, 307, 308];
function parseCookie(header: string): types.NetworkCookie | null {
const pairs = header.split(';').filter(s => s.trim().length > 0).map(p => p.split('=').map(s => s.trim()));
const pairs = header.split(';').filter(s => s.trim().length > 0).map(p => {
let key = '';
let value = '';
const separatorPos = p.indexOf('=');
if (separatorPos === -1) {
// If only a key is specified, the value is left undefined.
key = p.trim();
} else {
// Otherwise we assume that the key is the element before the first `=`
key = p.slice(0, separatorPos).trim();
// And the value is the rest of the string.
value = p.slice(separatorPos + 1).trim();
}
return [key, value];
});
if (!pairs.length)
return null;
const [name, value] = pairs[0];

View File

@ -166,6 +166,33 @@ it('should remove expired cookies', async ({ request, server }) => {
expect(serverRequest.headers.cookie).toBe('a=v');
});
it('should store cookie from Set-Cookie header even if it contains equal signs', async ({ request, server }) => {
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/11612' });
server.setRoute('/setcookie.html', (req, res) => {
res.setHeader('Set-Cookie', ['f=value == value=; secure; httpOnly; path=/some=value']);
res.end();
});
await request.get(`http://a.b.one.com:${server.PORT}/setcookie.html`);
const state = await request.storageState();
expect(state).toEqual({
'cookies': [
{
domain: 'a.b.one.com',
expires: -1,
name: 'f',
path: '/some=value',
sameSite: 'Lax',
httpOnly: true,
secure: true,
value: 'value == value=',
}
],
'origins': []
});
});
it('should export cookies to storage state', async ({ request, server }) => {
const expires = new Date('12/31/2100 PST');
server.setRoute('/setcookie.html', (req, res) => {