strict check of jwt token part count (avoid "TypeError: Cannot read property 'payload' of null" in some cases)

This commit is contained in:
Nico Kaiser
2014-08-14 11:22:49 +02:00
parent 64f4860623
commit 70579b14c8
2 changed files with 13 additions and 1 deletions
+1 -1
View File
@@ -36,7 +36,7 @@ module.exports.verify = function(jwtString, secretOrPublicKey, options, callback
if (!options) options = {};
var parts = jwtString.split('.');
if (parts.length < 3)
if (parts.length !== 3)
return callback(new Error('jwt malformed'));
if (parts[2].trim() === '' && secretOrPublicKey)
+12
View File
@@ -161,6 +161,18 @@ describe('RS256', function() {
});
});
describe('when decoding a jwt token with additional parts', function() {
var token = jwt.sign({ foo: 'bar' }, priv, { algorithm: 'RS256' });
it('should throw', function(done) {
jwt.verify(token + '.foo', pub, function(err, decoded) {
assert.isUndefined(decoded);
assert.isNotNull(err);
done();
});
});
});
describe('when decoding a invalid jwt token', function() {
it('should return null', function(done) {
var payload = jwt.decode('whatever.token');