Skip to content

fix: derive auth cookie Secure flag from request protocol#1

Merged
ruanbarroso merged 1 commit into
mainfrom
fix/auth-cookie-secure-flag
Jun 1, 2026
Merged

fix: derive auth cookie Secure flag from request protocol#1
ruanbarroso merged 1 commit into
mainfrom
fix/auth-cookie-secure-flag

Conversation

@ruanbarroso

Copy link
Copy Markdown
Owner

Problem

Auth and session cookies were hardcoded as Secure in production:

secure: process.env.NODE_ENV === 'production'
// login route: FORCE_HTTPS || (production && DISABLE_SECURE_COOKIE !== 'true')

Browsers discard Secure cookies over plain HTTP (except on localhost). So on any HTTP IP:port deployment (e.g. http://10.1.0.190:8081), POST /api/auth/login succeeds 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 the redis-explorer-session cookie in middleware, which would also break server-side Redis sessions over HTTP.

Fix

New helper src/lib/auth-cookie.ts derives the Secure flag from the actual request protocol instead of hardcoding it:

  • defaultSecure over HTTPS (incl. behind a TLS-terminating proxy via x-forwarded-proto), and not Secure over plain HTTP, so login works on internal IP:port access without weakening HTTPS deployments.
  • FORCE_HTTPS=true → always Secure.
  • DISABLE_SECURE_COOKIE=true → never Secure (explicit escape hatch).

Applied consistently to all cookie sites:

  • src/app/api/auth/login/route.ts
  • src/app/api/auth/logout/route.ts (now takes request to match flags on clear)
  • src/app/api/auth/setup/route.ts
  • src/middleware.ts (session cookie)

.env.example documents the two override vars.

Tests

  • New __tests__/auth-cookie.test.ts covering isSecureRequest, shouldUseSecureCookie (protocol default + both env overrides + precedence), and authCookieOptions.
  • Full suite: 12/12 passing. next build ✓, tsc --noEmit clean on all changed files.

🤖 Generated with Claude Code

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>
@ruanbarroso
ruanbarroso merged commit 7d079ca into main Jun 1, 2026
0 of 3 checks passed
@ruanbarroso
ruanbarroso deleted the fix/auth-cookie-secure-flag branch June 1, 2026 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant