From cc5726b0d0f90220f7b0d2a201c89458936e88cf Mon Sep 17 00:00:00 2001 From: Burak Keskin Date: Tue, 8 Sep 2026 16:59:55 +0300 Subject: [PATCH 1/2] fix: treat async substitution throws as render errors escapeExpression can throw when an async helper resolves to an object with a non-callable toHTML. That substitution runs on a later tick, outside the existing try/catch, and the process exits. Catch it and pass the error to the render callback. --- lib/hbs.js | 19 +++++++++-- test/4.x/async_helper_escape.js | 57 +++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 3 deletions(-) diff --git a/lib/hbs.js b/lib/hbs.js index d0cf27b..565ec81 100644 --- a/lib/hbs.js +++ b/lib/hbs.js @@ -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); @@ -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)) @@ -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)) @@ -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)) diff --git a/test/4.x/async_helper_escape.js b/test/4.x/async_helper_escape.js index 03d340b..a03cd73 100644 --- a/test/4.x/async_helper_escape.js +++ b/test/4.x/async_helper_escape.js @@ -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(/toHTML 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(/toHTML 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', '{{{body}}}{{bad}}') + 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(/toHTML 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) }) From 12816268e56e58e5be33a5f9954f8e25f4053870 Mon Sep 17 00:00:00 2001 From: Ulises Gascon Date: Tue, 8 Sep 2026 23:14:00 +0200 Subject: [PATCH 2/2] fix: add retro-compatibility for testing Old V8 (Node 0.8/0.10/0.12, io.js 1.x) phrases the non-callable-toHTML TypeError differently ('Property toHTML of object # is not a function', 'string is not a function'). Relax the assertions from /toHTML is not a function/ to /is not a function/ so the regression test passes on every supported runtime. lib/hbs.js unchanged. --- test/4.x/async_helper_escape.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/4.x/async_helper_escape.js b/test/4.x/async_helper_escape.js index a03cd73..22adff1 100644 --- a/test/4.x/async_helper_escape.js +++ b/test/4.x/async_helper_escape.js @@ -107,7 +107,7 @@ test('async helper with non-callable toHTML is a render error, not a process cra }) render(hbs, makeView('async-bad-tohtml.hbs', '{{bad}}'), function (err) { assert.ok(err, 'expected a render error instead of an uncaught throw') - assert.ok(/toHTML is not a function/.test(err.message), err.message) + assert.ok(/is not a function/.test(err.message), err.message) done() }) }) @@ -126,7 +126,7 @@ test('async helper with non-callable toHTML is a render error, not a process cra 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(/toHTML is not a function/.test(err2.message), err2.message) + assert.ok(/is not a function/.test(err2.message), err2.message) done() }) }) @@ -149,7 +149,7 @@ test('async helper with non-callable toHTML is a render error, not a process cra } }, function (err) { assert.ok(err, 'expected a render error on the layout path') - assert.ok(/toHTML is not a function/.test(err.message), err.message) + assert.ok(/is not a function/.test(err.message), err.message) done() }) })