Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

Commit

Permalink
Fix minor DoS attack on long headers or uris. Closes #168
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Jan 19, 2016
1 parent b5d51eb commit 0833f99
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 6 deletions.
12 changes: 10 additions & 2 deletions lib/server.js
Expand Up @@ -310,6 +310,11 @@ exports.header = function (credentials, artifacts, options) {
* 'hostHeaderName', 'localtimeOffsetMsec', 'host', 'port'
*/


// 1 2 3 4
internals.bewitRegex = /^(\/.*)([\?&])bewit\=([^&$]*)(?:&(.+))?$/;


exports.authenticateBewit = function (req, credentialsFunc, options, callback) {

callback = Hoek.nextTick(callback);
Expand All @@ -327,8 +332,11 @@ exports.authenticateBewit = function (req, credentialsFunc, options, callback) {

// Extract bewit

// 1 2 3 4
const resource = request.url.match(/^(\/.*)([\?&])bewit\=([^&$]*)(?:&(.+))?$/);
if (request.url.length > Utils.limits.maxMatchLength) {
return callback(Boom.badRequest('Resource path exceeds max length'));
}

const resource = request.url.match(internals.bewitRegex);
if (!resource) {
return callback(Utils.unauthorized());
}
Expand Down
23 changes: 20 additions & 3 deletions lib/utils.js
Expand Up @@ -17,6 +17,11 @@ exports.version = function () {
};


exports.limits = {
maxMatchLength: 4096 // Limit the length of uris and headers to avoid a DoS attack on string matching
};


// Extract host and port from request

// $1 $2
Expand All @@ -31,6 +36,10 @@ exports.parseHost = function (req, hostHeaderName) {
return null;
}

if (hostHeader.length > exports.limits.maxMatchLength) {
return null;
}

const hostParts = hostHeader.match(internals.hostHeaderRegex);
if (!hostParts) {
return null;
Expand Down Expand Up @@ -100,6 +109,10 @@ exports.nowSecs = function (localtimeOffsetMsec) {
};


internals.authHeaderRegex = /^(\w+)(?:\s+(.*))?$/; // Header: scheme[ something]
internals.attributeRegex = /^[ \w\!#\$%&'\(\)\*\+,\-\.\/\:;<\=>\?@\[\]\^`\{\|\}~]+$/; // !#$%&'()*+,-./:;<=>?@[]^_`{|}~ and space, a-z, A-Z, 0-9


// Parse Hawk HTTP Authorization header

exports.parseAuthorizationHeader = function (header, keys) {
Expand All @@ -110,7 +123,11 @@ exports.parseAuthorizationHeader = function (header, keys) {
return Boom.unauthorized(null, 'Hawk');
}

const headerParts = header.match(/^(\w+)(?:\s+(.*))?$/); // Header: scheme[ something]
if (header.length > exports.limits.maxMatchLength) {
return Boom.badRequest('Header length too long');
}

const headerParts = header.match(internals.authHeaderRegex);
if (!headerParts) {
return Boom.badRequest('Invalid header syntax');
}
Expand All @@ -136,9 +153,9 @@ exports.parseAuthorizationHeader = function (header, keys) {
return;
}

// Allowed attribute value characters: !#$%&'()*+,-./:;<=>?@[]^_`{|}~ and space, a-z, A-Z, 0-9
// Allowed attribute value characters

if ($2.match(/^[ \w\!#\$%&'\(\)\*\+,\-\.\/\:;<\=>\?@\[\]\^`\{\|\}~]+$/) === null) {
if ($2.match(internals.attributeRegex) === null) {
errorMessage = 'Bad attribute value: ' + $1;
return;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "hawk",
"description": "HTTP Hawk Authentication Scheme",
"version": "4.1.0",
"version": "4.1.1",
"author": "Eran Hammer <eran@hammer.io> (http://hueniverse.com)",
"repository": "git://github.com/hueniverse/hawk",
"main": "lib/index.js",
Expand Down
27 changes: 27 additions & 0 deletions test/server.js
Expand Up @@ -971,6 +971,33 @@ describe('Server', () => {
});
});

describe('authenticateBewit()', () => {

it('errors on uri too long', (done) => {

let long = '/';
for (let i = 0; i < 5000; ++i) {
long += 'x';
}

const req = {
method: 'GET',
url: long,
host: 'example.com',
port: 8080,
authorization: 'Hawk id="1", ts="1353788437", nonce="k3j4h2", mac="zy79QQ5/EYFmQqutVnYb73gAc/U=", ext="hello"'
};

Hawk.server.authenticateBewit(req, credentialsFunc, {}, (err, credentials, bewit) => {

expect(err).to.exist();
expect(err.output.statusCode).to.equal(400);
expect(err.message).to.equal('Resource path exceeds max length');
done();
});
});
});

describe('authenticateMessage()', () => {

it('errors on invalid authorization (ts)', (done) => {
Expand Down
28 changes: 28 additions & 0 deletions test/utils.js
Expand Up @@ -95,6 +95,34 @@ describe('Utils', () => {
expect(host.name).to.equal('[123:123:123]');
done();
});

it('errors on header too long', (done) => {

let long = '';
for (let i = 0; i < 5000; ++i) {
long += 'x';
}

expect(Hawk.utils.parseHost({ headers: { host: long } })).to.be.null();
done();
});
});

describe('parseAuthorizationHeader()', () => {

it('errors on header too long', (done) => {

let long = 'Scheme a="';
for (let i = 0; i < 5000; ++i) {
long += 'x';
}
long += '"';

const err = Hawk.utils.parseAuthorizationHeader(long, ['a']);
expect(err).to.be.instanceof(Error);
expect(err.message).to.equal('Header length too long');
done();
});
});

describe('version()', () => {
Expand Down

0 comments on commit 0833f99

Please sign in to comment.