From 9fffe88dca6048f01455f2c9185cba6358ded6ca Mon Sep 17 00:00:00 2001 From: Aditya <152765789+adityawrk@users.noreply.github.com> Date: Sun, 29 Nov 2026 07:58:31 +0530 Subject: [PATCH 1/2] Test that EOF inside the response headers is an error A response cut before the blank line was accepted, including a header line that never finished. --- test/net/http/test_httpresponse.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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 From 3653fb8fcc20f33d26d077f929011b6281cb478e Mon Sep 17 00:00:00 2001 From: Aditya <152765789+adityawrk@users.noreply.github.com> Date: Sun, 29 Nov 2026 07:58:31 +0530 Subject: [PATCH 2/2] Reject a response that ends inside the headers Header lines were read with ignore_eof, so a connection that closed before the blank line was a finished response. That read now fails, and the failure is Net::HTTPBadResponse. --- lib/net/http/response.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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