Check payload is not null when decoded. (#444)

* Check payload is not null when decoded.
Fixed "Cannot read property 'nbf' of null"

* Condition on obj !== null for clarity

* Added test for decoding null token
This commit is contained in:
Nicolas Cognaux
2018-04-05 13:28:41 +02:00
committed by Eduardo Diaz
parent cc7c41b109
commit e50764b070
2 changed files with 13 additions and 1 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ module.exports = function (jwt, options) {
if(typeof payload === 'string') {
try {
var obj = JSON.parse(payload);
if(typeof obj === 'object') {
if(obj !== null && typeof obj === 'object') {
payload = obj;
}
} catch (e) { }
+12
View File
@@ -0,0 +1,12 @@
var jwt = require('../index');
var expect = require('chai').expect;
var atob = require('atob');
describe('decoding', function() {
it('should not crash when decoding a null token', function () {
var decoded = jwt.decode("null");
expect(decoded).to.equal(null);
});
});