diff --git a/lib/net/http/response.rb b/lib/net/http/response.rb index 8e05ef56..cf58e0eb 100644 --- a/lib/net/http/response.rb +++ b/lib/net/http/response.rb @@ -185,7 +185,9 @@ def each_response_header(sock) key = value = nil remaining = MAX_RESPONSE_HEADER_LENGTH while true - line = read_line(sock, MAX_RESPONSE_HEADER_LENGTH, true) + # A missing blank line is a truncated message, not the end of the + # headers (net-http#377). ignore_eof stays false. + line = read_line(sock, MAX_RESPONSE_HEADER_LENGTH) remaining -= line.bytesize raise Net::HTTPBadResponse, 'response header too large' if remaining < 0 line = line.chomp @@ -203,6 +205,8 @@ def each_response_header(sock) end end yield key, value if key + rescue EOFError + raise Net::HTTPBadResponse, 'end of file reached before the end of the headers' end end diff --git a/test/net/http/test_httpresponse.rb b/test/net/http/test_httpresponse.rb index 7c213f30..d2ada33e 100644 --- a/test/net/http/test_httpresponse.rb +++ b/test/net/http/test_httpresponse.rb @@ -31,6 +31,22 @@ def test_response_header_too_large assert_equal 'response header too large', e.message end + def test_eof_before_the_end_of_the_headers_is_a_protocol_error + io = dummy_io("HTTP/1.1 200 OK\nContent-Type: application/json\n") + e = assert_raise(Net::HTTPBadResponse) do + Net::HTTPResponse.read_new(io) + end + assert_equal 'end of file reached before the end of the headers', e.message + end + + def test_eof_in_the_middle_of_a_header_line_is_a_protocol_error + io = dummy_io("HTTP/1.1 200 OK\nX-Classification: malic") + e = assert_raise(Net::HTTPBadResponse) do + Net::HTTPResponse.read_new(io) + end + assert_equal 'end of file reached before the end of the headers', e.message + end + def test_response_header_within_limit big_value = 'a' * (Net::HTTPHeader::MAX_FIELD_LENGTH - 100) count = (Net::HTTPResponse::MAX_RESPONSE_HEADER_LENGTH / big_value.bytesize) - 1