Skip to content

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

fix(network): treat loopback addresses as secure for cookies (#43136)

Pavel Feldmand0fd0f22ffad
diff --git a/packages/playwright-core/src/server/network.ts b/packages/playwright-core/src/server/network.ts
index 1d68e2b5b8..7c7cad66a9 100644
--- a/packages/playwright-core/src/server/network.ts
+++ b/packages/playwright-core/src/server/network.ts
@@ -54,8 +54,9 @@ export function filterCookies(cookies: channels.NetworkCookie[], urls: string[])
});
}
+// Mirrors Chromium's net::IsLocalhost(): localhost, *.localhost, 127.0.0.0/8 and [::1].
export function isLocalHostname(hostname: string): boolean {
- return hostname === 'localhost' || hostname.endsWith('.localhost');
+ return hostname === 'localhost' || hostname.endsWith('.localhost') || hostname === '[::1]' || /^127\.\d+\.\d+\.\d+$/.test(hostname);
}
// Forbidden request headers according to https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_request_header
diff --git a/tests/library/browsercontext-cookies.spec.ts b/tests/library/browsercontext-cookies.spec.ts
index 249611fd43..d34104d58b 100644
--- a/tests/library/browsercontext-cookies.spec.ts
+++ b/tests/library/browsercontext-cookies.spec.ts
@@ -305,6 +305,13 @@ it('should return secure cookies based on HTTP(S) protocol', async ({ context, b
}]);
});
+for (const host of ['127.0.0.1', '[::1]']) {
+ it(`should return secure cookies for http ${host}`, { annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43115' } }, async ({ context }) => {
+ await context.addCookies([{ name: 'a', value: 'v', domain: host, path: '/', secure: true }]);
+ expect((await context.cookies(`http://${host}/`)).map(c => c.name)).toEqual(['a']);
+ });
+}
+
it('should add cookies with an expiration', async ({ context }) => {
const expires = Math.floor((Date.now() / 1000)) + 3600;
await context.addCookies([{
diff --git a/tests/library/global-fetch-cookie.spec.ts b/tests/library/global-fetch-cookie.spec.ts
index 9fad71ad11..8503c13784 100644
--- a/tests/library/global-fetch-cookie.spec.ts
+++ b/tests/library/global-fetch-cookie.spec.ts
@@ -249,6 +249,23 @@ it('should send secure cookie over http for subdomains of localhost', async ({ r
expect(serverRequest.headers.cookie).toBe('a=v; b=v');
});
+for (const host of ['127.0.0.1', '[::1]']) {
+ it(`should send secure cookie over http for ${host}`, { annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43115' } }, async ({ request, server }) => {
+ server.setRoute('/setcookie.html', (req, res) => {
+ res.setHeader('Set-Cookie', ['a=v; secure', 'b=v']);
+ res.end();
+ });
+ const prefix = `http://${host}:${server.PORT}`;
+ await request.get(`${prefix}/setcookie.html`);
+ const [serverRequest] = await Promise.all([
+ server.waitForRequest('/empty.html'),
+ request.get(`${prefix}/empty.html`)
+ ]);
+ expect(serverRequest.headers.cookie).toBe('a=v; b=v');
+ expect((await request.cookies(`${prefix}/`)).map(c => c.name)).toEqual(['a', 'b']);
+ });
+}
+
it('should send not expired cookies', async ({ request, server }) => {
server.setRoute('/setcookie.html', (req, res) => {
const tomorrow = new Date();