1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
From 18d5289b4579822e391b3f5c16541e6552e9f06c Mon Sep 17 00:00:00 2001
From: Yusuke Endoh <mame@ruby-lang.org>
Date: Tue, 1 Oct 2019 12:29:18 +0900
Subject: [PATCH] WEBrick: prevent response splitting and header injection
This is a follow up to d9d4a28f1cdd05a0e8dabb36d747d40bbcc30f16.
The commit prevented CRLR, but did not address an isolated CR or an
isolated LF.
Upstream-Status: Backport https://github.com/ruby/ruby/commit/3ce238b5f9795581eb84114dcfbdf4aa086bfecc
CVE: CVE-2019-16254
Co-Authored-By: NARUSE, Yui <naruse@airemix.jp>
Signed-off-by: Rahul Chauhan <rahulchauhankitps@gmail.com>
---
lib/webrick/httpresponse.rb | 3 ++-
test/webrick/test_httpresponse.rb | 46 +++++++++++++++++++++++++++++++++++++--
2 files changed, 46 insertions(+), 3 deletions(-)
diff --git a/lib/webrick/httpresponse.rb b/lib/webrick/httpresponse.rb
index 6d77692..d26324c 100644
--- a/lib/webrick/httpresponse.rb
+++ b/lib/webrick/httpresponse.rb
@@ -367,7 +367,8 @@ def set_error(ex, backtrace=false)
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 @@ def setup
@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 @@ def test_prevent_response_splitting_headers
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 @@ def test_prevent_response_splitting_cookie_headers
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
--
2.7.4
|