Task #2591: Hardening Cookies Flags - #2580
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThe changes update Django cookie security settings and simplify HTMX CSRF header configuration for authenticated users on the profile page. ChangesCookie security and CSRF handling
Estimated code review effort: 2 (Simple) | ~10 minutes Merge Risk: ⚪ Minimal · up to The PR hardens session and CSRF cookie handling and updates the affected profile-page request flow; no actionable merge-blocking risk remains beyond normal checks and review. Possibly related issues
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 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 |
|
Good call on the lax instead of strict to not have problems with allauth callbacks. |
81d95a1 to
3c9ab6e
Compare
Issue: #2591
Summary & Context
Session and CSRF cookies were being set without
SecureorHttpOnly. This turns those flags on in every deployed environment, pinsSameSiteexplicitly, and removes the one piece of our JavaScript that depended on reading the session cookie.manage.py check --deployno longer reportssecurity.W012,security.W015, orsecurity.W016.v3waffle flag off)Changes
SESSION_COOKIE_SECUREandCSRF_COOKIE_SECUREtoTruein the existing non-local branch alongsideSECURE_PROXY_SSL_HEADER, so local development still works over plain HTTP.SESSION_COOKIE_HTTPONLYtoTrue— it was explicitlyFalse, an opt-out of Django's own default.CSRF_COOKIE_HTTPONLYwas already on.SESSION_COOKIE_SAMESITEandCSRF_COOKIE_SAMESITEto"Lax"explicitly. That matches Django's current default, so nothing changes today; it exists to stop a future default change or unrelated edit from silently loosening it."Strict"is deliberately avoided — the social auth callback is a cross-site redirect back into the site and must still carry the session cookie.templates/users/profile.htmlwith a server-side{% if request.user.is_authenticated %}guard around the script. This has to ship in the same PR: the old script attachedX-CSRFTokenonly if it could findconfig-sessionidindocument.cookie, so turning onHttpOnlywithout this edit would have silently dropped the header and broken every htmx POST on the page. The guard preserves the original intent without reading a cookie; the token is still the server-rendered{{ csrf_token }}. This was the last place in our own JavaScript that read the session cookie.Verified: the
Secureflag will not lock anyone out. Every one of the 20 hostnames across the production, stage and cppal-devALLOWED_HOSTSreturns a 301 fromhttp://tohttps://— checked live, no plain-HTTP origin anywhere. Enforced at the edge (Fastly, then a GCE IngressFrontendConfig.redirectToHttpson production and a GKE GatewayredirectHTTPRoute on all three), not by Django.Existing sessions keep their old cookie attributes until the cookie is re-issued, so anyone already logged in stays on a non-
HttpOnly, non-Securecookie until they log out and back in. No action needed, but the flags aren't universally in effect the moment this deploys.templates/users/profile.htmlonly renders when thev3waffle flag is off; the v3 profile templates take their CSRF token from{{ csrf_token }}and never read cookies.The vendored boost-gecko search bundle ships Algolia's insights code, which can read and write an
_ALGOLIAcookie. We pass it no insights configuration, and the write is gated on auseCookieoption we never set, so no such cookie is created. Unrelated to the session cookie either way.Peer-Testing Guidelines
v3waffle flag is off — the changed template is the pre-v3 profile page.config-sessionidshowsHttpOnlyandSameSite=Lax.Securewill be absent locally — expected, it's gated behindLOCAL_DEVELOPMENT.document.cookieand confirmconfig-sessionidis no longer listed.docker compose exec -e LOCAL_DEVELOPMENT=False web python manage.py check --deployshould report noW012,W015, orW016.Self-review Checklist
Frontend
N/A
Summary by CodeRabbit