Skip to content

Commit dea96f1

Browse files
committed
Add HttpOnly, Secure, SameSite, and maxAge flags to session cookie
express-session was configured with no cookie options, leaving the connect.sid session identifier readable via document.cookie (no HttpOnly), transmittable over HTTP (no Secure), and attachable by any cross-site request (no SameSite). Add cookie flags with environment-aware values: Secure and SameSite=Strict are active only when NODE_ENV=production so that local development over plain HTTP continues to work without disabling cookie behaviour. SameSite=lax in development still blocks third-party POST CSRF while allowing top-level navigation redirects. Set maxAge to 24 hours to bound the lifetime of an exposed session ID. Add ENV NODE_ENV=production to Dockerfile.prod so the production cookie policy activates automatically in containerised deploys. Document NODE_ENV in .env.sample so local developers set it explicitly. Closes #373
1 parent 8d17610 commit dea96f1

3 files changed

Lines changed: 10 additions & 0 deletions

File tree

backend/.env.sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
PORT=5000
22
MONGO_URI=mongodb://localhost:27017/githubTracker
33
SESSION_SECRET=your-secret-key
4+
NODE_ENV=development

backend/Dockerfile.prod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ RUN npm install --production
1313
# Copy the rest of the application files
1414
COPY . .
1515

16+
# Set production environment so session cookies get Secure + SameSite=Strict
17+
ENV NODE_ENV=production
18+
1619
# Expose the port for the application
1720
EXPOSE 5000
1821

backend/server.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ app.use(session({
2020
secret: process.env.SESSION_SECRET,
2121
resave: false,
2222
saveUninitialized: false,
23+
cookie: {
24+
httpOnly: true,
25+
secure: process.env.NODE_ENV === 'production',
26+
sameSite: process.env.NODE_ENV === 'production' ? 'strict' : 'lax',
27+
maxAge: 24 * 60 * 60 * 1000,
28+
},
2329
}));
2430
app.use(passport.initialize());
2531
app.use(passport.session());

0 commit comments

Comments
 (0)