Skip to content

Commit 4aa1978

Browse files
jeremyevansioquatix
authored andcommitted
Escape non-printable characters when logging.
1 parent e217a39 commit 4aa1978

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

lib/rack/common_logger.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class CommonLogger
2020
# The actual format is slightly different than the above due to the
2121
# separation of SCRIPT_NAME and PATH_INFO, and because the elapsed
2222
# time in seconds is included at the end.
23-
FORMAT = %{%s - %s [%s] "%s %s%s%s %s" %d %s %0.4f\n}
23+
FORMAT = %{%s - %s [%s] "%s %s%s%s %s" %d %s %0.4f }
2424

2525
# +logger+ can be any object that supports the +write+ or +<<+ methods,
2626
# which includes the standard library Logger. These methods are called
@@ -66,7 +66,8 @@ def log(env, status, response_headers, began_at)
6666
length,
6767
Utils.clock_time - began_at)
6868

69-
msg.gsub!(/[^[:print:]\n]/) { |c| sprintf("\\x%x", c.ord) }
69+
msg.gsub!(/[^[:print:]]/) { |c| sprintf("\\x%x", c.ord) }
70+
msg[-1] = "\n"
7071

7172
logger = @logger || request.get_header(RACK_ERRORS)
7273
# Standard library logger doesn't support write but it supports << which actually

test/spec_common_logger.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,17 @@ def with_mock_time(t = 0)
107107
(0..1).must_include duration.to_f
108108
end
109109

110-
it "escapes non printable characters except newline" do
110+
it "escapes non printable characters including newline" do
111111
logdev = StringIO.new
112112
log = Logger.new(logdev)
113113
Rack::MockRequest.new(Rack::CommonLogger.new(app_without_lint, log)).request("GET\x1f", "/hello")
114114

115115
logdev.string.must_match(/GET\\x1f \/hello HTTP\/1\.1/)
116+
117+
Rack::MockRequest.new(Rack::CommonLogger.new(app, log)).get("/", 'REMOTE_USER' => "foo\nbar", "QUERY_STRING" => "bar\nbaz")
118+
logdev.string[-1].must_equal "\n"
119+
logdev.string.must_include("foo\\xabar")
120+
logdev.string.must_include("bar\\xabaz")
116121
end
117122

118123
it "log path with PATH_INFO" do

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy