Skip to content

Commit 00e290e

Browse files
committed
Ruby: document rb/log-injection
1 parent c319957 commit 00e290e

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require 'logger'
2+
3+
class UsersController < ApplicationController
4+
def login
5+
logger = Logger.new STDOUT
6+
username = params[:username]
7+
8+
# BAD: log message constructed with unsanitized user input
9+
logger.info "attempting to login user: " + username
10+
11+
# ... login logic ...
12+
end
13+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require 'logger'
2+
3+
class UsersController < ApplicationController
4+
def login
5+
logger = Logger.new STDOUT
6+
username = params[:username]
7+
8+
# GOOD: log message constructed with unsanitized user input
9+
sanitized_username = username.gsub("\n", "")
10+
logger.info "attempting to login user: " + sanitized_username
11+
12+
# ... login logic ...
13+
end
14+
end

0 commit comments

Comments
 (0)