|
| 1 | +<!DOCTYPE qhelp PUBLIC |
| 2 | + "-//Semmle//qhelp//EN" |
| 3 | + "qhelp.dtd"> |
| 4 | +<qhelp> |
| 5 | + |
| 6 | +<overview> |
| 7 | +<p> |
| 8 | +If unsanitized user input is written to a log entry, a malicious user may |
| 9 | +able to forge new log entries. |
| 10 | +</p> |
| 11 | + |
| 12 | +<p> |
| 13 | +Forgery can occur if a user provides some input with characters that are |
| 14 | +interpreted when the log output is displayed. If the log is displayed as a plain |
| 15 | +text file, then new line characters can be used by a malicious user. If the log |
| 16 | +is displayed as HTML, then arbitrary HTML may be included to spoof log entries. |
| 17 | +</p> |
| 18 | +</overview> |
| 19 | + |
| 20 | +<recommendation> |
| 21 | +<p> |
| 22 | +User input should be suitably sanitized before it is logged. Suitable means of |
| 23 | +sanitization depend on how the log entries will be displayed or consumed. |
| 24 | +</p> |
| 25 | + |
| 26 | +<p> |
| 27 | +If the log entries are in plain text then line breaks should be removed from |
| 28 | +user input, using <code>String#gsub</code> or similar. Care should also be |
| 29 | +taken that user input is clearly marked in log entries. |
| 30 | +</p> |
| 31 | + |
| 32 | +<p> |
| 33 | +For log entries that will be displayed in HTML, user input should be |
| 34 | +HTML-encoded before being logged, to prevent forgery and other forms of HTML |
| 35 | +injection. |
| 36 | +</p> |
| 37 | +</recommendation> |
| 38 | + |
| 39 | +<example> |
| 40 | +<p> |
| 41 | +In the example, a username, provided by the user, is logged using `Logger#info`. |
| 42 | +</p> |
| 43 | + |
| 44 | +<p> |
| 45 | +In the first case, it is logged without any sanitization. If a malicious user |
| 46 | +provides `username=Guest%0a[INFO]+User:+Admin%0a` as a username parameter, the |
| 47 | +log entry will be split in two different lines, where the second line will |
| 48 | +be `[INFO]+User:+Admin`. |
| 49 | +</p> |
| 50 | +<sample src="examples/log_injection_bad.rb" /> |
| 51 | + |
| 52 | +<p> |
| 53 | +In the second example, <code>String#gsub</code> is used to ensure no line |
| 54 | +endings are present in the user input. |
| 55 | +</p> |
| 56 | +<sample src="examples/log_injection_good.rb" /> |
| 57 | +</example> |
| 58 | + |
| 59 | +<references> |
| 60 | +<li>OWASP: <a href="https://www.owasp.org/index.php/Log_Injection">Log Injection</a>.</li> |
| 61 | +</references> |
| 62 | +</qhelp> |
0 commit comments