Skip to content
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

res.clearCookie() now ignores maxAge #4852

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,7 @@ res.get = function(field){

res.clearCookie = function clearCookie(name, options) {
var opts = merge({ expires: new Date(1), path: '/' }, options);
delete opts.maxAge;
Comment on lines 807 to +808
Copy link
Member

@jonchurch jonchurch May 4, 2024

Choose a reason for hiding this comment

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

Suggested change
var opts = merge({ expires: new Date(1), path: '/' }, options);
delete opts.maxAge;
var opts = merge({ path: '/' }, options);
// Force cookie expiration by setting `expires` to a past date
opts.expires = new Date(1);
// Set maxAge for modern browsers to immediately delete the cookie regardless of system time
opts.maxAge = 0

Let's do this, to also ensure they cannot overwrite expires via the options object either

Copy link
Member

Choose a reason for hiding this comment

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

Hold off on this until we get clarity on #4851 (comment) however

Copy link
Member

Choose a reason for hiding this comment

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

we can't land this in v4, so will need to target the v5 dev branch


return this.cookie(name, '', opts);
};
Expand Down
13 changes: 13 additions & 0 deletions test/res.clearCookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,18 @@ describe('res', function(){
.expect('Set-Cookie', 'sid=; Path=/admin; Expires=Thu, 01 Jan 1970 00:00:00 GMT')
.expect(200, done)
})

it('should ignore maxAge', function(done){
var app = express();

app.use(function(req, res){
res.clearCookie('sid', { path: '/admin', maxAge: 900 }).end();

Choose a reason for hiding this comment

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

  // ... blah blah blah
  [Symbol(kOutHeaders)]: [Object: null prototype] {
    'x-powered-by': [ 'X-Powered-By', 'Express' ],
    'set-cookie': [
      'Set-Cookie',
      'sid=; Path=/admin; Expires=Thu, 01 Jan 1970 00:00:00 GMT'
    ]
  }
}

});

request(app)
.get('/')
.expect('Set-Cookie', 'sid=; Path=/admin; Expires=Thu, 01 Jan 1970 00:00:00 GMT')
.expect(200, done)
})

Choose a reason for hiding this comment

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

Takes the shape:

// ... blah blah blah
 _maxListeners: undefined,
  _enableHttp2: false,
  _agent: false,
  _formData: null,
  method: 'GET',
  url: 'http://127.0.0.1:46159/',
  _header: {},
  header: {},
  writable: true,
  _redirects: 0,
  _maxRedirects: 0,
  cookies: '',
// ... blah blah blah

})
})