Skip to content

signOut() never deletes cookies with a configured domain in Next.js — host-only clear variant from #240 overwrites the domain deletion (0.12.0) #256

Closed

Description

@britcent-youngeun

Bug report

  • I confirm this is a bug with Supabase, not with my own application.
  • I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

Since @supabase/ssr 0.12.0 (introduced by #240), when cookieOptions.domain is configured, cookie removals emit two entries with the same cookie name to the setAll callback: one with the configured options (including domain) and a host-only variant (same options minus domain), in that order — in removeItem, setItem chunk cleanup, and applyServerStorage.

#240 states the extra clear is "a no-op unless a stale host-only cookie at the same name actually exists". That assumption does not hold for Next.js. The documented adapter forwards each entry to the cookie store:

setAll(cookiesToSet) {
  cookiesToSet.forEach(({ name, value, options }) =>
    cookieStore.set(name, value, options)
  )
}

Next.js cookie stores (cookies() in Server Actions / Route Handlers, NextResponse.cookies in middleware) are backed by ResponseCookies from @edge-runtime/cookies, whose internal storage is a Map keyed by cookie name only — every set() replaces the previous entry for that name and rewrites the Set-Cookie headers from the map.

So the second (host-only) deletion overwrites the first (domain) deletion, and only the host-only variant reaches the browser:

set-cookie: sb-<ref>-auth-token.0=; Path=/; Max-Age=0; Secure; HttpOnly; SameSite=lax   ← no Domain
set-cookie: sb-<ref>-auth-token.1=; Path=/; Max-Age=0; Secure; HttpOnly; SameSite=lax   ← no Domain

Browsers identify cookies by (name, domain, path), so this host-only deletion never matches the cookie the library itself set with Domain=.... The session cookie is never deleted. The migration cleanup intended to fix stuck sessions (supabase/supabase-js#2392) instead re-creates the same class of stuck-session bug — now for every Next.js project that configures cookieOptions.domain, not just migrating ones.

Not present in 0.10.3. Also present in 0.12.1-rc.x.

To Reproduce

  1. Next.js App Router, createServerClient with the standard getAll/setAll adapter from the docs and:
    cookieOptions: { domain: '.example.com', path: '/', sameSite: 'lax', secure: true }
  2. Sign in — cookies are correctly set with Domain=.example.com.
  3. Call supabase.auth.signOut() in a Route Handler or Server Action.
  4. Inspect the response headers: the deletion Set-Cookie entries have no Domain attribute; the browser keeps the session cookies, and every subsequent request retries and fails the same deletion.

The same collapse affects stale-chunk cleanup during setItem (e.g. a session rewrite that shrinks the chunk count): the domain-scoped deletion of the leftover chunk is lost, and the surviving stale chunk can corrupt the reassembled chunked session on subsequent reads.

Note: the tests added in #240 assert the payload passed to setAll, so they pass — the collapse happens one layer down, inside Next.js's name-keyed cookie store.

Expected behavior

Deletions preserve the configured domain so the cookies the library set are actually removed. The host-only cleanup should be best-effort and must not displace the primary deletion.

Possible fixes:

  1. Reorder the emitted entries so the host-only variant comes first and the domain variant last. Name-keyed stores (Next.js) then keep the domain deletion — the one that matches the cookies this library sets — and only lose the best-effort migration cleanup. Adapters capable of multiple Set-Cookie headers for the same name still get both.
  2. Ship a Next.js-aware adapter/helper that appends the duplicate deletion as a raw Set-Cookie header (response.headers.append('set-cookie', ...)) instead of going through ResponseCookies.
  3. At minimum, document that setAll may receive multiple entries for the same cookie name and that name-keyed cookie stores must dedupe (preferring the domain variant) to avoid losing the primary deletion.

Screenshots

N/A — the Set-Cookie response headers above (captured from a deployed Next.js app on Vercel, project ref redacted) show the issue.

System information

  • OS: macOS (dev) / Vercel (deployed) — server-side issue, browser-independent
  • Browser: observed with Chrome, applies to any browser
  • Version of @supabase/ssr: 0.12.0
  • Version of supabase-js: 2.108.1
  • Version of Next.js: 16.2.9
  • Version of Node.js: 24.16.0

Additional context

Workaround we applied: in our setAll implementations we dedupe same-name entries preferring the variant whose options include domain; in middleware we additionally emit the dropped host-only deletions via response.headers.append('set-cookie', ...) right before returning the response, so the host-only cleanup still happens where the platform allows it.

Activity

  1. added
    bug
    on Jul 9, 2026
  2. added 2 commits that reference this issue on Jul 14, 2026
    d3a8a98
    dd2b20b
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions