WEBrick: prevent response splitting and header injection
authorYusuke Endoh <mame@ruby-lang.org>
Tue, 1 Oct 2019 03:29:18 +0000 (12:29 +0900)
committerSalvatore Bonaccorso <carnil@debian.org>
Sun, 15 Dec 2019 16:28:25 +0000 (16:28 +0000)
This is a follow up to d9d4a28f1cdd05a0e8dabb36d747d40bbcc30f16.
The commit prevented CRLR, but did not address an isolated CR or an
isolated LF.

Co-Authored-By: NARUSE, Yui <naruse@airemix.jp>
[Salvatore Bonaccorso: Backport to 2.3.3:
 - Context changes in test/webrick/test_httpresponse.rb
]

Gbp-Pq: Name WEBrick-prevent-response-splitting-and-header-inject.patch

lib/webrick/httpresponse.rb
test/webrick/test_httpresponse.rb

index c120cae09cca31febb5ee39b907af0b3fbb977d8..f6bdcc6ec256a6e4d140524b77d2c9f2b523bfcc 100644 (file)
@@ -367,7 +367,8 @@ module WEBrick
     private
 
     def check_header(header_value)
-      if header_value =~ /\r\n/
+      header_value = header_value.to_s
+      if /[\r\n]/ =~ header_value
         raise InvalidHeader
       else
         header_value
index 6263e0a71044b7b800a092a932ce23e25e889950..24a6968582e9bcf3e7824814cb8c2976491d0739 100644 (file)
@@ -29,7 +29,7 @@ module WEBrick
       @res.keep_alive  = true
     end
 
-    def test_prevent_response_splitting_headers
+    def test_prevent_response_splitting_headers_crlf
       res['X-header'] = "malicious\r\nCookie: hack"
       io = StringIO.new
       res.send_response io
@@ -39,7 +39,7 @@ module WEBrick
       refute_match 'hack', io.string
     end
 
-    def test_prevent_response_splitting_cookie_headers
+    def test_prevent_response_splitting_cookie_headers_crlf
       user_input = "malicious\r\nCookie: hack"
       res.cookies << WEBrick::Cookie.new('author', user_input)
       io = StringIO.new
@@ -50,6 +50,48 @@ module WEBrick
       refute_match 'hack', io.string
     end
 
+    def test_prevent_response_splitting_headers_cr
+      res['X-header'] = "malicious\rCookie: hack"
+      io = StringIO.new
+      res.send_response io
+      io.rewind
+      res = Net::HTTPResponse.read_new(Net::BufferedIO.new(io))
+      assert_equal '500', res.code
+      refute_match 'hack', io.string
+    end
+
+    def test_prevent_response_splitting_cookie_headers_cr
+      user_input = "malicious\rCookie: hack"
+      res.cookies << WEBrick::Cookie.new('author', user_input)
+      io = StringIO.new
+      res.send_response io
+      io.rewind
+      res = Net::HTTPResponse.read_new(Net::BufferedIO.new(io))
+      assert_equal '500', res.code
+      refute_match 'hack', io.string
+    end
+
+    def test_prevent_response_splitting_headers_lf
+      res['X-header'] = "malicious\nCookie: hack"
+      io = StringIO.new
+      res.send_response io
+      io.rewind
+      res = Net::HTTPResponse.read_new(Net::BufferedIO.new(io))
+      assert_equal '500', res.code
+      refute_match 'hack', io.string
+    end
+
+    def test_prevent_response_splitting_cookie_headers_lf
+      user_input = "malicious\nCookie: hack"
+      res.cookies << WEBrick::Cookie.new('author', user_input)
+      io = StringIO.new
+      res.send_response io
+      io.rewind
+      res = Net::HTTPResponse.read_new(Net::BufferedIO.new(io))
+      assert_equal '500', res.code
+      refute_match 'hack', io.string
+    end
+
     def test_304_does_not_log_warning
       res.status      = 304
       res.setup_header