Skip to content

Commit

Permalink
Coverage and fix error message leak. Closes #1538, for #1524
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed Mar 28, 2014
1 parent 9de8cf0 commit 647fa66
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 13 deletions.
9 changes: 4 additions & 5 deletions lib/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,7 @@ exports.parseCookies = function (request, next) {

var unsign = function (name, value, definition, innerNext) {

if (!definition ||
!definition.sign) {

if (!definition.sign) {
return innerNext(null, value);
}

Expand Down Expand Up @@ -426,6 +424,8 @@ exports.generateSetCookieHeader = function (cookies, server, callback) {

exports.prepareValue = function (name, value, options, callback) {

Utils.assert(options && typeof options === 'object', 'Missing or invalid options');

// Encode value

internals.encode(value, options, function (err, encoded) {
Expand Down Expand Up @@ -458,8 +458,7 @@ internals.encode = function (value, options, callback) {
return callback(null, value);
}

if (!options ||
!options.encoding ||
if (!options.encoding ||
options.encoding === 'none') {

return callback(null, value);
Expand Down
9 changes: 4 additions & 5 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ var internals = {};

internals.import = function () {

for (var i in Hoek) {
if (Hoek.hasOwnProperty(i)) {
exports[i] = Hoek[i];
}
}
Object.keys(Hoek).forEach(function (util) {

exports[util] = Hoek[util];
});
};

internals.import();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"dependencies": {
"hoek": "^1.5.x",
"boom": "^2.2.x",
"boom": "^2.3.x",
"joi": "^2.9.x",
"catbox": "^2.1.x",
"catbox-memory": "1.x.x",
Expand Down
14 changes: 14 additions & 0 deletions test/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,20 @@ describe('Pack', function () {
});
});

it('requires plugin with views and loader', function (done) {

var server = new Hapi.Server();
server.pack.require({ './pack/--viewsLoader': { message: 'viewing it' } }, function (err) {

expect(err).to.not.exist;
server.inject({ method: 'GET', url: '/' }, function (res) {

expect(res.result).to.equal('<h1>{{message}}</h1>|{"message":"viewing it"}');
done();
});
});
});

it('requires module', function (done) {

var server = new Hapi.Server();
Expand Down
25 changes: 25 additions & 0 deletions test/pack/--viewsLoader/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Declare internals

var internals = {};


// Plugin registration

exports.register = function (plugin, options, next) {

plugin.loader(require);

plugin.views({
engines: {
'html': {
compileMode: 'async',
module: '--custom'
}
},
path: './templates'
});

plugin.route({ path: '/', method: 'GET', handler: function (request, reply) { return reply.view('test', { message: options.message }); } });

return next();
};
14 changes: 14 additions & 0 deletions test/pack/--viewsLoader/node_modules/--custom/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions test/pack/--viewsLoader/node_modules/--custom/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions test/pack/--viewsLoader/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "--views",
"description": "Test plugin module",
"version": "0.0.1",
"private": true,
"main": "./index"
}
1 change: 1 addition & 0 deletions test/pack/--viewsLoader/templates/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>{{message}}</h1>
2 changes: 1 addition & 1 deletion test/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ describe('Proxy', function () {

server.inject('/maperror', function (res) {

expect(res.payload).to.contain('myerror');
expect(res.statusCode).to.equal(500);
done();
});
});
Expand Down
1 change: 0 additions & 1 deletion test/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,6 @@ describe('Response', function () {

expect(res.statusCode).to.equal(500);
expect(res.result).to.exist;
expect(res.result.message).to.equal('boom');
done();
});
});
Expand Down
37 changes: 37 additions & 0 deletions test/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,15 @@ describe('State', function () {
pass('a="b=123456789&c=something%20else"', { a: { b: '123456789', c: 'something else' } }, null, { a: { encoding: 'form' } });
pass('a="b=%p123456789"', { a: { b: '%p123456789' } }, null, { a: { encoding: 'form' } });
pass('a=dGVzdA; a=dGVzdA', { a: ['test', 'test'] }, null, { a: { encoding: 'base64' } });
pass('a=dGVzdA; a=dGVzdA; a=dGVzdA', { a: ['test', 'test', 'test'] }, null, { a: { encoding: 'base64' } });
pass('key=dGVzdA==', { key: 'test' }, null, { key: { encoding: 'base64' } });
pass('key=dGVzdA', { key: 'test' }, null, { key: { encoding: 'base64' } });
pass('key=dGVzdA', { key: 'dGVzdA' }, null, { key: { encoding: 'none' } });
pass('key=eyJ0ZXN0aW5nIjoianNvbiJ9', { key: { testing: 'json' } }, null, { key: { encoding: 'base64json' } });
pass('key=Fe26.2**f3fc42242467f7a97c042be866a32c1e7645045c2cc085124eadc66d25fc8395*URXpH8k-R0d4O5bnY23fRQ*uq9rd8ZzdjZqUrq9P2Ci0yZ-EEUikGzxTLn6QTcJ0bc**3880c0ac8bab054f529afec8660ebbbbc8050e192e39e5d622e7ac312b9860d0*r_g7N9kJYqXDrFlvOnuKpfpEWwrJLOKMXEI43LAGeFg', { key: { a: 1, b: 2, c: 3 } }, null, { key: { encoding: 'iron', password: 'password' } });
pass('key=Fe26.2**f3fc42242467f7a97c042be866a32c1e7645045c2cc085124eadc66d25fc8395*URXpH8k-R0d4O5bnY23fRQ*uq9rd8ZzdjZqUrq9P2Ci0yZ-EEUikGzxTLn6QTcJ0bc**3880c0ac8bab054f529afec8660ebbbbc8050e192e39e5d622e7ac312b9860d0*r_g7N9kJYqXDrFlvOnuKpfpEWwrJLOKMXEI43LAGeFg', { key: { a: 1, b: 2, c: 3 } }, null, { key: { encoding: 'iron', password: 'password', iron: Iron.defaults } });
pass('sid=a=1&b=2&c=3%20x.2d75635d74c1a987f84f3ee7f3113b9a2ff71f89d6692b1089f19d5d11d140f8*xGhc6WvkE55V-TzucCl0NVFmbijeCwgs5Hf5tAVbSUo', { sid: { a: '1', b: '2', c: '3 x' } }, null, { sid: { encoding: 'form', sign: { password: 'password' } } });
pass('sid=a=1&b=2&c=3%20x.2d75635d74c1a987f84f3ee7f3113b9a2ff71f89d6692b1089f19d5d11d140f8*xGhc6WvkE55V-TzucCl0NVFmbijeCwgs5Hf5tAVbSUo', { sid: { a: '1', b: '2', c: '3 x' } }, null, { sid: { encoding: 'form', sign: { password: 'password', integrity: Iron.defaults.integrity } } });

var loose = Hapi.utils.clone(Defaults.server.state);
loose.cookies.strictHeader = false;
Expand Down Expand Up @@ -243,6 +246,7 @@ describe('State', function () {
var setLog = Hapi.utils.clone(Defaults.server.state);
setLog.cookies.failAction = 'log';
fail('abc="xyzf', setLog, null, { abc: '"xyzf'});
fail('"abc=xyzf', setLog, null, { '"abc': 'xyzf' });
fail('key=XeyJ0ZXN0aW5nIjoianNvbiJ9', setLog, { key: { encoding: 'base64json' } });
fail('y=XeyJ0ZXN0aW5nIjoianNvbiJ9; y=XeyJ0ZXN0aW5dnIjoianNvbiJ9', setLog, { y: { encoding: 'base64json' } });
fail('sid=a=1&b=2&c=3%20x', setLog, { sid: { encoding: 'form', sign: { password: 'password' } } });
Expand Down Expand Up @@ -273,6 +277,16 @@ describe('State', function () {
});
});

it('skips an empty array', function (done) {

generateSetCookieHeader([], null, function (err, header) {

expect(err).to.not.exist;
expect(header).to.deep.equal([]);
done();
});
});

it('formats a header', function (done) {

generateSetCookieHeader({ name: 'sid', value: 'fihfieuhr9384hf', options: { ttl: 3600, isSecure: true, isHttpOnly: true, path: '/', domain: 'example.com' } }, null, function (err, header) {
Expand Down Expand Up @@ -405,6 +419,17 @@ describe('State', function () {
});
});

it('formats a header with server definition (iron + options)', function (done) {

var definitions = { sid: { encoding: 'iron', password: 'password', iron: Iron.defaults } };
generateSetCookieHeader({ name: 'sid', value: { a: 1, b: 2, c: 3 } }, definitions, function (err, header) {

expect(err).to.not.exist;
expect(header[0]).to.have.string('sid=Fe26.2*');
done();
});
});

it('fails a header with bad server definition (iron)', function (done) {

var definitions = { sid: { encoding: 'iron' } };
Expand Down Expand Up @@ -514,4 +539,16 @@ describe('State', function () {
});
});
});

describe('#prepareValue', function () {

it('throws when missing options', function (done) {

expect(function () {

Hapi.state.prepareValue('name', 'value');
}).to.throw('Missing or invalid options');
done();
});
});
});

0 comments on commit 647fa66

Please sign in to comment.