Skip to content

Commit

Permalink
Prevent throw on undefined/null secret
Browse files Browse the repository at this point in the history
  • Loading branch information
JackuB committed Mar 30, 2015
1 parent e46ca66 commit 0fdf78d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ module.exports.verify = function(jwtString, secretOrPublicKey, options, callback
return done(new JsonWebTokenError('jwt signature is required'));
}

if (typeof secretOrPublicKey === "undefined" || secretOrPublicKey === null) // secretOrPublicKey can be empty string
return done(new JsonWebTokenError('secret or publick key must be provided'));

if (!options.algorithms) {
options.algorithms = ~secretOrPublicKey.toString().indexOf('BEGIN CERTIFICATE') ||
~secretOrPublicKey.toString().indexOf('BEGIN PUBLIC KEY') ?
Expand Down
21 changes: 21 additions & 0 deletions test/undefined_secretOrPublickey.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var fs = require('fs');
var path = require('path');
var jwt = require('../index');
var JsonWebTokenError = require('../lib/JsonWebTokenError');
var expect = require('chai').expect;

var TOKEN = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.t-IDcSemACt8x4iTMCda8Yhe3iZaWbvV5XKSTbuAn0M';

describe('verifying without specified secret or public key', function () {
it('should not verify null', function () {
expect(function () {
jwt.verify(TOKEN, null);
}).to.throw(JsonWebTokenError, /secret or publick key must be provided/);
});

it('should not verify undefined', function () {
expect(function () {
jwt.verify(TOKEN);
}).to.throw(JsonWebTokenError, /secret or publick key must be provided/);
});
});

0 comments on commit 0fdf78d

Please sign in to comment.