Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix for invaild json format #73

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/sign-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ var base64url = require('base64url');
var DataStream = require('./data-stream');
var jwa = require('jwa');
var Stream = require('stream');
var toString = require('./tostring');
var toBuffer = require('./to-buffer');
var util = require('util');

function jwsSecuredInput(header, payload, encoding) {
encoding = encoding || 'utf8';
var encodedHeader = base64url(toString(header), 'binary');
var encodedPayload = base64url(toString(payload), encoding);
var encodedHeader = base64url(toBuffer(header));
var encodedPayload = base64url(toBuffer(payload, encoding));
return util.format('%s.%s', encodedHeader, encodedPayload);
}

Expand Down
19 changes: 19 additions & 0 deletions lib/to-buffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

var Buffer = require('safe-buffer').Buffer;

module.exports = function toBuffer(val, encoding) {
if (Buffer.isBuffer(val)) {
return val;
}
if (typeof val === 'string') {
return Buffer.from(val, encoding || 'utf8');
}
if (typeof val === 'number') {
// This won't work for very large or very small numbers, but is consistent
// with previous behaviour at least
val = val.toString();
return Buffer.from(val, 'utf8');
}
return Buffer.from(JSON.stringify(val), 'utf8');
};
6 changes: 5 additions & 1 deletion lib/verify-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ function jwsDecode(jwsSig, opts) {

var payload = payloadFromJWS(jwsSig);
if (header.typ === 'JWT' || opts.json)
payload = JSON.parse(payload, opts.encoding);
try {
payload = JSON.parse(payload, opts.encoding);
} catch(e) {
return null;
}

return {
header: header,
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ jws.createSign({
});

// is equivilant to this:
const signer = jws.createSign(
const signer = jws.createSign({
header: { alg: 'RS256' },
);
});
privateKeyStream.pipe(signer.privateKey);
payloadStream.pipe(signer.payload);
signer.on('done', function(signature) {
Expand Down
13 changes: 13 additions & 0 deletions test/jws.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,16 @@ test('jws.isValid', function (t) {
t.same(jws.isValid(valid), true);
t.end();
});

test('#50 mangled binary payload', function(t) {
const sig = jws.sign({
header: {
alg: 'HS256'
},
payload: new Buffer('TkJyotZe8NFpgdfnmgINqg==', 'base64'),
secret: new Buffer('8NRxgIkVxP8LyyXSL4b1dg==', 'base64')
});

t.same(sig, 'eyJhbGciOiJIUzI1NiJ9.TkJyotZe8NFpgdfnmgINqg.9XilaLN_sXqWFtlUCdAlGI85PCEbJZSIQpakyAle-vo');
t.end();
});