Module: Aspera::Inflate

Defined in:
lib/aspera/inflate.rb

Overview

Decompression of zlib data that comes from outside (e.g. a code pasted by a user, a token given by a client or a server). A small input can inflate to gigabytes (decompression bomb): the size of the result is checked while decompressing, not after.

Class Method Summary collapse

Class Method Details

.limited(data, max_size:, what:) ⇒ String

Returns Inflated data.

Parameters:

  • data (String) —

    Zlib data

  • max_size (Integer) —

    Maximum size of the result, in bytes

  • what (String) —

    What is decompressed, for error messages, e.g. Code

Returns:

Raises:

  • (ParameterError) —

    If the result is larger than max_size, or the data is incomplete

  • (Zlib::Error) —

    If the data is not zlib data



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/aspera/inflate.rb', line 26

def limited(data, max_size:, what:)
  output = +''
  inflater = Zlib::Inflate.new
  begin
    0.step(data.bytesize - 1, INPUT_SLICE) do |offset|
      output << inflater.inflate(data.byteslice(offset, INPUT_SLICE))
      raise ParameterError, "#{what} is too large when decompressed: more than #{max_size} bytes" if output.bytesize > max_size
      break if inflater.finished?
    end
    raise ParameterError, "#{what} is incomplete" unless inflater.finished?
  ensure
    inflater.close
  end
  output
end