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 thetemplateargument. - 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 maliciousNumberLiteral.valuefield. - Why exploitation is possible: When an AST object is provided, Handlebars emits the
NumberLiteral.valuedirectly 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 trustedstring. - Prefer a build-time pre-compilation workflow, using the Handlebars runtime-only build on the server (
handlebars/runtime) so thatcompile()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.

