Skip to content

Commit 1560d21

Browse files
committedFeb 19, 2021
Prevent command injection through the variable option
1 parent ded9bc6 commit 1560d21

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed
 

‎lodash.js

+12
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,16 @@
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

@@ -14865,6 +14875,8 @@
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)

‎test/test.js

+8
Original file line numberDiff line numberDiff line change
@@ -22296,6 +22296,14 @@
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

0 commit comments

Comments
 (0)