-
-
Notifications
You must be signed in to change notification settings - Fork 17.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
clearCookie should set maxAge not expires #3856
Comments
Everything you said is right, except as you can see in the current implementation, a user is allowed to even override the expires date. The clearCookie can be used to just clear the value and not actually delete the cookie if the user doesn't want to. This is why explicitly passing in either expires or maxAge will cause the cookie to remain in the browser, as that is currently how res.clearCookie is designed. This means that you are observing the correct behavior which is not a bug. Now, that being said, perhaps the discussion is one (or both?) of the following:
|
Assuming this is the right place to look, IMHO there are some ways to make the docs clearer. (This is less about express and more about the sometimes surprising semantics of cookies.) For example, having an example specifically for deleting cookies, and mentioning that
Perhaps not? If Anyways, thanks for the additional detail. I guess it makes sense to close this out as working as intended, and I'll try to find the time to submit a PR to clarify the documentation to hopefully prevent other folk from making the same faulty assumptions that I did. |
For what it's worth I just ran into this and think the current logic is not really defensible.
A much more obvious way to do that would be to use Also note that the I don't feel like we can blame standards here. The standards don't have a Now I recognize this is probably a pointless rant as it probably should be considered a breaking change. Just wanted to put this out there. Carry on. |
Calling
res.clearCookie
will fail to delete a cookie if amaxAge
value is passed in the cookie's options. This is because of a specific detail in how theSet-Cookie
header works and what I believe to be a bug in the Express implementation. (This may be the root cause for some users who came across #691.)Repro
Cause
The problem is rooted in the implementation of
clearCookie
:The
expires
field is set, which if that timestamp has passed, the cookie will be deleted. However, there is a separate cookie settingmaxAge
which can be used as well. And from the RFC, themaxAge
setting takes precedence.https://tools.ietf.org/html/rfc6265#section-4.1
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie
So if you call
clearCookie
with the cookie settings that contains amaxAge
, the result will be both theexpires
andmaxAge
properties will be set. And since themaxAge
property will take precedence, the cookie will not be deleted. (It's value will be cleared out, however.)I believe the right fix would just to be explicitly set
maxAge
on the merged cookie options, intentionally overwriting whatever settings were passed by the user. Since obviously the intent is to clear the cookie and not persist it. e.g.Assuming this all sounds right, I'm happy to submit a PR to fix it.
The text was updated successfully, but these errors were encountered: