REDHAT-BUG-2452523: High severity Handlebars Handlebars vulnerability
Handlebars provides the power necessary to let users build semantic templates. In versions 4.0.0 through 4.7.8, Handlebars.compile() accepts a pre-parsed AST object in addition to a template string. The value field of a NumberLiteral AST node is emitted directly into the generated JavaScript without quoting or sanitization. An attacker who can supply a crafted AST to compile() can therefore inject and execute arbitrary JavaScript, leading to Remote Code Execution on the server. Version 4.7.9 fixes the issue. Some workarounds are available. Validate input type before calling Handlebars.compile(); ensure the argument is always a string, never a plain object or JSON-deserialized value. Use the Handlebars runtime-only build (handlebars/runtime) on the server if templates are pre-compiled at build time; compile() will be unavailable.
Affected Software
Remediation
Recommended actions to resolve this vulnerability, in priority order.
- Upgrade
Upgrade to a fixed release to a version that resolves this vulnerability.
Fixed in 4.7.9 - Configuration
If templates are pre-compiled at build time, use the Handlebars runtime-only build (`handlebars/runtime`) on the server so `compile()` is unavailable.
Handlebars (server-side template compilation) Use runtime-only build handlebars/runtime vs full build = handlebars/runtime - Configuration
Before calling `Handlebars.compile()`, validate that the argument is always a `string` and ensure it is never a plain object or a JSON-deserialized value.
Handlebars.compile() Input validation for compile() argument type = string only
Event History
Frequently Asked Questions
What applications are realistically exposed to this issue?
Applications using Handlebars versions 4.0.0 through 4.7.8 are exposed if untrusted input can reach Handlebars.compile() as a pre-parsed AST object rather than as a template string. The impact is server-side arbitrary JavaScript execution.
What must an attacker control to exploit it?
An attacker must be able to supply a crafted AST object to Handlebars.compile(). Supplying only ordinary template text is not the condition described for exploitation.
Are default template compilation patterns affected?
The vulnerable behavior occurs because compile() accepts an AST object in addition to a string. Code that ensures its compile() argument is always a string is not exposed through this AST injection path.
What can be done if upgrading is not immediately possible?
Validate the input type before calling Handlebars.compile() and reject plain objects or JSON-deserialized values, allowing only strings. If templates are precompiled during the build process, use the handlebars/runtime server-side build so compile() is unavailable.
Which version fixes the issue?
Handlebars version 4.7.9 fixes the issue.