Repository navigation
fix: send Set-Cookie when only the session cookie changes - #1150
saadkhantareen wants to merge 1 commit into
Conversation
yxshee
left a comment
There was a problem hiding this comment.
I checked this out at 8635663: 175 passing, and the new tests fail on master. I also ran a few flows against an existing session (resave: false, saveUninitialized: false):
- Changing
maxAge,expires,httpOnly,pathorsameSitenow sendsSet-Cookie. On master none of them did. 👍 - Untouched requests still send nothing: 0 of 200 with
maxAge: 60000, 0 of 50 withoutmaxAge, 0 of 20 with a 30 ms handler, and 0 of 20 with an explicittouch()+save(). Assigning the samemaxAgeagain also sends nothing. - No textual conflicts with #1151 or #1152 (
git merge-tree).
One edge case: cookieHash includes originalMaxAge, and originalMaxAge isn't always stable across the automatic touch(). The maxAge setter builds expires from Date.now() + ms, and the expires setter then reads this.maxAge, which calls Date.now() again. If the millisecond clock ticks between those two calls, originalMaxAge comes out 1 ms short (60000 → 59999). The hash then changes and an untouched request gets a Set-Cookie. It's rare in practice (it didn't show up in the 200 requests above), but I could force it by making the second Date.now() read 1 ms later: with this branch the untouched request got a Set-Cookie, and on master it didn't.
It's harmless when it happens (the cookie is re-sent with a fresh expiry, much like rolling). If you'd rather the result not depend on timing, hashing originalMaxAge rounded to whole seconds would absorb the drift. That should be fine, since Max-Age/Expires only carry seconds anyway.
Summary
Fixes #1002. When a handler extends an existing session by changing only the cookie (for example setting
req.session.cookie.maxAgeto a larger value, then callingreq.session.touch()andreq.session.save()), the response contains noSet-Cookieheader, so the browser keeps the old expiry and the session is never actually extended client-side.The session data hash used for change detection (
hash()) deliberately ignores thecookieproperty, which is correct for save/resave decisions, but it also makes cookie-only changes invisible toshouldSetCookie(). For an existing session withrolling: false, the cookie was therefore only re-sent when the session data itself changed.Changes
index.js: snapshot a hash of the session cookie when the session is generated or inflated, and treat the cookie as modified when that snapshot no longer matches.shouldSetCookie()now also returnstruefor an existing session whose cookie changed during the request.cookie.datawithexpiresexcluded, because the expiry is refreshed by the automatic touch on every request and cannot take part in change detection. ChangingmaxAgestill registers, since setting it updatesoriginalMaxAgethrough theexpiressetter insession/cookie.js.test/session.js: regression tests covering the reported flow, plus a control test that an untouched session still receives noSet-Cookie.Tests
npm test— 175 passing, 0 failing (includes 2 new tests under "cookie option > .maxAge changed without modifying the session": "should set-cookie when only the cookie is extended and saved" and "should not set-cookie when nothing changed").index.jschange (AssertionError: should have a cookie header) and pass with it.npm run lint— clean.cookie.maxAgeto 24h, touches and saves): before the fix the second response had noSet-Cookie; after the fix it carriesSet-Cookiewith an expiry ~24 hours out.