Check your project using Semgrep
1. Running an OS command
1.A. Spawning a shell with exec function
Theexec() function executes shell commands. It spawns a shell and then executes the command within the spawned shell, buffering any generated output. Never pass unsanitized user input to this function. Any input containing shell metacharacters can be used to trigger the execution of arbitrary commands.
Example:
References
Mitigation
- Always try to use internal JavaScript APIs (if they exist) instead of running an OS command. In other words, use internal language features instead of invoking commands that can be manipulated by a malicious actor.
- Don’t pass user-controlled input or use an allowlist for inputs.
- If it is not possible, use an array with a sequence of program arguments instead of a single string.
- Avoid non-literal values for the command string. Strip everything except alphanumeric characters from an input provided for the command string and arguments.
Semgrep rule
javascript.lang.security.detect-child-process.detect-child-process
1.B. Spawning a process with spawn function
Thespawn() and spawnSync() functions spawn a new process using the command with a list of arguments. This allows spawning any programs or running shell processes with arbitrary arguments, which can result in a command injection vulnerability.
Example:
References
child_process module documentation
Mitigation
- Always try to use internal JavaScript APIs (if they exist) instead of running an OS command. In other words, use internal language features instead of invoking commands that can be manipulated by a malicious actor.
- Don’t pass user-controlled input or use an allowlist for inputs.
- Do not include command arguments in a command string, use parameterization instead. For example:
Use:Instead of: - Define a list of allowed arguments.
- Avoid non-literal values for the command string. Strip everything except alphanumeric characters from an input provided for the command string and arguments.
Semgrep rule
javascript.lang.security.detect-child-process.detect-child-process