Commit ed15b506 authored by GitLab Bot's avatar GitLab Bot

Add latest changes from gitlab-org/security/gitlab@13-0-stable-ee

parent 900b14e4
---
title: Fix file enuming using Group Import
merge_request:
author:
type: security
...@@ -4,6 +4,8 @@ module API ...@@ -4,6 +4,8 @@ module API
class GroupImport < Grape::API class GroupImport < Grape::API
MAXIMUM_FILE_SIZE = 50.megabytes.freeze MAXIMUM_FILE_SIZE = 50.megabytes.freeze
helpers Helpers::FileUploadHelpers
helpers do helpers do
def parent_group def parent_group
find_group!(params[:parent_id]) if params[:parent_id].present? find_group!(params[:parent_id]) if params[:parent_id].present?
...@@ -48,29 +50,20 @@ module API ...@@ -48,29 +50,20 @@ module API
params do params do
requires :path, type: String, desc: 'Group path' requires :path, type: String, desc: 'Group path'
requires :name, type: String, desc: 'Group name' requires :name, type: String, desc: 'Group name'
requires :file, type: ::API::Validations::Types::WorkhorseFile, desc: 'The group export file to be imported'
optional :parent_id, type: Integer, desc: "The ID of the parent group that the group will be imported into. Defaults to the current user's namespace." optional :parent_id, type: Integer, desc: "The ID of the parent group that the group will be imported into. Defaults to the current user's namespace."
optional 'file.path', type: String, desc: 'Path to locally stored body (generated by Workhorse)'
optional 'file.name', type: String, desc: 'Real filename as send in Content-Disposition (generated by Workhorse)'
optional 'file.type', type: String, desc: 'Real content type as send in Content-Type (generated by Workhorse)'
optional 'file.size', type: Integer, desc: 'Real size of file (generated by Workhorse)'
optional 'file.md5', type: String, desc: 'MD5 checksum of the file (generated by Workhorse)'
optional 'file.sha1', type: String, desc: 'SHA1 checksum of the file (generated by Workhorse)'
optional 'file.sha256', type: String, desc: 'SHA256 checksum of the file (generated by Workhorse)'
end end
post 'import' do post 'import' do
authorize_create_group! authorize_create_group!
require_gitlab_workhorse! require_gitlab_workhorse!
validate_file!
uploaded_file = UploadedFile.from_params(params, :file, ImportExportUploader.workhorse_local_upload_path)
bad_request!('Unable to process group import file') unless uploaded_file
group_params = { group_params = {
path: params[:path], path: params[:path],
name: params[:name], name: params[:name],
parent_id: params[:parent_id], parent_id: params[:parent_id],
visibility_level: closest_allowed_visibility_level, visibility_level: closest_allowed_visibility_level,
import_export_upload: ImportExportUpload.new(import_file: uploaded_file) import_export_upload: ImportExportUpload.new(import_file: params[:file])
} }
group = ::Groups::CreateService.new(current_user, group_params).execute group = ::Groups::CreateService.new(current_user, group_params).execute
......
...@@ -11,7 +11,7 @@ describe API::GroupImport do ...@@ -11,7 +11,7 @@ describe API::GroupImport do
let(:file) { File.join('spec', 'fixtures', 'group_export.tar.gz') } let(:file) { File.join('spec', 'fixtures', 'group_export.tar.gz') }
let(:export_path) { "#{Dir.tmpdir}/group_export_spec" } let(:export_path) { "#{Dir.tmpdir}/group_export_spec" }
let(:workhorse_token) { JWT.encode({ 'iss' => 'gitlab-workhorse' }, Gitlab::Workhorse.secret, 'HS256') } let(:workhorse_token) { JWT.encode({ 'iss' => 'gitlab-workhorse' }, Gitlab::Workhorse.secret, 'HS256') }
let(:workhorse_header) { { 'GitLab-Workhorse' => '1.0', Gitlab::Workhorse::INTERNAL_API_REQUEST_HEADER => workhorse_token } } let(:workhorse_headers) { { 'GitLab-Workhorse' => '1.0', Gitlab::Workhorse::INTERNAL_API_REQUEST_HEADER => workhorse_token } }
before do before do
allow_next_instance_of(Gitlab::ImportExport) do |import_export| allow_next_instance_of(Gitlab::ImportExport) do |import_export|
...@@ -35,7 +35,7 @@ describe API::GroupImport do ...@@ -35,7 +35,7 @@ describe API::GroupImport do
} }
end end
subject { post api('/groups/import', user), params: params, headers: workhorse_header } subject { upload_archive(file_upload, workhorse_headers, params) }
shared_examples 'when all params are correct' do shared_examples 'when all params are correct' do
context 'when user is authorized to create new group' do context 'when user is authorized to create new group' do
...@@ -151,7 +151,7 @@ describe API::GroupImport do ...@@ -151,7 +151,7 @@ describe API::GroupImport do
params[:file] = file_upload params[:file] = file_upload
expect do expect do
post api('/groups/import', user), params: params, headers: workhorse_header upload_archive(file_upload, workhorse_headers, params)
end.not_to change { Group.count }.from(1) end.not_to change { Group.count }.from(1)
expect(response).to have_gitlab_http_status(:bad_request) expect(response).to have_gitlab_http_status(:bad_request)
...@@ -171,7 +171,7 @@ describe API::GroupImport do ...@@ -171,7 +171,7 @@ describe API::GroupImport do
context 'without a file from workhorse' do context 'without a file from workhorse' do
it 'rejects the request' do it 'rejects the request' do
subject upload_archive(nil, workhorse_headers, params)
expect(response).to have_gitlab_http_status(:bad_request) expect(response).to have_gitlab_http_status(:bad_request)
end end
...@@ -179,7 +179,7 @@ describe API::GroupImport do ...@@ -179,7 +179,7 @@ describe API::GroupImport do
context 'without a workhorse header' do context 'without a workhorse header' do
it 'rejects request without a workhorse header' do it 'rejects request without a workhorse header' do
post api('/groups/import', user), params: params upload_archive(file_upload, {}, params)
expect(response).to have_gitlab_http_status(:forbidden) expect(response).to have_gitlab_http_status(:forbidden)
end end
...@@ -189,9 +189,7 @@ describe API::GroupImport do ...@@ -189,9 +189,7 @@ describe API::GroupImport do
let(:params) do let(:params) do
{ {
path: 'test-import-group', path: 'test-import-group',
name: 'test-import-group', name: 'test-import-group'
'file.path' => file_upload.path,
'file.name' => file_upload.original_filename
} }
end end
...@@ -229,9 +227,7 @@ describe API::GroupImport do ...@@ -229,9 +227,7 @@ describe API::GroupImport do
{ {
path: 'test-import-group', path: 'test-import-group',
name: 'test-import-group', name: 'test-import-group',
file: fog_file, file: fog_file
'file.remote_id' => file_name,
'file.size' => fog_file.size
} }
end end
...@@ -245,10 +241,21 @@ describe API::GroupImport do ...@@ -245,10 +241,21 @@ describe API::GroupImport do
include_examples 'when some params are missing' include_examples 'when some params are missing'
end end
end end
def upload_archive(file, headers = {}, params = {})
workhorse_finalize(
api('/groups/import', user),
method: :post,
file_key: :file,
params: params.merge(file: file),
headers: headers,
send_rewritten_field: true
)
end
end end
describe 'POST /groups/import/authorize' do describe 'POST /groups/import/authorize' do
subject { post api('/groups/import/authorize', user), headers: workhorse_header } subject { post api('/groups/import/authorize', user), headers: workhorse_headers }
it 'authorizes importing group with workhorse header' do it 'authorizes importing group with workhorse header' do
subject subject
...@@ -258,7 +265,7 @@ describe API::GroupImport do ...@@ -258,7 +265,7 @@ describe API::GroupImport do
end end
it 'rejects requests that bypassed gitlab-workhorse' do it 'rejects requests that bypassed gitlab-workhorse' do
workhorse_header.delete(Gitlab::Workhorse::INTERNAL_API_REQUEST_HEADER) workhorse_headers.delete(Gitlab::Workhorse::INTERNAL_API_REQUEST_HEADER)
subject subject
......
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment