Summary
Lodash's _.template function can be abused to inject arbitrary JavaScript code when untrusted input is used as key names in the options.imports object.
Because these keys are funneled into a Function() constructor without proper validation, an attacker may execute code at template compilation time or through polluted Object.prototype properties.
Affected context
This vulnerability affects applications that:
- Use Lodash's
_.templatefor server- or client-side templating - Pass user-controlled or otherwise untrusted data as key names in
options.imports - Run templates in an environment where injected JavaScript can execute with meaningful privileges
It also becomes relevant where Object.prototype may be polluted by other vulnerabilities or unsafe libraries, because inherited properties are merged into the imports object.
Exploit preconditions
- Attack surface: Code paths that call
_.templatewith theoptions.importsparameter. - Required access: Ability for an attacker to influence or control the key names of
options.imports, or to trigger/provide input to an Object.prototype pollution vector elsewhere in the application. - Trigger point: Template compilation in
_.template, where imports are merged and passed into aFunction()constructor. - Why exploitation is possible: Input used as imports key names is not subject to the same validation as the
variableoption, allowing default-parameter expressions or other crafted identifiers to be embedded directly into a dynamically constructed function. Additionally,assignInWithcombined withfor..inenumeration copies polluted prototype properties into the imports object, which are then interpreted as function parameters.
Why this happens
The root cause is the insecure construction of dynamic JavaScript functions from partially unvalidated input.
Lodash's _.template builds a function using the Function() constructor, mapping entries from options.imports directly into function parameters without applying the same validation and sanitization that was added for the variable option after CVE-2021-23337.
This pattern aligns with CWE-94: Improper Control of Generation of Code, where attacker-controlled data influences generated source code.
The use of assignInWith and for..in exacerbates the issue by copying inherited, potentially polluted properties from Object.prototype into the imports object, broadening the set of attacker-controlled parameters.
Potential impact
- Execution of arbitrary JavaScript code in the process where
_.templateruns - Unauthorized access to in-memory sensitive data such as secrets, tokens, or configuration values
- Escalation of impact in server-side environments, potentially leading to full application compromise
- Abuse of client-side templates to perform actions as the current user, where applicable
- Chaining with prototype pollution to achieve remote code execution even when imports keys appear controlled
General mitigation guidance
Effective mitigation revolves around restricting what data can influence dynamic code generation and removing unnecessary trust in imports:
- Avoid passing untrusted or user-derived values as key names in
options.imports; use only static, developer-controlled identifiers. - Treat any API that ultimately feeds into
Function(),eval, or similar dynamic code constructors as a strict trust boundary and validate or hardcode inputs crossing that boundary. - Isolate templating logic so that it operates with least privilege and cannot access more capabilities than necessary (e.g., no direct access to global objects or sensitive modules where avoidable).
- Reduce exposure to prototype pollution by preferring
Object.create(null)for objects that will be merged or enumerated, and by avoiding patterns that rely on inherited enumerable properties. - Adopt a defense-in-depth approach by enforcing coding standards that prohibit or tightly constrain dynamic function generation, favoring safer template engines that do not rely on
Function()where possible.
How teams detect issues like this
Detection typically combines static analysis, code review, and policy enforcement:
- Use static analysis or linters to flag calls to
_.templatewhereoptions.importsis derived from request parameters, query strings, headers, or other untrusted sources. - During code review, look for dynamic construction of
options.importskeys, especially when they are computed from user input or passed through generic object-merging utilities. - Establish secure coding guidelines and CI checks that highlight any use of
Function(),new Function,eval, or template engines that wrap these constructs, and require explicit justification. - Monitor dependency configuration and source for patterns that mix prototype-pollutable objects with templating or code-generation routines.
- Consider automated SAST tools or dependency scanners that understand Lodash patterns and can surface unsafe template usage.

