2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-07-18 04:16:56 -04:00
|
|
|
# Check a user's access to perform a git action. All public methods in this
|
|
|
|
# class return an instance of `GitlabAccessStatus`
|
2014-03-19 15:01:00 -04:00
|
|
|
module Gitlab
|
|
|
|
class GitAccess
|
2018-07-06 16:56:41 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2020-02-26 13:09:24 -05:00
|
|
|
ForbiddenError = Class.new(StandardError)
|
2017-05-19 15:58:45 -04:00
|
|
|
NotFoundError = Class.new(StandardError)
|
2018-10-22 10:49:20 -04:00
|
|
|
TimeoutError = Class.new(StandardError)
|
2016-11-02 17:50:44 -04:00
|
|
|
|
2018-12-20 11:25:48 -05:00
|
|
|
# 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-02 17:50:44 -04:00
|
|
|
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-02-06 12:25:36 -05:00
|
|
|
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-05-15 19:13:36 -04:00
|
|
|
no_repo: 'A repository for this project does not exist yet.',
|
2021-04-21 08:09:16 -04:00
|
|
|
project_not_found: "The project you were looking for could not be found or you don't have permission to view it.",
|
2017-05-16 12:02:52 -04:00
|
|
|
command_not_allowed: "The command you're trying to execute is not allowed.",
|
2017-05-22 14:28:51 -04:00
|
|
|
upload_pack_disabled_over_http: 'Pulling over HTTP is not allowed.',
|
2017-09-05 22:24:31 -04:00
|
|
|
receive_pack_disabled_over_http: 'Pushing over HTTP is not allowed.',
|
2017-09-19 03:44:58 -04:00
|
|
|
read_only: 'The repository is temporarily read-only. Please try again later.',
|
2018-12-20 11:40:56 -05:00
|
|
|
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-02-21 18:32:18 -05:00
|
|
|
}.freeze
|
2016-11-02 17:50:44 -04:00
|
|
|
|
2018-10-25 05:36:40 -04:00
|
|
|
INTERNAL_TIMEOUT = 50.seconds.freeze
|
2018-10-25 04:49:59 -04:00
|
|
|
LOG_HEADER = <<~MESSAGE
|
|
|
|
Push operation timed out
|
|
|
|
|
|
|
|
Timing information for debugging purposes:
|
|
|
|
MESSAGE
|
|
|
|
|
2018-09-20 12:54:14 -04:00
|
|
|
DOWNLOAD_COMMANDS = %w{git-upload-pack git-upload-archive}.freeze
|
|
|
|
PUSH_COMMANDS = %w{git-receive-pack}.freeze
|
2016-11-02 17:50:44 -04:00
|
|
|
ALL_COMMANDS = DOWNLOAD_COMMANDS + PUSH_COMMANDS
|
2014-03-19 15:01:00 -04:00
|
|
|
|
2020-07-21 14:09:45 -04:00
|
|
|
attr_reader :actor, :protocol, :authentication_abilities,
|
2020-12-01 07:09:17 -05:00
|
|
|
:repository_path, :redirected_path, :auth_result_type,
|
2020-07-21 14:09:45 -04:00
|
|
|
:cmd, :changes
|
|
|
|
attr_accessor :container
|
2014-03-19 15:01:00 -04:00
|
|
|
|
2020-10-05 23:08:19 -04:00
|
|
|
def self.error_message(key)
|
|
|
|
self.ancestors.each do |cls|
|
|
|
|
return cls.const_get('ERROR_MESSAGES', false).fetch(key)
|
|
|
|
rescue NameError, KeyError
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
raise ArgumentError, "No error message defined for #{key}"
|
|
|
|
end
|
|
|
|
|
2020-12-01 07:09:17 -05:00
|
|
|
def initialize(actor, container, protocol, authentication_abilities:, repository_path: nil, redirected_path: nil, auth_result_type: nil)
|
2020-07-21 14:09:45 -04:00
|
|
|
@actor = actor
|
|
|
|
@container = container
|
|
|
|
@protocol = protocol
|
2020-03-04 19:07:49 -05:00
|
|
|
@authentication_abilities = Array(authentication_abilities)
|
2020-07-21 14:09:45 -04:00
|
|
|
@repository_path = repository_path
|
2018-02-02 10:27:30 -05:00
|
|
|
@redirected_path = redirected_path
|
2018-03-27 11:35:27 -04:00
|
|
|
@auth_result_type = auth_result_type
|
2015-03-24 09:10:55 -04:00
|
|
|
end
|
|
|
|
|
2016-08-03 08:54:12 -04:00
|
|
|
def check(cmd, changes)
|
2018-07-06 16:56:41 -04:00
|
|
|
@changes = changes
|
2020-07-21 14:09:45 -04:00
|
|
|
@cmd = cmd
|
2018-07-06 16:56:41 -04:00
|
|
|
|
2016-11-02 17:50:44 -04:00
|
|
|
check_protocol!
|
2017-08-28 16:33:35 -04:00
|
|
|
check_valid_actor!
|
2016-12-20 08:19:07 -05:00
|
|
|
check_active_user!
|
2020-07-21 14:09:45 -04:00
|
|
|
check_authentication_abilities!
|
|
|
|
check_command_disabled!
|
|
|
|
check_command_existence!
|
2018-08-27 23:47:35 -04:00
|
|
|
|
2020-07-21 14:09:45 -04:00
|
|
|
custom_action = check_custom_action
|
2018-08-27 23:47:35 -04:00
|
|
|
return custom_action if custom_action
|
|
|
|
|
2020-07-21 14:09:45 -04:00
|
|
|
check_db_accessibility!
|
|
|
|
check_container!
|
2018-02-02 10:27:30 -05:00
|
|
|
check_repository_existence!
|
2015-05-10 17:07:35 -04:00
|
|
|
|
2014-03-19 15:01:00 -04:00
|
|
|
case cmd
|
|
|
|
when *DOWNLOAD_COMMANDS
|
2016-12-20 08:19:07 -05:00
|
|
|
check_download_access!
|
2014-03-19 15:01:00 -04:00
|
|
|
when *PUSH_COMMANDS
|
2018-07-06 16:56:41 -04:00
|
|
|
check_push_access!
|
2014-03-19 15:01:00 -04:00
|
|
|
end
|
2021-04-01 08:08:56 -04:00
|
|
|
check_additional_conditions!
|
2016-11-02 17:50:44 -04:00
|
|
|
|
2020-03-13 05:09:23 -04:00
|
|
|
success_result
|
2014-03-19 15:01:00 -04:00
|
|
|
end
|
|
|
|
|
2020-07-21 14:09:45 -04:00
|
|
|
def logger
|
|
|
|
@logger ||= Checks::TimedLogger.new(timeout: INTERNAL_TIMEOUT, header: LOG_HEADER)
|
|
|
|
end
|
|
|
|
|
2016-12-06 08:10:27 -05:00
|
|
|
def guest_can_download_code?
|
2020-07-21 14:09:45 -04:00
|
|
|
Guest.can?(download_ability, container)
|
2016-11-29 13:59:25 -05:00
|
|
|
end
|
|
|
|
|
2020-09-02 11:10:54 -04:00
|
|
|
def deploy_key_can_download_code?
|
|
|
|
authentication_abilities.include?(:download_code) &&
|
|
|
|
deploy_key? &&
|
|
|
|
deploy_key.has_access_to?(container) &&
|
|
|
|
(project? && project&.repository_access_level != ::Featurable::DISABLED)
|
|
|
|
end
|
|
|
|
|
2016-09-15 04:34:53 -04:00
|
|
|
def user_can_download_code?
|
2020-07-21 14:09:45 -04:00
|
|
|
authentication_abilities.include?(:download_code) &&
|
|
|
|
user_access.can_do_action?(download_ability)
|
|
|
|
end
|
|
|
|
|
|
|
|
# @return [Symbol] the name of a Declarative Policy ability to check
|
|
|
|
def download_ability
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
# @return [Symbol] the name of a Declarative Policy ability to check
|
|
|
|
def push_ability
|
|
|
|
raise NotImplementedError
|
2016-08-08 06:01:25 -04:00
|
|
|
end
|
|
|
|
|
2016-09-15 04:34:53 -04:00
|
|
|
def build_can_download_code?
|
2016-09-16 03:59:10 -04:00
|
|
|
authentication_abilities.include?(:build_download_code) && user_access.can_do_action?(:build_download_code)
|
2015-03-24 09:10:55 -04:00
|
|
|
end
|
|
|
|
|
2018-03-27 11:35:27 -04:00
|
|
|
def request_from_ci_build?
|
|
|
|
return false unless protocol == 'http'
|
|
|
|
|
|
|
|
auth_result_type == :build || auth_result_type == :ci
|
|
|
|
end
|
|
|
|
|
2016-06-27 12:14:44 -04:00
|
|
|
def protocol_allowed?
|
|
|
|
Gitlab::ProtocolAccess.allowed?(protocol)
|
|
|
|
end
|
|
|
|
|
2014-03-19 15:01:00 -04:00
|
|
|
private
|
|
|
|
|
2020-07-21 14:09:45 -04:00
|
|
|
def check_container!
|
2020-10-05 11:08:56 -04:00
|
|
|
# Strict nil check, to avoid any surprises with Object#present?
|
|
|
|
# which can delegate to #empty?
|
|
|
|
raise NotFoundError, not_found_message if container.nil?
|
|
|
|
|
2020-07-21 14:09:45 -04:00
|
|
|
check_project! if project?
|
2021-06-22 11:06:55 -04:00
|
|
|
add_container_moved_message!
|
2020-07-21 14:09:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_project!
|
2020-02-27 10:09:24 -05:00
|
|
|
check_project_accessibility!
|
|
|
|
end
|
|
|
|
|
2020-07-21 14:09:45 -04:00
|
|
|
def check_custom_action
|
|
|
|
# no-op: Overridden in EE
|
2018-08-27 23:47:35 -04:00
|
|
|
end
|
|
|
|
|
2020-03-13 05:09:23 -04:00
|
|
|
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-03-27 21:33:45 -04:00
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
2017-08-21 06:30:03 -04:00
|
|
|
def check_valid_actor!
|
2020-03-13 05:09:23 -04:00
|
|
|
return unless key?
|
2017-08-21 06:30:03 -04:00
|
|
|
|
|
|
|
unless actor.valid?
|
2020-02-26 13:09:24 -05:00
|
|
|
raise ForbiddenError, "Your SSH key #{actor.errors[:key].first}."
|
2017-08-21 06:30:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-02 17:50:44 -04:00
|
|
|
def check_protocol!
|
2018-03-27 11:35:27 -04:00
|
|
|
return if request_from_ci_build?
|
|
|
|
|
2016-11-02 17:50:44 -04:00
|
|
|
unless protocol_allowed?
|
2020-02-26 13:09:24 -05:00
|
|
|
raise ForbiddenError, "Git access over #{protocol.upcase} is not allowed"
|
2016-11-02 17:50:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_active_user!
|
2018-05-10 10:13:05 -04:00
|
|
|
return unless user
|
|
|
|
|
|
|
|
unless user_access.allowed?
|
2018-05-08 09:07:55 -04:00
|
|
|
message = Gitlab::Auth::UserAccessDeniedReason.new(user).rejection_message
|
2020-02-26 13:09:24 -05:00
|
|
|
raise ForbiddenError, message
|
2016-11-02 17:50:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-21 14:09:45 -04:00
|
|
|
def check_authentication_abilities!
|
2018-02-06 12:25:36 -05:00
|
|
|
case cmd
|
|
|
|
when *DOWNLOAD_COMMANDS
|
|
|
|
unless authentication_abilities.include?(:download_code) || authentication_abilities.include?(:build_download_code)
|
2020-07-21 14:09:45 -04:00
|
|
|
raise ForbiddenError, error_message(:auth_download)
|
2018-02-06 12:25:36 -05:00
|
|
|
end
|
|
|
|
when *PUSH_COMMANDS
|
|
|
|
unless authentication_abilities.include?(:push_code)
|
2020-07-21 14:09:45 -04:00
|
|
|
raise ForbiddenError, error_message(:auth_upload)
|
2018-02-06 12:25:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-02 10:27:30 -05:00
|
|
|
def check_project_accessibility!
|
2020-10-05 11:08:56 -04:00
|
|
|
raise NotFoundError, not_found_message unless can_read_project?
|
2016-11-02 17:50:44 -04:00
|
|
|
end
|
|
|
|
|
2020-07-21 14:09:45 -04:00
|
|
|
def not_found_message
|
|
|
|
error_message(:project_not_found)
|
|
|
|
end
|
|
|
|
|
2021-06-22 11:06:55 -04:00
|
|
|
def add_container_moved_message!
|
2017-12-08 12:42:43 -05:00
|
|
|
return if redirected_path.nil?
|
2017-06-15 20:03:54 -04:00
|
|
|
|
2021-06-22 11:06:55 -04:00
|
|
|
container_moved = Checks::ContainerMoved.new(repository, user, protocol, redirected_path)
|
2017-06-15 20:03:54 -04:00
|
|
|
|
2021-06-22 11:06:55 -04:00
|
|
|
container_moved.add_message
|
2017-06-15 20:03:54 -04:00
|
|
|
end
|
|
|
|
|
2020-07-21 14:09:45 -04:00
|
|
|
def check_command_disabled!
|
|
|
|
if upload_pack?
|
2017-05-22 14:28:51 -04:00
|
|
|
check_upload_pack_disabled!
|
2020-07-21 14:09:45 -04:00
|
|
|
elsif receive_pack?
|
2017-05-22 14:28:51 -04:00
|
|
|
check_receive_pack_disabled!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_upload_pack_disabled!
|
|
|
|
if http? && upload_pack_disabled_over_http?
|
2020-07-21 14:09:45 -04:00
|
|
|
raise ForbiddenError, error_message(:upload_pack_disabled_over_http)
|
2017-05-22 14:28:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_receive_pack_disabled!
|
|
|
|
if http? && receive_pack_disabled_over_http?
|
2020-07-21 14:09:45 -04:00
|
|
|
raise ForbiddenError, error_message(:receive_pack_disabled_over_http)
|
2017-05-16 12:02:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-21 14:09:45 -04:00
|
|
|
def check_command_existence!
|
2016-11-02 17:50:44 -04:00
|
|
|
unless ALL_COMMANDS.include?(cmd)
|
2020-07-21 14:09:45 -04:00
|
|
|
raise ForbiddenError, error_message(:command_not_allowed)
|
2016-11-02 17:50:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-21 14:09:45 -04:00
|
|
|
def check_db_accessibility!
|
|
|
|
return unless receive_pack?
|
2018-02-02 10:27:30 -05:00
|
|
|
|
|
|
|
if Gitlab::Database.read_only?
|
2020-02-26 13:09:24 -05:00
|
|
|
raise ForbiddenError, push_to_read_only_message
|
2018-02-02 10:27:30 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_repository_existence!
|
2020-07-21 14:09:45 -04:00
|
|
|
raise NotFoundError, no_repo_message unless repository.exists?
|
|
|
|
end
|
|
|
|
|
|
|
|
def no_repo_message
|
|
|
|
error_message(:no_repo)
|
2016-11-11 07:40:28 -05:00
|
|
|
end
|
|
|
|
|
2016-12-20 08:19:07 -05:00
|
|
|
def check_download_access!
|
2020-09-02 11:10:54 -04:00
|
|
|
passed = deploy_key_can_download_code? ||
|
2018-03-29 18:56:35 -04:00
|
|
|
deploy_token? ||
|
2018-02-02 10:27:30 -05:00
|
|
|
user_can_download_code? ||
|
2016-12-20 10:30:01 -05:00
|
|
|
build_can_download_code? ||
|
|
|
|
guest_can_download_code?
|
2016-12-20 08:19:07 -05:00
|
|
|
|
|
|
|
unless passed
|
2020-07-21 14:09:45 -04:00
|
|
|
raise ForbiddenError, download_forbidden_message
|
2016-12-20 08:19:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-21 14:09:45 -04:00
|
|
|
def download_forbidden_message
|
|
|
|
error_message(:download)
|
|
|
|
end
|
|
|
|
|
|
|
|
def project?
|
2020-10-05 11:08:56 -04:00
|
|
|
# Strict nil check, to avoid any surprises with Object#present?
|
|
|
|
# which can delegate to #empty?
|
|
|
|
!project.nil?
|
2020-07-21 14:09:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def project
|
|
|
|
container if container.is_a?(::Project)
|
|
|
|
end
|
|
|
|
|
2018-07-06 16:56:41 -04:00
|
|
|
def check_push_access!
|
2020-10-05 11:08:56 -04:00
|
|
|
if project&.repository_read_only?
|
2020-07-21 14:09:45 -04:00
|
|
|
raise ForbiddenError, error_message(:read_only)
|
2017-09-19 03:44:58 -04:00
|
|
|
end
|
|
|
|
|
2018-03-28 12:54:15 -04:00
|
|
|
if deploy_key?
|
2018-02-06 12:25:36 -05:00
|
|
|
unless deploy_key.can_push_to?(project)
|
2020-07-21 14:09:45 -04:00
|
|
|
raise ForbiddenError, error_message(:deploy_key_upload)
|
2018-02-06 12:25:36 -05:00
|
|
|
end
|
2016-12-20 08:19:07 -05:00
|
|
|
elsif user
|
2018-02-06 12:25:36 -05:00
|
|
|
# User access is verified in check_change_access!
|
2016-12-20 08:19:07 -05:00
|
|
|
else
|
2020-07-21 14:09:45 -04:00
|
|
|
raise ForbiddenError, error_message(:upload)
|
2016-12-20 08:19:07 -05:00
|
|
|
end
|
|
|
|
|
2018-07-06 16:56:41 -04:00
|
|
|
check_change_access!
|
2016-12-20 08:19:07 -05:00
|
|
|
end
|
|
|
|
|
2020-07-21 14:09:45 -04:00
|
|
|
def user_can_push?
|
|
|
|
user_access.can_do_action?(push_ability)
|
|
|
|
end
|
|
|
|
|
2018-07-06 16:56:41 -04:00
|
|
|
def check_change_access!
|
2018-12-20 11:40:56 -05:00
|
|
|
if changes == ANY
|
2021-02-16 04:09:36 -05:00
|
|
|
can_push = deploy_key? ||
|
2020-12-07 13:10:36 -05:00
|
|
|
user_can_push? ||
|
2020-07-21 14:09:45 -04:00
|
|
|
project&.any_branch_allows_collaboration?(user_access.user)
|
2018-03-29 19:10:43 -04:00
|
|
|
|
2018-12-20 11:40:56 -05:00
|
|
|
unless can_push
|
2020-07-21 14:09:45 -04:00
|
|
|
raise ForbiddenError, error_message(:push_code)
|
2018-12-20 11:40:56 -05:00
|
|
|
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.
|
2020-07-21 14:09:45 -04:00
|
|
|
project.repository.clean_stale_repository_files if project.present?
|
2018-12-20 11:40:56 -05:00
|
|
|
|
2021-06-15 14:09:57 -04:00
|
|
|
check_access!
|
2016-11-11 07:40:28 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-15 14:09:57 -04:00
|
|
|
def check_access!
|
|
|
|
Checks::ChangesAccess.new(
|
|
|
|
changes_list.changes,
|
2016-11-17 14:48:23 -05:00
|
|
|
user_access: user_access,
|
|
|
|
project: project,
|
2018-10-22 10:49:20 -04:00
|
|
|
protocol: protocol,
|
|
|
|
logger: logger
|
2020-07-21 14:09:45 -04:00
|
|
|
).validate!
|
2018-10-22 10:49:20 -04:00
|
|
|
rescue Checks::TimedLogger::TimeoutError
|
2018-10-25 04:49:59 -04:00
|
|
|
raise TimeoutError, logger.full_message
|
2016-11-11 09:26:05 -05:00
|
|
|
end
|
|
|
|
|
2016-07-18 04:16:56 -04:00
|
|
|
def deploy_key
|
2016-11-17 14:48:23 -05:00
|
|
|
actor if deploy_key?
|
|
|
|
end
|
|
|
|
|
|
|
|
def deploy_key?
|
|
|
|
actor.is_a?(DeployKey)
|
2014-09-23 07:18:36 -04:00
|
|
|
end
|
2014-11-14 11:23:55 -05:00
|
|
|
|
2018-03-29 18:56:35 -04:00
|
|
|
def deploy_token
|
|
|
|
actor if deploy_token?
|
|
|
|
end
|
|
|
|
|
|
|
|
def deploy_token?
|
|
|
|
actor.is_a?(DeployToken)
|
|
|
|
end
|
|
|
|
|
2017-05-16 15:58:46 -04:00
|
|
|
def ci?
|
|
|
|
actor == :ci
|
|
|
|
end
|
|
|
|
|
2020-03-13 05:09:23 -04:00
|
|
|
def key?
|
|
|
|
actor.is_a?(Key)
|
|
|
|
end
|
|
|
|
|
2016-11-02 17:50:44 -04:00
|
|
|
def can_read_project?
|
2017-05-16 15:58:46 -04:00
|
|
|
if deploy_key?
|
2016-11-17 14:48:23 -05:00
|
|
|
deploy_key.has_access_to?(project)
|
2018-04-05 09:49:18 -04:00
|
|
|
elsif deploy_token?
|
|
|
|
deploy_token.has_access_to?(project)
|
2016-11-11 07:40:28 -05:00
|
|
|
elsif user
|
2016-11-17 14:48:23 -05:00
|
|
|
user.can?(:read_project, project)
|
2017-05-16 15:58:46 -04:00
|
|
|
elsif ci?
|
|
|
|
true # allow CI (build without a user) for backwards compatibility
|
2016-11-17 14:48:23 -05:00
|
|
|
end || Guest.can?(:read_project, project)
|
2016-11-02 17:50:44 -04:00
|
|
|
end
|
|
|
|
|
2017-05-16 12:02:52 -04:00
|
|
|
def http?
|
|
|
|
protocol == 'http'
|
|
|
|
end
|
|
|
|
|
2020-12-10 01:09:47 -05:00
|
|
|
def ssh?
|
|
|
|
protocol == 'ssh'
|
|
|
|
end
|
|
|
|
|
2020-07-21 14:09:45 -04:00
|
|
|
def upload_pack?
|
|
|
|
cmd == 'git-upload-pack'
|
2017-05-16 12:02:52 -04:00
|
|
|
end
|
|
|
|
|
2020-07-21 14:09:45 -04:00
|
|
|
def receive_pack?
|
|
|
|
cmd == 'git-receive-pack'
|
2017-05-16 12:02:52 -04:00
|
|
|
end
|
|
|
|
|
2017-05-22 14:28:51 -04:00
|
|
|
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
|
|
|
|
|
2016-07-13 14:57:30 -04:00
|
|
|
protected
|
|
|
|
|
2020-07-21 14:09:45 -04:00
|
|
|
def error_message(key)
|
2020-10-05 23:08:19 -04:00
|
|
|
self.class.error_message(key)
|
2020-07-21 14:09:45 -04:00
|
|
|
end
|
|
|
|
|
2020-03-13 05:09:23 -04:00
|
|
|
def success_result
|
|
|
|
::Gitlab::GitAccessResult::Success.new(console_messages: check_for_console_messages)
|
2019-04-22 12:07:19 -04:00
|
|
|
end
|
|
|
|
|
2018-07-06 16:56:41 -04:00
|
|
|
def changes_list
|
2018-12-20 11:40:56 -05:00
|
|
|
@changes_list ||= Gitlab::ChangesList.new(changes == ANY ? [] : changes)
|
2018-07-06 16:56:41 -04:00
|
|
|
end
|
|
|
|
|
2016-07-18 04:16:56 -04:00
|
|
|
def user
|
2020-07-21 14:09:45 -04:00
|
|
|
strong_memoize(:user) do
|
2016-07-18 04:16:56 -04:00
|
|
|
case actor
|
|
|
|
when User
|
|
|
|
actor
|
2018-03-28 12:54:15 -04:00
|
|
|
when DeployKey
|
|
|
|
nil
|
2016-07-18 04:16:56 -04:00
|
|
|
when Key
|
2018-03-28 12:54:15 -04:00
|
|
|
actor.user
|
2017-05-16 15:58:46 -04:00
|
|
|
when :ci
|
|
|
|
nil
|
2016-07-18 04:16:56 -04:00
|
|
|
end
|
2020-07-21 14:09:45 -04:00
|
|
|
end
|
2016-07-18 04:16:56 -04:00
|
|
|
end
|
2017-05-23 15:26:46 -04:00
|
|
|
|
|
|
|
def user_access
|
|
|
|
@user_access ||= if ci?
|
|
|
|
CiAccess.new
|
2018-05-10 10:13:05 -04:00
|
|
|
elsif user && request_from_ci_build?
|
2020-07-21 14:09:45 -04:00
|
|
|
BuildAccess.new(user, container: container)
|
2021-02-16 04:09:36 -05:00
|
|
|
elsif deploy_key?
|
2020-12-07 13:10:36 -05:00
|
|
|
DeployKeyAccess.new(deploy_key, container: container)
|
2017-05-23 15:26:46 -04:00
|
|
|
else
|
2020-07-21 14:09:45 -04:00
|
|
|
UserAccess.new(user, container: container)
|
2017-05-23 15:26:46 -04:00
|
|
|
end
|
|
|
|
end
|
2017-12-07 11:13:40 -05:00
|
|
|
|
|
|
|
def push_to_read_only_message
|
2020-07-21 14:09:45 -04:00
|
|
|
error_message(:cannot_push_to_read_only)
|
2017-12-07 11:13:40 -05:00
|
|
|
end
|
2018-02-21 19:20:30 -05:00
|
|
|
|
|
|
|
def repository
|
2020-04-06 20:09:33 -04:00
|
|
|
container&.repository
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_size_before_push!
|
|
|
|
if check_size_limit? && size_checker.above_size_limit?
|
|
|
|
raise ForbiddenError, size_checker.error_message.push_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_push_size!
|
|
|
|
return unless check_size_limit?
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
repository.clean_stale_repository_files
|
|
|
|
|
|
|
|
# Use #check_repository_disk_size to get correct push size whenever a lot of changes
|
|
|
|
# gets pushed at the same time containing the same blobs. This is only
|
|
|
|
# doable if GIT_OBJECT_DIRECTORY_RELATIVE env var is set and happens
|
|
|
|
# when git push comes from CLI (not via UI and API).
|
|
|
|
#
|
|
|
|
# Fallback to determining push size using the changes_list so we can still
|
|
|
|
# determine the push size if env var isn't set (e.g. changes are made
|
|
|
|
# via UI and API).
|
|
|
|
if check_quarantine_size?
|
|
|
|
check_repository_disk_size
|
|
|
|
else
|
|
|
|
check_changes_size
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_quarantine_size?
|
|
|
|
git_env = ::Gitlab::Git::HookEnv.all(repository.gl_repository)
|
|
|
|
|
|
|
|
git_env['GIT_OBJECT_DIRECTORY_RELATIVE'].present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_repository_disk_size
|
|
|
|
check_size_against_limit(repository.object_directory_size)
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_changes_size
|
2021-07-26 23:10:05 -04:00
|
|
|
revs = ['--not', '--all', '--not']
|
|
|
|
revs += changes_list.map { |change| change[:newrev] }
|
2020-04-06 20:09:33 -04:00
|
|
|
|
2021-07-26 23:10:05 -04:00
|
|
|
changes_size = repository.blobs(revs).sum(&:size)
|
2021-06-24 11:07:28 -04:00
|
|
|
|
|
|
|
check_size_against_limit(changes_size)
|
2020-04-06 20:09:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_size_against_limit(size)
|
|
|
|
if size_checker.changes_will_exceed_size_limit?(size)
|
|
|
|
raise ForbiddenError, size_checker.error_message.new_changes_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_size_limit?
|
|
|
|
strong_memoize(:check_size_limit) do
|
|
|
|
changes_list.any? { |change| !Gitlab::Git.blank_ref?(change[:newrev]) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def size_checker
|
|
|
|
container.repository_size_checker
|
2018-02-21 19:20:30 -05:00
|
|
|
end
|
2021-04-01 08:08:56 -04:00
|
|
|
|
|
|
|
# overriden in EE
|
|
|
|
def check_additional_conditions!
|
|
|
|
end
|
2014-03-19 15:01:00 -04:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Gitlab::GitAccess.prepend_mod_with('Gitlab::GitAccess')
|