From 1b3f35440f99cab854315c610daa71c3778b6e45 Mon Sep 17 00:00:00 2001 From: Orien Madgwick Date: Sat, 28 Nov 2026 00:22:49 +1100 Subject: [PATCH] Fix chunked multipart bodies with empty or partly read files With Transfer-Encoding: chunked, a File or StringIO value in set_form was written as one chunk whose size came from #size. An empty file produced a zero-size chunk, which ends the chunked body, so the server read the remaining form parts as the start of a new request. For a value whose read position was past the start, the chunk-size line counted the whole file but only the unread bytes followed. Separately, any other IO whose read returned an empty string before end of stream wrote a zero-size chunk in the same way. Stream every IO value through flush_buffer instead, so each chunk is sized by the bytes actually read. flush_buffer now writes nothing for an empty buffer, so the only zero-size chunk in a multipart body is the final one. It also no longer clears the strings it writes, so a value whose read returns a frozen string is sent rather than raising FrozenError. --- lib/net/http/generic_request.rb | 24 +++++------ test/net/http/test_http.rb | 75 ++++++++++++++++++++++++++++++--- 2 files changed, 80 insertions(+), 19 deletions(-) diff --git a/lib/net/http/generic_request.rb b/lib/net/http/generic_request.rb index 125c09c3..e8f25424 100644 --- a/lib/net/http/generic_request.rb +++ b/lib/net/http/generic_request.rb @@ -374,18 +374,13 @@ def encode_multipart_form_data(out, params, opt) "name=\"#{key}\"; filename=\"#{filename}\"\r\n" \ "Content-Type: #{type}\r\n\r\n" if !out.respond_to?(:write) || !value.respond_to?(:read) - # if +out+ is not an IO or +value+ is not an IO buf << (value.respond_to?(:read) ? value.read : value) - elsif value.respond_to?(:size) && chunked_p - # if +out+ is an IO and +value+ is a File, use IO.copy_stream - flush_buffer(out, buf, chunked_p) - out << "%x\r\n" % value.size if chunked_p - IO.copy_stream(value, out) - out << "\r\n" if chunked_p else - # +out+ is an IO, and +value+ is not a File but an IO flush_buffer(out, buf, chunked_p) - 1 while flush_buffer(out, value.read(4096), chunked_p) + buf.clear + while (chunk = value.read(4096)) + flush_buffer(out, chunk, chunked_p) + end end else # non-file field: @@ -411,11 +406,12 @@ def quote_string(str, charset) end def flush_buffer(out, buf, chunked_p) - return unless buf - out << "%x\r\n"%buf.bytesize if chunked_p - out << buf - out << "\r\n" if chunked_p - buf.clear + return if buf.nil? || buf.empty? + if chunked_p + out.write("%x\r\n" % buf.bytesize, buf, "\r\n") + else + out << buf + end end ## diff --git a/test/net/http/test_http.rb b/test/net/http/test_http.rb index 0df1c565..25e38e82 100644 --- a/test/net/http/test_http.rb +++ b/test/net/http/test_http.rb @@ -957,16 +957,81 @@ def test_set_form_with_file /^Content-Disposition: form-data; name="file"; filename="#{filename}"\r\n/, header) assert_equal(expected, body) + } + } + end - # TODO: test with chunked - # data.each{|k,v|v.rewind rescue nil} - # req['Transfer-Encoding'] = 'chunked' - # res = http.request req - # assert_equal(expected, res.body) + def test_set_form_multipart_chunked_frames_file_parts_by_bytes_read + require 'tempfile' + Tempfile.create('ruby-test') {|empty| + Tempfile.create('ruby-test') {|file| + large = "x" * 5000 + file << "skipped " << large + file.rewind + file.read(8) + string_io = StringIO.new(+"skipped data") + string_io.read(8) + reads = ["", "data".freeze] + reader = Object.new + reader.define_singleton_method(:read) {|*| reads.shift } + req = Net::HTTP::Post.new('/') + req['Transfer-Encoding'] = 'chunked' + req.set_form([ + ['empty_file', empty, {filename: 'a.txt'}], + ['partly_read_large_file', file, {filename: 'b.txt'}], + ['partly_read_string_io', string_io, {filename: 'c.txt'}], + ['reads_empty_string_first', reader, {filename: 'd.txt'}], + ['after', 'value'], + ], 'multipart/form-data') + out = StringIO.new(+'') + req.send(:encode_multipart_form_data, out, req.instance_variable_get(:@body_data), {boundary: 'BOUNDARY'}) + + expected = <<~"__EOM__".gsub(/\n/, "\r\n") + --BOUNDARY + Content-Disposition: form-data; name="empty_file"; filename="a.txt" + Content-Type: application/octet-stream + + + --BOUNDARY + Content-Disposition: form-data; name="partly_read_large_file"; filename="b.txt" + Content-Type: application/octet-stream + + #{large} + --BOUNDARY + Content-Disposition: form-data; name="partly_read_string_io"; filename="c.txt" + Content-Type: application/octet-stream + + data + --BOUNDARY + Content-Disposition: form-data; name="reads_empty_string_first"; filename="d.txt" + Content-Type: application/octet-stream + + data + --BOUNDARY + Content-Disposition: form-data; name="after" + + value + --BOUNDARY-- + __EOM__ + assert_equal expected, decode_chunked(out.string) } } end + def decode_chunked(body) + chunks = [] + loop do + size_line, body = body.split("\r\n", 2) + size = Integer(size_line, 16) + assert_equal "\r\n", body.byteslice(size, 2) + chunk, body = body.byteslice(0, size), body.byteslice(size + 2..) + break if size == 0 + chunks << chunk + end + assert_equal "", body, 'nothing may follow the last chunk' + chunks.join + end + def test_set_form_multipart_crlf_injection build = ->(data, opt = {}) { req = Net::HTTP::Post.new('/')