Skip to content

fix(session): add httpOnly, Secure, and SameSite=strict cookie flags#576

Open
anshul23102 wants to merge 1 commit into
GitMetricsLab:mainfrom
anshul23102:fix/session-cookie-security-554
Open

fix(session): add httpOnly, Secure, and SameSite=strict cookie flags#576
anshul23102 wants to merge 1 commit into
GitMetricsLab:mainfrom
anshul23102:fix/session-cookie-security-554

Conversation

@anshul23102
Copy link
Copy Markdown
Contributor

Summary

Fixes #554

The express-session middleware was configured with no cookie options, so the session cookie was issued with browser defaults.

Attack vectors closed

Flag Risk without it Fix applied
httpOnly Any JS on the page reads document.cookie and exfiltrates the session token httpOnly: true
Secure Cookie sent over plain HTTP, readable by passive network observers secure: isProduction (on in prod, off for local HTTP dev)
SameSite Any cross-origin request automatically includes the cookie, enabling CSRF sameSite: 'strict'

Also added maxAge: 24h as an explicit session TTL.

Change

backend/server.js

const isProduction = process.env.NODE_ENV === 'production';

app.use(session({
    secret: process.env.SESSION_SECRET,
    resave: false,
    saveUninitialized: false,
    cookie: {
        httpOnly: true,
        secure: isProduction,
        sameSite: 'strict',
        maxAge: 24 * 60 * 60 * 1000,
    },
}));

Test Plan

  • Login and open DevTools > Application > Cookies. Confirm cookie has HttpOnly and SameSite=Strict checked.
  • In production, confirm Secure flag is present.
  • Confirm document.cookie returns an empty string (HttpOnly prevents JS access).

Important

GSSoC'26 contribution - Please add labels (gssoc26, type:security, level:intermediate) to help with point tracking. Thank you!

Fixes GitMetricsLab#554

The session cookie was issued with browser defaults because no cookie
options were set. This exposed three attack vectors:

- No httpOnly: any JavaScript on the page could read the session cookie
  via document.cookie and exfiltrate it to an attacker server.
- No Secure: the cookie was sent over plain HTTP, allowing passive
  observers on the same network to capture it.
- No SameSite: every cross-origin request the browser made to this
  origin included the cookie, enabling CSRF attacks.

Added httpOnly:true, sameSite:'strict', and secure conditioned on
NODE_ENV=production so local HTTP development still works. Also set
maxAge to 24 hours as an explicit session TTL.
@netlify
Copy link
Copy Markdown

netlify Bot commented May 27, 2026

Deploy Preview for github-spy ready!

Name Link
🔨 Latest commit 89c789e
🔍 Latest deploy log https://app.netlify.com/projects/github-spy/deploys/6a170f6d07df0900081c8f4d
😎 Deploy Preview https://deploy-preview-576--github-spy.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 27, 2026

Warning

Review limit reached

@anshul23102, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 20 minutes and 38 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f64cbfb8-b199-4cf1-974d-16a1a4183be9

📥 Commits

Reviewing files that changed from the base of the PR and between 4ae0ef6 and 89c789e.

📒 Files selected for processing (1)
  • backend/server.js
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@anshul23102
Copy link
Copy Markdown
Contributor Author

Hey @GitMetricsLab, pinging this as part of NSoC'26. Let me know if there is anything to address. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] [GSSoC'26] express-session cookie lacks httpOnly, Secure, and SameSite flags: all authenticated sessions are vulnerable to XSS hijack and CSRF

1 participant