Skip to content

Commit

Permalink
Fix "maxAge" option to reject invalid values
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Apr 14, 2022
1 parent f5b5b31 commit 0c77dd6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ unreleased

* Add `priority` option for Priority cookie support
* Fix accidental cookie name/value truncation when given invalid chars
* Fix `maxAge` option to reject invalid values
* Remove quotes from returned quoted cookie value
* Use `req.socket` over deprecated `req.connection`
* pref: small lookup regexp optimization
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ function Cookie(name, value, attrs) {
throw new TypeError('option domain is invalid');
}

if (typeof this.maxAge === 'number' ? (isNaN(this.maxAge) || !isFinite(this.maxAge)) : this.maxAge) {
throw new TypeError('option maxAge is invalid')
}

if (this.priority && !PRIORITY_REGEXP.test(this.priority)) {
throw new TypeError('option priority is invalid')
}
Expand Down
18 changes: 18 additions & 0 deletions test/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@ describe('new Cookie(name, value, [options])', function () {
var cookie = new cookies.Cookie('foo', 'bar', { maxAge: 86400 })
assert.equal(cookie.maxage, 86400)
})

it('should throw on invalid value', function () {
assert.throws(function () {
new cookies.Cookie('foo', 'bar', { maxAge: 'foo' })
}, /option maxAge is invalid/)
})

it('should throw on Infinity', function () {
assert.throws(function () {
new cookies.Cookie('foo', 'bar', { maxAge: Infinity })
}, /option maxAge is invalid/)
})

it('should throw on NaN', function () {
assert.throws(function () {
new cookies.Cookie('foo', 'bar', { maxAge: NaN })
}, /option maxAge is invalid/)
})
})

describe('priority', function () {
Expand Down

0 comments on commit 0c77dd6

Please sign in to comment.