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

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

9 10
    SUDO_HEADER = "HTTP_SUDO"
    GITLAB_SHARED_SECRET_HEADER = "Gitlab-Shared-Secret"
11
    SUDO_PARAM = :sudo
12
    API_USER_ENV = 'gitlab.api.user'
13
    API_EXCEPTION_ENV = 'gitlab.api.exception'
14

15 16 17 18 19
    def declared_params(options = {})
      options = { include_parent_namespaces: false }.merge(options)
      declared(params, options).to_h.symbolize_keys
    end

20 21
    def check_unmodified_since!(last_modified)
      if_unmodified_since = Time.parse(headers['If-Unmodified-Since']) rescue nil
22

23
      if if_unmodified_since && last_modified && last_modified > if_unmodified_since
24 25 26 27
        render_api_error!('412 Precondition Failed', 412)
      end
    end

28 29 30 31
    def destroy_conditionally!(resource, last_updated: nil)
      last_updated ||= resource.updated_at

      check_unmodified_since!(last_updated)
32 33

      status 204
34
      body false
35

36 37 38 39 40 41 42
      if block_given?
        yield resource
      else
        resource.destroy
      end
    end

43 44 45 46 47
    # rubocop:disable Gitlab/ModuleWithInstanceVariables
    # 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
Nihad Abbasov's avatar
Nihad Abbasov committed
48
    def current_user
49
      return @current_user if defined?(@current_user)
50

51
      @current_user = initial_current_user
52

53 54
      Gitlab::I18n.locale = @current_user&.preferred_language

55
      sudo!
56

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

59 60
      save_current_user_in_env(@current_user) if @current_user

61 62
      @current_user
    end
63
    # rubocop:enable Gitlab/ModuleWithInstanceVariables
64

65 66 67 68
    def save_current_user_in_env(user)
      env[API_USER_ENV] = { user_id: user.id, username: user.username }
    end

69 70
    def sudo?
      initial_current_user != current_user
Nihad Abbasov's avatar
Nihad Abbasov committed
71 72
    end

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

Nihad Abbasov's avatar
Nihad Abbasov committed
77
    def user_project
78
      @project ||= find_project!(params[:id])
79 80
    end

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

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

87
    def available_labels_for(label_parent, include_ancestor_groups: true)
88
      search_params = { include_ancestor_groups: include_ancestor_groups }
89 90 91 92 93 94

      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
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
        project
120
      else
121
        not_found!('Project')
122
      end
Nihad Abbasov's avatar
Nihad Abbasov committed
123 124
    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

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

145 146 147 148 149 150
    def check_namespace_access(namespace)
      return namespace if can?(current_user, :read_namespace, namespace)

      not_found!('Namespace')
    end

151
    # rubocop: disable CodeReuse/ActiveRecord
152 153 154 155 156 157 158
    def find_namespace(id)
      if id.to_s =~ /^\d+$/
        Namespace.find_by(id: id)
      else
        Namespace.find_by_full_path(id)
      end
    end
159
    # rubocop: enable CodeReuse/ActiveRecord
160 161

    def find_namespace!(id)
162 163
      check_namespace_access(find_namespace(id))
    end
164

165 166 167 168 169 170
    def find_namespace_by_path(path)
      Namespace.find_by_full_path(path)
    end

    def find_namespace_by_path!(path)
      check_namespace_access(find_namespace_by_path(path))
171 172
    end

173
    def find_branch!(branch_name)
Stan Hu's avatar
Stan Hu committed
174 175 176 177 178
      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
179 180
    end

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

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

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

197
    # rubocop: disable CodeReuse/ActiveRecord
198 199
    def find_merge_request_with_access(iid, access_level = :read_merge_request)
      merge_request = user_project.merge_requests.find_by!(iid: iid)
200 201 202
      authorize! access_level, merge_request
      merge_request
    end
203
    # rubocop: enable CodeReuse/ActiveRecord
204

205 206 207 208
    def find_build!(id)
      user_project.builds.find(id.to_i)
    end

Nihad Abbasov's avatar
Nihad Abbasov committed
209
    def authenticate!
210
      unauthorized! unless current_user
Nihad Abbasov's avatar
Nihad Abbasov committed
211
    end
randx's avatar
randx committed
212

213
    def authenticate_non_get!
214
      authenticate! unless %w[GET HEAD].include?(route.request_method)
215 216
    end

217
    def authenticate_by_gitlab_shell_token!
218 219 220 221 222 223
      input = params['secret_token']
      input ||= Base64.decode64(headers[GITLAB_SHARED_SECRET_HEADER]) if headers.key?(GITLAB_SHARED_SECRET_HEADER)

      input&.chomp!

      unauthorized! unless Devise.secure_compare(secret_token, input)
224 225
    end

226
    def authenticated_with_can_read_all_resources!
227
      authenticate!
228
      forbidden! unless current_user.can_read_all_resources?
229 230
    end

231
    def authenticated_as_admin!
232
      authenticate!
233
      forbidden! unless current_user.admin?
234 235
    end

236 237
    def authorize!(action, subject = :global, reason = nil)
      forbidden!(reason) unless can?(current_user, action, subject)
randx's avatar
randx committed
238 239
    end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
240 241 242 243
    def authorize_push_project
      authorize! :push_code, user_project
    end

Manoj MJ's avatar
Manoj MJ committed
244 245 246 247
    def authorize_admin_tag
      authorize! :admin_tag, user_project
    end

248 249 250 251
    def authorize_admin_project
      authorize! :admin_project, user_project
    end

252 253 254 255
    def authorize_read_builds!
      authorize! :read_build, user_project
    end

256 257 258 259
    def authorize_destroy_artifacts!
      authorize! :destroy_artifacts, user_project
    end

260 261 262 263
    def authorize_update_builds!
      authorize! :update_build, user_project
    end

264 265 266 267
    def require_repository_enabled!(subject = :global)
      not_found!("Repository") unless user_project.feature_available?(:repository, current_user)
    end

268
    def require_gitlab_workhorse!
269 270
      verify_workhorse_api!

271
      unless env['HTTP_GITLAB_WORKHORSE'].present?
272 273 274 275
        forbidden!('Request should be executed via GitLab Workhorse')
      end
    end

276 277 278 279 280 281 282 283
    def verify_workhorse_api!
      Gitlab::Workhorse.verify_api_request!(request.headers)
    rescue => e
      Gitlab::ErrorTracking.track_exception(e)

      forbidden!
    end

284 285 286 287
    def require_pages_enabled!
      not_found! unless user_project.pages_available?
    end

288 289 290 291
    def require_pages_config_enabled!
      not_found! unless Gitlab.config.pages.enabled
    end

292
    def can?(object, action, subject = :global)
293
      Ability.allowed?(object, action, subject)
294 295
    end

296 297 298 299 300 301 302 303 304 305 306
    # 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

Valery Sizov's avatar
Valery Sizov committed
307
    def attributes_for_keys(keys, custom_params = nil)
308
      params_hash = custom_params || params
Alex Denisov's avatar
Alex Denisov committed
309 310
      attrs = {}
      keys.each do |key|
311
        if params_hash[key].present? || (params_hash.key?(key) && params_hash[key] == false)
312
          attrs[key] = params_hash[key]
313
        end
Alex Denisov's avatar
Alex Denisov committed
314
      end
315
      permitted_attrs = ActionController::Parameters.new(attrs).permit!
Jasper Maes's avatar
Jasper Maes committed
316
      permitted_attrs.to_h
Alex Denisov's avatar
Alex Denisov committed
317 318
    end

319
    # rubocop: disable CodeReuse/ActiveRecord
320 321 322
    def filter_by_iid(items, iid)
      items.where(iid: iid)
    end
323
    # rubocop: enable CodeReuse/ActiveRecord
324

325 326 327 328 329 330
    # rubocop: disable CodeReuse/ActiveRecord
    def filter_by_title(items, title)
      items.where(title: title)
    end
    # rubocop: enable CodeReuse/ActiveRecord

331 332 333 334
    def filter_by_search(items, text)
      items.search(text)
    end

335 336
    def order_options_with_tie_breaker
      order_options = { params[:order_by] => params[:sort] }
337
      order_options['id'] ||= params[:sort] || 'asc'
338 339 340
      order_options
    end

341 342
    # error helpers

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

349 350
    def bad_request!(attribute)
      message = ["400 (Bad request)"]
351
      message << "\"" + attribute.to_s + "\" not given" if attribute
352 353 354
      render_api_error!(message.join(' '), 400)
    end

355 356 357 358
    def not_found!(resource = nil)
      message = ["404"]
      message << resource if resource
      message << "Not Found"
Alex Denisov's avatar
Alex Denisov committed
359
      render_api_error!(message.join(' '), 404)
360 361 362
    end

    def unauthorized!
Alex Denisov's avatar
Alex Denisov committed
363
      render_api_error!('401 Unauthorized', 401)
364 365 366
    end

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

Shinya Maeda's avatar
Shinya Maeda committed
370 371 372 373
    def service_unavailable!
      render_api_error!('503 Service Unavailable', 503)
    end

374 375 376 377
    def conflict!(message = nil)
      render_api_error!(message || '409 Conflict', 409)
    end

378
    def file_too_large!
379 380 381
      render_api_error!('413 Request Entity Too Large', 413)
    end

382
    def not_modified!
383
      render_api_error!('304 Not Modified', 304)
384 385
    end

386 387 388 389
    def no_content!
      render_api_error!('204 No Content', 204)
    end

390 391 392 393
    def created!
      render_api_error!('201 Created', 201)
    end

394 395 396 397
    def accepted!
      render_api_error!('202 Accepted', 202)
    end

398
    def render_validation_error!(model)
399
      if model.errors.any?
400 401
        render_api_error!(model.errors.messages || '400 Bad Request', 400)
      end
Alex Denisov's avatar
Alex Denisov committed
402 403
    end

404 405 406 407
    def render_spam_error!
      render_api_error!({ error: 'Spam detected' }, 400)
    end

Alex Denisov's avatar
Alex Denisov committed
408
    def render_api_error!(message, status)
409
      error!({ 'message' => message }, status, header)
410 411
    end

Stan Hu's avatar
Stan Hu committed
412
    def handle_api_exception(exception)
413
      if report_exception?(exception)
Stan Hu's avatar
Stan Hu committed
414
        define_params_for_grape_middleware
415 416
        Gitlab::ErrorTracking.with_context(current_user) do
          Gitlab::ErrorTracking.track_exception(exception, params)
417
        end
Stan Hu's avatar
Stan Hu committed
418 419
      end

420 421 422
      # This is used with GrapeLogging::Loggers::ExceptionLogger
      env[API_EXCEPTION_ENV] = exception

Stan Hu's avatar
Stan Hu committed
423 424 425
      # lifted from https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb#L60
      trace = exception.backtrace

426
      message = ["\n#{exception.class} (#{exception.message}):\n"]
Stan Hu's avatar
Stan Hu committed
427 428
      message << exception.annoted_source_code.to_s if exception.respond_to?(:annoted_source_code)
      message << "  " << trace.join("\n  ")
429
      message = message.join
Stan Hu's avatar
Stan Hu committed
430 431

      API.logger.add Logger::FATAL, message
432 433 434 435 436 437 438 439 440

      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
441 442
    end

443
    # project helpers
Valery Sizov's avatar
Valery Sizov committed
444

445
    # rubocop: disable CodeReuse/ActiveRecord
446
    def reorder_projects(projects)
447
      projects.reorder(order_options_with_tie_breaker)
Valery Sizov's avatar
Valery Sizov committed
448
    end
449
    # rubocop: enable CodeReuse/ActiveRecord
Valery Sizov's avatar
Valery Sizov committed
450

451
    def project_finder_params
452
      project_finder_params_ce.merge(project_finder_params_ee)
453 454
    end

455 456
    # file helpers

457
    def present_disk_file!(path, filename, content_type = 'application/octet-stream')
458
      filename ||= File.basename(path)
Heinrich Lee Yu's avatar
Heinrich Lee Yu committed
459
      header['Content-Disposition'] = ActionDispatch::Http::ContentDisposition.format(disposition: 'attachment', filename: filename)
460 461 462 463 464 465 466 467 468
      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
469
        file path
470 471 472
      end
    end

473
    def present_carrierwave_file!(file, supports_direct_download: true)
474
      return not_found! unless file&.exists?
vanadium23's avatar
vanadium23 committed
475

476 477 478 479
      if file.file_storage?
        present_disk_file!(file.path, file.filename)
      elsif supports_direct_download && file.class.direct_download_enabled?
        redirect(file.url)
480
      else
481
        header(*Gitlab::Workhorse.send_url(file.url))
482 483
        status :ok
        body
Kamil Trzcinski's avatar
Kamil Trzcinski committed
484 485 486
      end
    end

487 488 489 490 491 492 493 494 495 496 497
    def track_event(action = action_name, **args)
      category = args.delete(:category) || self.options[:for].name
      raise "invalid category" unless category

      ::Gitlab::Tracking.event(category, action.to_s, **args)
    rescue => error
      Rails.logger.warn( # rubocop:disable Gitlab/RailsLogger
        "Tracking event failed for action: #{action}, category: #{category}, message: #{error.message}"
      )
    end

498 499 500 501 502 503 504 505 506 507 508 509 510
    protected

    def project_finder_params_ce
      finder_params = { without_deleted: true }
      finder_params[:owned] = true if params[:owned].present?
      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]
      finder_params[:archived] = archived_param unless params[:archived].nil?
      finder_params[:search] = params[:search] if params[:search]
      finder_params[:user] = params.delete(:user) if params[:user]
      finder_params[:custom_attributes] = params[:custom_attributes] if params[:custom_attributes]
      finder_params[:min_access_level] = params[:min_access_level] if params[:min_access_level]
511 512
      finder_params[:id_after] = params[:id_after] if params[:id_after]
      finder_params[:id_before] = params[:id_before] if params[:id_before]
513 514 515 516 517 518 519 520
      finder_params
    end

    # Overridden in EE
    def project_finder_params_ee
      {}
    end

521
    private
randx's avatar
randx committed
522

523
    # rubocop:disable Gitlab/ModuleWithInstanceVariables
524
    def initial_current_user
525
      return @initial_current_user if defined?(@initial_current_user)
526

527
      begin
528
        @initial_current_user = Gitlab::Auth::UniqueIpsLimiter.limit_user! { find_current_user! }
529
      rescue Gitlab::Auth::UnauthorizedError
530
        unauthorized!
531 532
      end
    end
533
    # rubocop:enable Gitlab/ModuleWithInstanceVariables
534 535 536

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

538
      unauthorized! unless initial_current_user
539

540
      unless initial_current_user.admin?
541 542 543
        forbidden!('Must be admin to use sudo')
      end

Douwe Maan's avatar
Douwe Maan committed
544 545
      unless access_token
        forbidden!('Must be authenticated using an OAuth or Personal Access Token to use sudo')
546 547
      end

Douwe Maan's avatar
Douwe Maan committed
548 549
      validate_access_token!(scopes: [:sudo])

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

553
      @current_user = sudoed_user # rubocop:disable Gitlab/ModuleWithInstanceVariables
554 555 556
    end

    def sudo_identifier
557
      @sudo_identifier ||= params[SUDO_PARAM] || env[SUDO_HEADER]
558 559
    end

560
    def secret_token
561
      Gitlab::Shell.secret_token
562
    end
Vinnie Okada's avatar
Vinnie Okada committed
563

564 565 566
    def send_git_blob(repository, blob)
      env['api.format'] = :txt
      content_type 'text/plain'
Heinrich Lee Yu's avatar
Heinrich Lee Yu committed
567
      header['Content-Disposition'] = ActionDispatch::Http::ContentDisposition.format(disposition: 'inline', filename: blob.name)
568 569 570 571

      # Let Workhorse examine the content and determine the better content disposition
      header[Gitlab::Workhorse::DETECT_HEADER] = "true"

Douwe Maan's avatar
Douwe Maan committed
572
      header(*Gitlab::Workhorse.send_git_blob(repository, blob))
573 574
    end

575 576
    def send_git_archive(repository, **kwargs)
      header(*Gitlab::Workhorse.send_git_archive(repository, **kwargs))
577
    end
578

579 580 581 582
    def send_artifacts_entry(build, entry)
      header(*Gitlab::Workhorse.send_artifacts_entry(build, entry))
    end

583 584 585
    # 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
586
    def define_params_for_grape_middleware
587
      self.define_singleton_method(:request) { ActionDispatch::Request.new(env) }
588
      self.define_singleton_method(:params) { request.params.symbolize_keys }
Stan Hu's avatar
Stan Hu committed
589 590 591 592 593 594 595 596 597
    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
598 599 600 601 602 603

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

      params[:archived]
    end
604 605 606 607

    def ip_address
      env["action_dispatch.remote_ip"].to_s || request.ip
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
608 609
  end
end
610

611
API::Helpers.prepend_if_ee('EE::API::Helpers')