File tree 2 files changed +20
-0
lines changed
2 files changed +20
-0
lines changed Original file line number Diff line number Diff line change 165
165
/** Used to match words composed of alphanumeric characters. */
166
166
var reAsciiWord = /[^\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f]+/g;
167
167
168
+ /**
169
+ * used to validate the template variable. Forbids chars changing the argument definition to inject things:
170
+ * - parenthesis and comma (as that controls the argument list)
171
+ * - = sign (default value)
172
+ * - curly braces and square braces, to forbid destructuring in the argument name
173
+ * - / (start of a comment hiding some parts)
174
+ * - whitespaces
175
+ */
176
+ var reForbiddenIdentifierChars = /[()=,{}\[\]\/\s]/
177
+
168
178
/** Used to match backslashes in property paths. */
169
179
var reEscapeChar = /\\(\\)?/g;
170
180
14865
14875
var variable = hasOwnProperty.call(options, 'variable') && options.variable;
14866
14876
if (!variable) {
14867
14877
source = 'with (obj) {\n' + source + '\n}\n';
14878
+ } else if (reForbiddenIdentifierChars.test(variable)) {
14879
+ throw new Error('Invalid variable name. It must be a valid EcmaScript identifier.')
14868
14880
}
14869
14881
// Cleanup code by stripping empty strings.
14870
14882
source = (isEvaluating ? source.replace(reEmptyStringLeading, '') : source)
Original file line number Diff line number Diff line change 22296
22296
}
22297
22297
});
22298
22298
22299
+ QUnit.test('should forbid code injection through the "variable" options', function(assert) {
22300
+ assert.expect(1);
22301
+
22302
+ assert.throws(function () {
22303
+ _.template('', { 'variable': '){console.log(process.env)}; with(obj' });
22304
+ });
22305
+ });
22306
+
22299
22307
QUnit.test('should support custom delimiters', function(assert) {
22300
22308
assert.expect(2);
22301
22309
You can’t perform that action at this time.
0 commit comments