Skip to content

context.cookies(url) drops secure cookies for *.localhost and 127.0.0.0/8 hosts #3469

Open

Description

@shrey150

Bug

filterCookies in packages/extension/understudy/cookies.ts already treats localhost, 127.0.0.1, and [::1] as loopback, so secure cookies still match those hosts over http:. Playwright's isLocalHostname (microsoft/playwright#43136), which mirrors Chromium's net::IsLocalhost(), also includes *.localhost and the rest of 127.0.0.0/8. Those still drop.

Confirmed on upstream main at 9d3cfa51589ab1fdc85e16ddfc09f8c44dc7e2b9 (and on @browserbasehq/[email protected], whose filterCookies uses the same loopback check). This is provider-independent and does not require an LLM.

Minimal reproduction

With an initialized local browser:

await context.addCookies([{ name: "s", value: "1", url: "https://foo.localhost:3000/", secure: true }]);
console.log(await context.cookies("http://foo.localhost:3000/"));
// Stagehand: []
// Playwright: [{ name: "s", ... }]

await context.addCookies([{ name: "s", value: "1", url: "https://127.0.0.2:3000/", secure: true }]);
console.log(await context.cookies("http://127.0.0.2:3000/"));
// Stagehand: []
// Playwright: [{ name: "s", ... }]

Observed results

Live repros on a local browser, plus a deterministic filterCookies unit check imported from main:

  • Stagehand: @browserbasehq/[email protected] (same isLoopback check as main at 9d3cfa5), Google Chrome 156.0.8078.47
  • Playwright: 1.65.1, Chromium 159.0.8154.6
Query URL after adding a secure cookie for that host Stagehand Playwright
http://localhost:3000/ [s] [s]
http://127.0.0.1:3000/ [s] [s]
http://[::1]:3000/ [s] [s]
http://foo.localhost:3000/ [] [s]
http://127.0.0.2:3000/ [] [s]
http://example.com/ (control) [] []

The same pattern holds when the cookie is added with { domain, path: "/", secure: true } instead of a url.

Expected behavior

  • secure cookies match http://localhost, *.localhost, 127.0.0.0/8, and [::1].
  • Other http: hosts still exclude secure cookies.
  • Add regression tests next to the cookie tests from #3465 / #3468.

Playwright implements this with isLocalHostname in packages/playwright-core/src/server/network.ts:

export function isLocalHostname(hostname: string): boolean {
  return hostname === 'localhost' || hostname.endsWith('.localhost') || hostname === '[::1]' || /^127\.\d+\.\d+\.\d+$/.test(hostname);
}

This is a follow-up to #3465 (merged via #3468) and a good small contributor issue.

Activity

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