CVE-2026-33940: Handlebars.js has JavaScript Injection via AST Type Confusion when passing an object as dynamic partial

Published Mar 27, 2026
·
Updated

Summary

A crafted object placed in the template context can bypass all conditional guards in resolvePartial() and cause invokePartial() to return undefined. The Handlebars runtime then treats the unresolved partial as a source that needs to be compiled, passing the crafted object to env.compile(). Because the object is a valid Handlebars AST containing injected code, the generated JavaScript executes arbitrary commands on the server. The attack requires the adversary to control a value that can be returned by a dynamic partial lookup.

Description

The vulnerable code path spans two functions in lib/handlebars/runtime.js:

resolvePartial(): A crafted object with call: true satisfies the first branch condition (partial.call) and causes an early return of the original object itself, because none of the remaining conditionals (string check, options.partials lookup, etc.) match a plain object. The function returns the crafted object as-is.

invokePartial(): When resolvePartial returns a non-function object, invokePartial produces undefined. The runtime interprets undefined as "partial not yet compiled" and calls env.compile(partial, ...) where partial is the crafted AST object. The JavaScript code generator processes the AST and emits JavaScript containing the injected payload, which is then evaluated.

Minimum prerequisites: 1. The template uses a dynamic partial lookup: {{> (lookup . "key")}} or equivalent. 2. The adversary can set the value of the looked-up context property to a crafted object.

In server-side rendering scenarios where templates process user-supplied context data, this enables full Remote Code Execution.

Proof of Concept

javascript const Handlebars = require('handlebars');

const vulnerableTemplate = {{> (lookup . "payload")}};

const maliciousContext = { payload: { call: true, // bypasses the primary resolvePartial branch type: "Program", body: [ { type: "MustacheStatement", depth: 0, path: { type: "PathExpression", parts: ["pop"], original: "this.pop", // Injected code breaks out of the generated function's argument list depth: "0])),function () {console.error('VULNERABLE: object -> dynamic partial -> RCE');}()));//", }, }, ], }, };

Handlebars.compile(vulnerableTemplate)(maliciousContext); // Prints: VULNERABLE: object -> dynamic partial -> RCE

Workarounds

- Use the runtime-only build (require('handlebars/runtime')). Without compile(), the fallback compilation path in invokePartial is unreachable. - Sanitize context data before rendering: ensure no value in the context is a non-primitive object that could be passed to a dynamic partial. - Avoid dynamic partial lookups ({{> (lookup ...)}}) when context data is user-controlled.

Other sources

Handlebars provides the power necessary to let users build semantic templates. In versions 4.0.0 through 4.7.8, a crafted object placed in the template context can bypass all conditional guards in resolvePartial() and cause invokePartial() to return undefined. The Handlebars runtime then treats the unresolved partial as a source that needs to be compiled, passing the crafted object to env.compile(). Because the object is a valid Handlebars AST containing injected code, the generated JavaScript executes arbitrary commands on the server. The attack requires the adversary to control a value that can be returned by a dynamic partial lookup. Version 4.7.9 fixes the issue. Some workarounds are available. First, use the runtime-only build (require('handlebars/runtime')). Without compile(), the fallback compilation path in invokePartial is unreachable. Second, sanitize context data before rendering: Ensure no value in the context is a non-primitive object that could be passed to a dynamic partial. Third, avoid dynamic partial lookups ({

IBM

Affected Software

3 affected componentsFixes available
npm/handlebars>=4.0.0<=4.7.8
4.7.9
Handlebarsjs Handlebars Node.js>=4.0.0<4.7.9
IBM API Connect V12 OnPrem<=All

Remediation

Recommended actions to resolve this vulnerability, in priority order.

  1. Upgrade

    Upgrade npm/handlebars to a version that resolves this vulnerability.

    Fixed in 4.7.9
  2. Upgrade

    Upgrade handlebars to a version that resolves this vulnerability.

    Fixed in 4.7.9
  3. Configuration

    If rendering server-side with user-supplied context, avoid dynamic partial lookups like `{{> (lookup ...)}}` where the lookup input comes from user-controlled context.

    Handlebars templates dynamic partial lookup usage = avoid {{> (lookup ...)}} when context data is user-controlled
  4. Configuration

    Sanitize template context before rendering: ensure no value in the context is a non-primitive object that could be passed to a dynamic partial.

    Handlebars template rendering context context data types = sanitize so no value is a non-primitive object
  5. Configuration

    Use the runtime-only build, e.g., `require('handlebars/runtime')`, instead of the full Handlebars build.

    Handlebars module usage build import = use runtime-only build via require('handlebars/runtime')

Event History

Mar 27, 2026
Advisory Published
via GitHub·06:21 PM
Data Sourced
via GitHub·06:21 PM
DescriptionSeverityWeaknessAffected Software
CVE Published
via MITRE·09:11 PM
Data Sourced
via MITRE·09:11 PM
DescriptionSeverityWeakness
Data Sourced
via Red Hat·10:03 PM
DescriptionSeverityAffected Software
Data Sourced
via NVD·10:16 PM
RemedyDescriptionSeverityWeaknessAffected Software
Jul 7, 2026
Data Sourced
via IBM·12:00 AM
DescriptionAffected Software

Parent advisories

This vulnerability appears in the following advisories.

Free Weekly Intel

Don't miss critical vulnerabilities

Join thousands of security professionals who receive our weekly digest of trending CVEs, zero-days, and exploited vulnerabilities.

No spam. Unsubscribe anytime.

Frequently Asked Questions

1

What is the severity of CVE-2026-33940?

CVE-2026-33940 is classified as a high-severity vulnerability that can lead to unexpected behavior in the Handlebars runtime.

2

How do I fix CVE-2026-33940?

To mitigate CVE-2026-33940, update Handlebars to version 4.7.9 or later, as this version addresses the vulnerability.

3

Which versions of Handlebars are affected by CVE-2026-33940?

CVE-2026-33940 affects Handlebars versions from 4.0.0 to 4.7.8.

4

What are the potential impacts of CVE-2026-33940?

The potential impacts of CVE-2026-33940 include returning undefined results from invokePartial(), which can lead to application misbehavior.

5

Is CVE-2026-33940 specific to certain environments?

CVE-2026-33940 specifically affects environments using Handlebars within web applications that utilize the templating engine.

Contact

SecAlerts Pty Ltd.
132 Wickham Terrace
Fortitude Valley,
QLD 4006, Australia
info@secalerts.co
By using SecAlerts services, you agree to our services end-user license agreement. This website is safeguarded by reCAPTCHA and governed by the Google Privacy Policy and Terms of Service. All names, logos, and brands of products are owned by their respective owners, and any usage of these names, logos, and brands for identification purposes only does not imply endorsement. If you possess any content that requires removal, please get in touch with us.
© 2026 SecAlerts Pty Ltd.
ABN: 70 645 966 203, ACN: 645 966 203