Skip to content

Commit

Permalink
Fix expires option to reject invalid dates
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Mar 29, 2022
1 parent 7fab32e commit 042073f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ unreleased
==========

* Add `priority` option
* Fix `expires` option to reject invalid dates

0.4.2 / 2022-02-02
==================
Expand Down
19 changes: 17 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ exports.serialize = serialize;
* @private
*/

var __toString = Object.prototype.toString
var decode = decodeURIComponent;
var encode = encodeURIComponent;

Expand Down Expand Up @@ -145,11 +146,13 @@ function serialize(name, val, options) {
}

if (opt.expires) {
if (typeof opt.expires.toUTCString !== 'function') {
var expires = opt.expires

if (!isDate(expires) || isNaN(expires.valueOf())) {
throw new TypeError('option expires is invalid');
}

str += '; Expires=' + opt.expires.toUTCString();
str += '; Expires=' + expires.toUTCString()
}

if (opt.httpOnly) {
Expand Down Expand Up @@ -205,6 +208,18 @@ function serialize(name, val, options) {
return str;
}

/**
* Determine if value is a Date.
*
* @param {*} val
* @private
*/

function isDate (val) {
return __toString.call(val) === '[object Date]' ||
val instanceof Date
}

/**
* Try decoding a string using a decoding function.
*
Expand Down
4 changes: 4 additions & 0 deletions test/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ test('expires', function() {
assert.throws(cookie.serialize.bind(cookie, 'foo', 'bar', {
expires: Date.now()
}), /option expires is invalid/);

assert.throws(cookie.serialize.bind(cookie, 'foo', 'bar', {
expires: new Date(NaN)
}), /option expires is invalid/)
});

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

0 comments on commit 042073f

Please sign in to comment.