Skip to content

Cookie Expiration Age Fix #7327

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

jonathandunne
Copy link

Fixes #7301

@jonathandunne jonathandunne requested a review from a team as a code owner April 26, 2025 03:23
Copy link
Member

@code-asher code-asher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the contribution! Looks good to me so far. We will have to add it to src/node/cli.ts as well.

The . syntax is intriguing, but I want to say we call it auth-cookie-max-age for now.

@@ -326,6 +335,7 @@ export const getCookieOptions = (req: express.Request): express.CookieOptions =>
domain: getCookieDomain(url.host, req.args["proxy-domain"]),
path: normalize(url.pathname) || "/",
sameSite: "lax",
maxAge: getConfigCookieMaxAgeAsMilliseconds(req) || 0,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wait is 0 the right default? This will delete the cookie instantly, I think? We may want -1.

Copy link
Member

@code-asher code-asher Apr 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm actually zero or negative immediately expires the cookie it seems.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Set-Cookie#max-agenumber
https://datatracker.ietf.org/doc/html/rfc6265#section-5.2.2

Should it just be undefined? Also we may want ?? instead of || in case someone does explicitly set it to zero for some reason.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd read it in the Express JS docs setting it to 0 makes it session
https://expressjs.com/en/api.html#res.cookie
image

They gave this explanation to the expires parameter, but also said maxAge is a "Convenient option for setting the expiry time relative to the current time in milliseconds". They didn't specify what to put here to make it session, nor what the default is.

Copy link
Member

@code-asher code-asher Apr 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I also thought maybe it was an Express thing, but when I checked the source it seems to just immediately expire.

If maxAge is zero, it passes expires set to now and maxAge set to zero into cookie.serialize():

https://github.com/expressjs/express/blob/f9954dd317a1b534e62a5ae3aa7f52f3582b8881/lib/response.js#L764-L771

cookie.serialize() simply adds Max-Age=0 and Expires=<the now date> to the cookie:

https://github.com/jshttp/cookie/blob/2a1a4d8b2679b208f354e848e711dc8471fb83af/src/index.ts#L268-L274

Now, if expires is zero (although this is actually a type error), and there is no max age it does indeed skip setting Expires in that case, which would result in a session cookie. The difference is they do if (options.maxAge !== undefined) versus if (options.expires), so zero is not ignored in the max age case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow configuring login cookie with an expiry duration
3 participants