lodash
4.17.234.18.1
template.js~
template.jsModified+16−4
Index: package/template.js
===================================================================
--- package/template.js
+++ package/template.js
@@ -1,5 +1,6 @@
-var assignInWith = require('./assignInWith'),
+var arrayEach = require('./_arrayEach'),
+ assignWith = require('./assignWith'),
attempt = require('./attempt'),
baseValues = require('./_baseValues'),
customDefaultsAssignIn = require('./_customDefaultsAssignIn'),
escapeStringChar = require('./_escapeStringChar'),
@@ -10,9 +11,10 @@
templateSettings = require('./templateSettings'),
toString = require('./toString');
/** Error message constants. */
-var INVALID_TEMPL_VAR_ERROR_TEXT = 'Invalid `variable` option passed into `_.template`';
+var INVALID_TEMPL_VAR_ERROR_TEXT = 'Invalid `variable` option passed into `_.template`',
+ INVALID_TEMPL_IMPORTS_ERROR_TEXT = 'Invalid `imports` option passed into `_.template`';
/** Used to match empty string literals in compiled template source. */
var reEmptyStringLeading = /\b__p \+= '';/g,
reEmptyStringMiddle = /\b(__p \+=) '' \+/g,
@@ -54,8 +56,12 @@
* "escape" delimiters, and execute JavaScript in "evaluate" delimiters. Data
* properties may be accessed as free variables in the template. If a setting
* object is given, it takes precedence over `_.templateSettings` values.
*
+ * **Security:** `_.template` is insecure and should not be used. It will be
+ * removed in Lodash v5. Avoid untrusted input. See
+ * [threat model](https://github.com/lodash/lodash/blob/main/threat-model.md).
+ *
* **Note:** In the development build `_.template` utilizes
* [sourceURLs](http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl)
* for easier debugging.
*
@@ -161,14 +167,20 @@
if (guard && isIterateeCall(string, options, guard)) {
options = undefined;
}
string = toString(string);
- options = assignInWith({}, options, settings, customDefaultsAssignIn);
+ options = assignWith({}, options, settings, customDefaultsAssignIn);
- var imports = assignInWith({}, options.imports, settings.imports, customDefaultsAssignIn),
+ var imports = assignWith({}, options.imports, settings.imports, customDefaultsAssignIn),
importsKeys = keys(imports),
importsValues = baseValues(imports, importsKeys);
+ arrayEach(importsKeys, function(key) {
+ if (reForbiddenIdentifierChars.test(key)) {
+ throw new Error(INVALID_TEMPL_IMPORTS_ERROR_TEXT);
+ }
+ });
+
var isEscaping,
isEvaluating,
index = 0,
interpolate = options.interpolate || reNoMatch,