Skip to content

Commit

Permalink
Fix signedCookie to return undefined for non-string arguments
Browse files Browse the repository at this point in the history
closes #18
  • Loading branch information
Alex Goretoy authored and dougwilson committed Sep 18, 2015
1 parent 54c8977 commit 8a9907d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 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
==========

* Fix `JSONCookie` to return `undefined` for non-string arguments
* Fix `signedCookie` to return `undefined` for non-string arguments
* deps: cookie@0.1.5

1.3.5 / 2015-05-19
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ function JSONCookies(obj) {
*/

function signedCookie(str, secret) {
if (typeof str !== 'string') {
return undefined;
}

return str.substr(0, 2) === 's:'
? signature.unsign(str.slice(2), secret)
: str;
Expand Down
9 changes: 9 additions & 0 deletions test/cookieParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ describe('cookieParser.JSONCookie(str)', function () {
})

describe('cookieParser.signedCookie(str, secret)', function () {
it('should return undefined for non-string arguments', function () {
assert.strictEqual(cookieParser.signedCookie(undefined, 'keyboard cat'), undefined)
assert.strictEqual(cookieParser.signedCookie(null, 'keyboard cat'), undefined)
assert.strictEqual(cookieParser.signedCookie(42, 'keyboard cat'), undefined)
assert.strictEqual(cookieParser.signedCookie({}, 'keyboard cat'), undefined)
assert.strictEqual(cookieParser.signedCookie([], 'keyboard cat'), undefined)
assert.strictEqual(cookieParser.signedCookie(function(){}, 'keyboard cat'), undefined)
})

it('should pass through non-signed string', function () {
assert.strictEqual(cookieParser.signedCookie('', 'keyboard cat'), '')
assert.strictEqual(cookieParser.signedCookie('foo', 'keyboard cat'), 'foo')
Expand Down

0 comments on commit 8a9907d

Please sign in to comment.