@@ -283,47 +283,16 @@ const sessionConfig = {
283283 }
284284} ;
285285
286- // Configure cookie settings based on environment
287- if ( process . env . NODE_ENV === 'production' ) {
288- // Production: HTTPS via CloudFront/ALB
289- sessionConfig . cookie . secure = true ; // Require HTTPS
290-
291- // CRITICAL FIX: Use 'lax' instead of 'none' for CloudFront
292- // CloudFront -> ALB is not cross-site from browser perspective (both appear as same origin)
293- // 'lax' allows cookies on top-level navigation and GET requests
294- // 'none' requires strict CORS and doesn't work well with CloudFront architecture
295- sessionConfig . cookie . sameSite = 'lax' ;
296-
297- // Do NOT set explicit domain - let browser use request origin (CloudFront domain)
298- console . log ( '[SESSION] Production: secure=true, sameSite=lax, httpOnly=true' ) ;
299- } else {
300- // Development: Only allow HTTP (secure=false) if running on true localhost (127.0.0.1 or localhost HOST or origin)
301- // Only disable secure cookies for explicit, trusted local development.
302- const hostHeader = ( process . env . HOST || '' ) . toLowerCase ( ) ;
303- const isLocalhost = (
304- hostHeader === 'localhost' ||
305- hostHeader === '127.0.0.1' ||
306- ( process . env . HOST === undefined && (
307- process . env . NODE_ENV === 'development' || process . env . NODE_ENV === undefined
308- ) )
309- ) ;
286+ // SECURITY: Always enforce secure cookies (HTTPS-only) to prevent session hijacking
287+ sessionConfig . cookie . secure = true ; // Always require HTTPS
288+ sessionConfig . cookie . sameSite = 'lax' ; // CSRF protection
310289
311- if ( isLocalhost ) {
312- // SECURITY WARNING: Cookies sent over HTTP are vulnerable to interception!
313- // Only allow insecure cookies for true localhost development. Never set secure=false outside this case.
314- sessionConfig . cookie . secure = false ; // Allow HTTP only on localhost
315- sessionConfig . cookie . sameSite = 'lax' ; // Allow cross-port requests
316- sessionConfig . cookie . domain = 'localhost' ; // Explicit domain for dev
317- } else {
318- // Outside trusted localhost, always enforce secure cookies!
319- // This prevents cleartext cookie transmission and mitigates session hijacking risk.
320- sessionConfig . cookie . secure = true ;
321- sessionConfig . cookie . sameSite = 'lax' ;
322- // Remove explicit domain for broader compatibility
323- delete sessionConfig . cookie . domain ;
324- }
290+ if ( process . env . NODE_ENV !== 'production' ) {
291+ delete sessionConfig . cookie . domain ; // Flexible domain for development
325292}
326293
294+ console . log ( `[SESSION] secure=true, sameSite=lax, httpOnly=true (${ process . env . NODE_ENV || 'development' } )` ) ;
295+
327296app . use ( session ( sessionConfig ) ) ;
328297
329298// Security: CSRF token endpoint
0 commit comments