|
| 1 | +<!DOCTYPE qhelp PUBLIC |
| 2 | +"-//Semmle//qhelp//EN" |
| 3 | +"qhelp.dtd"> |
| 4 | +<qhelp> |
| 5 | +<overview> |
| 6 | + <p> |
| 7 | + Dynamically constructing a shell command with inputs from exported |
| 8 | + functions may inadvertently change the meaning of the shell command. |
| 9 | + |
| 10 | + Clients using the exported function may use inputs containing |
| 11 | + characters that the shell interprets in a special way, for instance |
| 12 | + quotes and spaces. |
| 13 | + |
| 14 | + This can result in the shell command misbehaving, or even |
| 15 | + allowing a malicious user to execute arbitrary commands on the system. |
| 16 | + </p> |
| 17 | + |
| 18 | + |
| 19 | +</overview> |
| 20 | +<recommendation> |
| 21 | + |
| 22 | + <p> |
| 23 | + If possible, provide the dynamic arguments to the shell as an array |
| 24 | + to APIs such as <code>system(..)</code> to avoid interpretation by the shell. |
| 25 | + </p> |
| 26 | + |
| 27 | + <p> |
| 28 | + Alternatively, if the shell command must be constructed |
| 29 | + dynamically, then add code to ensure that special characters |
| 30 | + do not alter the shell command unexpectedly. |
| 31 | + </p> |
| 32 | + |
| 33 | +</recommendation> |
| 34 | +<example> |
| 35 | + |
| 36 | + <p> |
| 37 | + The following example shows a dynamically constructed shell |
| 38 | + command that downloads a file from a remote URL. |
| 39 | + </p> |
| 40 | + |
| 41 | + <sample src="examples/unsafe-shell-command-construction.rb" /> |
| 42 | + |
| 43 | + <p> |
| 44 | + The shell command will, however, fail to work as intended if the |
| 45 | + input contains spaces or other special characters interpreted in a |
| 46 | + special way by the shell. |
| 47 | + </p> |
| 48 | + |
| 49 | + <p> |
| 50 | + Even worse, a client might pass in user-controlled |
| 51 | + data, not knowing that the input is interpreted as a shell command. |
| 52 | + This could allow a malicious user to provide the input <code>http://example.org; cat /etc/passwd</code> |
| 53 | + in order to execute the command <code>cat /etc/passwd</code>. |
| 54 | + </p> |
| 55 | + |
| 56 | + <p> |
| 57 | + To avoid such potentially catastrophic behaviors, provide the |
| 58 | + inputs from exported functions as an argument that does not |
| 59 | + get interpreted by a shell: |
| 60 | + </p> |
| 61 | + |
| 62 | + <sample src="examples/unsafe-shell-command-construction_fixed.rb" /> |
| 63 | + |
| 64 | +</example> |
| 65 | +<references> |
| 66 | + |
| 67 | + <li> |
| 68 | + OWASP: |
| 69 | + <a href="https://www.owasp.org/index.php/Command_Injection">Command Injection</a>. |
| 70 | + </li> |
| 71 | + |
| 72 | +</references> |
| 73 | +</qhelp> |
0 commit comments