fix: shell command built from environment values #2454
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
mongosh/scripts/generate-authors.ts
Line 18 in 53febd4
mongosh/scripts/generate-authors.ts
Line 43 in 53febd4
To fix the issue, we will replace the use of
execSync
with a dynamically constructed shell command by usingexecFileSync
. This method allows us to pass the command and its arguments as separate parameters, avoiding interpretation by the shell. Specifically:`git log --reverse --format='%aN <%aE>' --use-mailmap -- ${packagePath}`
with a static command (git log
) and an array of arguments.packagePath
as an argument in the array, ensuring it is treated as a literal value and not interpreted by the shell.This change will ensure that special characters in
packagePath
do not alter the behavior of the command.Dynamically constructing a shell command with values from the local environment, such as file paths, may inadvertently change the meaning of the shell command. Such changes can occur when an environment value contains characters that the shell interprets in a special way, for instance quotes and spaces. This can result in the shell command misbehaving, or even allowing a malicious user to execute arbitrary commands on the system.
POC
The following shows a dynamically constructed shell command that recursively removes a temporary directory that is located next to the currently executing JavaScript file. Such utilities are often found in custom build scripts.
The shell command will, however, fail to work as intended if the absolute path of the script's directory contains spaces. In that case, the shell command will interpret the absolute path as multiple paths, instead of a single path. if the absolute path of the temporary directory is
/home/username/important project/temp
, then the shell command will recursively delete/home/username/important
andproject/temp
, where the latter path gets resolved relative to the working directory of the JavaScript process.Even worse, although less likely, a malicious user could provide the path
/home/username/; cat /etc/passwd #/important project/temp
in order to execute the commandcat /etc/passwd
. To avoid such potentially catastrophic behaviors, provide the directory as an argument that does not get interpreted by a shell:References
Command Injection
CWE-78
CWE-88