2021-05-24 01:24:22 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-01-21 05:31:06 +08:00
|
|
|
require 'test_helper'
|
|
|
|
|
2014-07-15 23:23:48 +08:00
|
|
|
class ZipFileExtractTest < MiniTest::Test
|
2014-01-21 05:31:06 +08:00
|
|
|
include CommonZipFileFixture
|
2015-03-21 16:27:44 +08:00
|
|
|
EXTRACTED_FILENAME = 'test/data/generated/extEntry'
|
2014-01-21 05:31:06 +08:00
|
|
|
ENTRY_TO_EXTRACT, *REMAINING_ENTRIES = TEST_ZIP.entry_names.reverse
|
|
|
|
|
|
|
|
def setup
|
|
|
|
super
|
2014-02-07 07:00:38 +08:00
|
|
|
::File.delete(EXTRACTED_FILENAME) if ::File.exist?(EXTRACTED_FILENAME)
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
2019-09-13 05:01:38 +08:00
|
|
|
def teardown
|
|
|
|
::Zip.reset!
|
|
|
|
end
|
|
|
|
|
2014-01-21 05:31:06 +08:00
|
|
|
def test_extract
|
2015-03-21 16:10:37 +08:00
|
|
|
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
2014-01-21 05:31:06 +08:00
|
|
|
zf.extract(ENTRY_TO_EXTRACT, EXTRACTED_FILENAME)
|
|
|
|
|
2014-02-07 07:00:38 +08:00
|
|
|
assert(File.exist?(EXTRACTED_FILENAME))
|
2015-03-21 16:16:06 +08:00
|
|
|
AssertEntry.assert_contents(EXTRACTED_FILENAME,
|
2019-09-27 05:38:28 +08:00
|
|
|
zf.get_input_stream(ENTRY_TO_EXTRACT, &:read))
|
2014-01-21 05:31:06 +08:00
|
|
|
|
|
|
|
::File.unlink(EXTRACTED_FILENAME)
|
|
|
|
|
|
|
|
entry = zf.get_entry(ENTRY_TO_EXTRACT)
|
|
|
|
entry.extract(EXTRACTED_FILENAME)
|
|
|
|
|
2014-02-07 07:00:38 +08:00
|
|
|
assert(File.exist?(EXTRACTED_FILENAME))
|
2015-03-21 16:16:06 +08:00
|
|
|
AssertEntry.assert_contents(EXTRACTED_FILENAME,
|
2019-09-27 05:38:28 +08:00
|
|
|
entry.get_input_stream(&:read))
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
2015-03-25 00:02:54 +08:00
|
|
|
def test_extract_exists
|
2020-02-19 18:52:49 +08:00
|
|
|
text = 'written text'
|
|
|
|
::File.open(EXTRACTED_FILENAME, 'w') { |f| f.write(text) }
|
2014-01-21 05:31:06 +08:00
|
|
|
|
2015-03-21 16:10:37 +08:00
|
|
|
assert_raises(::Zip::DestinationFileExistsError) do
|
|
|
|
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
2014-01-21 05:31:06 +08:00
|
|
|
zf.extract(zf.entries.first, EXTRACTED_FILENAME)
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
|
|
|
end
|
2015-03-21 16:27:44 +08:00
|
|
|
File.open(EXTRACTED_FILENAME, 'r') do |f|
|
2020-02-19 18:52:49 +08:00
|
|
|
assert_equal(text, f.read)
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
2015-03-25 00:02:54 +08:00
|
|
|
def test_extract_exists_overwrite
|
2020-02-19 18:52:49 +08:00
|
|
|
text = 'written text'
|
|
|
|
::File.open(EXTRACTED_FILENAME, 'w') { |f| f.write(text) }
|
2014-01-21 05:31:06 +08:00
|
|
|
|
2020-02-19 18:52:49 +08:00
|
|
|
called_correctly = false
|
2015-03-21 16:10:37 +08:00
|
|
|
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
2020-02-18 06:35:08 +08:00
|
|
|
zf.extract(zf.entries.first, EXTRACTED_FILENAME) do |entry, extract_loc|
|
2020-02-19 18:52:49 +08:00
|
|
|
called_correctly = zf.entries.first == entry &&
|
|
|
|
extract_loc == EXTRACTED_FILENAME
|
2014-01-21 05:31:06 +08:00
|
|
|
true
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
|
|
|
end
|
2014-01-21 05:31:06 +08:00
|
|
|
|
2020-02-19 18:52:49 +08:00
|
|
|
assert(called_correctly)
|
2015-03-21 16:27:44 +08:00
|
|
|
::File.open(EXTRACTED_FILENAME, 'r') do |f|
|
2020-02-19 18:52:49 +08:00
|
|
|
assert(text != f.read)
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
2015-03-25 00:02:54 +08:00
|
|
|
def test_extract_non_entry
|
2014-01-21 05:31:06 +08:00
|
|
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
2015-03-21 16:27:44 +08:00
|
|
|
assert_raises(Errno::ENOENT) { zf.extract('nonExistingEntry', 'nonExistingEntry') }
|
2014-01-21 05:31:06 +08:00
|
|
|
ensure
|
|
|
|
zf.close if zf
|
|
|
|
end
|
|
|
|
|
2015-03-25 00:02:54 +08:00
|
|
|
def test_extract_non_entry_2
|
2020-02-19 18:52:49 +08:00
|
|
|
out_file = 'outfile'
|
2015-03-21 16:10:37 +08:00
|
|
|
assert_raises(Errno::ENOENT) do
|
2014-01-21 05:31:06 +08:00
|
|
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
2020-02-19 18:52:49 +08:00
|
|
|
non_entry = 'hotdog-diddelidoo'
|
|
|
|
assert(!zf.entries.include?(non_entry))
|
|
|
|
zf.extract(non_entry, out_file)
|
2014-01-21 05:31:06 +08:00
|
|
|
zf.close
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
2020-02-19 18:52:49 +08:00
|
|
|
assert(!File.exist?(out_file))
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
2019-09-13 05:01:38 +08:00
|
|
|
|
|
|
|
def test_extract_incorrect_size
|
|
|
|
# The uncompressed size fields in the zip file cannot be trusted. This makes
|
|
|
|
# it harder for callers to validate the sizes of the files they are
|
|
|
|
# extracting, which can lead to denial of service. See also
|
|
|
|
# https://en.wikipedia.org/wiki/Zip_bomb
|
|
|
|
Dir.mktmpdir do |tmp|
|
|
|
|
real_zip = File.join(tmp, 'real.zip')
|
|
|
|
fake_zip = File.join(tmp, 'fake.zip')
|
|
|
|
file_name = 'a'
|
|
|
|
true_size = 500_000
|
|
|
|
fake_size = 1
|
|
|
|
|
|
|
|
::Zip::File.open(real_zip, ::Zip::File::CREATE) do |zf|
|
|
|
|
zf.get_output_stream(file_name) do |os|
|
|
|
|
os.write 'a' * true_size
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
compressed_size = nil
|
|
|
|
::Zip::File.open(real_zip) do |zf|
|
|
|
|
a_entry = zf.find_entry(file_name)
|
|
|
|
compressed_size = a_entry.compressed_size
|
|
|
|
assert_equal true_size, a_entry.size
|
|
|
|
end
|
|
|
|
|
2020-03-26 01:45:09 +08:00
|
|
|
true_size_bytes = [compressed_size, true_size, file_name.size].pack('VVv')
|
|
|
|
fake_size_bytes = [compressed_size, fake_size, file_name.size].pack('VVv')
|
2019-09-13 05:01:38 +08:00
|
|
|
|
|
|
|
data = File.binread(real_zip)
|
|
|
|
assert data.include?(true_size_bytes)
|
|
|
|
data.gsub! true_size_bytes, fake_size_bytes
|
|
|
|
|
|
|
|
File.open(fake_zip, 'wb') do |file|
|
|
|
|
file.write data
|
|
|
|
end
|
|
|
|
|
|
|
|
Dir.chdir tmp do
|
|
|
|
::Zip::File.open(fake_zip) do |zf|
|
|
|
|
a_entry = zf.find_entry(file_name)
|
|
|
|
assert_equal fake_size, a_entry.size
|
|
|
|
|
|
|
|
::Zip.validate_entry_sizes = false
|
2019-10-13 22:40:49 +08:00
|
|
|
assert_output('', /.+\'a\'.+1B.+/) do
|
|
|
|
a_entry.extract
|
|
|
|
end
|
2019-09-13 05:01:38 +08:00
|
|
|
assert_equal true_size, File.size(file_name)
|
|
|
|
FileUtils.rm file_name
|
|
|
|
|
|
|
|
::Zip.validate_entry_sizes = true
|
|
|
|
error = assert_raises ::Zip::EntrySizeError do
|
|
|
|
a_entry.extract
|
|
|
|
end
|
|
|
|
assert_equal \
|
2019-10-12 14:26:15 +08:00
|
|
|
"entry 'a' should be 1B, but is larger when inflated.",
|
2019-09-13 05:01:38 +08:00
|
|
|
error.message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|