/

CVE-2026-33940

Severity:

HIGH RISK

(8.1)

Code Injection in Handlebars Dynamic Partials

CVE-2026-33940

Modified

Vulnerability type

Category:

Code Injection

CWE:

CWE-94, CWE-843

Impact:

Remote Code Execution in server-side Handlebars environments

Severity:

HIGH RISK

(8.1)

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 contain a code injection flaw where a crafted object in the template context can bypass conditional guards in resolvePartial() and reach env.compile() as if it were template source.

When this object is a valid Handlebars AST containing injected logic, the resulting JavaScript can execute arbitrary commands on the server in environments that use server-side compilation of templates with user-influenced dynamic partial lookups.

Affected context

This vulnerability appears in server-side or privileged Handlebars usage where:

  • Versions 4.0.0 through 4.7.8 are used, and
  • Templates rely on dynamic partials (e.g., {{> (lookup ...)}}), and
  • Context data used in the dynamic lookup can be influenced by untrusted or semi-trusted input, and
  • The full Handlebars build (with compile()) is available at runtime.

In such setups, an attacker-controlled value can be returned by the dynamic partial lookup and treated as a partial that, under specific conditions, is passed directly into env.compile() as if it were template source code.

Exploit preconditions

  • Attack surface: Server-side Handlebars template rendering where dynamic partials are used.
  • Required access: Ability to control values in the template context that participate in dynamic partial resolution (for example, data fields used in lookup or helper logic that returns a partial name/value).
  • Trigger point: Rendering a template that includes a dynamic partial reference (e.g., {{> (lookup someObj "key")}} or similar constructs) while the attacker-supplied context value is present.
  • Why exploitation is possible:
  • resolvePartial() can be bypassed by specially crafted objects so that invokePartial() receives a value it cannot resolve in the usual way.
  • When the partial remains unresolved, invokePartial() falls back to treating the value as template source and passes it to env.compile().
  • If the crafted object is a Handlebars AST embedding malicious logic, the compiled template is executed with the server's privileges, enabling arbitrary code execution in the runtime environment.

Why this happens

The root cause is a logic flaw in how Handlebars handles unresolved dynamic partials, combined with overly permissive assumptions about the type and trustworthiness of values coming from the template context.

Specifically:

  • The partial resolution system (resolvePartial() and invokePartial()) is designed to accept flexible values from the context, including objects.
  • A crafted object can be constructed such that it bypasses all conditional guards in resolvePartial() and leads invokePartial() to treat the unresolved partial as needing compilation.
  • Instead of strictly enforcing that only trusted string template sources are compiled, invokePartial() passes the crafted object to env.compile().
  • When that object is structured as a Handlebars AST, the engine effectively compiles attacker-supplied "code" into executable JavaScript.

This aligns with:

  • CWE-94 (Improper Control of Generation of Code): untrusted data is used as input to code generation (compile()).
  • CWE-843 (Access of Resource Using Incompatible Type): an object of an unexpected type (AST object rather than a plain string or known-safe partial reference) is incorrectly treated as a valid template source, leading to unsafe behavior.

Potential impact

  • Execution of arbitrary JavaScript code in the server process that uses Handlebars.
  • Full compromise of the application runtime, including reading or modifying sensitive data in memory.
  • Execution of arbitrary system commands or abuse of server APIs, depending on the surrounding runtime and available libraries.
  • Lateral movement within the hosting environment (e.g., pivoting to databases, internal services, or cloud metadata endpoints).
  • Template-based supply-chain attacks, where malicious logic is injected into shared or multi-tenant template rendering paths.

General mitigation guidance

Mitigation focuses on tightening type assumptions for partials, reducing trust in context data, and decoupling untrusted input from compilation:

  • Prefer the runtime-only build (handlebars/runtime) in production so templates are precompiled and compile() is not available at runtime. This removes the fallback compilation path in invokePartial().
  • Avoid or strictly limit dynamic partial lookups when any part of the lookup path can be influenced by user-controlled data. Prefer static partial references where feasible.
  • Normalize and sanitize context data before rendering:
  • Ensure values used for dynamic partials are simple, vetted primitives (e.g., whitelisted strings), not arbitrary objects.
  • Reject or strip complex objects that could be misinterpreted as AST-like structures.
  • Introduce allowlists for partial names:
  • Map user input to a constrained set of known-safe partial identifiers.
  • Fail closed when an unknown partial is requested instead of falling back to compilation of the provided value.
  • Apply least-privilege principles around template execution:
  • Run template rendering in restricted contexts or processes where possible.
  • Limit access from the template environment to system-level APIs and highly sensitive data.

How teams detect issues like this

Detection strategies can combine code-level analysis with runtime monitoring:

  • Static analysis and code review:
  • Search for uses of the full Handlebars build in server-side code (require('handlebars')) where runtime compilation is enabled.
  • Flag templates that use dynamic partials (e.g., {{> (lookup ...)}}, helpers that return partial names, or computed partial keys) with user-influenced sources.
  • Review custom helpers that read user input and return values used in partial resolution or template selection.
  • Pattern-based checks:
  • Implement linting or custom rules that:
  • Disallow or warn on dynamic partials in server-side templates.
  • Enforce that partial names originate from constrained enumerations or configuration, not raw context data.
  • Use SAST rules targeting flows from untrusted input sources into Handlebars compile() or equivalent APIs.
  • Runtime monitoring:
  • Log unexpected partial resolution failures or compilation events at runtime, especially where the "template" is not a simple string literal or originates from context.
  • Monitor for unusual spikes in template compilation frequency or unexpected error patterns that could indicate probing.
  • Automated tools (SAST/DAST/IAST) can be configured to recognize this bug pattern by tracing data flows from request parameters into template engines and identifying untrusted inputs reaching compile() or dynamic partial resolution paths.

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-xhpv-hc6g-r9c6
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-33940
bugzilla.redhat.combugzilla.redhat.com/show_bug.cgi?id=2452521
security.access.redhat.comsecurity.access.redhat.com/data/csaf/v2/vex/2026/cve-2026-33940.json

Frequently asked

questions

Exploitability depends on how Handlebars is integrated. It is most relevant when the full Handlebars build (with runtime compilation) is used server-side, templates rely on dynamic partials, and attackers can influence the values that drive partial resolution. If user input never affects partial selection and templates are precompiled using the runtime-only build, the exposure is significantly reduced.

An attacker needs the ability to control context data feeding into a dynamic partial lookup or similar mechanism. This often corresponds to normal web application inputs such as request parameters, JSON bodies, or database fields that are rendered in templates without strict validation of how they are used.

The safest pattern is to remove runtime compilation from the attack surface by using precompiled templates and the runtime-only Handlebars build, combined with eliminating or strictly constraining dynamic partials. Additionally, ensure that any data used for partial resolution is drawn from validated, whitelisted values rather than raw user input.

Treat template engines as code execution environments: precompile templates, avoid mixing untrusted data with template selection or structure, and enforce strong typing and validation for any dynamic constructs (like partials or helpers). Integrate static checks and code review guidelines that flag dynamic template behaviors, and encapsulate template rendering behind safe abstractions that do not expose compile() or equivalent APIs to untrusted input.

Find out what's already
exploitable in your codebase.