From eb2b13b40c286b5fc5ba1c728d9183a58aa70c0e Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Thu, 29 Oct 2026 16:31:18 +0900 Subject: [PATCH 1/2] Do not reuse the connection unless the request body was fully sent When the server answers a request that carries Expect: 100-continue with a final status before the body has been sent, or when writing the body fails partway, the announced body never reaches the server in full. Close the connection after reading the response instead of keeping it alive for the next request. This follows the behavior of Java's java.net.http.HttpClient, which closes the connection after any response other than 100 to such a request, and of libcurl, which closes it when the body has not been sent completely. Reported-by: Claude (Anthropic) and Stephan Lipp (Ada Logics) Co-Authored-By: Claude Mythos 5.1 --- lib/net/http.rb | 3 +++ test/net/http/test_http.rb | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/lib/net/http.rb b/lib/net/http.rb index 150e2277..38752b33 100644 --- a/lib/net/http.rb +++ b/lib/net/http.rb @@ -2516,10 +2516,12 @@ def send_entity(path, data, initheader, dest, type, &block) def transport_request(req) count = 0 begin + body_sent = false begin_transport req res = catch(:response) { begin req.exec @socket, @curr_http_version, edit_path(req.path) + body_sent = true rescue Errno::EPIPE # Failure when writing full request, but we can probably # still read the received response. @@ -2566,6 +2568,7 @@ def transport_request(req) raise end + @socket.close unless body_sent end_transport req, res res rescue => exception diff --git a/test/net/http/test_http.rb b/test/net/http/test_http.rb index 8a306920..d7adf39c 100644 --- a/test/net/http/test_http.rb +++ b/test/net/http/test_http.rb @@ -1142,6 +1142,43 @@ def test_expect_continue_error_while_waiting assert_match(/Expect: 100-continue/, @debug.string) assert_not_match(/HTTP\/1.1 100 continue/, @debug.string) end + + def test_expect_continue_early_response + server = TCPServer.new(config('host'), 0) + th = Thread.new do + 2.times do + sock = server.accept + begin + while (reqline = sock.gets) + clen = 0 + while (line = sock.gets) && line != "\r\n" + clen = line.split(':', 2)[1].to_i if line.start_with?('Content-Length') + end + if reqline.start_with?('PUT') + sock.write "HTTP/1.1 429 Too Many Requests\r\nContent-Length: 0\r\n\r\n" + sock.read(clen) + else + sock.write "HTTP/1.1 200 OK\r\nContent-Length: #{reqline.bytesize}\r\n\r\n#{reqline}" + end + end + ensure + sock.close + end + end + end + http = Net::HTTP.new(config('host'), server.addr[1]) + http.continue_timeout = 1 + http.start { + res = http.request_put('/first', 'data!', 'expect' => '100-continue') + assert_equal('429', res.code) + res = http.get('/second') + assert_equal("GET /second HTTP/1.1\r\n", res.body) + } + ensure + th&.kill + th&.join + server&.close + end end class TestNetHTTPSwitchingProtocols < Test::Unit::TestCase From 3ba12ae558807af4f3b126d87ac57961fe7b0091 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Thu, 12 Nov 2026 15:29:25 +0900 Subject: [PATCH 2/2] Wait for 100 Continue past other informational responses wait_for_continue treated any response other than 100 Continue as the final response, so an informational response such as 103 Early Hints before 100 Continue made the request return 103 without sending its body. Skip informational responses other than 100 and 101 until 100 Continue, a final response, or the continue timeout. Reported-by: Ashton D (https://hackerone.com/ashdfrkl) Co-Authored-By: Claude Opus 5.5 --- lib/net/http/generic_request.rb | 20 ++++++++++++++---- test/net/http/test_http.rb | 37 +++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/lib/net/http/generic_request.rb b/lib/net/http/generic_request.rb index 96396b0a..d62d8252 100644 --- a/lib/net/http/generic_request.rb +++ b/lib/net/http/generic_request.rb @@ -411,12 +411,24 @@ def flush_buffer(out, buf, chunked_p) def wait_for_continue(sock, ver) if ver >= '1.1' and @header['expect'] and @header['expect'].include?('100-continue') - if sock.io.to_io.wait_readable(sock.continue_timeout) + timeout = sock.continue_timeout + deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout if timeout + informational_count = 0 + while sock.io.to_io.wait_readable(timeout) res = Net::HTTPResponse.read_new(sock) - unless res.kind_of?(Net::HTTPContinue) - res.decode_content = @decode_content - throw :response, res + break if res.kind_of?(Net::HTTPContinue) + if res.kind_of?(Net::HTTPInformation) && !res.kind_of?(Net::HTTPSwitchProtocol) + informational_count += 1 + raise Net::HTTPBadResponse, 'too many informational responses' if + informational_count > Net::HTTPResponse::MAX_INFORMATIONAL_RESPONSES + if deadline + timeout = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC) + break if timeout <= 0 + end + next end + res.decode_content = @decode_content + throw :response, res end end end diff --git a/test/net/http/test_http.rb b/test/net/http/test_http.rb index d7adf39c..97e2f57d 100644 --- a/test/net/http/test_http.rb +++ b/test/net/http/test_http.rb @@ -1143,6 +1143,43 @@ def test_expect_continue_error_while_waiting assert_not_match(/HTTP\/1.1 100 continue/, @debug.string) end + def test_expect_continue_after_early_hints + server = TCPServer.new(config('host'), 0) + th = Thread.new do + sock = server.accept + begin + while (reqline = sock.gets) + clen = 0 + while (line = sock.gets) && line != "\r\n" + clen = line.split(':', 2)[1].to_i if line.start_with?('Content-Length') + end + if reqline.start_with?('PUT') + sock.write "HTTP/1.1 103 Early Hints\r\nLink: \r\n\r\nHTTP/1.1 100 Continue\r\n\r\n" + body = sock.read(clen) + sock.write "HTTP/1.1 201 Created\r\nContent-Length: #{body.bytesize}\r\n\r\n#{body}" + else + sock.write "HTTP/1.1 200 OK\r\nContent-Length: #{reqline.bytesize}\r\n\r\n#{reqline}" + end + end + ensure + sock.close + end + end + http = Net::HTTP.new(config('host'), server.addr[1]) + http.continue_timeout = 1 + http.start { + res = http.request_put('/first', 'data!', 'expect' => '100-continue') + assert_equal('201', res.code) + assert_equal('data!', res.body) + res = http.get('/second') + assert_equal("GET /second HTTP/1.1\r\n", res.body) + } + ensure + th&.kill + th&.join + server&.close + end + def test_expect_continue_early_response server = TCPServer.new(config('host'), 0) th = Thread.new do