Skip to content

Commit 0c77dd6

Browse files
committed
Fix "maxAge" option to reject invalid values
1 parent f5b5b31 commit 0c77dd6

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

HISTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ unreleased
33

44
* Add `priority` option for Priority cookie support
55
* Fix accidental cookie name/value truncation when given invalid chars
6+
* Fix `maxAge` option to reject invalid values
67
* Remove quotes from returned quoted cookie value
78
* Use `req.socket` over deprecated `req.connection`
89
* pref: small lookup regexp optimization

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ function Cookie(name, value, attrs) {
172172
throw new TypeError('option domain is invalid');
173173
}
174174

175+
if (typeof this.maxAge === 'number' ? (isNaN(this.maxAge) || !isFinite(this.maxAge)) : this.maxAge) {
176+
throw new TypeError('option maxAge is invalid')
177+
}
178+
175179
if (this.priority && !PRIORITY_REGEXP.test(this.priority)) {
176180
throw new TypeError('option priority is invalid')
177181
}

test/cookie.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,24 @@ describe('new Cookie(name, value, [options])', function () {
6161
var cookie = new cookies.Cookie('foo', 'bar', { maxAge: 86400 })
6262
assert.equal(cookie.maxage, 86400)
6363
})
64+
65+
it('should throw on invalid value', function () {
66+
assert.throws(function () {
67+
new cookies.Cookie('foo', 'bar', { maxAge: 'foo' })
68+
}, /option maxAge is invalid/)
69+
})
70+
71+
it('should throw on Infinity', function () {
72+
assert.throws(function () {
73+
new cookies.Cookie('foo', 'bar', { maxAge: Infinity })
74+
}, /option maxAge is invalid/)
75+
})
76+
77+
it('should throw on NaN', function () {
78+
assert.throws(function () {
79+
new cookies.Cookie('foo', 'bar', { maxAge: NaN })
80+
}, /option maxAge is invalid/)
81+
})
6482
})
6583

6684
describe('priority', function () {

0 commit comments

Comments
 (0)