ruby-changes:58072
From: usa <ko1@a...>
Date: Tue, 1 Oct 2019 20:43:24 +0900 (JST)
Subject: [ruby-changes:58072] 8eff476bce (ruby_2_4): merge revision(s) 3ce238b5f9795581eb84114dcfbdf4aa086bfecc
https://git.ruby-lang.org/ruby.git/commit/?id=8eff476bce From 8eff476bce40b52f244b8c912d1a5f40aa64b683 Mon Sep 17 00:00:00 2001 From: usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> Date: Tue, 1 Oct 2019 11:05:56 +0000 Subject: merge revision(s) 3ce238b5f9795581eb84114dcfbdf4aa086bfecc WEBrick: prevent response splitting and header injection This is a follow up to d9d4a28. The commit prevented CRLR, but did not address an isolated CR or an isolated LF. Co-Authored-By: NARUSE, Yui <naruse@a...> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_4@67819 b2dd03c8-39d4-4d8f-98ff-823fe69b080e diff --git a/lib/webrick/httpresponse.rb b/lib/webrick/httpresponse.rb index d1dd1ba..b64730b 100644 --- a/lib/webrick/httpresponse.rb +++ b/lib/webrick/httpresponse.rb @@ -367,7 +367,8 @@ module WEBrick https://github.com/ruby/ruby/blob/trunk/lib/webrick/httpresponse.rb#L367 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 diff --git a/test/webrick/test_httpresponse.rb b/test/webrick/test_httpresponse.rb index 6263e0a..24a6968 100644 --- a/test/webrick/test_httpresponse.rb +++ b/test/webrick/test_httpresponse.rb @@ -29,7 +29,7 @@ module WEBrick https://github.com/ruby/ruby/blob/trunk/test/webrick/test_httpresponse.rb#L29 @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 https://github.com/ruby/ruby/blob/trunk/test/webrick/test_httpresponse.rb#L39 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 https://github.com/ruby/ruby/blob/trunk/test/webrick/test_httpresponse.rb#L50 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 diff --git a/version.h b/version.h index 27894d4..5652cc0 100644 --- a/version.h +++ b/version.h @@ -1,6 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/version.h#L1 #define RUBY_VERSION "2.4.8" #define RUBY_RELEASE_DATE "2019-10-01" -#define RUBY_PATCHLEVEL 359 +#define RUBY_PATCHLEVEL 360 #define RUBY_RELEASE_YEAR 2019 #define RUBY_RELEASE_MONTH 10 -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/