debian-mirror-gitlab/lib/gitlab/git_access.rb

438 lines
12 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2016-08-24 12:49:21 +05:30
# Check a user's access to perform a git action. All public methods in this
# class return an instance of `GitlabAccessStatus`
2014-09-02 18:07:02 +05:30
module Gitlab
class GitAccess
2018-11-18 11:00:15 +05:30
include Gitlab::Utils::StrongMemoize
2020-04-08 14:13:33 +05:30
ForbiddenError = Class.new(StandardError)
2017-09-10 17:25:29 +05:30
NotFoundError = Class.new(StandardError)
2018-03-17 18:26:18 +05:30
ProjectCreationError = Class.new(StandardError)
2018-12-13 13:39:08 +05:30
TimeoutError = Class.new(StandardError)
2017-09-10 17:25:29 +05:30
ProjectMovedError = Class.new(NotFoundError)
2016-11-24 13:41:30 +05:30
2019-02-15 15:39:39 +05:30
# Use the magic string '_any' to indicate we do not know what the
# changes are. This is also what gitlab-shell does.
ANY = '_any'
2016-11-24 13:41:30 +05:30
ERROR_MESSAGES = {
upload: 'You are not allowed to upload code for this project.',
download: 'You are not allowed to download code from this project.',
2018-03-17 18:26:18 +05:30
auth_upload: 'You are not allowed to upload code.',
auth_download: 'You are not allowed to download code.',
deploy_key_upload: 'This deploy key does not have write access to this project.',
2017-09-10 17:25:29 +05:30
no_repo: 'A repository for this project does not exist yet.',
project_not_found: 'The project you were looking for could not be found.',
command_not_allowed: "The command you're trying to execute is not allowed.",
upload_pack_disabled_over_http: 'Pulling over HTTP is not allowed.',
2018-03-17 18:26:18 +05:30
receive_pack_disabled_over_http: 'Pushing over HTTP is not allowed.',
read_only: 'The repository is temporarily read-only. Please try again later.',
2019-02-15 15:39:39 +05:30
cannot_push_to_read_only: "You can't push code to a read-only GitLab instance.",
push_code: 'You are not allowed to push code to this project.'
2017-08-17 22:00:37 +05:30
}.freeze
2016-11-24 13:41:30 +05:30
2018-12-13 13:39:08 +05:30
INTERNAL_TIMEOUT = 50.seconds.freeze
LOG_HEADER = <<~MESSAGE
Push operation timed out
Timing information for debugging purposes:
MESSAGE
2018-12-05 23:21:45 +05:30
DOWNLOAD_COMMANDS = %w{git-upload-pack git-upload-archive}.freeze
PUSH_COMMANDS = %w{git-receive-pack}.freeze
2016-11-24 13:41:30 +05:30
ALL_COMMANDS = DOWNLOAD_COMMANDS + PUSH_COMMANDS
2014-09-02 18:07:02 +05:30
2020-04-08 14:13:33 +05:30
attr_reader :actor, :project, :protocol, :authentication_abilities, :namespace_path, :repository_path, :redirected_path, :auth_result_type, :changes, :logger
2014-09-02 18:07:02 +05:30
2020-04-08 14:13:33 +05:30
def initialize(actor, project, protocol, authentication_abilities:, namespace_path: nil, repository_path: nil, redirected_path: nil, auth_result_type: nil)
2015-04-26 12:48:37 +05:30
@actor = actor
@project = project
2016-08-24 12:49:21 +05:30
@protocol = protocol
2020-04-08 14:13:33 +05:30
@authentication_abilities = Array(authentication_abilities)
2020-03-09 13:42:32 +05:30
@namespace_path = namespace_path || project&.namespace&.full_path
2020-04-08 14:13:33 +05:30
@repository_path = repository_path || project&.path
2018-03-17 18:26:18 +05:30
@redirected_path = redirected_path
2018-05-09 12:01:36 +05:30
@auth_result_type = auth_result_type
2015-04-26 12:48:37 +05:30
end
2016-09-13 17:45:13 +05:30
def check(cmd, changes)
2018-12-13 13:39:08 +05:30
@logger = Checks::TimedLogger.new(timeout: INTERNAL_TIMEOUT, header: LOG_HEADER)
2018-11-18 11:00:15 +05:30
@changes = changes
2016-11-24 13:41:30 +05:30
check_protocol!
2018-03-17 18:26:18 +05:30
check_valid_actor!
2016-11-24 13:41:30 +05:30
check_active_user!
2018-03-17 18:26:18 +05:30
check_authentication_abilities!(cmd)
2017-09-10 17:25:29 +05:30
check_command_disabled!(cmd)
2016-11-24 13:41:30 +05:30
check_command_existence!(cmd)
2018-11-20 20:47:30 +05:30
custom_action = check_custom_action(cmd)
return custom_action if custom_action
2018-03-17 18:26:18 +05:30
check_db_accessibility!(cmd)
2020-04-08 14:13:33 +05:30
check_project!(changes, cmd)
2017-08-17 22:00:37 +05:30
check_repository_existence!
2015-09-11 14:41:01 +05:30
2014-09-02 18:07:02 +05:30
case cmd
when *DOWNLOAD_COMMANDS
2017-08-17 22:00:37 +05:30
check_download_access!
2014-09-02 18:07:02 +05:30
when *PUSH_COMMANDS
2018-11-18 11:00:15 +05:30
check_push_access!
2014-09-02 18:07:02 +05:30
end
2016-11-24 13:41:30 +05:30
2020-04-08 14:13:33 +05:30
success_result
2014-09-02 18:07:02 +05:30
end
2017-08-17 22:00:37 +05:30
def guest_can_download_code?
Guest.can?(:download_code, project)
2015-04-26 12:48:37 +05:30
end
2016-09-29 09:46:39 +05:30
def user_can_download_code?
authentication_abilities.include?(:download_code) && user_access.can_do_action?(:download_code)
end
def build_can_download_code?
authentication_abilities.include?(:build_download_code) && user_access.can_do_action?(:build_download_code)
end
2018-05-09 12:01:36 +05:30
def request_from_ci_build?
return false unless protocol == 'http'
auth_result_type == :build || auth_result_type == :ci
end
2016-08-24 12:49:21 +05:30
def protocol_allowed?
Gitlab::ProtocolAccess.allowed?(protocol)
2014-09-02 18:07:02 +05:30
end
private
2020-04-08 14:13:33 +05:30
def check_project!(changes, cmd)
check_namespace!
ensure_project_on_push!(cmd, changes)
check_project_accessibility!
add_project_moved_message!
end
2018-11-20 20:47:30 +05:30
def check_custom_action(cmd)
nil
end
2020-04-08 14:13:33 +05:30
def check_for_console_messages
return console_messages unless key?
key_status = Gitlab::Auth::KeyStatusChecker.new(actor)
if key_status.show_console_message?
console_messages.push(key_status.console_message)
else
console_messages
end
end
def console_messages
2019-07-07 11:18:12 +05:30
[]
end
2018-03-17 18:26:18 +05:30
def check_valid_actor!
2020-04-08 14:13:33 +05:30
return unless key?
2018-03-17 18:26:18 +05:30
unless actor.valid?
2020-04-08 14:13:33 +05:30
raise ForbiddenError, "Your SSH key #{actor.errors[:key].first}."
2018-03-17 18:26:18 +05:30
end
end
2016-11-24 13:41:30 +05:30
def check_protocol!
2018-05-09 12:01:36 +05:30
return if request_from_ci_build?
2016-11-24 13:41:30 +05:30
unless protocol_allowed?
2020-04-08 14:13:33 +05:30
raise ForbiddenError, "Git access over #{protocol.upcase} is not allowed"
2016-11-24 13:41:30 +05:30
end
end
2020-03-09 13:42:32 +05:30
def check_namespace!
return if namespace_path.present?
raise NotFoundError, ERROR_MESSAGES[:project_not_found]
end
2016-11-24 13:41:30 +05:30
def check_active_user!
2018-10-15 14:42:47 +05:30
return unless user
unless user_access.allowed?
message = Gitlab::Auth::UserAccessDeniedReason.new(user).rejection_message
2020-04-08 14:13:33 +05:30
raise ForbiddenError, message
2016-11-24 13:41:30 +05:30
end
end
2018-03-17 18:26:18 +05:30
def check_authentication_abilities!(cmd)
case cmd
when *DOWNLOAD_COMMANDS
unless authentication_abilities.include?(:download_code) || authentication_abilities.include?(:build_download_code)
2020-04-08 14:13:33 +05:30
raise ForbiddenError, ERROR_MESSAGES[:auth_download]
2018-03-17 18:26:18 +05:30
end
when *PUSH_COMMANDS
unless authentication_abilities.include?(:push_code)
2020-04-08 14:13:33 +05:30
raise ForbiddenError, ERROR_MESSAGES[:auth_upload]
2018-03-17 18:26:18 +05:30
end
end
end
2016-11-24 13:41:30 +05:30
def check_project_accessibility!
if project.blank? || !can_read_project?
2017-09-10 17:25:29 +05:30
raise NotFoundError, ERROR_MESSAGES[:project_not_found]
end
end
2018-05-09 12:01:36 +05:30
def add_project_moved_message!
2018-03-17 18:26:18 +05:30
return if redirected_path.nil?
2017-09-10 17:25:29 +05:30
2020-04-08 14:13:33 +05:30
project_moved = Checks::ProjectMoved.new(repository, user, protocol, redirected_path)
2017-09-10 17:25:29 +05:30
2018-05-09 12:01:36 +05:30
project_moved.add_message
2017-09-10 17:25:29 +05:30
end
def check_command_disabled!(cmd)
if upload_pack?(cmd)
check_upload_pack_disabled!
elsif receive_pack?(cmd)
check_receive_pack_disabled!
end
end
def check_upload_pack_disabled!
if http? && upload_pack_disabled_over_http?
2020-04-08 14:13:33 +05:30
raise ForbiddenError, ERROR_MESSAGES[:upload_pack_disabled_over_http]
2017-09-10 17:25:29 +05:30
end
end
def check_receive_pack_disabled!
if http? && receive_pack_disabled_over_http?
2020-04-08 14:13:33 +05:30
raise ForbiddenError, ERROR_MESSAGES[:receive_pack_disabled_over_http]
2016-11-24 13:41:30 +05:30
end
end
def check_command_existence!(cmd)
unless ALL_COMMANDS.include?(cmd)
2020-04-08 14:13:33 +05:30
raise ForbiddenError, ERROR_MESSAGES[:command_not_allowed]
2016-11-24 13:41:30 +05:30
end
end
2018-03-17 18:26:18 +05:30
def check_db_accessibility!(cmd)
return unless receive_pack?(cmd)
if Gitlab::Database.read_only?
2020-04-08 14:13:33 +05:30
raise ForbiddenError, push_to_read_only_message
2018-03-17 18:26:18 +05:30
end
end
def ensure_project_on_push!(cmd, changes)
return if project || deploy_key?
2019-02-15 15:39:39 +05:30
return unless receive_pack?(cmd) && changes == ANY && authentication_abilities.include?(:push_code)
2018-03-17 18:26:18 +05:30
namespace = Namespace.find_by_full_path(namespace_path)
return unless user&.can?(:create_projects, namespace)
project_params = {
2020-04-08 14:13:33 +05:30
path: repository_path,
2018-03-17 18:26:18 +05:30
namespace_id: namespace.id,
visibility_level: Gitlab::VisibilityLevel::PRIVATE
}
project = Projects::CreateService.new(user, project_params).execute
unless project.saved?
raise ProjectCreationError, "Could not create project: #{project.errors.full_messages.join(', ')}"
end
@project = project
user_access.project = @project
2020-04-08 14:13:33 +05:30
Checks::ProjectCreated.new(repository, user, protocol).add_message
2018-03-17 18:26:18 +05:30
end
2017-08-17 22:00:37 +05:30
def check_repository_existence!
2018-03-17 18:26:18 +05:30
unless repository.exists?
raise NotFoundError, ERROR_MESSAGES[:no_repo]
2017-08-17 22:00:37 +05:30
end
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
2017-08-17 22:00:37 +05:30
def check_download_access!
2018-03-17 18:26:18 +05:30
passed = deploy_key? ||
2018-05-09 12:01:36 +05:30
deploy_token? ||
2018-03-17 18:26:18 +05:30
user_can_download_code? ||
2017-08-17 22:00:37 +05:30
build_can_download_code? ||
guest_can_download_code?
unless passed
2020-04-08 14:13:33 +05:30
raise ForbiddenError, ERROR_MESSAGES[:download]
2017-08-17 22:00:37 +05:30
end
2015-04-26 12:48:37 +05:30
end
2018-11-18 11:00:15 +05:30
def check_push_access!
2018-03-17 18:26:18 +05:30
if project.repository_read_only?
2020-04-08 14:13:33 +05:30
raise ForbiddenError, ERROR_MESSAGES[:read_only]
2018-03-17 18:26:18 +05:30
end
2018-05-09 12:01:36 +05:30
if deploy_key?
2018-03-17 18:26:18 +05:30
unless deploy_key.can_push_to?(project)
2020-04-08 14:13:33 +05:30
raise ForbiddenError, ERROR_MESSAGES[:deploy_key_upload]
2018-03-17 18:26:18 +05:30
end
2017-08-17 22:00:37 +05:30
elsif user
2018-03-17 18:26:18 +05:30
# User access is verified in check_change_access!
2015-04-26 12:48:37 +05:30
else
2020-04-08 14:13:33 +05:30
raise ForbiddenError, ERROR_MESSAGES[:upload]
2015-04-26 12:48:37 +05:30
end
2017-08-17 22:00:37 +05:30
2018-11-18 11:00:15 +05:30
check_change_access!
2015-04-26 12:48:37 +05:30
end
2018-11-18 11:00:15 +05:30
def check_change_access!
2019-02-15 15:39:39 +05:30
# Deploy keys with write access can push anything
return if deploy_key?
if changes == ANY
can_push = user_access.can_do_action?(:push_code) ||
project.any_branch_allows_collaboration?(user_access.user)
unless can_push
2020-04-08 14:13:33 +05:30
raise ForbiddenError, ERROR_MESSAGES[:push_code]
2019-02-15 15:39:39 +05:30
end
else
# If there are worktrees with a HEAD pointing to a non-existent object,
# calls to `git rev-list --all` will fail in git 2.15+. This should also
# clear stale lock files.
project.repository.clean_stale_repository_files
# Iterate over all changes to find if user allowed all of them to be applied
changes_list.each.with_index do |change, index|
first_change = index == 0
# If user does not have access to make at least one change, cancel all
# push by allowing the exception to bubble up
check_single_change_access(change, skip_lfs_integrity_check: !first_change)
end
2016-11-24 13:41:30 +05:30
end
end
2018-03-17 18:26:18 +05:30
def check_single_change_access(change, skip_lfs_integrity_check: false)
2018-12-13 13:39:08 +05:30
change_access = Checks::ChangeAccess.new(
2017-08-17 22:00:37 +05:30
change,
user_access: user_access,
project: project,
2018-03-17 18:26:18 +05:30
skip_lfs_integrity_check: skip_lfs_integrity_check,
2018-12-13 13:39:08 +05:30
protocol: protocol,
logger: logger
)
change_access.exec
rescue Checks::TimedLogger::TimeoutError
raise TimeoutError, logger.full_message
2017-08-17 22:00:37 +05:30
end
def deploy_key
actor if deploy_key?
end
def deploy_key?
actor.is_a?(DeployKey)
end
2018-05-09 12:01:36 +05:30
def deploy_token
actor if deploy_token?
end
def deploy_token?
actor.is_a?(DeployToken)
end
2017-09-10 17:25:29 +05:30
def ci?
actor == :ci
end
2020-04-08 14:13:33 +05:30
def key?
actor.is_a?(Key)
end
2017-08-17 22:00:37 +05:30
def can_read_project?
2017-09-10 17:25:29 +05:30
if deploy_key?
2017-08-17 22:00:37 +05:30
deploy_key.has_access_to?(project)
2018-05-09 12:01:36 +05:30
elsif deploy_token?
deploy_token.has_access_to?(project)
2017-08-17 22:00:37 +05:30
elsif user
user.can?(:read_project, project)
2017-09-10 17:25:29 +05:30
elsif ci?
true # allow CI (build without a user) for backwards compatibility
2017-08-17 22:00:37 +05:30
end || Guest.can?(:read_project, project)
end
2017-09-10 17:25:29 +05:30
def http?
protocol == 'http'
end
def upload_pack?(command)
command == 'git-upload-pack'
end
def receive_pack?(command)
command == 'git-receive-pack'
end
def upload_pack_disabled_over_http?
!Gitlab.config.gitlab_shell.upload_pack
end
def receive_pack_disabled_over_http?
!Gitlab.config.gitlab_shell.receive_pack
end
2015-04-26 12:48:37 +05:30
protected
2020-04-08 14:13:33 +05:30
def success_result
::Gitlab::GitAccessResult::Success.new(console_messages: check_for_console_messages)
2019-07-31 22:56:46 +05:30
end
2018-11-18 11:00:15 +05:30
def changes_list
2019-02-15 15:39:39 +05:30
@changes_list ||= Gitlab::ChangesList.new(changes == ANY ? [] : changes)
2018-11-18 11:00:15 +05:30
end
2016-08-24 12:49:21 +05:30
def user
return @user if defined?(@user)
@user =
case actor
when User
actor
2018-05-09 12:01:36 +05:30
when DeployKey
nil
2016-08-24 12:49:21 +05:30
when Key
2018-05-09 12:01:36 +05:30
actor.user
2017-09-10 17:25:29 +05:30
when :ci
nil
2016-08-24 12:49:21 +05:30
end
end
2017-09-10 17:25:29 +05:30
def user_access
@user_access ||= if ci?
CiAccess.new
2018-10-15 14:42:47 +05:30
elsif user && request_from_ci_build?
BuildAccess.new(user, project: project)
2017-09-10 17:25:29 +05:30
else
UserAccess.new(user, project: project)
end
2015-04-26 12:48:37 +05:30
end
2018-03-17 18:26:18 +05:30
def push_to_read_only_message
ERROR_MESSAGES[:cannot_push_to_read_only]
end
def repository
project.repository
end
2014-09-02 18:07:02 +05:30
end
end
2019-12-04 20:38:33 +05:30
Gitlab::GitAccess.prepend_if_ee('EE::Gitlab::GitAccess')