Welcome to mirror list, hosted at ThFree Co, Russian Federation.

permanent_failures_controller.rb « mailgun « members « controllers « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 685faa3469411e56181c6e23f2002cd05c6763f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
# frozen_string_literal: true

module Members
  module Mailgun
    class PermanentFailuresController < ApplicationController
      respond_to :json

      skip_before_action :authenticate_user!
      skip_before_action :verify_authenticity_token

      before_action :ensure_feature_enabled!
      before_action :authenticate_signature!
      before_action :validate_invite_email!

      feature_category :authentication_and_authorization

      def create
        webhook_processor.execute

        head :ok
      end

      private

      def ensure_feature_enabled!
        render_406 unless Gitlab::CurrentSettings.mailgun_events_enabled?
      end

      def authenticate_signature!
        access_denied! unless valid_signature?
      end

      def valid_signature?
        return false if Gitlab::CurrentSettings.mailgun_signing_key.blank?

        # per this guide: https://documentation.mailgun.com/en/latest/user_manual.html#webhooks
        digest = OpenSSL::Digest.new('SHA256')
        data = [params.dig(:signature, :timestamp), params.dig(:signature, :token)].join

        hmac_digest = OpenSSL::HMAC.hexdigest(digest, Gitlab::CurrentSettings.mailgun_signing_key, data)

        ActiveSupport::SecurityUtils.secure_compare(params.dig(:signature, :signature), hmac_digest)
      end

      def validate_invite_email!
        # permanent_failures webhook does not provide a way to filter failures, so we'll get them all on this endpoint
        # and we only care about our invite_emails
        render_406 unless payload[:tags]&.include?(::Members::Mailgun::INVITE_EMAIL_TAG)
      end

      def webhook_processor
        ::Members::Mailgun::ProcessWebhookService.new(payload)
      end

      def payload
        @payload ||= params.permit!['event-data']
      end

      def render_406
        # failure to stop retries per https://documentation.mailgun.com/en/latest/user_manual.html#webhooks
        head :not_acceptable
      end
    end
  end
end