debian-mirror-gitlab/spec/lib/gitlab/import_export/file_importer_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

237 lines
7.3 KiB
Ruby
Raw Normal View History

2020-01-01 13:55:28 +05:30
# frozen_string_literal: true
2016-11-03 12:29:30 +05:30
require 'spec_helper'
2023-09-09 17:08:58 +05:30
RSpec.describe Gitlab::ImportExport::FileImporter, feature_category: :importers do
2018-12-21 20:21:59 +05:30
include ExportFileHelper
2018-03-27 19:54:05 +05:30
let(:shared) { Gitlab::ImportExport::Shared.new(nil) }
2018-11-20 20:47:30 +05:30
let(:storage_path) { "#{Dir.tmpdir}/file_importer_spec" }
2016-11-03 12:29:30 +05:30
let(:valid_file) { "#{shared.export_path}/valid.json" }
let(:symlink_file) { "#{shared.export_path}/invalid.json" }
2017-08-17 22:00:37 +05:30
let(:hidden_symlink_file) { "#{shared.export_path}/.hidden" }
2016-11-03 12:29:30 +05:30
let(:subfolder_symlink_file) { "#{shared.export_path}/subfolder/invalid.json" }
2018-08-20 21:37:37 +05:30
let(:evil_symlink_file) { "#{shared.export_path}/.\nevil" }
2018-12-21 20:21:59 +05:30
let(:custom_mode_symlink_file) { "#{shared.export_path}/symlink.mode" }
2016-11-03 12:29:30 +05:30
before do
stub_const('Gitlab::ImportExport::FileImporter::MAX_RETRIES', 0)
2018-11-20 20:47:30 +05:30
stub_uploads_object_storage(FileUploader)
2020-01-01 13:55:28 +05:30
allow_next_instance_of(Gitlab::ImportExport) do |instance|
allow(instance).to receive(:storage_path).and_return(storage_path)
end
allow_next_instance_of(Gitlab::ImportExport::CommandLineUtil) do |instance|
allow(instance).to receive(:untar_zxf).and_return(true)
end
allow_next_instance_of(Gitlab::ImportExport::Shared) do |instance|
allow(instance).to receive(:relative_archive_path).and_return('test')
end
2018-03-17 18:26:18 +05:30
allow(SecureRandom).to receive(:hex).and_return('abcd')
2016-11-03 12:29:30 +05:30
setup_files
end
after do
2018-11-20 20:47:30 +05:30
FileUtils.rm_rf(storage_path)
2016-11-03 12:29:30 +05:30
end
2018-03-17 18:26:18 +05:30
context 'normal run' do
before do
2020-01-01 13:55:28 +05:30
described_class.import(importable: build(:project), archive_file: '', shared: shared)
2018-03-17 18:26:18 +05:30
end
2016-11-03 12:29:30 +05:30
2018-03-17 18:26:18 +05:30
it 'removes symlinks in root folder' do
expect(File.exist?(symlink_file)).to be false
end
it 'removes hidden symlinks in root folder' do
expect(File.exist?(hidden_symlink_file)).to be false
end
2018-08-20 21:37:37 +05:30
it 'removes evil symlinks in root folder' do
expect(File.exist?(evil_symlink_file)).to be false
end
2018-03-17 18:26:18 +05:30
it 'removes symlinks in subfolders' do
expect(File.exist?(subfolder_symlink_file)).to be false
end
2017-08-17 22:00:37 +05:30
2018-12-21 20:21:59 +05:30
it 'removes symlinks without any file permissions' do
expect(File.exist?(custom_mode_symlink_file)).to be false
end
2018-03-17 18:26:18 +05:30
it 'does not remove a valid file' do
expect(File.exist?(valid_file)).to be true
end
2018-12-21 20:21:59 +05:30
it 'does not change a valid file permissions' do
expect(file_permissions(valid_file)).not_to eq(0000)
end
2018-03-17 18:26:18 +05:30
it 'creates the file in the right subfolder' do
expect(shared.export_path).to include('test/abcd')
end
2021-06-08 01:23:25 +05:30
2022-05-07 20:08:51 +05:30
context 'when the import file is not remote' do
include AfterNextHelpers
it 'downloads the file from a remote object storage' do
import_export_upload = build(:import_export_upload)
project = build( :project, import_export_upload: import_export_upload)
expect_next(described_class)
.to receive(:download_or_copy_upload)
.with(
import_export_upload.import_file,
kind_of(String),
size_limit: ::Import::GitlabProjects::RemoteFileValidator::FILE_SIZE_LIMIT
)
described_class.import(importable: project, archive_file: nil, shared: shared)
end
end
2021-06-08 01:23:25 +05:30
context 'when the import file is remote' do
include AfterNextHelpers
it 'downloads the file from a remote object storage' do
file_url = 'https://remote.url/file'
import_export_upload = build(:import_export_upload, remote_import_url: file_url)
project = build( :project, import_export_upload: import_export_upload)
expect_next(described_class)
.to receive(:download)
2022-05-07 20:08:51 +05:30
.with(
file_url,
kind_of(String),
size_limit: ::Import::GitlabProjects::RemoteFileValidator::FILE_SIZE_LIMIT
)
2021-06-08 01:23:25 +05:30
described_class.import(importable: project, archive_file: nil, shared: shared)
end
end
2016-11-03 12:29:30 +05:30
end
2018-03-17 18:26:18 +05:30
context 'error' do
2023-09-09 17:08:58 +05:30
subject(:import) { described_class.import(importable: build(:project), archive_file: '', shared: shared) }
2018-03-17 18:26:18 +05:30
before do
2020-01-01 13:55:28 +05:30
allow_next_instance_of(described_class) do |instance|
2023-09-09 17:08:58 +05:30
allow(instance).to receive(:wait_for_archived_file).and_raise(StandardError, 'foo')
2020-01-01 13:55:28 +05:30
end
2018-03-17 18:26:18 +05:30
end
it 'removes symlinks in root folder' do
2023-09-09 17:08:58 +05:30
import
2018-03-17 18:26:18 +05:30
expect(File.exist?(symlink_file)).to be false
end
it 'removes hidden symlinks in root folder' do
2023-09-09 17:08:58 +05:30
import
2018-03-17 18:26:18 +05:30
expect(File.exist?(hidden_symlink_file)).to be false
end
it 'removes symlinks in subfolders' do
2023-09-09 17:08:58 +05:30
import
2018-03-17 18:26:18 +05:30
expect(File.exist?(subfolder_symlink_file)).to be false
end
it 'does not remove a valid file' do
2023-09-09 17:08:58 +05:30
import
2018-03-17 18:26:18 +05:30
expect(File.exist?(valid_file)).to be true
end
2023-09-09 17:08:58 +05:30
it 'returns false and sets an error on shared' do
result = import
expect(result).to eq(false)
expect(shared.errors.join).to eq('foo')
end
context 'when files in the archive share hard links' do
let(:hard_link_file) { "#{shared.export_path}/hard_link_file.txt" }
before do
FileUtils.link(valid_file, hard_link_file)
end
it 'returns false and sets an error on shared' do
result = import
expect(result).to eq(false)
expect(shared.errors.join).to eq('File shares hard link')
end
it 'removes all files in export path' do
expect(Dir).to exist(shared.export_path)
expect(File).to exist(symlink_file)
expect(File).to exist(hard_link_file)
expect(File).to exist(valid_file)
import
expect(File).not_to exist(symlink_file)
expect(File).not_to exist(hard_link_file)
expect(File).not_to exist(valid_file)
expect(Dir).not_to exist(shared.export_path)
end
end
2016-11-03 12:29:30 +05:30
end
2020-08-18 19:51:02 +05:30
context 'when file exceeds acceptable decompressed size' do
let(:project) { create(:project) }
let(:shared) { Gitlab::ImportExport::Shared.new(project) }
let(:filepath) { File.join(Dir.tmpdir, 'file_importer_spec.gz') }
subject { described_class.new(importable: project, archive_file: filepath, shared: shared) }
before do
Zlib::GzipWriter.open(filepath) do |gz|
gz.write('Hello World!')
end
end
context 'when validate_import_decompressed_archive_size feature flag is enabled' do
before do
stub_feature_flags(validate_import_decompressed_archive_size: true)
allow(Gitlab::ImportExport::DecompressedArchiveSizeValidator).to receive(:max_bytes).and_return(1)
end
2023-09-09 17:08:58 +05:30
it 'returns false and sets an error on shared' do
result = subject.import
expect(result).to eq(false)
2020-08-18 19:51:02 +05:30
expect(shared.errors.join).to eq('Decompressed archive size validation failed.')
end
end
context 'when validate_import_decompressed_archive_size feature flag is disabled' do
before do
stub_feature_flags(validate_import_decompressed_archive_size: false)
end
it 'skips validation' do
2022-10-11 01:57:18 +05:30
expect(subject).not_to receive(:validate_decompressed_archive_size)
2020-08-18 19:51:02 +05:30
subject.import
end
end
end
2016-11-03 12:29:30 +05:30
def setup_files
FileUtils.mkdir_p("#{shared.export_path}/subfolder/")
FileUtils.touch(valid_file)
FileUtils.ln_s(valid_file, symlink_file)
FileUtils.ln_s(valid_file, subfolder_symlink_file)
2018-08-20 21:37:37 +05:30
FileUtils.ln_s(valid_file, hidden_symlink_file)
FileUtils.ln_s(valid_file, evil_symlink_file)
2018-12-21 20:21:59 +05:30
FileUtils.ln_s(valid_file, custom_mode_symlink_file)
FileUtils.chmod_R(0000, custom_mode_symlink_file)
2016-11-03 12:29:30 +05:30
end
end