fix: derive auth cookie Secure flag from request protocol#1
Merged
Conversation
Auth and session cookies were hardcoded as Secure in production, so browsers dropped them over plain HTTP on non-localhost hosts, breaking login on internal IP:port deployments (e.g. http://host:8081 — login succeeded server-side but the session never stuck in the browser). Add src/lib/auth-cookie.ts, which sets the Secure flag from the actual request protocol (honoring x-forwarded-proto behind a TLS-terminating proxy), while keeping FORCE_HTTPS and DISABLE_SECURE_COOKIE as explicit overrides. Apply it to the login, logout and setup routes and to the session cookie in middleware. Add unit tests for the helper and document the env vars in .env.example. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Auth and session cookies were hardcoded as
Securein production:Browsers discard
Securecookies over plain HTTP (except onlocalhost). So on any HTTPIP:portdeployment (e.g.http://10.1.0.190:8081),POST /api/auth/loginsucceeds server-side and returns the cookie, but the browser drops it — the user is bounced straight back to the login screen and can never stay authenticated. Same for theredis-explorer-sessioncookie in middleware, which would also break server-side Redis sessions over HTTP.Fix
New helper
src/lib/auth-cookie.tsderives theSecureflag from the actual request protocol instead of hardcoding it:Secureover HTTPS (incl. behind a TLS-terminating proxy viax-forwarded-proto), and notSecureover plain HTTP, so login works on internalIP:portaccess without weakening HTTPS deployments.FORCE_HTTPS=true→ alwaysSecure.DISABLE_SECURE_COOKIE=true→ neverSecure(explicit escape hatch).Applied consistently to all cookie sites:
src/app/api/auth/login/route.tssrc/app/api/auth/logout/route.ts(now takesrequestto match flags on clear)src/app/api/auth/setup/route.tssrc/middleware.ts(session cookie).env.exampledocuments the two override vars.Tests
__tests__/auth-cookie.test.tscoveringisSecureRequest,shouldUseSecureCookie(protocol default + both env overrides + precedence), andauthCookieOptions.next build✓,tsc --noEmitclean on all changed files.🤖 Generated with Claude Code