Skip to content

Commit

Permalink
Prevent command injection through _.template's variable option
Browse files Browse the repository at this point in the history
Closes #5085.
  • Loading branch information
stof authored and bnjmnt4n committed Feb 20, 2021
1 parent ded9bc6 commit 3469357
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
21 changes: 20 additions & 1 deletion lodash.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

/** Error message constants. */
var CORE_ERROR_TEXT = 'Unsupported core-js use. Try https://npms.io/search?q=ponyfill.',
FUNC_ERROR_TEXT = 'Expected a function';
FUNC_ERROR_TEXT = 'Expected a function',
INVALID_TEMPL_VAR_ERROR_TEXT = 'Invalid `variable` option passed into `_.template`';

/** Used to stand-in for `undefined` hash values. */
var HASH_UNDEFINED = '__lodash_hash_undefined__';
Expand Down Expand Up @@ -165,6 +166,18 @@
/** Used to match words composed of alphanumeric characters. */
var reAsciiWord = /[^\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f]+/g;

/**
* Used to validate the `validate` option in `_.template` variable.
*
* Forbids characters which could potentially change the meaning of the function argument definition:
* - "()," (modification of function parameters)
* - "=" (default value)
* - "[]{}" (destructuring of function parameters)
* - "/" (beginning of a comment)
* - whitespace
*/
var reForbiddenIdentifierChars = /[()=,{}\[\]\/\s]/;

/** Used to match backslashes in property paths. */
var reEscapeChar = /\\(\\)?/g;

Expand Down Expand Up @@ -14866,6 +14879,12 @@
if (!variable) {
source = 'with (obj) {\n' + source + '\n}\n';
}
// Throw an error if a forbidden character was found in `variable`, to prevent
// potential command injection attacks.
else if (reForbiddenIdentifierChars.test(variable)) {
throw new Error(INVALID_TEMPL_VAR_ERROR_TEXT);
}

// Cleanup code by stripping empty strings.
source = (isEvaluating ? source.replace(reEmptyStringLeading, '') : source)
.replace(reEmptyStringMiddle, '$1')
Expand Down
8 changes: 8 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22296,6 +22296,14 @@
}
});

QUnit.test('should forbid code injection through the "variable" options', function(assert) {
assert.expect(1);

assert.raises(function () {
_.template('', { 'variable': '){console.log(process.env)}; with(obj' });
});
});

QUnit.test('should support custom delimiters', function(assert) {
assert.expect(2);

Expand Down

0 comments on commit 3469357

Please sign in to comment.