Skip to content

Commit cbcc609

Browse files
authored
fix: return non-secure cookies with HTTPS URLs (#5507)
Cookies have a "Secure" attribute which tells the browsers that a given cookie should only be sent via HTTPS. In it's absense "Secure" is falsy and these cookies should be sent with both HTTP and HTTPS requests. Playwright now returns only the "Non-Secure" cookies for HTTP URLs, and both "Secure" and "Non-Secure" cookies for HTTPS URLs. Fixes #5504
1 parent a9c91b0 commit cbcc609

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

src/server/network.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function filterCookies(cookies: types.NetworkCookie[], urls: string[]): t
3636
continue;
3737
if (!parsedURL.pathname.startsWith(c.path))
3838
continue;
39-
if ((parsedURL.protocol === 'https:') !== c.secure)
39+
if (parsedURL.protocol !== 'https:' && c.secure)
4040
continue;
4141
return true;
4242
}

test/browsercontext-cookies.spec.ts

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,9 @@ it('should get multiple cookies', async ({context, page, server}) => {
106106
document.cookie = 'password=1234';
107107
return document.cookie.split('; ').sort().join('; ');
108108
});
109-
const cookies = await context.cookies();
110-
cookies.sort((a, b) => a.name.localeCompare(b.name));
109+
const cookies = new Set(await context.cookies());
111110
expect(documentCookie).toBe('password=1234; username=John Doe');
112-
expect(cookies).toEqual([
111+
expect(cookies).toEqual(new Set([
113112
{
114113
name: 'password',
115114
value: '1234',
@@ -130,7 +129,7 @@ it('should get multiple cookies', async ({context, page, server}) => {
130129
secure: false,
131130
sameSite: 'None',
132131
},
133-
]);
132+
]));
134133
});
135134

136135
it('should get cookies from multiple urls', async ({context}) => {
@@ -147,9 +146,8 @@ it('should get cookies from multiple urls', async ({context}) => {
147146
name: 'birdo',
148147
value: 'tweets',
149148
}]);
150-
const cookies = await context.cookies(['https://foo.com', 'https://baz.com']);
151-
cookies.sort((a, b) => a.name.localeCompare(b.name));
152-
expect(cookies).toEqual([{
149+
const cookies = new Set(await context.cookies(['https://foo.com', 'https://baz.com']));
150+
expect(cookies).toEqual(new Set([{
153151
name: 'birdo',
154152
value: 'tweets',
155153
domain: 'baz.com',
@@ -167,7 +165,7 @@ it('should get cookies from multiple urls', async ({context}) => {
167165
httpOnly: false,
168166
secure: true,
169167
sameSite: 'None',
170-
}]);
168+
}]));
171169
});
172170

173171
it('should work with subdomain cookie', async ({context, page, server}) => {
@@ -210,3 +208,46 @@ it('should not return cookies with empty value', async ({context, page, server})
210208
expect(cookies.length).toBe(0);
211209
});
212210

211+
it('should return secure cookies based on HTTP(S) protocol', async ({context}) => {
212+
await context.addCookies([{
213+
url: 'https://foo.com',
214+
name: 'doggo',
215+
value: 'woofs',
216+
secure: true
217+
}, {
218+
url: 'http://foo.com',
219+
name: 'catto',
220+
value: 'purrs',
221+
secure: false
222+
}]);
223+
const cookies = new Set(await context.cookies('https://foo.com'));
224+
expect(cookies).toEqual(new Set([{
225+
name: 'catto',
226+
value: 'purrs',
227+
domain: 'foo.com',
228+
path: '/',
229+
expires: -1,
230+
httpOnly: false,
231+
secure: false,
232+
sameSite: 'None',
233+
}, {
234+
name: 'doggo',
235+
value: 'woofs',
236+
domain: 'foo.com',
237+
path: '/',
238+
expires: -1,
239+
httpOnly: false,
240+
secure: true,
241+
sameSite: 'None',
242+
}]));
243+
expect(await context.cookies('http://foo.com/')).toEqual([{
244+
name: 'catto',
245+
value: 'purrs',
246+
domain: 'foo.com',
247+
path: '/',
248+
expires: -1,
249+
httpOnly: false,
250+
secure: false,
251+
sameSite: 'None',
252+
}]);
253+
});

0 commit comments

Comments
 (0)