Skip to content

Commit 6dea09c

Browse files
fix(sign): prevent double callback when payload already has a property
1 parent bc28861 commit 6dea09c

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

sign.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,15 @@ module.exports = function (payload, secretOrPrivateKey, options, callback) {
214214
}
215215
}
216216

217-
Object.keys(options_to_payload).forEach(function (key) {
217+
for (const key of Object.keys(options_to_payload)) {
218218
const claim = options_to_payload[key];
219219
if (typeof options[key] !== 'undefined') {
220220
if (typeof payload[claim] !== 'undefined') {
221221
return failure(new Error('Bad "options.' + key + '" option. The payload already has an "' + claim + '" property.'));
222222
}
223223
payload[claim] = options[key];
224224
}
225-
});
225+
};
226226

227227
const encoding = options.encoding || 'utf8';
228228

test/callback-issue.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const assert = require('assert');
2+
const jwt = require('../index.js');
3+
4+
describe('Fix: jwt.sign callback should not be called twice', function () {
5+
it('should call callback only once when payload.iss conflicts with options.issuer', function (done) {
6+
let callbackCount = 0;
7+
8+
jwt.sign(
9+
{ iss: 'bar', iat: 1757476476 },
10+
'secret',
11+
{ algorithm: 'HS256', issuer: 'foo' },
12+
(err) => {
13+
callbackCount++;
14+
assert.ok(err, 'Expected an error due to issuer conflict');
15+
assert.strictEqual(
16+
err.message,
17+
'Bad "options.issuer" option. The payload already has an "iss" property.'
18+
);
19+
assert.strictEqual(callbackCount, 1, 'Callback was called more than once');
20+
done();
21+
}
22+
);
23+
});
24+
});

0 commit comments

Comments
 (0)