fix(csrf): remove hardcoded fallback secret and static anonymous session#1599
fix(csrf): remove hardcoded fallback secret and static anonymous session#1599Pcmhacker-piro wants to merge 1 commit into
Conversation
- Replace fallback CSRF_SECRET with a thrown error when missing - Replace static 'anonymous-session' with cryptographically random UUID stored in a session cookie for cross-request consistency - Set httpOnly csrf_anon_id cookie on first CSRF token fetch
|
There is a subtle Express request lifecycle bug in the /api/csrf-token endpoint that will break validation for all anonymous users. The Issue: When u call res.cookie(ANON_SESSION_COOKIE, anonId), it queues the cookie for the response headers, but it does not update req.cookies. When generateToken(req, res) runs on the very next line, getSessionIdentifier still sees req.cookies[ANON_SESSION_COOKIE] as undefined, causing it to generate a second, completely different randomUUID(). Result: The token is signed with a different UUID than the one sent to the browser! The Fix: You just need to mutate the req.cookies object explicitly so generateToken reads the exact same UUID you just created. Add this one-line fix, and we are ready to merge this security patch! |
Closes #1564
Changes
getSecretnow throws ifCSRF_SECRETenv var is not set, instead of falling back to a hardcoded stringcrypto.randomUUID()stored in an httpOnlycsrf_anon_idcookie, instead of the static"anonymous-session"valueSecurity
"fallback-secret-change-in-production")