lodash vulnerable to Code Injection via `_.template` imports key names
Affected releases · lodash
- 4.x4.0.0 – 4.17.23Fixed in 4.18.0
On 4.x, the newest line:
npm install lodash@4.18.0Every release on a timeline
Not affected
0.1.0 – 3.10.1
57 releases
Affected
4.0.0 – 4.17.23
54 releases
Fixed
4.18.0 – 4.18.1
2 releases, latest
What happens
Impact
The fix for CVE-2021-23337 added validation for the variable option in _.template but did not apply the same validation to options.imports key names. Both paths flow into the same Function() constructor sink.
When an application passes untrusted input as options.imports key names, an attacker can inject default-parameter expressions that execute arbitrary code at template compilation time.
Additionally, _.template uses assignInWith to merge imports, which enumerates inherited properties via for..in. If Object.prototype has been polluted by any other vector, the polluted keys are copied into the imports object and passed to Function().
Patches
Users should upgrade to version 4.18.0.
The fix applies two changes:
Validate
importsKeysagainst the existingreForbiddenIdentifierCharsregex (same check already used for thevariableoption)Replace
assignInWithwithassignWithwhen merging imports, so only own properties are enumerated
Workarounds
Do not pass untrusted input as key names in options.imports. Only use developer-controlled, static key names.