Skip to content

Commit b92fca9

Browse files
committed
When decoding, if JWT payload is not valid it returns null
1 parent 585d0e1 commit b92fca9

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

lib/verify-stream.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,13 @@ function jwsDecode(jwsSig, opts) {
6767
return null;
6868

6969
var payload = payloadFromJWS(jwsSig);
70-
if (header.typ === 'JWT' || opts.json)
71-
payload = JSON.parse(payload, opts.encoding);
70+
if (header.typ === 'JWT' || opts.json){
71+
try {
72+
payload = JSON.parse(payload, opts.encoding);
73+
} catch (e) {
74+
return null;
75+
}
76+
}
7277

7378
return {
7479
header: header,

test/jws.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ test('Streaming verify: ECDSA, with invalid key', function (t) {
222222
test('jws.decode: not a jws signature', function (t) {
223223
t.same(jws.decode('some garbage string'), null);
224224
t.same(jws.decode('http://sub.domain.org'), null);
225+
t.same(jws.decode('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e3.t-IDcSemACt8x4iTMCda8Yhe3iZaWbvV5XKSTbuAn0M'), null);
225226
t.end();
226227
});
227228

0 commit comments

Comments
 (0)