|
| 1 | +<!DOCTYPE qhelp PUBLIC |
| 2 | +"-//Semmle//qhelp//EN" |
| 3 | +"qhelp.dtd"> |
| 4 | +<qhelp> |
| 5 | +<overview> |
| 6 | + <p> |
| 7 | + |
| 8 | + Dynamically constructing a shell command with values from the |
| 9 | + local environment, such as file paths, may inadvertently |
| 10 | + change the meaning of the shell command. |
| 11 | + |
| 12 | + Such changes can occur when an environment value contains |
| 13 | + characters that the shell interprets in a special way, for instance |
| 14 | + quotes and spaces. |
| 15 | + |
| 16 | + This can result in the shell command misbehaving, or even |
| 17 | + allowing a malicious user to execute arbitrary commands on the system. |
| 18 | + |
| 19 | + </p> |
| 20 | + |
| 21 | + |
| 22 | +</overview> |
| 23 | +<recommendation> |
| 24 | + |
| 25 | + <p> |
| 26 | + |
| 27 | + If possible, use hard-coded string literals to specify the |
| 28 | + shell command to run, and provide the dynamic arguments to the shell |
| 29 | + command separately to avoid interpretation by the shell. |
| 30 | + |
| 31 | + </p> |
| 32 | + |
| 33 | + <p> |
| 34 | + |
| 35 | + Alternatively, if the shell command must be constructed |
| 36 | + dynamically, then add code to ensure that special characters in |
| 37 | + environment values do not alter the shell command unexpectedly. |
| 38 | + |
| 39 | + </p> |
| 40 | + |
| 41 | +</recommendation> |
| 42 | +<example> |
| 43 | + |
| 44 | + <p> |
| 45 | + |
| 46 | + The following example shows a dynamically constructed shell |
| 47 | + command that recursively removes a temporary directory that is located |
| 48 | + next to the currently executing JavaScript file. Such utilities are |
| 49 | + often found in custom build scripts. |
| 50 | + |
| 51 | + </p> |
| 52 | + |
| 53 | + <sample src="examples/shell-command-injection-from-environment.js" /> |
| 54 | + |
| 55 | + <p> |
| 56 | + |
| 57 | + The shell command will, however, fail to work as intended if the |
| 58 | + absolute path of the script's directory contains spaces. In that |
| 59 | + case, the shell command will interpret the absolute path as multiple |
| 60 | + paths, instead of a single path. |
| 61 | + </p> |
| 62 | + |
| 63 | + <p> |
| 64 | + |
| 65 | + For instance, if the absolute path of |
| 66 | + the temporary directory is <code>/home/username/important |
| 67 | + project/temp</code>, then the shell command will recursively delete |
| 68 | + <code>/home/username/important</code> and <code>project/temp</code>, |
| 69 | + where the latter path gets resolved relative to the working directory |
| 70 | + of the JavaScript process. |
| 71 | + |
| 72 | + </p> |
| 73 | + |
| 74 | + |
| 75 | + <p> |
| 76 | + |
| 77 | + Even worse, although less likely, a malicious user could |
| 78 | + provide the path <code>/home/username/; cat /etc/passwd #/important |
| 79 | + project/temp</code> in order to execute the command <code>cat |
| 80 | + /etc/passwd</code>. |
| 81 | + |
| 82 | + </p> |
| 83 | + |
| 84 | + <p> |
| 85 | + |
| 86 | + To avoid such potentially catastrophic behaviors, provide the |
| 87 | + directory as an argument that does not get interpreted by a |
| 88 | + shell: |
| 89 | + |
| 90 | + </p> |
| 91 | + |
| 92 | + <sample src="examples/shell-command-injection-from-environment_fixed.js" /> |
| 93 | + |
| 94 | +</example> |
| 95 | +<references> |
| 96 | + |
| 97 | + <li> |
| 98 | + OWASP: |
| 99 | + <a href="https://www.owasp.org/index.php/Command_Injection">Command Injection</a>. |
| 100 | + </li> |
| 101 | + |
| 102 | +</references> |
| 103 | +</qhelp> |
0 commit comments