git_http_client_controller.rb 3.8 KB
Newer Older
J
Jacob Vosmaer 已提交
1 2 3 4 5 6
# This file should be identical in GitLab Community Edition and Enterprise Edition

class Projects::GitHttpClientController < Projects::ApplicationController
  include ActionController::HttpAuthentication::Basic
  include KerberosSpnegoHelper

7
  attr_reader :actor
J
Jacob Vosmaer 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

  # Git clients will not know what authenticity token to send along
  skip_before_action :verify_authenticity_token
  skip_before_action :repository
  before_action :authenticate_user
  before_action :ensure_project_found!

  private

  def authenticate_user
    if project && project.public? && download_request?
      return # Allow access
    end

    if allow_basic_auth? && basic_auth_provided?
      login, password = user_name_and_password(request)
P
Patricio Cano 已提交
24

25
      if handle_basic_authentication(login, password)
J
Jacob Vosmaer 已提交
26 27 28
        return # Allow access
      end
    elsif allow_kerberos_spnego_auth? && spnego_provided?
29
      @actor = find_kerberos_user
J
Jacob Vosmaer 已提交
30

31
      if actor
J
Jacob Vosmaer 已提交
32 33 34 35 36 37 38
        send_final_spnego_response
        return # Allow access
      end
    end

    send_challenges
    render plain: "HTTP Basic: Access denied\n", status: 401
39
  rescue Gitlab::Auth::MissingPersonalTokenError
P
Patricio Cano 已提交
40
    render_missing_personal_token
J
Jacob Vosmaer 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  end

  def basic_auth_provided?
    has_basic_credentials?(request)
  end

  def send_challenges
    challenges = []
    challenges << 'Basic realm="GitLab"' if allow_basic_auth?
    challenges << spnego_challenge if allow_kerberos_spnego_auth?
    headers['Www-Authenticate'] = challenges.join("\n") if challenges.any?
  end

  def ensure_project_found!
    render_not_found if project.blank?
  end

  def project
    return @project if defined?(@project)

    project_id, _ = project_id_with_suffix
    if project_id.blank?
      @project = nil
    else
      @project = Project.find_with_namespace("#{params[:namespace_id]}/#{project_id}")
    end
  end

  # 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] || ''

    %w[.wiki.git .git].each do |suffix|
      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
    end

    # Something is wrong with params[:project_id]; do not pass it on.
    [nil, nil]
  end

87
  def render_missing_personal_token
88 89
    render plain: "HTTP Basic: Access denied\n" \
                  "You have 2FA enabled, please use a personal access token for Git over HTTP.\n" \
90 91
                  "You can generate one at #{profile_personal_access_tokens_url}",
           status: 401
92 93
  end

J
Jacob Vosmaer 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107
  def repository
    _, suffix = project_id_with_suffix
    if suffix == '.wiki.git'
      project.wiki.repository
    else
      project.repository
    end
  end

  def render_not_found
    render plain: 'Not Found', status: :not_found
  end

  def ci?
108
    @ci
J
Jacob Vosmaer 已提交
109
  end
110

111 112 113 114
  def user
    @actor
  end

115
  def handle_basic_authentication(login, password)
P
Patricio Cano 已提交
116 117
    auth_result = Gitlab::Auth.find_for_git_client(login, password, project: project, ip: request.ip)

118 119
    case auth_result.type
    when :ci
120 121 122 123 124
      if download_request?
        @ci = true
      else
        return false
      end
125
    when :oauth
126 127 128 129 130
      if download_request?
        @actor = auth_result.actor
      else
        return false
      end
131 132 133
    when :lfs_deploy_token
      if download_request?
        @lfs_deploy_key = true
134
        @actor = auth_result.actor
135 136
      end
    when :lfs_token, :personal_token, :gitlab_or_ldap
137
      @actor = auth_result.actor
P
Patricio Cano 已提交
138
    else
139
      # Not allowed
140
      return false
P
Patricio Cano 已提交
141
    end
142 143

    true
P
Patricio Cano 已提交
144 145 146
  end

  def lfs_deploy_key?
147
    @lfs_deploy_key && actor && actor.projects.include?(project)
P
Patricio Cano 已提交
148 149
  end

150 151 152
  def verify_workhorse_api!
    Gitlab::Workhorse.verify_api_request!(request.headers)
  end
J
Jacob Vosmaer 已提交
153
end