Skip to content

Commit c99ab5a

Browse files
author
senatorsagent
authored
fix(session): honor x-forwarded-proto / NODE_ENV for cookie secure flag (#25)
Same fix already applied to `middleware.ts` in PR #22 — the session cookie had the same bug: it only looked at `APP_BASE_URL.startsWith('https://')`, which breaks behind TLS-terminating proxies (Vercel/Cloudflare/Railway/nginx). Detect `x-forwarded-proto` and treat `NODE_ENV=production` as secure, matching the middleware cookie behavior.
1 parent 7c1c5df commit c99ab5a

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

lib/session.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@ export function signSession(payload: { sub: string; email: string | null; name:
2626
return `${body}.${sig}`;
2727
}
2828

29-
/** Cookie options for the session cookie — secure only when served over https. */
29+
/** Cookie options for the session cookie — secure only when served over https.
30+
* Detects TLS proxies (Vercel/Cloudflare/Railway) via x-forwarded-proto and honors NODE_ENV=production. */
3031
export function sessionCookieOptions() {
31-
const secure = (process.env.APP_BASE_URL || "").startsWith("https://");
32+
const proto = (process.env.X_FORWARDED_PROTO || process.env.FORWARDED_PROTO || "").split(",")[0].trim().toLowerCase();
33+
const secure =
34+
(process.env.NODE_ENV === "production") ||
35+
(process.env.APP_BASE_URL || "").startsWith("https://") ||
36+
proto === "https";
3237
return { httpOnly: true, sameSite: "lax" as const, secure, path: "/", maxAge: SESSION_TTL };
3338
}
3439

0 commit comments

Comments
 (0)