[blitz-auth] RFC 6265 Violation: Multi-cookie concatenation into a single Set-Cookie header drops session & anti-CSRF cookies in App Router / Response objects
What is the problem?
What is the problem?
When using @blitzjs/auth in Next.js App Router (such as Route Handlers wrapped with withBlitzAuth or App Router RPC endpoints), session cookies, anti-CSRF cookies, and public data cookies are corrupted or completely dropped by the client browser.
In packages/blitz-auth/src/server/auth-sessions.ts, SessionContextClass.setSession() handles Web standard Response objects by calling:
response.headers.append("Set-Cookie", cookieHeaders!)cookieHeaders is the raw string returned by this._headers.get("set-cookie"), which contains all 4 session cookies concatenated together with commas (", ").
Under RFC 6265 §3 and RFC 9110 §5.3, Set-Cookie is the explicit exception to HTTP header folding rules. Because HTTP cookie attributes contain dates with commas (e.g. Expires=Sat, 26 Sep 2026 11:20:53 GMT), servers must not fold multiple cookies into a single header. Browsers parse each Set-Cookie header as an individual directive; when presented with a comma-separated list of cookies containing dates, modern browser parsers (Chrome, Safari, Firefox) fail to parse cookies following the first date or drop the entire header.
Consequently, blitz_sAntiCsrfToken and blitz_sPublicDataToken never get set in the user's browser, and subsequent authenticated POST requests fail immediately with CSRFTokenMismatchError. Furthermore, if this._headers.get("set-cookie") is null, response.headers.append("Set-Cookie", null as any) emits Set-Cookie: null.
Paste all your error logs here:
Paste all your error logs here:
[blitz:session] getSessionKernel {
anonymousSessionToken: undefined,
sessionToken: undefined,
idRefreshToken: undefined,
antiCSRFToken: null
}
CSRFTokenMismatchError: Authentication failed: Anti-CSRF token mismatch.
at getSessionKernel (packages/blitz-auth/src/server/auth-sessions.ts:832:13)
at async getSession (packages/blitz-auth/src/server/auth-sessions.ts:250:23)
at async /node_modules/@blitzjs/auth/dist/server/index.js:140:27Paste all relevant code snippets here:
Paste all relevant code snippets here:
// packages/blitz-auth/src/server/auth-sessions.ts (lines 462-473)
setSession(response: Response | ServerResponse) {
if (this._isRsc) {
void NotSupportedMessage("setSession")
return
}
const cookieHeaders = this._headers.get("set-cookie")
if (response instanceof Response) {
// BUG: Appends a single comma-joined string containing multiple cookies with Expires dates
response.headers.append("Set-Cookie", cookieHeaders!)
} else {
// ServerResponse correctly uses splitCookiesString
append(response, "Set-Cookie", splitCookiesString(cookieHeaders!))
}
...
}
// packages/blitz-auth/src/server/auth-sessions.ts (lines 683-704)
function replaceOrAppendValueInSetCookieHeader(
headers: Headers,
cookieName: string,
newValue: string,
): string {
const cookies = headers.get("set-cookie")
if (!cookies) return newValue
const cookiesAsArray = splitCookiesString(cookies!)
for (let i = 0; i < cookiesAsArray.length; i++) {
const cookie = cookiesAsArray[i]
// BUG: Matches prefixes without '=' and fails on leading whitespace
if (cookie?.startsWith(cookieName)) {
cookiesAsArray[i] = newValue
return cookiesAsArray.join(", ")
}
}
return cookiesAsArray.filter(Boolean).join(", ")
}What are detailed steps to reproduce this?
What are detailed steps to reproduce this?
- Scaffold an App Router Blitz application with
@blitzjs/auth. - Create an App Router route handler at
src/app/api/auth/login/route.ts:import {withBlitzAuth} from "src/blitz-server" export const POST = withBlitzAuth(async (request, params, ctx) => { await ctx.session.$create({ userId: 1, role: "USER" }) return new Response(JSON.stringify({ ok: true }), { status: 200, headers: { "Content-Type": "application/json" }, }) }) - Send a POST request to
/api/auth/login. - Inspect the HTTP response headers in browser DevTools Network tab.
- Notice that only a single
Set-Cookieheader is returned with all cookies merged with commas. - Open Application > Storage > Cookies: notice that
blitz_sAntiCsrfTokenandblitz_sPublicDataTokenare missing. Subsequent authenticated mutations fail withCSRFTokenMismatchError.
Run blitz -v and paste the output here:
Run blitz -v and paste the output here:
macOS / Windows / Linux
blitz: 3.0.2 (local)
@blitzjs/auth: 3.0.2 (local)
@blitzjs/next: 3.0.2 (local)
@blitzjs/rpc: 3.0.2 (local)Please include below any other applicable logs and screenshots that show your problem:
Please include below any other applicable logs and screenshots that show your problem:
Raw HTTP Response Header emitted:
HTTP/1.1 200 OK
Content-Type: application/json
Set-Cookie: blitz_sSessionToken=abc12345; Path=/; Expires=Mon, 26 Oct 2026 05:00:00 GMT; HttpOnly, blitz_sAntiCsrfToken=xyz98765; Path=/; Expires=Mon, 26 Oct 2026 05:00:00 GMT, blitz_sPublicDataToken=eyJ1c2VySWQiOjF9; Path=/; Expires=Mon, 26 Oct 2026 05:00:00 GMTProposed Fix (Pull Request diff):
--- a/packages/blitz-auth/src/server/auth-sessions.ts
+++ b/packages/blitz-auth/src/server/auth-sessions.ts
@@ -465,9 +465,15 @@ export class SessionContextClass implements SessionContext {
return
}
const cookieHeaders = this._headers.get("set-cookie")
+ if (!cookieHeaders) return
+
+ const individualCookies = splitCookiesString(cookieHeaders)
if (response instanceof Response) {
- response.headers.append("Set-Cookie", cookieHeaders!)
+ for (const cookieStr of individualCookies) {
+ response.headers.append("Set-Cookie", cookieStr.trim())
+ }
} else {
- append(response, "Set-Cookie", splitCookiesString(cookieHeaders!))
+ append(response, "Set-Cookie", individualCookies)
}
const headers = this._headers.entries()
@@ -690,7 +696,8 @@ function replaceOrAppendValueInSetCookieHeader(
const cookiesAsArray = splitCookiesString(cookies!)
for (let i = 0; i < cookiesAsArray.length; i++) {
const cookie = cookiesAsArray[i]
- if (cookie?.startsWith(cookieName)) {
+ const trimmed = cookie ? cookie.trim() : ""
+ if (trimmed.startsWith(`${cookieName}=`)) {
cookiesAsArray[i] = newValue
return cookiesAsArray.join(", ")
} else {Source: blitz-js/blitz