Skip to content

fix(auth): harden JWT cookie with secure and sameSite flags#106

Open
sricharan12-hub wants to merge 1 commit into
niharika-mente:mainfrom
sricharan12-hub:fix/auth-cookie-secure-samesite
Open

fix(auth): harden JWT cookie with secure and sameSite flags#106
sricharan12-hub wants to merge 1 commit into
niharika-mente:mainfrom
sricharan12-hub:fix/auth-cookie-secure-samesite

Conversation

@sricharan12-hub

Copy link
Copy Markdown

Overview

Hardens the authentication JWT cookie in backend/src/controllers/userAuth.js.
Previously the cookie was set with only httpOnly, so it was attached to
cross-site requests (no CSRF protection) and could be sent over plain HTTP in
production.

Closes #83

Changes

  • Define a shared cookieOptions object with secure and sameSite:
    • secure: isProd — HTTPS-only in production, off in local HTTP development.
    • sameSite: isProd ? "none" : "lax""none" supports a cross-site
      frontend/API in production (requires secure), "lax" keeps local dev working.
  • Reuse cookieOptions in register, login, and logout so the cookie is
    set and cleared with matching attributes — required for the browser to
    reliably remove it on logout.

httpOnly (JS-access protection) is preserved; this adds CSRF and
insecure-transport protection on top.

Acceptance criteria

  • Auth cookie is set with sameSite and secure (secure enabled in production).
  • The same attributes are applied in register, login, and logout.
  • Login/logout continue to work in local development (HTTP) and in production (HTTPS).

Testing

  • node --check passes and the module imports cleanly (import() resolves without error).
  • In development (NODE_ENV !== "production"): secure=false, sameSite=lax — cookie works over local HTTP.
  • In production (NODE_ENV === "production"): secure=true, sameSite=none — cookie restricted to HTTPS.

The auth token cookie was set with only httpOnly, leaving the session
cookie sent on cross-site requests (no CSRF protection) and transmittable
over plain HTTP in production.

- Define shared cookieOptions with secure and sameSite, enabling secure
  and sameSite=none in production while keeping sameSite=lax over HTTP in
  development so local login continues to work.
- Reuse the options in register, login, and logout so the cookie is set
  and cleared with matching attributes (required for reliable removal).

Closes niharika-mente#83
@coderabbitai

coderabbitai Bot commented Jul 2, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@sricharan12-hub, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 56 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

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.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 1f72b4c9-cb6b-4acf-8a45-19a6cf81cb70

📥 Commits

Reviewing files that changed from the base of the PR and between 87148ba and da74ae8.

📒 Files selected for processing (1)
  • backend/src/controllers/userAuth.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.

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.

Auth JWT cookie is missing secure and sameSite flags (CSRF / insecure transport risk)

1 participant