Skip to content

Commit

Permalink
Check payload is not null when decoded. (#444)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Gp2mv3 authored and ziluvatar committed Apr 5, 2018
1 parent e8ac1be commit 1232ae9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion decode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
Expand Down
12 changes: 12 additions & 0 deletions test/decoding.tests.js
Original file line number Diff line number Diff line change
@@ -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);
});

});

0 comments on commit 1232ae9

Please sign in to comment.