Handlebars Remote Code Execution Vulnerability via AST Injection

Vulnerability

A critical vulnerability allowing remote code execution has been identified in Handlebars versions 4.0.0 through 4.7.8. The issue arises because `Handlebars.compile()` can accept a pre-parsed Abstract Syntax Tree (AST) object, and the `value` field of a `NumberLiteral` AST node is emitted directly into the generated JavaScript without proper quoting or sanitization. This flaw enables an attacker to inject and execute arbitrary JavaScript on the server by supplying a crafted AST. The vulnerability can be exploited in any environment that deserializes user-controlled JSON and passes it to `Handlebars.compile()` without validation.

Impact

Exploitation of this vulnerability allows for arbitrary JavaScript execution on the server, leading to remote code execution.

Reproduction

To reproduce this vulnerability, send a POST request to an endpoint that uses Handlebars for template rendering. The request body must include a JSON object that represents a `NumberLiteral` AST node, with the `value` field containing unquoted JavaScript code, such as a command injection payload. When the server processes this request, the injected code will be executed, demonstrating the vulnerability.

Remediation

Users can upgrade to Handlebars version 4.7.9, which addresses this vulnerability by adding input validation for AST nodes. If templates are pre-compiled, use the Handlebars runtime-only build (`handlebars/runtime`) on the server.

Added: Mar 27, 2026, 9:23 PM
Updated: Mar 27, 2026, 9:23 PM

Vulnerability Rating

Custom Algorithm
spread
7.8
impact
10.0
exploitability
6.0
remediation
7.9
relevance
4.8
threat
6.4
urgency
2.9
incentive
0.0

Our algorithm analyzes dozens of metrics to generate these 8 key vulnerability categories, which are then combined to calculate the overall risk score.