fix(auth): harden JWT cookie with secure and sameSite flags#106
fix(auth): harden JWT cookie with secure and sameSite flags#106sricharan12-hub wants to merge 1 commit into
Conversation
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
|
Warning Review limit reached
Next review available in: 56 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the 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 configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
Overview
Hardens the authentication JWT cookie in
backend/src/controllers/userAuth.js.Previously the cookie was set with only
httpOnly, so it was attached tocross-site requests (no CSRF protection) and could be sent over plain HTTP in
production.
Closes #83
Changes
cookieOptionsobject withsecureandsameSite:secure: isProd— HTTPS-only in production, off in local HTTP development.sameSite: isProd ? "none" : "lax"—"none"supports a cross-sitefrontend/API in production (requires
secure),"lax"keeps local dev working.cookieOptionsinregister,login, andlogoutso the cookie isset and cleared with matching attributes — required for the browser to
reliably remove it on logout.
httpOnly(JS-access protection) is preserved; this adds CSRF andinsecure-transport protection on top.
Acceptance criteria
sameSiteandsecure(secure enabled in production).register,login, andlogout.Testing
node --checkpasses and the module imports cleanly (import()resolves without error).NODE_ENV !== "production"):secure=false,sameSite=lax— cookie works over local HTTP.NODE_ENV === "production"):secure=true,sameSite=none— cookie restricted to HTTPS.