summaryrefslogtreecommitdiff
path: root/app/controllers/concerns/spammable_actions/captcha_check/html_format_actions_support.rb
blob: 23db6a4b368b548f4f16d069ecb984f18f9f772e (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
# frozen_string_literal: true

# This module should *ONLY* be included if needed to support forms submits with HTML MIME type.
# In other words, forms handled by actions which use a `respond_to` of `format.html`.
#
# If the request is handled by actions via `format.json`, for example, for all Javascript based form
# submissions and Vue components which use Apollo and Axios, then the corresponding module
# which supports JSON format should be used instead.
module SpammableActions::CaptchaCheck::HtmlFormatActionsSupport
  extend ActiveSupport::Concern
  include SpammableActions::CaptchaCheck::Common

  included do
    before_action :convert_html_spam_params_to_headers, only: [:create, :update]
  end

  private

  def with_captcha_check_html_format(spammable:, &block)
    captcha_render_lambda = -> { render :captcha_check }
    with_captcha_check_common(spammable: spammable, captcha_render_lambda: captcha_render_lambda, &block)
  end

  # Convert spam/CAPTCHA values from form field params to headers, because all spam-related services
  # expect these values to be passed as headers.
  #
  # The 'g-recaptcha-response' field name comes from `Recaptcha::Adapters::ViewMethods#recaptcha_tags` in the
  # recaptcha gem. This is a field which is automatically included by calling the
  # `#recaptcha_tags` method within a HAML template's form.
  def convert_html_spam_params_to_headers
    request.headers['X-GitLab-Captcha-Response'] = params['g-recaptcha-response'] if params['g-recaptcha-response']
    request.headers['X-GitLab-Spam-Log-Id'] = params[:spam_log_id] if params[:spam_log_id]
  end
end

SpammableActions::CaptchaCheck::HtmlFormatActionsSupport.prepend_mod