Skip to content

Modifying session.cookie.expires does not send an updated Set-Cookie header #452

@kblomster

Description

@kblomster

This is, as far as I can tell, essentially the same as issue #189 from way back in 2015.

One would think that if session.cookie.expires (or maxAge, for that matter) is modified, a Set-Cookie header would be set on the response with the new expiration time. The documentation has an example that strongly implies that this is supposed to be possible. Unless you have rolling set to true, though, that doesn't actually happen. If the session store supports touch, the stored session will be touched properly with the modified expiration time, but the session cookie will never be updated, neither in the store nor in the browser.

The browser cookie will never get updated because shouldSetCookie ends up checking for req.session.cookie.expires != null && isModified(req.session) (index.js line 447), and isModified will always return false if the only thing you changed was the cookie, because the hash function ignores the cookie part of the session data (index.js line 582). Changing anything else (other than the expiration) in the cookie also has the same issue, of course, but that's a more exotic use case.

The same root cause (hash ignoring the entire cookie part of the session) also causes another annoyance. If you're using a touch-supporting session store that separates the storage expiration from the expiration timestamp inside the session data itself (probably most of them; connect-session-sequelize does it and I believe connect-redis does as well), rolling doesn't work properly without resave, as pointed out in the issue referenced above, because shouldSave ends up checking for !isSaved, which does the same thing - checks if the hash has changed, and it hasn't because the cookie part is ignored. The session store is touched properly because shouldTouch does the right thing, so the data storage timeout is updated, but not the actual session data which contains the cookie expiration. Unless you change something else in the session that makes the store resave it, req.session.cookie will always retain its original expiration timestamp and maxAge will keep counting down.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions