Copilot commented on code in PR #3851:
URL: https://github.com/apache/avro/pull/3851#discussion_r3567457102
##########
lang/ruby/lib/avro/data_file.rb:
##########
@@ -339,24 +372,55 @@ def codec_name; 'snappy'; end
def decompress(data)
load_snappy!
- crc32 = data.slice(-4..-1).unpack('N').first
- uncompressed = Snappy.inflate(data.slice(0..-5))
-
- if crc32 == Zlib.crc32(uncompressed)
- uncompressed
- else
- # older versions of avro-ruby didn't write the checksum, so if it
- # doesn't match this must assume that it wasn't there and return
- # the entire payload uncompressed.
- Snappy.inflate(data)
+ # The Snappy block header declares the uncompressed length as a varint;
+ # reject an over-large block before allocating for it.
+ limit = DataFile.max_decompress_length
+ declared = self.class.snappy_declared_length(data)
+ # Fail closed: a block whose length header cannot be parsed is
malformed
+ # and must not be handed to the decompressor with the guard bypassed.
+ if declared.nil?
+ raise DataFileError, "Snappy block has an unparseable length header"
end
+ if declared > limit
+ raise DecompressionSizeError, "Decompressed block size exceeds the
maximum allowed of #{limit} bytes"
+ end
+ # A well-formed block ends with a 4-byte CRC32 trailer. If the buffer
is
+ # too short to contain it, don't slice past the start (data[-4..-1]
would
+ # be nil and raise on unpack); treat it as a legacy no-checksum block.
+ if data.bytesize >= 4
+ crc32 = data.slice(-4..-1).unpack('N').first
+ uncompressed = Snappy.inflate(data.slice(0..-5))
Review Comment:
The CRC32-path guard allows `data.bytesize == 4`, but then
`data.slice(0..-5)` returns nil (end index is out of range), which can raise a
TypeError/NoMethodError rather than a controlled Avro error. Require at least 5
bytes before attempting to strip the 4-byte trailer, and use `byteslice(0,
data.bytesize - 4)` to avoid fragile negative-range slicing.
--
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]