Skip to content

Commit 1232ae9

Browse files
Gp2mv3ziluvatar
authored andcommitted
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
1 parent e8ac1be commit 1232ae9

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

decode.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = function (jwt, options) {
1010
if(typeof payload === 'string') {
1111
try {
1212
var obj = JSON.parse(payload);
13-
if(typeof obj === 'object') {
13+
if(obj !== null && typeof obj === 'object') {
1414
payload = obj;
1515
}
1616
} catch (e) { }

test/decoding.tests.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var jwt = require('../index');
2+
var expect = require('chai').expect;
3+
var atob = require('atob');
4+
5+
describe('decoding', function() {
6+
7+
it('should not crash when decoding a null token', function () {
8+
var decoded = jwt.decode("null");
9+
expect(decoded).to.equal(null);
10+
});
11+
12+
});

0 commit comments

Comments
 (0)