From b66abb69ef2795793ab0445b37ea32e20c92b941 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Thu, 12 Nov 2026 08:59:57 +0900 Subject: [PATCH] Only accept a chunk size at the start of the chunk-size line MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit read_chunked took the first run of hex digits anywhere in the line, so lines such as "0x1f" or ";ext=1f" were read as different sizes. Require the size at the start of the line, followed only by optional whitespace and a chunk extension, as RFC 9112 does. Co-authored-by: Clément Joubert (https://hackerone.com/czjfr) Co-Authored-By: Claude Opus 5.5 --- lib/net/http/response.rb | 2 +- test/net/http/test_httpresponse.rb | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/net/http/response.rb b/lib/net/http/response.rb index 137744ad..b1670e08 100644 --- a/lib/net/http/response.rb +++ b/lib/net/http/response.rb @@ -649,7 +649,7 @@ def read_chunked(dest, chunk_data_io) # :nodoc: total = 0 while true line = self.class.read_line(@socket, MAX_RESPONSE_HEADER_LENGTH).chop - hexlen = line.slice(/[0-9a-fA-F]+/) or + hexlen = line[/\A\h+(?=[ \t]*(?:;|\r?\z))/] or raise Net::HTTPBadResponse, "wrong chunk size line: #{line}" len = hexlen.hex break if len == 0 diff --git a/test/net/http/test_httpresponse.rb b/test/net/http/test_httpresponse.rb index e902b4f3..7c213f30 100644 --- a/test/net/http/test_httpresponse.rb +++ b/test/net/http/test_httpresponse.rb @@ -147,6 +147,25 @@ def test_read_body assert_equal 'hello', body end + def test_chunk_size_line + ["1f", "1F", "1f;ext=1", "1f ;ext", "1f\t; ext=\"a\""].each do |size| + io = dummy_io("HTTP/1.1 200 OK\nConnection: close\nTransfer-Encoding: chunked\n\n" \ + "#{size}\n#{"a" * 31}\n0\n\n") + res = Net::HTTPResponse.read_new(io) + res.reading_body(io, true) {} + assert_equal("a" * 31, res.body, size) + end + + ["0x1f", "1_0", ";ext=1f", "zz1f", "-5", "+5", "g5", " 5", "5x"].each do |size| + io = dummy_io("HTTP/1.1 200 OK\nConnection: close\nTransfer-Encoding: chunked\n\n" \ + "#{size}\n#{"a" * 31}\n0\n\n") + res = Net::HTTPResponse.read_new(io) + assert_raise(Net::HTTPBadResponse, size) do + res.reading_body(io, true) {} + end + end + end + def test_read_body_chunked_is_final_transfer_coding io = dummy_io(<