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

270 lines
7.1 KiB
Ruby
Raw Normal View History

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
2016-11-24 13:41:30 +05:30
UnauthorizedError = Class.new(StandardError)
2017-09-10 17:25:29 +05:30
NotFoundError = Class.new(StandardError)
ProjectMovedError = Class.new(NotFoundError)
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.',
2017-08-17 22:00:37 +05:30
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.',
account_blocked: 'Your account has been blocked.',
command_not_allowed: "The command you're trying to execute is not allowed.",
upload_pack_disabled_over_http: 'Pulling over HTTP is not allowed.',
receive_pack_disabled_over_http: 'Pushing over HTTP is not allowed.'
2017-08-17 22:00:37 +05:30
}.freeze
2016-11-24 13:41:30 +05:30
2017-08-17 22:00:37 +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
2017-09-10 17:25:29 +05:30
attr_reader :actor, :project, :protocol, :authentication_abilities, :redirected_path
2014-09-02 18:07:02 +05:30
2017-09-10 17:25:29 +05:30
def initialize(actor, project, protocol, authentication_abilities:, redirected_path: nil)
2015-04-26 12:48:37 +05:30
@actor = actor
@project = project
2016-08-24 12:49:21 +05:30
@protocol = protocol
2017-09-10 17:25:29 +05:30
@redirected_path = redirected_path
2016-09-29 09:46:39 +05:30
@authentication_abilities = authentication_abilities
2015-04-26 12:48:37 +05:30
end
2016-09-13 17:45:13 +05:30
def check(cmd, changes)
2016-11-24 13:41:30 +05:30
check_protocol!
check_active_user!
check_project_accessibility!
2017-09-10 17:25:29 +05:30
check_project_moved!
check_command_disabled!(cmd)
2016-11-24 13:41:30 +05:30
check_command_existence!(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
2017-08-17 22:00:37 +05:30
check_push_access!(changes)
2014-09-02 18:07:02 +05:30
end
2016-11-24 13:41:30 +05:30
2017-09-10 17:25:29 +05:30
true
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
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
2016-11-24 13:41:30 +05:30
def check_protocol!
unless protocol_allowed?
raise UnauthorizedError, "Git access over #{protocol.upcase} is not allowed"
end
end
def check_active_user!
2017-08-17 22:00:37 +05:30
return if deploy_key?
2016-11-24 13:41:30 +05:30
if user && !user_access.allowed?
2017-09-10 17:25:29 +05:30
raise UnauthorizedError, ERROR_MESSAGES[:account_blocked]
2016-11-24 13:41:30 +05:30
end
end
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
def check_project_moved!
return unless redirected_path
url = protocol == 'ssh' ? project.ssh_url_to_repo : project.http_url_to_repo
message = <<-MESSAGE.strip_heredoc
Project '#{redirected_path}' was moved to '#{project.full_path}'.
Please update your Git remote and try again:
git remote set-url origin #{url}
MESSAGE
raise ProjectMovedError, message
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?
raise UnauthorizedError, ERROR_MESSAGES[:upload_pack_disabled_over_http]
end
end
def check_receive_pack_disabled!
if http? && receive_pack_disabled_over_http?
raise UnauthorizedError, 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)
2017-09-10 17:25:29 +05:30
raise UnauthorizedError, ERROR_MESSAGES[:command_not_allowed]
2016-11-24 13:41:30 +05:30
end
end
2017-08-17 22:00:37 +05:30
def check_repository_existence!
unless project.repository.exists?
raise UnauthorizedError, ERROR_MESSAGES[:no_repo]
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!
return if deploy_key?
passed = user_can_download_code? ||
build_can_download_code? ||
guest_can_download_code?
unless passed
raise UnauthorizedError, ERROR_MESSAGES[:download]
end
2015-04-26 12:48:37 +05:30
end
2017-08-17 22:00:37 +05:30
def check_push_access!(changes)
2016-08-24 12:49:21 +05:30
if deploy_key
2017-08-17 22:00:37 +05:30
check_deploy_key_push_access!
elsif user
check_user_push_access!
2015-04-26 12:48:37 +05:30
else
2017-08-17 22:00:37 +05:30
raise UnauthorizedError, ERROR_MESSAGES[:upload]
2015-04-26 12:48:37 +05:30
end
2017-08-17 22:00:37 +05:30
return if changes.blank? # Allow access.
check_change_access!(changes)
2015-04-26 12:48:37 +05:30
end
2017-08-17 22:00:37 +05:30
def check_user_push_access!
unless authentication_abilities.include?(:push_code)
raise UnauthorizedError, ERROR_MESSAGES[:upload]
end
end
def check_deploy_key_push_access!
unless deploy_key.can_push_to?(project)
raise UnauthorizedError, ERROR_MESSAGES[:deploy_key_upload]
end
end
def check_change_access!(changes)
changes_list = Gitlab::ChangesList.new(changes)
# Iterate over all changes to find if user allowed all of them to be applied
changes_list.each do |change|
2017-09-10 17:25:29 +05:30
# 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)
2016-11-24 13:41:30 +05:30
end
end
2017-08-17 22:00:37 +05:30
def check_single_change_access(change)
Checks::ChangeAccess.new(
change,
user_access: user_access,
project: project,
skip_authorization: deploy_key?,
protocol: protocol
).exec
end
def matching_merge_request?(newrev, branch_name)
Checks::MatchingMergeRequest.new(newrev, branch_name, project).match?
end
def deploy_key
actor if deploy_key?
end
def deploy_key?
actor.is_a?(DeployKey)
end
2017-09-10 17:25:29 +05:30
def ci?
actor == :ci
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)
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
2016-08-24 12:49:21 +05:30
def user
return @user if defined?(@user)
@user =
case actor
when User
actor
when Key
2017-09-10 17:25:29 +05:30
actor.user unless actor.is_a?(DeployKey)
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
else
UserAccess.new(user, project: project)
end
2015-04-26 12:48:37 +05:30
end
2014-09-02 18:07:02 +05:30
end
end