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

@jonchurchjonchurchMay 4, 2024

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
varopts=merge({expires: newDate(1),path: '/'},options);
deleteopts.maxAge;
varopts=merge({path: '/'},options);
// Force cookie expiration by setting `expires` to a past date
opts.expires=newDate(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
Copy Markdown
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
Copy Markdown
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 numberDiff line numberDiff 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();

Copy link
Copy Markdown

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)
})

Copy link
Copy Markdown

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

})
})