Skip to content
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
19 changes: 16 additions & 3 deletions lib/hbs.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ function middleware(filename, options, cb) {
return res
}

// Waiter.resolve() may invoke this on a later tick, outside the try/catch
// around template(). Turn substitution throws into a render error so they
// do not become uncaught exceptions.
function finishAsync (filename, res, values, cb) {
var out
try {
out = replaceAsyncValues(res, values)
} catch (err) {
return cb(prependFilenameToError(filename, err))
}
cb(null, out)
}

// grab extension from filename
// if we need a layout, we will look for one matching out extension
var extension = path.extname(filename);
Expand All @@ -77,7 +90,7 @@ function middleware(filename, options, cb) {
try {
var res = template(locals, handlebarsOpts)
self.async.done(function (values) {
cb(null, replaceAsyncValues(res, values))
finishAsync(filename, res, values, cb)
})
} catch (err) {
cb(prependFilenameToError(filename, err))
Expand All @@ -99,7 +112,7 @@ function middleware(filename, options, cb) {
try {
var res = template(locals, handlebarsOpts);
self.async.done(function(values) {
cb(null, replaceAsyncValues(res, values));
finishAsync(filename, res, values, cb);
});
} catch (err) {
cb(prependFilenameToError(filename, err))
Expand All @@ -119,7 +132,7 @@ function middleware(filename, options, cb) {
try {
var res = template(locals, handlebarsOpts)
self.async.done(function (values) {
cb(null, replaceAsyncValues(res, values))
finishAsync(filename, res, values, cb)
})
} catch (err) {
cb(prependFilenameToError(filename, err))
Expand Down
57 changes: 57 additions & 0 deletions test/4.x/async_helper_escape.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,63 @@ test('async helper non-string values keep their pre-escaping text output', funct
})
})

test('async helper with non-callable toHTML is a render error, not a process crash (uncached)', function (done) {
var hbs = require('../../').create()
var payload = JSON.parse('{"toHTML":"x"}')
hbs.registerAsyncHelper('bad', function (_opts, cb) {
process.nextTick(function () {
cb(payload)
})
})
render(hbs, makeView('async-bad-tohtml.hbs', '{{bad}}'), function (err) {
assert.ok(err, 'expected a render error instead of an uncaught throw')
assert.ok(/is not a function/.test(err.message), err.message)
done()
})
})

test('async helper with non-callable toHTML is a render error, not a process crash (cached)', function (done) {
var hbs = require('../../').create()
var payload = JSON.parse('{"toHTML":"x"}')
hbs.registerAsyncHelper('bad', function (_opts, cb) {
process.nextTick(function () {
cb(payload)
})
})
var view = makeView('async-bad-tohtml-cached.hbs', '{{bad}}')
render(hbs, view, { cache: true }, function (err) {
assert.ok(err, 'expected a render error on the uncached path')
assert.ok(hbs.cache[view], 'expected first render to cache the template')
render(hbs, view, { cache: true }, function (err2) {
assert.ok(err2, 'expected a render error on the cached path')
assert.ok(/is not a function/.test(err2.message), err2.message)
done()
})
})
})

test('async helper with non-callable toHTML is a render error, not a process crash (layout)', function (done) {
var hbs = require('../../').create()
var payload = JSON.parse('{"toHTML":"x"}')
hbs.registerAsyncHelper('bad', function (_opts, cb) {
process.nextTick(function () {
cb(payload)
})
})
var layout = makeView('layout-bad-tohtml.hbs', '<html><body>{{{body}}}{{bad}}</body></html>')
var view = makeView('async-bad-tohtml-layout.hbs', 'ok')
hbs.__express(view, {
settings: {
views: tmpDir,
'view options': { layout: path.basename(layout, '.hbs') }
}
}, function (err) {
assert.ok(err, 'expected a render error on the layout path')
assert.ok(/is not a function/.test(err.message), err.message)
done()
})
})

test('async helper output is HTML-escaped by {{...}} (layout path)', function (done) {
var hbs = require('../../').create()
hbs.registerAsyncHelper('userBio', function (_opts, cb) { cb(PAYLOAD) })
Expand Down
Loading