helpers.rb 14.6 KB
Newer Older
1 2
# frozen_string_literal: true

3 4
module API
  module Helpers
5
    include Gitlab::Utils
6
    include Helpers::Pagination
7

Douwe Maan's avatar
Douwe Maan committed
8
    SUDO_HEADER = "HTTP_SUDO".freeze
9
    SUDO_PARAM = :sudo
10
    API_USER_ENV = 'gitlab.api.user'.freeze
11

12 13 14 15 16
    def declared_params(options = {})
      options = { include_parent_namespaces: false }.merge(options)
      declared(params, options).to_h.symbolize_keys
    end

17 18
    def check_unmodified_since!(last_modified)
      if_unmodified_since = Time.parse(headers['If-Unmodified-Since']) rescue nil
19

Robert Schilling's avatar
Robert Schilling committed
20
      if if_unmodified_since && last_modified && last_modified > if_unmodified_since
21 22 23 24
        render_api_error!('412 Precondition Failed', 412)
      end
    end

25 26 27 28
    def destroy_conditionally!(resource, last_updated: nil)
      last_updated ||= resource.updated_at

      check_unmodified_since!(last_updated)
29 30

      status 204
31

32 33 34 35 36 37 38
      if block_given?
        yield resource
      else
        resource.destroy
      end
    end

39
    # rubocop:disable Gitlab/ModuleWithInstanceVariables
40 41 42 43
    # We can't rewrite this with StrongMemoize because `sudo!` would
    # actually write to `@current_user`, and `sudo?` would immediately
    # call `current_user` again which reads from `@current_user`.
    # We should rewrite this in a way that using StrongMemoize is possible
44
    def current_user
45
      return @current_user if defined?(@current_user)
46

47
      @current_user = initial_current_user
48

49 50
      Gitlab::I18n.locale = @current_user&.preferred_language

51
      sudo!
52

Douwe Maan's avatar
Douwe Maan committed
53 54
      validate_access_token!(scopes: scopes_registered_for_endpoint) unless sudo?

55 56
      save_current_user_in_env(@current_user) if @current_user

57 58
      @current_user
    end
59
    # rubocop:enable Gitlab/ModuleWithInstanceVariables
60

61 62 63 64
    def save_current_user_in_env(user)
      env[API_USER_ENV] = { user_id: user.id, username: user.username }
    end

65 66
    def sudo?
      initial_current_user != current_user
67 68
    end

69 70 71 72
    def user_namespace
      @user_namespace ||= find_namespace!(params[:id])
    end

73 74 75 76
    def user_group
      @group ||= find_group!(params[:id])
    end

77
    def user_project
78
      @project ||= find_project!(params[:id])
79 80
    end

blackst0ne's avatar
blackst0ne committed
81
    def wiki_page
82
      page = ProjectWiki.new(user_project, current_user).find_page(params[:slug])
blackst0ne's avatar
blackst0ne committed
83 84 85 86

      page || not_found!('Wiki Page')
    end

Felipe Artur's avatar
Felipe Artur committed
87
    def available_labels_for(label_parent)
88 89 90 91 92 93 94
      search_params = { include_ancestor_groups: true }

      if label_parent.is_a?(Project)
        search_params[:project_id] = label_parent.id
      else
        search_params.merge!(group_id: label_parent.id, only_group_labels: true)
      end
Felipe Artur's avatar
Felipe Artur committed
95 96

      LabelsFinder.new(current_user, search_params).execute
97 98
    end

99
    def find_user(id)
100
      UserFinder.new(id).find_by_id_or_username
101 102
    end

103
    # rubocop: disable CodeReuse/ActiveRecord
104
    def find_project(id)
105 106
      projects = Project.without_deleted

107
      if id.is_a?(Integer) || id =~ /^\d+$/
108
        projects.find_by(id: id)
109
      elsif id.include?("/")
110
        projects.find_by_full_path(id)
111 112
      end
    end
113
    # rubocop: enable CodeReuse/ActiveRecord
114 115 116

    def find_project!(id)
      project = find_project(id)
117

118
      if can?(current_user, :read_project, project)
119 120
        project
      else
121
        not_found!('Project')
122 123 124
      end
    end

125
    # rubocop: disable CodeReuse/ActiveRecord
126
    def find_group(id)
127
      if id.to_s =~ /^\d+$/
128 129
        Group.find_by(id: id)
      else
130
        Group.find_by_full_path(id)
131 132
      end
    end
133
    # rubocop: enable CodeReuse/ActiveRecord
134 135 136

    def find_group!(id)
      group = find_group(id)
137 138 139 140 141 142 143 144

      if can?(current_user, :read_group, group)
        group
      else
        not_found!('Group')
      end
    end

145
    # rubocop: disable CodeReuse/ActiveRecord
146 147 148 149 150 151 152
    def find_namespace(id)
      if id.to_s =~ /^\d+$/
        Namespace.find_by(id: id)
      else
        Namespace.find_by_full_path(id)
      end
    end
153
    # rubocop: enable CodeReuse/ActiveRecord
154 155 156 157

    def find_namespace!(id)
      namespace = find_namespace(id)

158
      if can?(current_user, :read_namespace, namespace)
159 160 161 162 163 164
        namespace
      else
        not_found!('Namespace')
      end
    end

165
    def find_branch!(branch_name)
Stan Hu's avatar
Stan Hu committed
166 167 168 169 170
      if Gitlab::GitRefValidator.validate(branch_name)
        user_project.repository.find_branch(branch_name) || not_found!('Branch')
      else
        render_api_error!('The branch refname is invalid', 400)
      end
171 172
    end

173
    def find_project_label(id)
Felipe Artur's avatar
Felipe Artur committed
174 175 176
      labels = available_labels_for(user_project)
      label = labels.find_by_id(id) || labels.find_by_title(id)

177 178 179
      label || not_found!('Label')
    end

180
    # rubocop: disable CodeReuse/ActiveRecord
181 182
    def find_project_issue(iid)
      IssuesFinder.new(current_user, project_id: user_project.id).find_by!(iid: iid)
183
    end
184
    # rubocop: enable CodeReuse/ActiveRecord
185

186
    # rubocop: disable CodeReuse/ActiveRecord
187 188
    def find_project_merge_request(iid)
      MergeRequestsFinder.new(current_user, project_id: user_project.id).find_by!(iid: iid)
189
    end
190
    # rubocop: enable CodeReuse/ActiveRecord
191

192 193 194 195
    def find_project_commit(id)
      user_project.commit_by(oid: id)
    end

196
    def find_project_snippet(id)
197
      finder_params = { project: user_project }
198
      SnippetsFinder.new(current_user, finder_params).find(id)
199 200
    end

201
    # rubocop: disable CodeReuse/ActiveRecord
202 203
    def find_merge_request_with_access(iid, access_level = :read_merge_request)
      merge_request = user_project.merge_requests.find_by!(iid: iid)
204 205 206
      authorize! access_level, merge_request
      merge_request
    end
207
    # rubocop: enable CodeReuse/ActiveRecord
208

209 210 211 212
    def find_build!(id)
      user_project.builds.find(id.to_i)
    end

213
    def authenticate!
214
      unauthorized! unless current_user
215 216
    end

217
    def authenticate_non_get!
218
      authenticate! unless %w[GET HEAD].include?(route.request_method)
219 220
    end

221 222 223 224 225 226 227
    def authenticate_by_gitlab_shell_token!
      input = params['secret_token'].try(:chomp)
      unless Devise.secure_compare(secret_token, input)
        unauthorized!
      end
    end

228 229 230 231 232
    def authenticated_with_full_private_access!
      authenticate!
      forbidden! unless current_user.full_private_access?
    end

233
    def authenticated_as_admin!
234
      authenticate!
blackst0ne's avatar
blackst0ne committed
235
      forbidden! unless current_user.admin?
236 237
    end

238
    def authorize!(action, subject = :global)
239
      forbidden! unless can?(current_user, action, subject)
240 241 242 243 244 245 246 247 248 249
    end

    def authorize_push_project
      authorize! :push_code, user_project
    end

    def authorize_admin_project
      authorize! :admin_project, user_project
    end

250 251 252 253 254 255 256 257
    def authorize_read_builds!
      authorize! :read_build, user_project
    end

    def authorize_update_builds!
      authorize! :update_build, user_project
    end

258 259 260 261 262 263
    def require_gitlab_workhorse!
      unless env['HTTP_GITLAB_WORKHORSE'].present?
        forbidden!('Request should be executed via GitLab Workhorse')
      end
    end

264 265 266 267
    def require_pages_enabled!
      not_found! unless user_project.pages_available?
    end

268 269 270 271
    def require_pages_config_enabled!
      not_found! unless Gitlab.config.pages.enabled
    end

272
    def can?(object, action, subject = :global)
http://jneen.net/'s avatar
http://jneen.net/ committed
273
      Ability.allowed?(object, action, subject)
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
    end

    # Checks the occurrences of required attributes, each attribute must be present in the params hash
    # or a Bad Request error is invoked.
    #
    # Parameters:
    #   keys (required) - A hash consisting of keys that must be present
    def required_attributes!(keys)
      keys.each do |key|
        bad_request!(key) unless params[key].present?
      end
    end

    def attributes_for_keys(keys, custom_params = nil)
      params_hash = custom_params || params
      attrs = {}
      keys.each do |key|
291
        if params_hash[key].present? || (params_hash.key?(key) && params_hash[key] == false)
292 293 294
          attrs[key] = params_hash[key]
        end
      end
295
      permitted_attrs = ActionController::Parameters.new(attrs).permit!
Jasper Maes's avatar
Jasper Maes committed
296
      permitted_attrs.to_h
297 298
    end

299
    # rubocop: disable CodeReuse/ActiveRecord
300 301 302
    def filter_by_iid(items, iid)
      items.where(iid: iid)
    end
303
    # rubocop: enable CodeReuse/ActiveRecord
304

305 306 307 308
    def filter_by_search(items, text)
      items.search(text)
    end

309 310 311 312 313 314 315 316 317 318
    # error helpers

    def forbidden!(reason = nil)
      message = ['403 Forbidden']
      message << " - #{reason}" if reason
      render_api_error!(message.join(' '), 403)
    end

    def bad_request!(attribute)
      message = ["400 (Bad request)"]
319
      message << "\"" + attribute.to_s + "\" not given" if attribute
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
      render_api_error!(message.join(' '), 400)
    end

    def not_found!(resource = nil)
      message = ["404"]
      message << resource if resource
      message << "Not Found"
      render_api_error!(message.join(' '), 404)
    end

    def unauthorized!
      render_api_error!('401 Unauthorized', 401)
    end

    def not_allowed!
      render_api_error!('405 Method Not Allowed', 405)
    end

    def conflict!(message = nil)
      render_api_error!(message || '409 Conflict', 409)
    end

    def file_to_large!
      render_api_error!('413 Request Entity Too Large', 413)
    end

    def not_modified!
      render_api_error!('304 Not Modified', 304)
    end

350 351 352 353
    def no_content!
      render_api_error!('204 No Content', 204)
    end

354 355 356 357
    def accepted!
      render_api_error!('202 Accepted', 202)
    end

358 359 360 361 362 363
    def render_validation_error!(model)
      if model.errors.any?
        render_api_error!(model.errors.messages || '400 Bad Request', 400)
      end
    end

364 365 366 367
    def render_spam_error!
      render_api_error!({ error: 'Spam detected' }, 400)
    end

368
    def render_api_error!(message, status)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
369
      error!({ 'message' => message }, status, header)
370 371
    end

Stan Hu's avatar
Stan Hu committed
372
    def handle_api_exception(exception)
373
      if report_exception?(exception)
Stan Hu's avatar
Stan Hu committed
374
        define_params_for_grape_middleware
375 376
        Gitlab::Sentry.context(current_user)
        Gitlab::Sentry.track_acceptable_exception(exception, extra: params)
Stan Hu's avatar
Stan Hu committed
377 378 379 380 381
      end

      # lifted from https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb#L60
      trace = exception.backtrace

382
      message = ["\n#{exception.class} (#{exception.message}):\n"]
Stan Hu's avatar
Stan Hu committed
383 384
      message << exception.annoted_source_code.to_s if exception.respond_to?(:annoted_source_code)
      message << "  " << trace.join("\n  ")
385
      message = message.join
Stan Hu's avatar
Stan Hu committed
386 387

      API.logger.add Logger::FATAL, message
388 389 390 391 392 393 394 395 396

      response_message =
        if Rails.env.test?
          message
        else
          '500 Internal Server Error'
        end

      rack_response({ 'message' => response_message }.to_json, 500)
Stan Hu's avatar
Stan Hu committed
397 398
    end

Markus Koller's avatar
Markus Koller committed
399
    # project helpers
400

401
    # rubocop: disable CodeReuse/ActiveRecord
402
    def reorder_projects(projects)
Robert Schilling's avatar
Robert Schilling committed
403
      projects.reorder(params[:order_by] => params[:sort])
404
    end
405
    # rubocop: enable CodeReuse/ActiveRecord
406

407
    def project_finder_params
408
      finder_params = { without_deleted: true }
409
      finder_params[:owned] = true if params[:owned].present?
410 411 412
      finder_params[:non_public] = true if params[:membership].present?
      finder_params[:starred] = true if params[:starred].present?
      finder_params[:visibility_level] = Gitlab::VisibilityLevel.level_value(params[:visibility]) if params[:visibility]
413
      finder_params[:archived] = archived_param unless params[:archived].nil?
414
      finder_params[:search] = params[:search] if params[:search]
vanadium23's avatar
vanadium23 committed
415
      finder_params[:user] = params.delete(:user) if params[:user]
416
      finder_params[:custom_attributes] = params[:custom_attributes] if params[:custom_attributes]
417
      finder_params[:min_access_level] = params[:min_access_level] if params[:min_access_level]
418 419 420
      finder_params
    end

421 422
    # file helpers

423
    def present_disk_file!(path, filename, content_type = 'application/octet-stream')
424 425 426 427 428 429 430 431 432 433 434
      filename ||= File.basename(path)
      header['Content-Disposition'] = "attachment; filename=#{filename}"
      header['Content-Transfer-Encoding'] = 'binary'
      content_type content_type

      # Support download acceleration
      case headers['X-Sendfile-Type']
      when 'X-Sendfile'
        header['X-Sendfile'] = path
        body
      else
Robert Schilling's avatar
Robert Schilling committed
435
        file path
436 437 438
      end
    end

439 440
    def present_carrierwave_file!(file, supports_direct_download: true)
      return not_found! unless file.exists?
vanadium23's avatar
vanadium23 committed
441

442 443 444 445
      if file.file_storage?
        present_disk_file!(file.path, file.filename)
      elsif supports_direct_download && file.class.direct_download_enabled?
        redirect(file.url)
446
      else
447
        header(*Gitlab::Workhorse.send_url(file.url))
448 449
        status :ok
        body
Kamil Trzcinski's avatar
Kamil Trzcinski committed
450 451 452
      end
    end

453 454
    private

455
    # rubocop:disable Gitlab/ModuleWithInstanceVariables
456 457 458
    def initial_current_user
      return @initial_current_user if defined?(@initial_current_user)

459
      begin
Douwe Maan's avatar
Douwe Maan committed
460
        @initial_current_user = Gitlab::Auth::UniqueIpsLimiter.limit_user! { find_current_user! }
461
      rescue Gitlab::Auth::UnauthorizedError
462
        unauthorized!
463 464
      end
    end
465
    # rubocop:enable Gitlab/ModuleWithInstanceVariables
466 467 468

    def sudo!
      return unless sudo_identifier
Douwe Maan's avatar
Douwe Maan committed
469

Douwe Maan's avatar
Douwe Maan committed
470
      unauthorized! unless initial_current_user
471

blackst0ne's avatar
blackst0ne committed
472
      unless initial_current_user.admin?
473 474 475
        forbidden!('Must be admin to use sudo')
      end

Douwe Maan's avatar
Douwe Maan committed
476 477
      unless access_token
        forbidden!('Must be authenticated using an OAuth or Personal Access Token to use sudo')
478 479
      end

Douwe Maan's avatar
Douwe Maan committed
480 481
      validate_access_token!(scopes: [:sudo])

482
      sudoed_user = find_user(sudo_identifier)
Douwe Maan's avatar
Douwe Maan committed
483
      not_found!("User with ID or username '#{sudo_identifier}'") unless sudoed_user
484

485
      @current_user = sudoed_user # rubocop:disable Gitlab/ModuleWithInstanceVariables
486 487 488
    end

    def sudo_identifier
489
      @sudo_identifier ||= params[SUDO_PARAM] || env[SUDO_HEADER]
490 491
    end

492
    def secret_token
493
      Gitlab::Shell.secret_token
494 495
    end

496 497 498
    def send_git_blob(repository, blob)
      env['api.format'] = :txt
      content_type 'text/plain'
499
      header['Content-Disposition'] = "attachment; filename=#{blob.name.inspect}"
Douwe Maan's avatar
Douwe Maan committed
500
      header(*Gitlab::Workhorse.send_git_blob(repository, blob))
501 502
    end

503 504
    def send_git_archive(repository, **kwargs)
      header(*Gitlab::Workhorse.send_git_archive(repository, **kwargs))
505
    end
506

507 508 509 510
    def send_artifacts_entry(build, entry)
      header(*Gitlab::Workhorse.send_artifacts_entry(build, entry))
    end

511 512 513
    # The Grape Error Middleware only has access to `env` but not `params` nor
    # `request`. We workaround this by defining methods that returns the right
    # values.
Stan Hu's avatar
Stan Hu committed
514
    def define_params_for_grape_middleware
515 516
      self.define_singleton_method(:request) { Rack::Request.new(env) }
      self.define_singleton_method(:params) { request.params.symbolize_keys }
Stan Hu's avatar
Stan Hu committed
517 518 519 520 521 522 523 524 525
    end

    # We could get a Grape or a standard Ruby exception. We should only report anything that
    # is clearly an error.
    def report_exception?(exception)
      return true unless exception.respond_to?(:status)

      exception.status == 500
    end
526 527 528 529 530 531

    def archived_param
      return 'only' if params[:archived]

      params[:archived]
    end
532 533
  end
end