Skip to content

Stale host-only auth cookies survive a Domain migration and shadow the new session #308

Open

Description

@RaphaelFakhri

After you switch cookieOptions.domain from unset (host-only) to a parent domain such as .example.com, the browser keeps the old host-only auth cookies until sign-out. They shadow the new domain-scoped cookies, so the client reads the old session.

#240 added a host-only clear when domain is set, but setItem and applyServerStorage only emit it for chunks they remove. Chunks that are overwritten keep the same names (sb-<ref>-auth-token.0, .1, ...), so they never get a host-only clear. After the first write with the new domain, the browser holds two cookies per name:

  • the old host-only cookie (Domain not set)
  • the new cookie with Domain=.example.com

Browsers list the older cookie first in document.cookie and the Cookie header, and cookie.parse keeps the first value for a name:

require("cookie").parse("sb-auth.0=OLD; sb-auth.0=NEW"); // { 'sb-auth.0': 'OLD' }

The client then reads the old session. Its refresh token has already been rotated, so the next refresh fails and the user gets signed out.

Reproduction

@supabase/ssr 0.12.7 (main 5064794).

const calls = [];
const { storage } = createStorageFromOptions(
  {
    cookieEncoding: "raw",
    cookieOptions: { domain: ".example.com" },
    cookies: {
      getAll: async () => [{ name: "storage-key", value: "old" }],
      setAll: async (c) => calls.push(...c),
    },
  },
  false,
);
await storage.setItem("storage-key", "new");
// calls: only { name: "storage-key", value: "new", options: { domain: ".example.com", ... } }
// expected: a host-only clear for "storage-key" before the set

Expected

When domain is set, the write also clears the host-only copy of every existing chunk it overwrites, as it already does for the chunks it removes.

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