gitlab-org--gitlab-foss/app/controllers/projects/git_http_controller.rb

148 lines
3.3 KiB
Ruby
Raw Normal View History

2016-03-23 13:34:16 -04:00
class Projects::GitHttpController < Projects::ApplicationController
2016-04-25 12:05:05 -04:00
attr_reader :user
2016-03-23 13:34:16 -04:00
skip_before_action :repository
before_action :authenticate_user
2016-04-25 12:05:05 -04:00
before_action :ensure_project_found!
# GET /foo/bar.git/info/refs?service=git-upload-pack (git pull)
# GET /foo/bar.git/info/refs?service=git-receive-pack (git push)
def info_refs
2016-03-23 13:34:16 -04:00
if upload_pack? && upload_pack_allowed?
2016-03-24 13:58:29 -04:00
render_ok
elsif receive_pack? && receive_pack_allowed?
render_ok
else
render_not_found
2016-03-23 13:34:16 -04:00
end
end
2016-04-15 06:40:43 -04:00
# POST /foo/bar.git/git-upload-pack (git pull)
def git_upload_pack
if upload_pack? && upload_pack_allowed?
render_ok
else
render_not_found
end
end
# POST /foo/bar.git/git-receive-pack" (git push)
def git_receive_pack
if receive_pack? && receive_pack_allowed?
render_ok
else
render_not_found
end
2016-03-23 13:34:16 -04:00
end
private
def authenticate_user
return if project && project.public? && upload_pack?
authenticate_or_request_with_http_basic do |login, password|
2016-06-03 08:57:34 -04:00
auth_result = Gitlab::Auth.find(login, password, project: project, ip: request.ip)
2016-03-23 13:34:16 -04:00
2016-06-03 08:57:34 -04:00
if auth_result.type == :ci && upload_pack?
2016-04-29 12:58:55 -04:00
@ci = true
2016-06-03 08:57:34 -04:00
elsif auth_result.type == :oauth && !upload_pack?
# Not allowed
2016-04-29 12:58:55 -04:00
else
2016-06-03 08:57:34 -04:00
@user = auth_result.user
2016-04-29 12:58:55 -04:00
end
2016-06-03 08:57:34 -04:00
ci? || user
2016-03-23 13:34:16 -04:00
end
end
2016-04-25 12:05:05 -04:00
def ensure_project_found!
2016-03-24 13:58:29 -04:00
render_not_found if project.blank?
2016-03-23 13:34:16 -04:00
end
def project
return @project if defined?(@project)
2016-04-22 07:58:40 -04:00
project_id, _ = project_id_with_suffix
if project_id.blank?
@project = nil
else
@project = Project.find_with_namespace("#{params[:namespace_id]}/#{project_id}")
end
2016-03-23 13:34:16 -04:00
end
2016-04-22 07:58:40 -04:00
# This method returns two values so that we can parse
# params[:project_id] (untrusted input!) in exactly one place.
def project_id_with_suffix
id = params[:project_id] || ''
2016-04-15 06:40:43 -04:00
2016-06-03 08:57:34 -04:00
%w[.wiki.git .git].each do |suffix|
2016-04-22 07:58:40 -04:00
if id.end_with?(suffix)
# Be careful to only remove the suffix from the end of 'id'.
# Accidentally removing it from the middle is how security
# vulnerabilities happen!
return [id.slice(0, id.length - suffix.length), suffix]
end
2016-03-23 13:34:16 -04:00
end
2016-04-06 13:25:47 -04:00
2016-04-22 07:58:40 -04:00
# Something is wrong with params[:project_id]; do not pass it on.
[nil, nil]
2016-03-23 13:34:16 -04:00
end
2016-04-06 11:52:12 -04:00
def repository
@repository ||= begin
2016-04-22 07:58:40 -04:00
_, suffix = project_id_with_suffix
if suffix == '.wiki.git'
2016-04-06 11:52:12 -04:00
project.wiki.repository
2016-03-23 13:34:16 -04:00
else
2016-04-06 11:52:12 -04:00
project.repository
2016-03-23 13:34:16 -04:00
end
end
end
def upload_pack?
git_command == 'git-upload-pack'
2016-03-24 13:58:29 -04:00
end
def receive_pack?
git_command == 'git-receive-pack'
2016-03-24 13:58:29 -04:00
end
def git_command
2016-03-23 13:34:16 -04:00
if action_name == 'info_refs'
2016-03-24 13:58:29 -04:00
params[:service]
2016-03-23 13:34:16 -04:00
else
2016-06-03 08:57:34 -04:00
action_name.dasherize
2016-03-23 13:34:16 -04:00
end
end
2016-04-15 06:40:43 -04:00
2016-03-23 13:34:16 -04:00
def render_ok
2016-04-06 11:52:12 -04:00
render json: Gitlab::Workhorse.git_http_ok(repository, user)
2016-03-23 13:34:16 -04:00
end
2016-04-15 06:40:43 -04:00
2016-03-23 13:34:16 -04:00
def render_not_found
render text: 'Not Found', status: :not_found
end
def ci?
2016-06-03 09:49:52 -04:00
@ci.present?
2016-03-23 13:34:16 -04:00
end
2016-04-15 06:40:43 -04:00
2016-03-23 13:34:16 -04:00
def upload_pack_allowed?
return false unless Gitlab.config.gitlab_shell.upload_pack
if user
2016-03-23 13:34:16 -04:00
Gitlab::GitAccess.new(user, project).download_access_check.allowed?
else
ci? || project.public?
2016-03-23 13:34:16 -04:00
end
end
2016-03-24 13:58:29 -04:00
def receive_pack_allowed?
return false unless Gitlab.config.gitlab_shell.receive_pack
# Skip user authorization on upload request.
# It will be done by the pre-receive hook in the repository.
user.present?
2016-03-24 13:58:29 -04:00
end
2016-03-23 13:34:16 -04:00
end