2020-02-13 15:08:52 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.describe Gitlab::GitAccessSnippet do
|
2020-02-27 15:09:24 +00:00
|
|
|
include ProjectHelpers
|
|
|
|
include TermsHelper
|
2020-11-11 12:09:06 +00:00
|
|
|
include AdminModeHelper
|
2020-02-27 15:09:24 +00:00
|
|
|
include_context 'ProjectPolicyTable context'
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
2020-02-13 15:08:52 +00:00
|
|
|
|
|
|
|
let_it_be(:user) { create(:user) }
|
2020-02-27 15:09:24 +00:00
|
|
|
let_it_be(:project) { create(:project, :public) }
|
|
|
|
let_it_be(:snippet) { create(:project_snippet, :public, :repository, project: project) }
|
2020-05-06 18:09:38 +00:00
|
|
|
let_it_be(:migration_bot) { User.migration_bot }
|
2020-02-13 15:08:52 +00:00
|
|
|
|
2020-05-06 18:09:38 +00:00
|
|
|
let(:repository) { snippet.repository }
|
2020-02-27 15:09:24 +00:00
|
|
|
let(:actor) { user }
|
2020-02-13 15:08:52 +00:00
|
|
|
let(:protocol) { 'ssh' }
|
|
|
|
let(:changes) { Gitlab::GitAccess::ANY }
|
2020-02-27 15:09:24 +00:00
|
|
|
let(:authentication_abilities) { [:download_code, :push_code] }
|
|
|
|
|
2020-02-13 15:08:52 +00:00
|
|
|
let(:push_access_check) { access.check('git-receive-pack', changes) }
|
|
|
|
let(:pull_access_check) { access.check('git-upload-pack', changes) }
|
|
|
|
|
2020-02-27 15:09:24 +00:00
|
|
|
subject(:access) { Gitlab::GitAccessSnippet.new(actor, snippet, protocol, authentication_abilities: authentication_abilities) }
|
|
|
|
|
|
|
|
describe 'when actor is a DeployKey' do
|
|
|
|
let(:actor) { build(:deploy_key) }
|
|
|
|
|
|
|
|
it 'does not allow push and pull access' do
|
2020-05-06 18:09:38 +00:00
|
|
|
expect { push_access_check }.to raise_forbidden(described_class::ERROR_MESSAGES[:authentication_mechanism])
|
2020-02-27 18:09:21 +00:00
|
|
|
expect { pull_access_check }.to raise_forbidden(described_class::ERROR_MESSAGES[:authentication_mechanism])
|
2020-02-13 15:08:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-06 18:09:38 +00:00
|
|
|
shared_examples 'actor is migration bot' do
|
|
|
|
context 'when user is the migration bot' do
|
|
|
|
let(:user) { migration_bot }
|
|
|
|
|
|
|
|
it 'can perform git operations' do
|
|
|
|
expect { push_access_check }.not_to raise_error
|
|
|
|
expect { pull_access_check }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-13 15:08:52 +00:00
|
|
|
describe '#check_snippet_accessibility!' do
|
|
|
|
context 'when the snippet exists' do
|
2020-02-27 15:09:24 +00:00
|
|
|
it 'allows access' do
|
|
|
|
project.add_developer(actor)
|
|
|
|
|
|
|
|
expect { pull_access_check }.not_to raise_error
|
2020-02-13 15:08:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the snippet is nil' do
|
|
|
|
let(:snippet) { nil }
|
|
|
|
|
2020-02-27 15:09:24 +00:00
|
|
|
it 'blocks access with "not found"' do
|
|
|
|
expect { pull_access_check }.to raise_snippet_not_found
|
2020-02-13 15:08:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the snippet does not have a repository' do
|
|
|
|
let(:snippet) { build_stubbed(:personal_snippet) }
|
|
|
|
|
2020-02-27 15:09:24 +00:00
|
|
|
it 'blocks access with "not found"' do
|
|
|
|
expect { pull_access_check }.to raise_snippet_not_found
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'terms are enforced', :aggregate_failures do
|
|
|
|
before do
|
|
|
|
enforce_terms
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:user) { snippet.author }
|
|
|
|
|
|
|
|
it 'blocks access when the user did not accept terms' do
|
|
|
|
message = /must accept the Terms of Service in order to perform this action/
|
|
|
|
|
2020-02-27 18:09:21 +00:00
|
|
|
expect { push_access_check }.to raise_forbidden(message)
|
|
|
|
expect { pull_access_check }.to raise_forbidden(message)
|
2020-02-27 15:09:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows access when the user accepted the terms' do
|
|
|
|
accept_terms(user)
|
|
|
|
|
|
|
|
expect { push_access_check }.not_to raise_error
|
|
|
|
expect { pull_access_check }.not_to raise_error
|
|
|
|
end
|
2020-05-06 18:09:38 +00:00
|
|
|
|
|
|
|
it_behaves_like 'actor is migration bot' do
|
|
|
|
before do
|
|
|
|
expect(migration_bot.required_terms_not_accepted?).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
2020-02-27 15:09:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'project snippet accessibility', :aggregate_failures do
|
|
|
|
let(:snippet) { create(:project_snippet, :private, :repository, project: project) }
|
|
|
|
let(:user) { membership == :author ? snippet.author : create_user_from_membership(project, membership) }
|
|
|
|
|
|
|
|
shared_examples_for 'checks accessibility' do
|
|
|
|
[:anonymous, :non_member, :guest, :reporter, :maintainer, :admin, :author].each do |membership|
|
|
|
|
context membership.to_s do
|
|
|
|
let(:membership) { membership }
|
|
|
|
|
|
|
|
it 'respects accessibility' do
|
|
|
|
if Ability.allowed?(user, :update_snippet, snippet)
|
|
|
|
expect { push_access_check }.not_to raise_error
|
|
|
|
else
|
2020-02-27 18:09:21 +00:00
|
|
|
expect { push_access_check }.to raise_error(described_class::ForbiddenError)
|
2020-02-27 15:09:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if Ability.allowed?(user, :read_snippet, snippet)
|
|
|
|
expect { pull_access_check }.not_to raise_error
|
|
|
|
else
|
2020-02-27 18:09:21 +00:00
|
|
|
expect { pull_access_check }.to raise_error(described_class::ForbiddenError)
|
2020-02-27 15:09:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project is public' do
|
|
|
|
it_behaves_like 'checks accessibility'
|
2020-05-06 18:09:38 +00:00
|
|
|
it_behaves_like 'actor is migration bot'
|
2020-02-27 15:09:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project is public but snippet feature is private' do
|
|
|
|
let(:project) { create(:project, :public) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
update_feature_access_level(project, :private)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'checks accessibility'
|
2020-05-06 18:09:38 +00:00
|
|
|
it_behaves_like 'actor is migration bot'
|
2020-02-27 15:09:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project is not accessible' do
|
|
|
|
let(:project) { create(:project, :private) }
|
|
|
|
|
|
|
|
[:anonymous, :non_member].each do |membership|
|
|
|
|
context membership.to_s do
|
|
|
|
let(:membership) { membership }
|
|
|
|
|
|
|
|
it 'respects accessibility' do
|
2020-05-06 18:09:38 +00:00
|
|
|
expect { push_access_check }.to raise_snippet_not_found
|
|
|
|
expect { pull_access_check }.to raise_snippet_not_found
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'actor is migration bot'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project is archived' do
|
|
|
|
let(:project) { create(:project, :public, :archived) }
|
|
|
|
|
|
|
|
[:anonymous, :non_member].each do |membership|
|
|
|
|
context membership.to_s do
|
|
|
|
let(:membership) { membership }
|
|
|
|
|
|
|
|
it 'cannot perform git operations' do
|
|
|
|
expect { push_access_check }.to raise_error(described_class::ForbiddenError)
|
|
|
|
expect { pull_access_check }.to raise_error(described_class::ForbiddenError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-17 18:10:14 +00:00
|
|
|
[:guest, :reporter, :maintainer, :author].each do |membership|
|
2020-05-06 18:09:38 +00:00
|
|
|
context membership.to_s do
|
|
|
|
let(:membership) { membership }
|
|
|
|
|
|
|
|
it 'cannot perform git pushes' do
|
|
|
|
expect { push_access_check }.to raise_error(described_class::ForbiddenError)
|
|
|
|
expect { pull_access_check }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-17 18:10:14 +00:00
|
|
|
context 'admin' do
|
|
|
|
let(:membership) { :admin }
|
|
|
|
|
|
|
|
context 'when admin mode is enabled', :enable_admin_mode do
|
|
|
|
it 'cannot perform git pushes' do
|
|
|
|
expect { push_access_check }.to raise_error(described_class::ForbiddenError)
|
|
|
|
expect { pull_access_check }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when admin mode is disabled' do
|
|
|
|
it 'cannot perform git operations' do
|
|
|
|
expect { push_access_check }.to raise_error(described_class::ForbiddenError)
|
|
|
|
expect { pull_access_check }.to raise_error(described_class::ForbiddenError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-06 18:09:38 +00:00
|
|
|
it_behaves_like 'actor is migration bot'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when snippet feature is disabled' do
|
|
|
|
let(:project) { create(:project, :public, :snippets_disabled) }
|
|
|
|
|
|
|
|
[:anonymous, :non_member, :author, :admin].each do |membership|
|
|
|
|
context membership.to_s do
|
|
|
|
let(:membership) { membership }
|
|
|
|
|
|
|
|
it 'cannot perform git operations' do
|
|
|
|
expect { push_access_check }.to raise_error(described_class::ForbiddenError)
|
|
|
|
expect { pull_access_check }.to raise_error(described_class::ForbiddenError)
|
2020-02-27 15:09:24 +00:00
|
|
|
end
|
2020-02-13 15:08:52 +00:00
|
|
|
end
|
|
|
|
end
|
2020-05-06 18:09:38 +00:00
|
|
|
|
|
|
|
it_behaves_like 'actor is migration bot'
|
2020-02-13 15:08:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-27 15:09:24 +00:00
|
|
|
context 'personal snippet accessibility', :aggregate_failures do
|
|
|
|
let(:snippet) { create(:personal_snippet, snippet_level, :repository) }
|
|
|
|
let(:user) { membership == :author ? snippet.author : create_user_from_membership(nil, membership) }
|
|
|
|
|
2020-11-11 12:09:06 +00:00
|
|
|
where(:snippet_level, :membership, :admin_mode, :_expected_count) do
|
2020-02-27 15:09:24 +00:00
|
|
|
permission_table_for_personal_snippet_access
|
|
|
|
end
|
2020-02-13 15:08:52 +00:00
|
|
|
|
2020-02-27 15:09:24 +00:00
|
|
|
with_them do
|
|
|
|
it "respects accessibility" do
|
2020-11-11 12:09:06 +00:00
|
|
|
enable_admin_mode!(user) if admin_mode
|
2020-02-27 18:09:21 +00:00
|
|
|
error_class = described_class::ForbiddenError
|
2020-02-27 15:09:24 +00:00
|
|
|
|
|
|
|
if Ability.allowed?(user, :update_snippet, snippet)
|
|
|
|
expect { push_access_check }.not_to raise_error
|
|
|
|
else
|
|
|
|
expect { push_access_check }.to raise_error(error_class)
|
|
|
|
end
|
|
|
|
|
|
|
|
if Ability.allowed?(user, :read_snippet, snippet)
|
|
|
|
expect { pull_access_check }.not_to raise_error
|
|
|
|
else
|
|
|
|
expect { pull_access_check }.to raise_error(error_class)
|
|
|
|
end
|
|
|
|
end
|
2020-05-06 18:09:38 +00:00
|
|
|
|
|
|
|
it_behaves_like 'actor is migration bot'
|
2020-02-27 15:09:24 +00:00
|
|
|
end
|
2020-02-13 15:08:52 +00:00
|
|
|
end
|
|
|
|
|
2020-02-27 15:09:24 +00:00
|
|
|
context 'when changes are specific' do
|
2020-03-17 06:09:21 +00:00
|
|
|
let(:changes) { "2d1db523e11e777e49377cfb22d368deec3f0793 ddd0f15ae83993f5cb66a927a28673882e99100b master" }
|
2020-02-27 15:09:24 +00:00
|
|
|
let(:user) { snippet.author }
|
|
|
|
|
2020-05-06 18:09:38 +00:00
|
|
|
shared_examples 'snippet checks' do
|
|
|
|
it 'does not raise error if SnippetCheck does not raise error' do
|
|
|
|
expect_next_instance_of(Gitlab::Checks::SnippetCheck) do |check|
|
|
|
|
expect(check).to receive(:validate!).and_call_original
|
|
|
|
end
|
|
|
|
expect_next_instance_of(Gitlab::Checks::PushFileCountCheck) do |check|
|
|
|
|
expect(check).to receive(:validate!)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect { push_access_check }.not_to raise_error
|
2020-02-27 15:09:24 +00:00
|
|
|
end
|
|
|
|
|
2020-05-06 18:09:38 +00:00
|
|
|
it 'raises error if SnippetCheck raises error' do
|
|
|
|
expect_next_instance_of(Gitlab::Checks::SnippetCheck) do |check|
|
|
|
|
allow(check).to receive(:validate!).and_raise(Gitlab::GitAccess::ForbiddenError, 'foo')
|
|
|
|
end
|
2020-02-27 15:09:24 +00:00
|
|
|
|
2020-05-06 18:09:38 +00:00
|
|
|
expect { push_access_check }.to raise_forbidden('foo')
|
2020-02-27 15:09:24 +00:00
|
|
|
end
|
2020-05-19 12:08:21 +00:00
|
|
|
|
|
|
|
it 'sets the file count limit from Snippet class' do
|
|
|
|
service = double
|
|
|
|
|
|
|
|
expect(service).to receive(:validate!).and_return(nil)
|
2020-10-06 18:08:49 +00:00
|
|
|
expect(Snippet).to receive(:max_file_limit).and_return(5)
|
2020-05-19 12:08:21 +00:00
|
|
|
expect(Gitlab::Checks::PushFileCountCheck).to receive(:new).with(anything, hash_including(limit: 5)).and_return(service)
|
|
|
|
|
|
|
|
push_access_check
|
|
|
|
end
|
2020-05-06 18:09:38 +00:00
|
|
|
end
|
2020-02-27 15:09:24 +00:00
|
|
|
|
2020-05-06 18:09:38 +00:00
|
|
|
it_behaves_like 'snippet checks'
|
|
|
|
|
|
|
|
context 'when user is migration bot' do
|
|
|
|
let(:user) { migration_bot }
|
|
|
|
|
|
|
|
it_behaves_like 'snippet checks'
|
2020-02-27 15:09:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-07 00:09:33 +00:00
|
|
|
describe 'repository size restrictions' do
|
|
|
|
let(:snippet) { create(:personal_snippet, :public, :repository) }
|
|
|
|
let(:actor) { snippet.author }
|
|
|
|
|
|
|
|
let(:oldrev) { TestEnv::BRANCH_SHA["snippet/single-file"] }
|
|
|
|
let(:newrev) { TestEnv::BRANCH_SHA["snippet/edit-file"] }
|
|
|
|
let(:ref) { "refs/heads/snippet/edit-file" }
|
|
|
|
let(:changes) { "#{oldrev} #{newrev} #{ref}" }
|
|
|
|
|
2020-05-11 12:10:28 +00:00
|
|
|
shared_examples 'migration bot does not err' do
|
|
|
|
let(:actor) { migration_bot }
|
|
|
|
|
|
|
|
it 'does not err' do
|
|
|
|
expect(snippet.repository_size_checker).not_to receive(:above_size_limit?)
|
|
|
|
|
|
|
|
expect { push_access_check }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-07 00:09:33 +00:00
|
|
|
shared_examples_for 'a push to repository already over the limit' do
|
|
|
|
it 'errs' do
|
|
|
|
expect(snippet.repository_size_checker).to receive(:above_size_limit?).and_return(true)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
push_access_check
|
|
|
|
end.to raise_error(described_class::ForbiddenError, /Your push has been rejected/)
|
|
|
|
end
|
2020-05-11 12:10:28 +00:00
|
|
|
|
|
|
|
it_behaves_like 'migration bot does not err'
|
2020-04-07 00:09:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'a push to repository below the limit' do
|
|
|
|
it 'does not err' do
|
|
|
|
expect(snippet.repository_size_checker).to receive(:above_size_limit?).and_return(false)
|
|
|
|
expect(snippet.repository_size_checker)
|
|
|
|
.to receive(:changes_will_exceed_size_limit?)
|
|
|
|
.with(change_size)
|
|
|
|
.and_return(false)
|
|
|
|
|
|
|
|
expect { push_access_check }.not_to raise_error
|
|
|
|
end
|
2020-05-11 12:10:28 +00:00
|
|
|
|
|
|
|
it_behaves_like 'migration bot does not err'
|
2020-04-07 00:09:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'a push to repository to make it over the limit' do
|
|
|
|
it 'errs' do
|
|
|
|
expect(snippet.repository_size_checker).to receive(:above_size_limit?).and_return(false)
|
|
|
|
expect(snippet.repository_size_checker)
|
|
|
|
.to receive(:changes_will_exceed_size_limit?)
|
|
|
|
.with(change_size)
|
|
|
|
.and_return(true)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
push_access_check
|
|
|
|
end.to raise_error(described_class::ForbiddenError, /Your push to this repository would cause it to exceed the size limit/)
|
|
|
|
end
|
2020-05-11 12:10:28 +00:00
|
|
|
|
|
|
|
it_behaves_like 'migration bot does not err'
|
2020-04-07 00:09:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when GIT_OBJECT_DIRECTORY_RELATIVE env var is set' do
|
|
|
|
let(:change_size) { 100 }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Gitlab::Git::HookEnv)
|
|
|
|
.to receive(:all)
|
|
|
|
.with(repository.gl_repository)
|
|
|
|
.and_return({ 'GIT_OBJECT_DIRECTORY_RELATIVE' => 'objects' })
|
|
|
|
|
|
|
|
# Stub the object directory size to "simulate" quarantine size
|
|
|
|
allow(repository).to receive(:object_directory_size).and_return(change_size)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a push to repository already over the limit'
|
|
|
|
it_behaves_like 'a push to repository below the limit'
|
|
|
|
it_behaves_like 'a push to repository to make it over the limit'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when GIT_OBJECT_DIRECTORY_RELATIVE env var is not set' do
|
|
|
|
let(:change_size) { 200 }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(snippet.repository).to receive(:new_blobs).and_return(
|
|
|
|
[double(:blob, size: change_size)]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a push to repository already over the limit'
|
|
|
|
it_behaves_like 'a push to repository below the limit'
|
|
|
|
it_behaves_like 'a push to repository to make it over the limit'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-27 15:09:24 +00:00
|
|
|
private
|
|
|
|
|
2020-02-13 15:08:52 +00:00
|
|
|
def raise_snippet_not_found
|
|
|
|
raise_error(Gitlab::GitAccess::NotFoundError, Gitlab::GitAccess::ERROR_MESSAGES[:snippet_not_found])
|
|
|
|
end
|
2020-02-27 15:09:24 +00:00
|
|
|
|
|
|
|
def raise_project_not_found
|
|
|
|
raise_error(Gitlab::GitAccess::NotFoundError, Gitlab::GitAccess::ERROR_MESSAGES[:project_not_found])
|
|
|
|
end
|
|
|
|
|
2020-02-27 18:09:21 +00:00
|
|
|
def raise_forbidden(message)
|
|
|
|
raise_error(Gitlab::GitAccess::ForbiddenError, message)
|
2020-02-27 15:09:24 +00:00
|
|
|
end
|
2020-02-13 15:08:52 +00:00
|
|
|
end
|