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

fix jwt decoded with missing characters in body #91

Merged
merged 1 commit into from
Aug 21, 2015
Merged
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
14 changes: 12 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internals.implementation = function (server, options) {
return {
authenticate: function (request, reply) {
var token = extract(request, options);

if (!token && request.auth.mode === 'optional') {
return reply.continue({ credentials: {} });
}
Expand All @@ -41,7 +41,17 @@ internals.implementation = function (server, options) {
}

var keyFunc = (internals.isFunction(options.key)) ? options.key : function (decoded, callback) { callback(null, options.key); };
keyFunc(JWT.decode(token), function (err, key, extraInfo) {

var decoded;
try {
decoded = JWT.decode(token);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JWT2 uses the _asynchronous_ version of JWT.decode to avoid blocking the V8 event loop.
This try/catch uses the _synchronous_ (_blocking_) version of JWT.decode which will introduce a bottle-neck into the authorization process which could easily be exploited by a DOS attacker.

}
catch(e)
{
return reply(Boom.unauthorized('Invalid token format', 'Token'));
}

keyFunc(decoded, function (err, key, extraInfo) {
if (err) {
return reply(Boom.wrap(err));
}
Expand Down
23 changes: 23 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,29 @@ test("Malformed JWT", function(t) {
});
});

test("Try using a token with missing characters in body", function(t) {
// use the token as the 'authorization' header in requests
var token = JWT.sign({ id:123,"name":"Charlie" }, secret);
// delete some characters in body
var tokenData = token.split('.');
var header = tokenData[0],
body = tokenData[1],
signature = tokenData[2];
token = header + '.' + body.substring(0, body.length - 1) + '.' + signature;
/*console.log(" - - - - - - token - - - - -");
console.log(token);*/
var options = {
method: "POST",
url: "/privado",
headers: { authorization: "Bearer "+token }
};
// server.inject lets us similate an http request
server.inject(options, function(response) {
t.equal(response.statusCode, 401, "INVALID Token should fail");
t.end();
});
});

test("Try using an incorrect secret to sign the JWT", function(t) {
// use the token as the 'authorization' header in requests
var token = JWT.sign({ id:123,"name":"Charlie" }, 'incorrectSecret');
Expand Down