Repository navigation
Description
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
- Next.js App Router,
createServerClientwith the standardgetAll/setAlladapter from the docs and:cookieOptions: { domain: '.example.com', path: '/', sameSite: 'lax', secure: true }
- Sign in — cookies are correctly set with
Domain=.example.com. - Call
supabase.auth.signOut()in a Route Handler or Server Action. - Inspect the response headers: the deletion
Set-Cookieentries have noDomainattribute; 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:
- 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-Cookieheaders for the same name still get both. - Ship a Next.js-aware adapter/helper that appends the duplicate deletion as a raw
Set-Cookieheader (response.headers.append('set-cookie', ...)) instead of going throughResponseCookies. - At minimum, document that
setAllmay 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
fix(cookies): keep domain-scoped deletion in name-keyed cookie stores (…
chore(main): release 0.12.3 (#264)