res.cookie does not allow setting Max-Age only, without Expires (One should be able to set maxAge alone, with expires: 0)

Open
#5,150 4 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
55/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
javascript, nodejs
Domain
backend

Research direction

Start in lib/response.js around lines 871-878, where res.cookie derives Expires from maxAge. Compare the current options handling with the issue's expires: 0 example, then verify that the resulting Set-Cookie header contains Max-Age and Path without Expires.

Written by the indexing model from the issue text.

Description

discuss enhancement

I want to set a cookie with Max-Age only, without having Expires. But the following lines 875-876 keep adding the unwanted Expires:

https://github.com/expressjs/express/blob/0debedf4f31bb20203da0534719b9b10d6ac9a29/lib/response.js#L871-L878

Setting expires: 0 does no good to override that behavior.

I think one should be allowed to only set Max-Age without automatically having Expires also set.

res.cookie('MyCookie', 'TheValue', {
  expires: 0, // I don't want any Expires in the resulting Set-Cookie statement
  maxAge: 60000,
});
Actual result:
MyCookie=TheValue; Max-Age=60; Path=/; Expires=Sun, 26 Mar 2023 06:00:31 GMT
Desired result:
MyCookie=TheValue; Max-Age=60; Path=/; 
Workaround

The workaround for this is our having to manually write the Set-Cookie statement, possibly mimicking the same logic in the express code.

Possible solution
if (opts.expires !== 0) {
  opts.expires = new Date(Date.now() + maxAge)
}
Dominant language
JavaScript
Stars
69.7k
Forks
25.6k
Avg merge
2d 9h
Merged PRs (30d)
4

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from expressjs/express

All issues in expressjs/express

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.