Skip to content
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
9 changes: 8 additions & 1 deletion lib/hbs.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,14 @@ ExpressHbs.prototype.cacheLayout = function(layoutFile, useCache, cb) {
var self = this;

if (this.restrictLayoutsTo) {
if (!layoutFile.startsWith(this.restrictLayoutsTo)) {
var restrictLayoutsTo = path.resolve(this.restrictLayoutsTo);
var resolvedLayoutFile = path.resolve(layoutFile);
var relativeLayoutPath = path.relative(restrictLayoutsTo, resolvedLayoutFile);
var isOutsideRestrictedDir = relativeLayoutPath === '..' ||
relativeLayoutPath.startsWith('..' + path.sep) ||
path.isAbsolute(relativeLayoutPath);

if (isOutsideRestrictedDir) {
var err = new Error('Cannot read ' + layoutFile + ' it does not reside in ' + this.restrictLayoutsTo);
return cb(err, null);
}
Expand Down
30 changes: 30 additions & 0 deletions test/layoutSpecs.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ describe('layouts', function() {
});
});

it ('should allow absolute layout path when restrictLayoutsTo is relative', function(done) {
var render = hbs.create().express3({
restrictLayoutsTo: path.relative(process.cwd(), dirname)
});
var locals = createLocals('express3', dirname, {
layout: path.resolve(path.join(dirname, 'layouts/default'))
});

render(dirname + '/aside.hbs', locals, function(err, html) {
assert.equal('<dld>aside</dld>', stripWs(html));
done();
});
});

it('should error when using a layout outside of the restrictLayoutsTo', function(done) {
var render = hbs.create().express3({
restrictLayoutsTo: path.resolve(path.join(__dirname, '../'))
Expand All @@ -102,6 +116,22 @@ describe('layouts', function() {
});
});

it('should error when layout path only matches restrictLayoutsTo as a prefix', function(done) {
var render = hbs.create().express3({
restrictLayoutsTo: dirname
});
var locals = createLocals('express3', dirname, {
layout: path.resolve(path.join(__dirname, 'views/disableLayoutDirectiveEvil/layouts/default'))
});

render(dirname + '/aside.hbs', locals, function(err, html) {
if (!err) {
return done(new Error('We expect an error when reading'));
}
return done();
});
});

it ('should not process template-specified layout when options.layout is falsy', function(done) {
var render = hbs.create().express3({
restrictLayoutsTo: dirname
Expand Down
1 change: 1 addition & 0 deletions test/views/disableLayoutDirectiveEvil/layouts/default.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<evil>{{{body}}}</evil>