2021-09-04 01:27:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
# File Decompression Service allows gzipped files decompression into tmp directory.
|
|
|
|
#
|
|
|
|
# @param tmpdir [String] Temp directory to store downloaded file to. Must be located under `Dir.tmpdir`.
|
|
|
|
# @param filename [String] Name of the file to decompress.
|
2021-09-04 01:27:46 +05:30
|
|
|
module BulkImports
|
|
|
|
class FileDecompressionService
|
|
|
|
include Gitlab::ImportExport::CommandLineUtil
|
|
|
|
|
|
|
|
ServiceError = Class.new(StandardError)
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
def initialize(tmpdir:, filename:)
|
|
|
|
@tmpdir = tmpdir
|
2021-09-04 01:27:46 +05:30
|
|
|
@filename = filename
|
2022-03-02 08:16:31 +05:30
|
|
|
@filepath = File.join(@tmpdir, @filename)
|
2021-09-04 01:27:46 +05:30
|
|
|
@decompressed_filename = File.basename(@filename, '.gz')
|
2022-03-02 08:16:31 +05:30
|
|
|
@decompressed_filepath = File.join(@tmpdir, @decompressed_filename)
|
2021-09-04 01:27:46 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2022-03-02 08:16:31 +05:30
|
|
|
validate_tmpdir
|
|
|
|
validate_filepath
|
2022-07-16 23:28:13 +05:30
|
|
|
validate_decompressed_file_size if Feature.enabled?(:validate_import_decompressed_archive_size)
|
2021-09-04 01:27:46 +05:30
|
|
|
validate_symlink(filepath)
|
|
|
|
|
|
|
|
decompress_file
|
|
|
|
|
|
|
|
validate_symlink(decompressed_filepath)
|
|
|
|
|
|
|
|
filepath
|
|
|
|
rescue StandardError => e
|
|
|
|
File.delete(filepath) if File.exist?(filepath)
|
|
|
|
File.delete(decompressed_filepath) if File.exist?(decompressed_filepath)
|
|
|
|
|
|
|
|
raise e
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
attr_reader :tmpdir, :filename, :filepath, :decompressed_filename, :decompressed_filepath
|
2021-09-04 01:27:46 +05:30
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
def validate_filepath
|
|
|
|
Gitlab::Utils.check_path_traversal!(filepath)
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_tmpdir
|
|
|
|
Gitlab::Utils.check_allowed_absolute_path!(tmpdir, [Dir.tmpdir])
|
2021-09-04 01:27:46 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def validate_decompressed_file_size
|
|
|
|
raise(ServiceError, 'File decompression error') unless size_validator.valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_symlink(filepath)
|
|
|
|
raise(ServiceError, 'Invalid file') if File.lstat(filepath).symlink?
|
|
|
|
end
|
|
|
|
|
|
|
|
def decompress_file
|
2022-03-02 08:16:31 +05:30
|
|
|
gunzip(dir: tmpdir, filename: filename)
|
2021-09-04 01:27:46 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def size_validator
|
|
|
|
@size_validator ||= Gitlab::ImportExport::DecompressedArchiveSizeValidator.new(archive_path: filepath)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|