iemejia commented on code in PR #3851:
URL: https://github.com/apache/avro/pull/3851#discussion_r3567491219
##########
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:
Already addressed in commit 4761267655 — the CRC path now requires
`data.bytesize > 4` and uses `byteslice(0, data.bytesize - 4)` for the body
(and `byteslice(-4, 4)` for the trailer), so a 4-byte buffer no longer reaches
Snappy.inflate with a nil slice.
--
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]