Skip to content

[Bug]: Secure cookies are dropped over http for 127.0.0.1 and loopback addresses #43115

Closed

Description

@alok-108

Version

1.57.0 (and current main)

Steps to reproduce

Run the following self-contained test:

import { test, expect } from '@playwright/test';

test('send secure cookie over http for 127.0.0.1', async ({ request }) => {
  // Add a secure cookie on 127.0.0.1
  await request.addCookies([
    {
      name: 'auth_token',
      value: 'secret123',
      domain: '127.0.0.1',
      path: '/',
      secure: true,
    }
  ]);

  // Retrieve cookies for http://127.0.0.1
  const cookies = await request.cookies(['http://127.0.0.1/']);
  expect(cookies.map(c => c.name)).toEqual(['auth_token']);
});

Or when an HTTP server running on http://127.0.0.1:<PORT> sends Set-Cookie: a=secret; Secure; Path=/:
On subsequent requests made to http://127.0.0.1:<PORT>/, the Cookie: a=secret header is omitted.

In contrast, replacing 127.0.0.1 with localhost preserves and sends the Secure cookie as expected.

Expected behavior

Under the W3C Secure Contexts specification (§3.1) and RFC 6761, IPv4 loopback (127.0.0.0/8, including 127.0.0.1) and IPv6 loopback (::1, [::1]) are defined as potentially trustworthy origins (secure contexts).

All major browsers (Chromium, WebKit/Safari, Firefox) treat http://127.0.0.1:<port> as a secure context and allow setting and sending Secure cookies over plain HTTP.

APIRequestContext and browserContext.cookies('http://127.0.0.1:<port>') should recognize loopback addresses as secure contexts and retain Secure cookies just like localhost.

Actual behavior

Secure cookies are filtered out and dropped on 127.0.0.1 and [::1]:

  • request.cookies(['http://127.0.0.1/']) returns [].
  • APIRequestContext omits the Cookie header for Secure cookies on HTTP requests to http://127.0.0.1:<PORT>.
  • browserContext.cookies('http://127.0.0.1:<PORT>') omits Secure cookies.

Additional context

Root Cause:
In packages/playwright-core/src/server/network.ts:

export function isLocalHostname(hostname: string): boolean {
  return hostname === 'localhost' || hostname.endsWith('.localhost');
}

This helper only matches localhost and *.localhost. It does not match 127.0.0.1 (or 127.0.0.0/8) or ::1 / [::1].

As a result:

  1. In packages/playwright-core/src/server/cookieStore.ts:33:
    if (this._raw.secure && (url.protocol !== 'https:' && !isLocalHostname(url.hostname)))
      return false;
  2. In packages/playwright-core/src/server/network.ts:69:
    if (parsedURL.protocol !== 'https:' && !isLocalHostname(parsedURL.hostname) && c.secure)
      continue;

Many testing environments and CI setups explicitly configure http://127.0.0.1 instead of localhost to avoid dual-stack IPv4/IPv6 resolution delays or mismatches in Node.js, which currently leads to Secure cookies being silently dropped.

Environment

System:
  OS: Windows 11 10.0.26200
  CPU: (12) x64 12th Gen Intel(R) Core(TM) i5-1235U
  Memory: 2.62 GB / 15.68 GB
Binaries:
  Node: 22.14.0 - C:\Program Files\nodejs\node.EXE
  npm: 10.9.2 - C:\Program Files\nodejs\npm.CMD

Activity

  1. github-actions commented on Oct 5, 2026

    @github-actions
    Contributor

    Hi, I'm the Playwright bot and I took a first look.

    Reproduced on tip-of-tree (1.64.0-next, sha a18dade). This is a live bug in Playwright's URL filtering: secure cookies are excluded for plain-HTTP IPv4 and IPv6 loopback URLs even though loopback is potentially trustworthy. It affects APIRequestContext.cookies(), outgoing APIRequestContext cookie headers, and BrowserContext.cookies(); the result is the same across Chromium, Firefox, and WebKit.

    #43116 contains the matching fix. Applying its source diff to the tested sha makes the full 12-case matrix pass.

    Condensed repro
    import type { APIRequestContext } from 'playwright-core';
    import { expect, playwrightTest } from '../config/browserTest';
    
    const test = playwrightTest.extend<{ request: APIRequestContext }>({
      request: async ({ playwright }, use) => {
        const request = await playwright.request.newContext();
        await use(request);
        await request.dispose();
      },
    });
    
    for (const [host, domain] of [['127.0.0.1', '127.0.0.1'], ['[::1]', '[::1]']]) {
      test(`APIRequestContext handles secure cookies for HTTP ${host}`, {
        annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43115' },
      }, async ({ request, server }) => {
        const url = `http://${host}:${server.PORT}/`;
        await request.addCookies([{ name: 'cookie', value: 'value', domain, path: '/', secure: true }]);
        expect.soft((await request.cookies(url)).map(cookie => cookie.name)).toContain('cookie');
        const [serverRequest] = await Promise.all([
          server.waitForRequest('/empty.html'),
          request.get(`${url}empty.html`),
        ]);
        expect(serverRequest.headers.cookie).toContain('cookie=value');
      });
    
      test(`BrowserContext handles secure cookies for HTTP ${host}`, {
        annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43115' },
      }, async ({ context, server }) => {
        const url = `http://${host}:${server.PORT}/`;
        await context.addCookies([{ name: 'cookie', value: 'value', domain, path: '/', secure: true }]);
        expect((await context.cookies(url)).map(cookie => cookie.name)).toContain('cookie');
      });
    }
    What I ran
    • version: tip-of-tree 1.64.0-next at a18dade0076912787d4e848d08a3047dc4c8fc05
    • addresses: 127.0.0.1 and [::1]
    • surfaces: APIRequestContext.cookies(), an actual outgoing APIRequestContext request, and BrowserContext.cookies()
    • browsers: Chromium, Firefox, and WebKit; all 12 checks fail without the fix
    • proposed fix: applied the source diff from fix(network): send secure cookies over http to loopback addresses #43116 (3c26cbb); all 12 checks pass
    • related-report search: no earlier matching issue or fix found in the Playwright family repositories

    Triaged on Linux by the Playwright bot - agent run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions