Copilot commented on code in PR #3701:
URL: https://github.com/apache/thrift/pull/3701#discussion_r3717275534


##########
lib/rb/lib/thrift/transport/header_transport.rb:
##########
@@ -292,9 +296,16 @@ def read_frame(req_sz)
       if frame_size > @max_frame_size
         raise TransportException.new(TransportException::UNKNOWN, "Frame size 
#{frame_size} exceeds maximum #{@max_frame_size}")
       end
+      if frame_size < 4
+        raise TransportException.new(TransportException::UNKNOWN, "Frame size 
#{frame_size} is too small")
+      end
 
       # Read the complete frame
-      frame_data = @transport.read_all(frame_size)
+      begin
+        frame_data = @transport.read_all(frame_size)
+      rescue EOFError
+        raise TransportException.new(TransportException::END_OF_FILE, 
"Unexpected EOF reading frame")
+      end

Review Comment:
   When converting EOFError into a typed TransportException, consider chaining 
the original exception as the cause for better diagnostics (consistent with 
other transports).



##########
lib/rb/lib/thrift/transport/header_transport.rb:
##########
@@ -268,7 +268,11 @@ def read_frame(req_sz)
       @read_headers = {}
 
       # Read first 4 bytes - could be frame length or protocol magic
-      first_word = @transport.read_all(4)
+      begin
+        first_word = @transport.read_all(4)
+      rescue EOFError
+        raise TransportException.new(TransportException::END_OF_FILE, 
"Unexpected EOF reading frame size")
+      end

Review Comment:
   When rethrowing EOF as a TransportException, preserve the original EOFError 
as the exception cause (the codebase already uses `cause:` elsewhere). This 
keeps debugging context without changing behavior.



##########
lib/rb/spec/header_transport_spec.rb:
##########
@@ -393,6 +393,54 @@ def framed(message)
     end
 
     describe "header parsing protections" do
+      it "rejects frame sizes shorter than a protocol signature" do
+        (0..3).each do |frame_size|
+          frame = [frame_size].pack('N') + ("\x00" * frame_size)
+          read_trans = 
Thrift::HeaderTransport.new(Thrift::MemoryBufferTransport.new(frame))
+
+          expect { read_trans.read(1) }.to raise_error(
+            Thrift::TransportException,
+            "Frame size #{frame_size} is too small"
+          ) do |error|
+            expect(error.type).to eq(Thrift::TransportException::UNKNOWN)
+          end
+        end
+      end
+
+      it "reports EOF when the frame size is fragmented" do
+        (0..3).each do |available_size|
+          read_trans = Thrift::HeaderTransport.new(
+            Thrift::MemoryBufferTransport.new("\x00" * available_size)
+          )
+
+          expect { read_trans.read(1) }.to raise_error(
+            Thrift::TransportException,
+            "Unexpected EOF reading frame size"
+          ) do |error|
+            expect(error.type).to eq(Thrift::TransportException::END_OF_FILE)
+          end
+        end
+      end
+
+      it "reports EOF when the declared frame is fragmented" do
+        frame = [4].pack('N') + "\x80\x01\x00"
+        read_trans = 
Thrift::HeaderTransport.new(Thrift::MemoryBufferTransport.new(frame))

Review Comment:
   The string literal containing non-ASCII bytes should be forced to binary 
encoding to avoid Encoding::CompatibilityError when concatenated with the 
ASCII-8BIT result of pack('N') (similar to other specs in this file that use 
".b").



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to