Skip to content

Commit 9f24ffd

Browse files
committedApr 10, 2015
Renaming header.alg mismatch exception to invalid algorithm and adding more mismatch tests.
1 parent 19e6cc6 commit 9f24ffd

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed
 

Diff for: ‎index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ module.exports.verify = function(jwtString, secretOrPublicKey, options, callback
126126
var header = decodedToken.header;
127127

128128
if (!~options.algorithms.indexOf(header.alg)) {
129-
return done(new JsonWebTokenError('invalid signature'));
129+
return done(new JsonWebTokenError('invalid algorithm'));
130130
}
131131

132132
var valid;

Diff for: ‎test/wrong_alg.tests.js

+28-6
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,32 @@ var pub = fs.readFileSync(path.join(__dirname, 'pub.pem'), 'utf8');
1111

1212
var TOKEN = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE0MjY1NDY5MTl9.ETgkTn8BaxIX4YqvUWVFPmum3moNZ7oARZtSBXb_vP4';
1313

14-
describe('signing with pub key as symmetric', function () {
15-
it('should not verify', function () {
16-
expect(function () {
17-
jwt.verify(TOKEN, pub);
18-
}).to.throw(JsonWebTokenError, /invalid signature/);
14+
describe('when setting a wrong `header.alg`', function () {
15+
16+
describe('signing with pub key as symmetric', function () {
17+
it('should not verify', function () {
18+
expect(function () {
19+
jwt.verify(TOKEN, pub);
20+
}).to.throw(JsonWebTokenError, /invalid algorithm/);
21+
});
22+
});
23+
24+
describe('signing with pub key as HS256 and whitelisting only RS256', function () {
25+
it('should not verify', function () {
26+
expect(function () {
27+
jwt.verify(TOKEN, pub, {algorithms: ['RS256']});
28+
}).to.throw(JsonWebTokenError, /invalid algorithm/);
29+
});
1930
});
20-
});
31+
32+
describe('signing with HS256 and checking with HS384', function () {
33+
it('should not verify', function () {
34+
expect(function () {
35+
var token = jwt.sign({foo: 'bar'}, 'secret', {algorithm: 'HS256'});
36+
jwt.verify(token, 'some secret', {algorithms: ['HS384']});
37+
}).to.throw(JsonWebTokenError, /invalid algorithm/);
38+
});
39+
});
40+
41+
42+
});

0 commit comments

Comments
 (0)
Please sign in to comment.