Skip to content

Commit

Permalink
Renaming header.alg mismatch exception to invalid algorithm and a…
Browse files Browse the repository at this point in the history
…dding more mismatch tests.
  • Loading branch information
pose committed Apr 10, 2015
1 parent 19e6cc6 commit 9f24ffd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ module.exports.verify = function(jwtString, secretOrPublicKey, options, callback
var header = decodedToken.header;

if (!~options.algorithms.indexOf(header.alg)) {
return done(new JsonWebTokenError('invalid signature'));
return done(new JsonWebTokenError('invalid algorithm'));
}

var valid;
Expand Down
34 changes: 28 additions & 6 deletions test/wrong_alg.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,32 @@ var pub = fs.readFileSync(path.join(__dirname, 'pub.pem'), 'utf8');

var TOKEN = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE0MjY1NDY5MTl9.ETgkTn8BaxIX4YqvUWVFPmum3moNZ7oARZtSBXb_vP4';

describe('signing with pub key as symmetric', function () {
it('should not verify', function () {
expect(function () {
jwt.verify(TOKEN, pub);
}).to.throw(JsonWebTokenError, /invalid signature/);
describe('when setting a wrong `header.alg`', function () {

describe('signing with pub key as symmetric', function () {
it('should not verify', function () {
expect(function () {
jwt.verify(TOKEN, pub);
}).to.throw(JsonWebTokenError, /invalid algorithm/);
});
});

describe('signing with pub key as HS256 and whitelisting only RS256', function () {
it('should not verify', function () {
expect(function () {
jwt.verify(TOKEN, pub, {algorithms: ['RS256']});
}).to.throw(JsonWebTokenError, /invalid algorithm/);
});
});
});

describe('signing with HS256 and checking with HS384', function () {
it('should not verify', function () {
expect(function () {
var token = jwt.sign({foo: 'bar'}, 'secret', {algorithm: 'HS256'});
jwt.verify(token, 'some secret', {algorithms: ['HS384']});
}).to.throw(JsonWebTokenError, /invalid algorithm/);
});
});


});

0 comments on commit 9f24ffd

Please sign in to comment.