Skip to content

Commit 1b6ec8d

Browse files
Ricardo Gamaziluvatar
Ricardo Gama
authored andcommitted
Fix handling non string tokens (#305)
1 parent 35d8415 commit 1b6ec8d

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

test/issue_304.tests.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
var jwt = require('../index');
2+
var expect = require('chai').expect;
3+
4+
describe('issue 304 - verifying values other than strings', function() {
5+
6+
it('should fail with numbers', function (done) {
7+
jwt.verify(123, 'foo', function (err, decoded) {
8+
expect(err.name).to.equal('JsonWebTokenError');
9+
done();
10+
});
11+
});
12+
13+
it('should fail with objects', function (done) {
14+
jwt.verify({ foo: 'bar' }, 'biz', function (err, decoded) {
15+
expect(err.name).to.equal('JsonWebTokenError');
16+
done();
17+
});
18+
});
19+
20+
it('should fail with arrays', function (done) {
21+
jwt.verify(['foo'], 'bar', function (err, decoded) {
22+
expect(err.name).to.equal('JsonWebTokenError');
23+
done();
24+
});
25+
});
26+
27+
it('should fail with functions', function (done) {
28+
jwt.verify(function() {}, 'foo', function (err, decoded) {
29+
expect(err.name).to.equal('JsonWebTokenError');
30+
done();
31+
});
32+
});
33+
34+
it('should fail with booleans', function (done) {
35+
jwt.verify(true, 'foo', function (err, decoded) {
36+
expect(err.name).to.equal('JsonWebTokenError');
37+
done();
38+
});
39+
});
40+
41+
});

verify.js

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ module.exports = function (jwtString, secretOrPublicKey, options, callback) {
3838
return done(new JsonWebTokenError('jwt must be provided'));
3939
}
4040

41+
if (typeof jwtString !== 'string') {
42+
return done(new JsonWebTokenError('jwt must be a string'));
43+
}
44+
4145
var parts = jwtString.split('.');
4246

4347
if (parts.length !== 3){

0 commit comments

Comments
 (0)