Skip to content

Commit 2e4e30b

Browse files
authored
Merge pull request auth0#386 from ziluvatar/issue_381
Fix breaking change on 7.4.2 for empty secret + "none" algorithm (sync code style)
2 parents e56f904 + 2a3404f commit 2e4e30b

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

sign.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ module.exports = function (payload, secretOrPrivateKey, options, callback) {
6666
throw err;
6767
}
6868

69-
if (!secretOrPrivateKey) {
69+
if (!secretOrPrivateKey && options.algorithm !== 'none') {
7070
return failure(new Error('secretOrPrivateKey must have a value'));
7171
}
7272

test/async_sign.tests.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ describe('signing a token asynchronously', function() {
3232
});
3333
});
3434

35+
it('should work with none algorithm where secret is set', function(done) {
36+
jwt.sign({ foo: 'bar' }, 'secret', { algorithm: 'none' }, function(err, token) {
37+
expect(token).to.be.a('string');
38+
expect(token.split('.')).to.have.length(3);
39+
done();
40+
});
41+
});
42+
43+
//Known bug: https://github.com/brianloveswords/node-jws/issues/62
44+
//If you need this use case, you need to go for the non-callback-ish code style.
45+
it.skip('should work with none algorithm where secret is falsy', function(done) {
46+
jwt.sign({ foo: 'bar' }, undefined, { algorithm: 'none' }, function(err, token) {
47+
expect(token).to.be.a('string');
48+
expect(token.split('.')).to.have.length(3);
49+
done();
50+
});
51+
});
52+
3553
it('should return error when secret is not a cert for RS256', function(done) {
3654
//this throw an error because the secret is not a cert and RS256 requires a cert.
3755
jwt.sign({ foo: 'bar' }, secret, { algorithm: 'RS256' }, function (err) {
@@ -66,7 +84,7 @@ describe('signing a token asynchronously', function() {
6684

6785
describe('secret must have a value', function(){
6886
[undefined, '', 0].forEach(function(secret){
69-
it('should return an error if the secret is falsy: ' + (typeof secret === 'string' ? '(empty string)' : secret), function(done) {
87+
it('should return an error if the secret is falsy and algorithm is not set to none: ' + (typeof secret === 'string' ? '(empty string)' : secret), function(done) {
7088
// This is needed since jws will not answer for falsy secrets
7189
jwt.sign('string', secret, {}, function(err, token) {
7290
expect(err).to.be.exist();

test/jwt.hs.tests.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ describe('HS256', function() {
5151
});
5252
});
5353

54+
it('should work with falsy secret and token not signed', function(done) {
55+
var signed = jwt.sign({ foo: 'bar' }, null, { algorithm: 'none' });
56+
var unsigned = signed.split('.')[0] + '.' + signed.split('.')[1] + '.';
57+
jwt.verify(unsigned, 'secret', function(err, decoded) {
58+
assert.isUndefined(decoded);
59+
assert.isNotNull(err);
60+
done();
61+
});
62+
});
63+
5464
it('should throw when verifying null', function(done) {
5565
jwt.verify(null, 'secret', function(err, decoded) {
5666
assert.isUndefined(decoded);

0 commit comments

Comments
 (0)