File tree Expand file tree Collapse file tree 2 files changed +20
-0
lines changed
Expand file tree Collapse file tree 2 files changed +20
-0
lines changed Original file line number Diff line number Diff line change 165165 /** Used to match words composed of alphanumeric characters. */
166166 var reAsciiWord = /[^\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f]+/g;
167167
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+
168178 /** Used to match backslashes in property paths. */
169179 var reEscapeChar = /\\(\\)?/g;
170180
1486514875 var variable = hasOwnProperty.call(options, 'variable') && options.variable;
1486614876 if (!variable) {
1486714877 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.')
1486814880 }
1486914881 // Cleanup code by stripping empty strings.
1487014882 source = (isEvaluating ? source.replace(reEmptyStringLeading, '') : source)
Original file line number Diff line number Diff line change 2229622296 }
2229722297 });
2229822298
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+
2229922307 QUnit.test('should support custom delimiters', function(assert) {
2230022308 assert.expect(2);
2230122309
You can’t perform that action at this time.
0 commit comments