Skip to content

Commit 5855339

Browse files
cjbarthdougwilson
authored andcommitted
Fix behavior of null/undefined as "maxAge" in res.cookie
fixes #3935 closes #3936
1 parent 1cc8169 commit 5855339

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

History.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ unreleased
44
* Add "root" option to `res.download`
55
* Allow `options` without `filename` in `res.download`
66
* Deprecate string and non-integer arguments to `res.status`
7+
* Fix behavior of `null`/`undefined` as `maxAge` in `res.cookie`
78
* Ignore `Object.prototype` values in settings through `app.set`/`app.get`
89
* Invoke `default` with same arguments as types in `res.format`
910
* Support proper 205 responses using `res.send`

lib/response.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -868,9 +868,13 @@ res.cookie = function (name, value, options) {
868868
val = 's:' + sign(val, secret);
869869
}
870870

871-
if ('maxAge' in opts) {
872-
opts.expires = new Date(Date.now() + opts.maxAge);
873-
opts.maxAge /= 1000;
871+
if (opts.maxAge != null) {
872+
var maxAge = opts.maxAge - 0
873+
874+
if (!isNaN(maxAge)) {
875+
opts.expires = new Date(Date.now() + maxAge)
876+
opts.maxAge = Math.floor(maxAge / 1000)
877+
}
874878
}
875879

876880
if (opts.path == null) {

test/res.cookie.js

+30
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,36 @@ describe('res', function(){
111111
.expect(200, optionsCopy, done)
112112
})
113113

114+
it('should not throw on null', function (done) {
115+
var app = express()
116+
117+
app.use(function (req, res) {
118+
res.cookie('name', 'tobi', { maxAge: null })
119+
res.end()
120+
})
121+
122+
request(app)
123+
.get('/')
124+
.expect(200)
125+
.expect('Set-Cookie', 'name=tobi; Path=/')
126+
.end(done)
127+
})
128+
129+
it('should not throw on undefined', function (done) {
130+
var app = express()
131+
132+
app.use(function (req, res) {
133+
res.cookie('name', 'tobi', { maxAge: undefined })
134+
res.end()
135+
})
136+
137+
request(app)
138+
.get('/')
139+
.expect(200)
140+
.expect('Set-Cookie', 'name=tobi; Path=/')
141+
.end(done)
142+
})
143+
114144
it('should throw an error with invalid maxAge', function (done) {
115145
var app = express()
116146

0 commit comments

Comments
 (0)