Fix Shell command built from environment values #15490
Merged
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.
nest/tools/gulp/tasks/samples.ts
Lines 81 to 86 in b6d4188
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.
Fix the problem, we should avoid constructing a shell command string that includes untrusted or unsanitized values. Instead, we should use
childProcess.execFile(or its promisified version) to run the command with arguments provided as an array, which avoids shell interpretation and the associated risks.Detailed steps:
exec(which runs a shell command string) withexecFile(which runs a command with arguments, without a shell).scriptandappendScriptparameters into an array of arguments.execFilewith the command and argument array.execFileas needed.execFilefromchild_process.executeNPMScriptInDirectoryto use the new approach.Required changes:
tools/gulp/tasks/samples.ts:execFilefromchild_process.execFile.executeNPMScriptInDirectoryto useexecFileinstead ofexec, and to build the argument array safely.scriptstring into command and arguments (e.g.,'npm install --legacy-peer-deps'→['npm', 'install', '--legacy-peer-deps']).--prefixanddiras arguments.appendScriptis provided, split it into arguments and append.PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Issue Number: N/A
What is the new behavior?
Does this PR introduce a breaking change?
Other information