b8524a
From 8a81d04d2588d9c7a898473b431a0dabcab39fbd Mon Sep 17 00:00:00 2001
b8524a
From: usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>
b8524a
Date: Thu, 14 Sep 2017 11:37:47 +0000
b8524a
Subject: [PATCH] merge revision(s) 59897:
b8524a
b8524a
	lib/webrick/log.rb: sanitize any type of logs
b8524a
b8524a
	It had failed to sanitize some type of exception messages.  Reported and
b8524a
	patched by Yusuke Endoh (mame) at https://hackerone.com/reports/223363
b8524a
b8524a
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_2@59902 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
b8524a
---
b8524a
 ChangeLog                     |    7 +++++++
b8524a
 lib/webrick/httpstatus.rb     |    4 ----
b8524a
 lib/webrick/log.rb            |    4 ++--
b8524a
 test/webrick/test_httpauth.rb |   36 ++++++++++++++++++++++++++++++++++++
b8524a
 4 files changed, 45 insertions(+), 6 deletions(-)
b8524a
b8524a
diff --git a/ChangeLog b/ChangeLog
b8524a
index a4594f678f8c..7561c35eb705 100644
b8524a
--- a/ChangeLog
b8524a
+++ b/ChangeLog
b8524a
@@ -4,6 +4,13 @@
b8524a
 	  protocol list.
b8524a
 	  The protocol list from OpenSSL is not null-terminated.
b8524a
 	  patched by Kazuki Yamaguchi [Bug #11810] [ruby-core:72082]
b8524a
+
b8524a
+Thu Sep 14 20:36:54 2017  Yusuke Endoh  <mame@ruby-lang.org>
b8524a
+
b8524a
+	lib/webrick/log.rb: sanitize any type of logs
b8524a
+
b8524a
+	It had failed to sanitize some type of exception messages.  Reported and
b8524a
+	patched by Yusuke Endoh (mame) at https://hackerone.com/reports/223363
b8524a
 
b8524a
 Thu Sep 14 20:33:52 2017  Nobuyoshi Nakada  <nobu@ruby-lang.org>
b8524a
 
b8524a
diff --git a/lib/webrick/httpstatus.rb b/lib/webrick/httpstatus.rb
b8524a
index 7ffda64cf0f9..5dc136f88f70 100644
b8524a
--- a/lib/webrick/httpstatus.rb
b8524a
+++ b/lib/webrick/httpstatus.rb
b8524a
@@ -20,10 +20,6 @@ module HTTPStatus
b8524a
     ##
b8524a
     # Root of the HTTP status class hierarchy
b8524a
     class Status < StandardError
b8524a
-      def initialize(*args) # :nodoc:
b8524a
-        args[0] = AccessLog.escape(args[0]) unless args.empty?
b8524a
-        super(*args)
b8524a
-      end
b8524a
       class << self
b8524a
         attr_reader :code, :reason_phrase # :nodoc:
b8524a
       end
b8524a
diff --git a/lib/webrick/log.rb b/lib/webrick/log.rb
b8524a
index 41cde4a74084..4f069ac0c549 100644
b8524a
--- a/lib/webrick/log.rb
b8524a
+++ b/lib/webrick/log.rb
b8524a
@@ -117,10 +117,10 @@ def debug?; @level >= DEBUG; end
b8524a
     # * Otherwise it will return +arg+.inspect.
b8524a
     def format(arg)
b8524a
       if arg.is_a?(Exception)
b8524a
-        "#{arg.class}: #{arg.message}\n\t" <<
b8524a
+        "#{arg.class}: #{AccessLog.escape(arg.message)}\n\t" <<
b8524a
         arg.backtrace.join("\n\t") << "\n"
b8524a
       elsif arg.respond_to?(:to_str)
b8524a
-        arg.to_str
b8524a
+        AccessLog.escape(arg.to_str)
b8524a
       else
b8524a
         arg.inspect
b8524a
       end
b8524a
diff --git a/test/webrick/test_httpauth.rb b/test/webrick/test_httpauth.rb
b8524a
index 27c37f36770b..0aebb7a231c7 100644
b8524a
--- a/test/webrick/test_httpauth.rb
b8524a
+++ b/test/webrick/test_httpauth.rb
b8524a
@@ -79,6 +79,43 @@ def test_basic_auth3
b8524a
       WEBrick::HTTPAuth::Htpasswd.new(tmpfile.path)
b8524a
     }
b8524a
     tmpfile.close(true)
b8524a
+  end
b8524a
+
b8524a
+  def test_bad_username_with_control_characters
b8524a
+    log_tester = lambda {|log, access_log|
b8524a
+      assert_equal(2, log.length)
b8524a
+      assert_match(/ERROR Basic WEBrick's realm: foo\\ebar: the user is not allowed./, log[0])
b8524a
+      assert_match(/ERROR WEBrick::HTTPStatus::Unauthorized/, log[1])
b8524a
+    }
b8524a
+    TestWEBrick.start_httpserver_with_log({}, log_tester) {|server, addr, port, log|
b8524a
+      realm = "WEBrick's realm"
b8524a
+      path = "/basic_auth"
b8524a
+
b8524a
+      Tempfile.open("test_webrick_auth") {|tmpfile|
b8524a
+        tmpfile.close
b8524a
+        tmp_pass = WEBrick::HTTPAuth::Htpasswd.new(tmpfile.path)
b8524a
+        tmp_pass.set_passwd(realm, "webrick", "supersecretpassword")
b8524a
+        tmp_pass.set_passwd(realm, "foo", "supersecretpassword")
b8524a
+        tmp_pass.flush
b8524a
+
b8524a
+        htpasswd = WEBrick::HTTPAuth::Htpasswd.new(tmpfile.path)
b8524a
+        users = []
b8524a
+        htpasswd.each{|user, pass| users << user }
b8524a
+        server.mount_proc(path){|req, res|
b8524a
+          auth = WEBrick::HTTPAuth::BasicAuth.new(
b8524a
+            :Realm => realm, :UserDB => htpasswd,
b8524a
+            :Logger => server.logger
b8524a
+          )
b8524a
+          auth.authenticate(req, res)
b8524a
+          res.body = "hoge"
b8524a
+        }
b8524a
+        http = Net::HTTP.new(addr, port)
b8524a
+        g = Net::HTTP::Get.new(path)
b8524a
+        g.basic_auth("foo\ebar", "passwd")
b8524a
+        http.request(g){|res| assert_not_equal("hoge", res.body, log.call) }
b8524a
+        File.unlink tmpfile.path rescue nil
b8524a
+      }
b8524a
+    }
b8524a
   end
b8524a
 
b8524a
   DIGESTRES_ = /
b8524a
diff --git a/test/webrick/utils.rb b/test/webrick/utils.rb
b8524a
index e1c2344fb1aa..0e94ad34da71 100644
b8524a
--- a/test/webrick/utils.rb
b8524a
+++ b/test/webrick/utils.rb
b8524a
@@ -54,4 +54,43 @@
b8524a
   def start_httpproxy(config={}, &block)
b8524a
     start_server(WEBrick::HTTPProxyServer, config, &block)
b8524a
   end
b8524a
+
b8524a
+  DefaultLogTester = lambda {|log, access_log| assert_equal([], log) }
b8524a
+
b8524a
+  def start_server_with_log(klass, config={}, log_tester=DefaultLogTester, &block)
b8524a
+    log_ary = []
b8524a
+    access_log_ary = []
b8524a
+    log = proc { "webrick log start:\n" + (log_ary+access_log_ary).join.gsub(/^/, "  ").chomp + "\nwebrick log end" }
b8524a
+    server = klass.new({
b8524a
+      :BindAddress => "127.0.0.1", :Port => 0,
b8524a
+      :ServerType => Thread,
b8524a
+      :Logger => WEBrick::Log.new(log_ary, WEBrick::BasicLog::WARN),
b8524a
+      :AccessLog => [[access_log_ary, ""]]
b8524a
+    }.update(config))
b8524a
+    server_thread = server.start
b8524a
+    server_thread2 = Thread.new {
b8524a
+      server_thread.join
b8524a
+      if log_tester
b8524a
+        log_tester.call(log_ary, access_log_ary)
b8524a
+      end
b8524a
+    }
b8524a
+    addr = server.listeners[0].addr
b8524a
+    client_thread = Thread.new {
b8524a
+      begin
b8524a
+        block.yield([server, addr[3], addr[1], log])
b8524a
+      ensure
b8524a
+        server.shutdown
b8524a
+      end
b8524a
+    }
b8524a
+    client_thread.join
b8524a
+    server_thread2.join
b8524a
+  end
b8524a
+
b8524a
+  def start_httpserver_with_log(config={}, log_tester=DefaultLogTester, &block)
b8524a
+    start_server_with_log(WEBrick::HTTPServer, config, log_tester, &block)
b8524a
+  end
b8524a
+
b8524a
+  def start_httpproxy_with_log(config={}, log_tester=DefaultLogTester, &block)
b8524a
+    start_server_with_log(WEBrick::HTTPProxyServer, config, log_tester, &block)
b8524a
+  end
b8524a
 end