/

CVE-2026-33937

Severity:

CRITICAL RISK

(9.8)

Code Injection in Handlebars.compile()

CVE-2026-33937

Modified

Vulnerability type

Category:

Code Injection

CWE:

CWE-94, CWE-843

Impact:

Remote Code Execution

Severity:

CRITICAL RISK

(9.8)

CVSS version:

3.1

Catch this CVE while code is being written

Detect vulnerable code directly in pull requests, so CVEs are fixed before merge.

Summary

Handlebars versions 4.0.0 through 4.7.8 allow attackers to supply a pre-parsed AST object to Handlebars.compile(), where the value field of NumberLiteral nodes is emitted directly into generated JavaScript without quoting or sanitization.

If an attacker can influence or control the AST passed to compile(), they may inject arbitrary JavaScript into the output and achieve remote code execution on the server.

Affected context

The vulnerability occurs in server- or build-side usages of Handlebars where Handlebars.compile() is called and the argument may be a non-string value, such as a plain object or JSON-deserialized data.

It is particularly relevant when:

  • Template sources or ASTs are dynamically provided from user input, configuration, or remote services.
  • The application permits or assumes that callers may pass already-parsed AST objects instead of raw template strings.
  • The compiled templates are executed in a Node.js or similar JavaScript runtime with access to process resources.

When these conditions hold, a crafted AST can bypass the normal string template parsing logic and inject raw JavaScript into the generated function body.

Exploit preconditions

  • Attack surface: Code paths that call Handlebars.compile() and accept untrusted or partially trusted data as the template argument.
  • Required access: Ability to supply or influence the AST object passed into compile() (for example, via API parameters, configuration files, database records, or template management UIs).
  • Trigger point: Invocation of Handlebars.compile() on a non-string argument (a pre-parsed AST object) containing a malicious NumberLiteral.value field.
  • Why exploitation is possible: When an AST object is provided, Handlebars emits the NumberLiteral.value directly into the generated JavaScript source without quoting or sanitization, turning that field into raw executable code in the compiled template function.

Why this happens

This issue stems from treating an AST object as trusted code structure and emitting certain node values directly into generated JavaScript without escaping or validation.

In affected Handlebars versions, the NumberLiteral AST node's value property is assumed to be a safe numeric literal.

When an attacker controls the AST, they can set value to arbitrary JavaScript fragments, which are injected verbatim into the compiled function body.

This maps to CWE-94: Improper Control of Generation of Code ('Code Injection'), because untrusted data is incorporated into dynamically generated code, and CWE-843: Access of Resource Using Incompatible Type ('Type Confusion'), since the API implicitly accepts both strings and objects, allowing attackers to bypass normal parsing logic by presenting a crafted object instead of a simple template string.

Potential impact

  • Execution of arbitrary JavaScript code in the server runtime.
  • Reading or modification of application data and configuration in memory.
  • Access to environment variables, filesystem, network, or credentials available to the server process.
  • Escalation to full system compromise if the runtime permissions and environment allow.
  • Use of the compromised server as a pivot point for lateral movement within the infrastructure.

General mitigation guidance

Effective mitigation focuses on strictly controlling the types and trust boundaries of data passed to Handlebars.compile() and minimizing exposure to dynamic compilation:

  • Enforce strict input typing for compile() so that it only accepts template strings and never plain objects or JSON-deserialized structures.
  • Introduce centralized wrappers around Handlebars.compile() that validate input type and reject anything other than a trusted string.
  • Prefer a build-time pre-compilation workflow, using the Handlebars runtime-only build on the server (handlebars/runtime) so that compile() is not available in production environments.
  • Treat any data that could originate from users, external systems, or configuration as untrusted, and do not feed it into compilation or code generation APIs.
  • Run template execution in least-privileged environments, limiting filesystem, network, and process permissions to reduce impact if a template is abused.

How teams detect issues like this

Detection efforts can focus on both code and runtime behavior:

  • Static analysis and code search:
  • Identify all call sites of Handlebars.compile() and flag those where the argument type is not statically guaranteed to be a string.
  • Look for flows where the compile argument originates from JSON parsing, request bodies, databases, or configuration files that may carry attacker-controlled values.
  • Code review signals:
  • Use manual or automated review to mandate wrapper utilities for template compilation, disallowing direct calls to Handlebars.compile() in business logic.
  • Require explicit input validation before compilation, with reviewers checking for type assertions or runtime checks.
  • Policy enforcement:
  • Implement lint rules or CI checks that prohibit importing the full Handlebars build in server code when only runtime execution is needed.
  • Use SAST/IAST tools configured with rules for dynamic code generation sinks (e.g., evaluation or function construction) that originate from templating libraries.

Sources

SourceLink
github.comgithub.com/handlebars-lang/handlebars.js/commit/68d8df5a88e0a26fe9e6084c5c6aaebe67b07da2
github.comgithub.com/handlebars-lang/handlebars.js/releases/tag/v4.7.9
github.comgithub.com/handlebars-lang/handlebars.js/security/advisories/GHSA-2w6w-674q-4c4q
access.redhat.comaccess.redhat.com/errata/RHSA-2026:10175
access.redhat.comaccess.redhat.com/errata/RHSA-2026:34342
access.redhat.comaccess.redhat.com/security/cve/CVE-2026-33937
bugzilla.redhat.combugzilla.redhat.com/show_bug.cgi?id=2452523
security.access.redhat.comsecurity.access.redhat.com/data/csaf/v2/vex/2026/cve-2026-33937.json

Frequently asked

questions

It can be, if remote users can influence the AST or any data that is passed into Handlebars.compile() as something other than a trusted string. In such cases, the attack can be launched over normal application interfaces without local access.

The attacker must be able to control or significantly influence the object passed into Handlebars.compile(). This might occur through template editing features, misconfigured APIs that accept JSON for templates, or any mechanism that deserializes user-controlled data into objects later used as compile inputs.

The safest pattern is to restrict Handlebars.compile() to known-safe string templates only, enforce runtime type checks around it, and ideally move template compilation to a trusted build-time step while using the runtime-only Handlebars library on the server.

Avoid passing untrusted data into dynamic code generation or compilation APIs, enforce strict typing and validation on such boundaries, and centralize the usage of powerful functions like compile() behind vetted utility layers combined with automated checks and code review guidelines.

Find out what's already
exploitable in your codebase.