Skip to content

Commit

Permalink
Fix maxAge option to reject invalid values
Browse files Browse the repository at this point in the history
fixes #103
closes #104
  • Loading branch information
smondal authored and dougwilson committed Apr 22, 2020
1 parent 7e1398f commit e248786
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
unreleased
==========

* Fix `maxAge` option to reject invalid values

0.4.0 / 2019-05-15
==================

Expand Down
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ function serialize(name, val, options) {

if (null != opt.maxAge) {
var maxAge = opt.maxAge - 0;
if (isNaN(maxAge)) throw new Error('maxAge should be a Number');

if (isNaN(maxAge) || !isFinite(maxAge)) {
throw new TypeError('option maxAge is invalid')
}

str += '; Max-Age=' + Math.floor(maxAge);
}

Expand Down
8 changes: 7 additions & 1 deletion test/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ test('maxAge', function() {
cookie.serialize('foo', 'bar', {
maxAge: 'buzz'
});
}, /maxAge should be a Number/);
}, /option maxAge is invalid/)

assert.throws(function () {
cookie.serialize('foo', 'bar', {
maxAge: Infinity
})
}, /option maxAge is invalid/)

assert.equal('foo=bar; Max-Age=1000', cookie.serialize('foo', 'bar', {
maxAge: 1000
Expand Down

0 comments on commit e248786

Please sign in to comment.