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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
# frozen_string_literal: false
require "webrick"
require "stringio"
require "net/http"
module WEBrick
class TestHTTPResponse < Test::Unit::TestCase
class FakeLogger
attr_reader :messages
def initialize
@messages = []
end
def warn msg
@messages << msg
end
end
attr_reader :config, :logger, :res
def setup
super
@logger = FakeLogger.new
@config = Config::HTTP
@config[:Logger] = logger
@res = HTTPResponse.new config
@res.keep_alive = true
end
def test_response_body_not_frozen
refute @res.body.frozen?
end
def test_prevent_response_splitting_headers_crlf
res['X-header'] = "malicious\r\nCookie: cracked_indicator_for_test"
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 'cracked_indicator_for_test', io.string
end
def test_prevent_response_splitting_cookie_headers_crlf
user_input = "malicious\r\nCookie: cracked_indicator_for_test"
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 'cracked_indicator_for_test', io.string
end
def test_prevent_response_splitting_headers_cr
res['X-header'] = "malicious\rCookie: cracked_indicator_for_test"
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 'cracked_indicator_for_test', io.string
end
def test_prevent_response_splitting_cookie_headers_cr
user_input = "malicious\rCookie: cracked_indicator_for_test"
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 'cracked_indicator_for_test', io.string
end
def test_prevent_response_splitting_headers_lf
res['X-header'] = "malicious\nCookie: cracked_indicator_for_test"
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 'cracked_indicator_for_test', io.string
end
def test_prevent_response_splitting_cookie_headers_lf
user_input = "malicious\nCookie: cracked_indicator_for_test"
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 'cracked_indicator_for_test', io.string
end
def test_set_redirect_response_splitting
url = "malicious\r\nCookie: cracked_indicator_for_test"
assert_raise(URI::InvalidURIError) do
res.set_redirect(WEBrick::HTTPStatus::MultipleChoices, url)
end
end
def test_set_redirect_html_injection
url = 'http://example.com////?a</a><head></head><body><img src=1></body>'
assert_raise(WEBrick::HTTPStatus::MultipleChoices) do
res.set_redirect(WEBrick::HTTPStatus::MultipleChoices, url)
end
res.status = 300
io = StringIO.new
res.send_response(io)
io.rewind
res = Net::HTTPResponse.read_new(Net::BufferedIO.new(io))
assert_equal '300', res.code
refute_match(/<img/, io.string)
end
def test_304_does_not_log_warning
res.status = 304
res.setup_header
assert_equal 0, logger.messages.length
end
def test_204_does_not_log_warning
res.status = 204
res.setup_header
assert_equal 0, logger.messages.length
end
def test_1xx_does_not_log_warnings
res.status = 105
res.setup_header
assert_equal 0, logger.messages.length
end
def test_200_chunked_does_not_set_content_length
res.chunked = false
res["Transfer-Encoding"] = 'chunked'
res.setup_header
assert_nil res.header.fetch('content-length', nil)
end
def test_send_body_io
IO.pipe {|body_r, body_w|
body_w.write 'hello'
body_w.close
@res.body = body_r
IO.pipe {|r, w|
@res.send_body w
w.close
assert_equal 'hello', r.read
}
}
assert_equal 0, logger.messages.length
end
def test_send_body_string
@res.body = 'hello'
IO.pipe {|r, w|
@res.send_body w
w.close
assert_equal 'hello', r.read
}
assert_equal 0, logger.messages.length
end
def test_send_body_string_io
@res.body = StringIO.new 'hello'
IO.pipe {|r, w|
@res.send_body w
w.close
assert_equal 'hello', r.read
}
assert_equal 0, logger.messages.length
end
def test_send_body_io_chunked
@res.chunked = true
IO.pipe {|body_r, body_w|
body_w.write 'hello'
body_w.close
@res.body = body_r
IO.pipe {|r, w|
@res.send_body w
w.close
r.binmode
assert_equal "5\r\nhello\r\n0\r\n\r\n", r.read
}
}
assert_equal 0, logger.messages.length
end
def test_send_body_string_chunked
@res.chunked = true
@res.body = 'hello'
IO.pipe {|r, w|
@res.send_body w
w.close
r.binmode
assert_equal "5\r\nhello\r\n0\r\n\r\n", r.read
}
assert_equal 0, logger.messages.length
end
def test_send_body_string_io_chunked
@res.chunked = true
@res.body = StringIO.new 'hello'
IO.pipe {|r, w|
@res.send_body w
w.close
r.binmode
assert_equal "5\r\nhello\r\n0\r\n\r\n", r.read
}
assert_equal 0, logger.messages.length
end
def test_send_body_proc
@res.body = Proc.new { |out| out.write('hello') }
IO.pipe do |r, w|
@res.send_body(w)
w.close
r.binmode
assert_equal 'hello', r.read
end
assert_equal 0, logger.messages.length
end
def test_send_body_proc_chunked
@res.body = Proc.new { |out| out.write('hello') }
@res.chunked = true
IO.pipe do |r, w|
@res.send_body(w)
w.close
r.binmode
assert_equal "5\r\nhello\r\n0\r\n\r\n", r.read
end
assert_equal 0, logger.messages.length
end
def test_send_body_proc_upgrade
@res.body = Proc.new { |out| out.write('hello'); out.close }
@res.upgrade!("text")
IO.pipe do |r, w|
@res.send_response(w)
w.close
assert_match(/Connection: upgrade\r\nUpgrade: text\r\n\r\nhello/, r.read)
end
assert_empty logger.messages
end
def test_send_body_proc_stream
@res.body = Proc.new do |socket|
chunk = socket.read
socket.write(chunk)
socket.close
end
UNIXSocket.pair do |s1, s2|
thread = Thread.new do
@res.send_response(s1)
end
s2.write("hello")
s2.close_write
chunk = s2.read
assert_match(/Connection: close\r\n\r\nhello/, chunk)
s2.close
thread.join
end
assert_empty logger.messages
end
def test_set_error
status = 400
message = 'missing attribute'
@res.status = status
error = WEBrick::HTTPStatus[status].new(message)
body = @res.set_error(error)
assert_match(/#{@res.reason_phrase}/, body)
assert_match(/#{message}/, body)
end
def test_no_extraneous_space
[200, 300, 400, 500].each do |status|
@res.status = status
assert_match(/\S\r\n/, @res.status_line)
end
end
end
end
|