Skip to content

Prevent command injection through the variable option #5085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lodash.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@
/** Used to match words composed of alphanumeric characters. */
var reAsciiWord = /[^\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f]+/g;

/**
* used to validate the template variable. Forbids chars changing the argument definition to inject things:
* - parenthesis and comma (as that controls the argument list)
* - = sign (default value)
* - curly braces and square braces, to forbid destructuring in the argument name
* - / (start of a comment hiding some parts)
* - whitespaces
*/
var reForbiddenIdentifierChars = /[()=,{}\[\]\/\s]/

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

Expand Down Expand Up @@ -14865,6 +14875,8 @@
var variable = hasOwnProperty.call(options, 'variable') && options.variable;
if (!variable) {
source = 'with (obj) {\n' + source + '\n}\n';
} else if (reForbiddenIdentifierChars.test(variable)) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed there is no check that variable is typeof 'string'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RegExp.prototype.test coerces its argument to a string before making matches.

throw new Error('Invalid variable name. It must be a valid EcmaScript identifier.')
}
// Cleanup code by stripping empty strings.
source = (isEvaluating ? source.replace(reEmptyStringLeading, '') : source)
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.throws(function () {
_.template('', { 'variable': '){console.log(process.env)}; with(obj' });
});
});

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

Expand Down