Skip to content

Commit

Permalink
fix signing method with sealed objects, do not modify the params obje…
Browse files Browse the repository at this point in the history
…ct. closes #147
  • Loading branch information
jfromaniello committed Jan 4, 2016
1 parent 42145bc commit be9c09a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var jws = require('jws');
var ms = require('ms');
var timespan = require('./lib/timespan');
var xtend = require('xtend');

var JWT = module.exports;

Expand Down Expand Up @@ -39,7 +40,7 @@ JWT.decode = function (jwt, options) {

JWT.sign = function(payload, secretOrPrivateKey, options, callback) {
options = options || {};

payload = typeof payload === 'object' ? xtend(payload) : payload;

This comment has been minimized.

Copy link
@jzaefferer

jzaefferer Jan 5, 2016

For whatever its worth, this turned out to be a breaking change for my application. It was relying on the modification of the payload, using the iat and exp properties elsewhere. I've now fixed it in my app.

An entry in the changelog would've help track it down faster, a major version bump would've avoided introducing this with a regular npm-install (depending on "jsonwebtoken": "^5.4.1").

That said, thanks for your work on the module.

This comment has been minimized.

Copy link
@jfromaniello

jfromaniello Feb 16, 2016

Author Member

Sorry for breaking your app :( , yes it should have been a major version.

var header = {};

if (typeof payload === 'object') {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
},
"dependencies": {
"jws": "^3.0.0",
"ms": "^0.7.1"
"ms": "^0.7.1",
"xtend": "^4.0.1"
},
"devDependencies": {
"atob": "^1.1.2",
Expand Down
12 changes: 12 additions & 0 deletions test/bug_147.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var jwt = require('../index');
var expect = require('chai').expect;

describe('signing with a sealed payload', function() {

it('should put the expiration claim', function () {
var token = jwt.sign(Object.seal({foo: 123}), '123', { expiresIn: 10 });
var result = jwt.verify(token, '123');
expect(result.exp).to.be.closeTo(Math.floor(Date.now() / 1000) + 10, 0.2);
});

});
4 changes: 2 additions & 2 deletions test/jwt.rs.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,14 +391,14 @@ describe('RS256', function() {
var obj = { foo: 'bar' };
var token = jwt.sign(obj, priv, { algorithm: 'RS256' });
var payload = jwt.decode(token);
assert.deepEqual(payload, obj);
assert.equal(payload.foo, obj.foo);
done();
});
it('should return the header and payload and signature if complete option is set', function(done) {
var obj = { foo: 'bar' };
var token = jwt.sign(obj, priv, { algorithm: 'RS256' });
var decoded = jwt.decode(token, { complete: true });
assert.deepEqual(decoded.payload, obj);
assert.equal(decoded.payload.foo, obj.foo);
assert.deepEqual(decoded.header, { typ: 'JWT', alg: 'RS256' });
assert.ok(typeof decoded.signature == 'string');
done();
Expand Down

0 comments on commit be9c09a

Please sign in to comment.