Class: Spam::SpamParams

Inherits:
Object
  • Object
show all
Defined in:
app/services/spam/spam_params.rb

Overview

This class is a Parameter Object (refactoring.com/catalog/introduceParameterObject.html) which acts as an container abstraction for multiple values related to spam and captcha processing for a provided HTTP request object.

It is used to encapsulate these values and allow them to be passed from the Controller/GraphQL layers down into to the Service layer, without needing to pass the entire request and therefore unnecessarily couple the Service layer to the HTTP request.

Values contained are:

captcha_response: The response resulting from the user solving a captcha. Currently it is

a scalar reCAPTCHA response string, but it can be expanded to an object in the future to
support other captcha implementations such as FriendlyCaptcha. Obtained from
request.headers['X-GitLab-Captcha-Response']

spam_log_id: The id of a SpamLog record. Obtained from request.headers ip_address = The remote IP. Obtained from request.env user_agent = The user agent. Obtained from request.env referer = The HTTP referer. Obtained from request.env

NOTE: The presence of these values in the request is not currently enforced. If they are missing,

then the spam check may fail, or the SpamLog or UserAgentDetail may have missing fields.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(captcha_response:, spam_log_id:, ip_address:, user_agent:, referer:) ⇒ SpamParams

Returns a new instance of SpamParams.



40
41
42
43
44
45
46
# File 'app/services/spam/spam_params.rb', line 40

def initialize(captcha_response:, spam_log_id:, ip_address:, user_agent:, referer:)
  @captcha_response = captcha_response
  @spam_log_id = spam_log_id
  @ip_address = ip_address
  @user_agent = user_agent
  @referer = referer
end

Instance Attribute Details

#captcha_responseObject (readonly)

Returns the value of attribute captcha_response.



38
39
40
# File 'app/services/spam/spam_params.rb', line 38

def captcha_response
  @captcha_response
end

#ip_addressObject (readonly)

Returns the value of attribute ip_address.



38
39
40
# File 'app/services/spam/spam_params.rb', line 38

def ip_address
  @ip_address
end

#refererObject (readonly)

Returns the value of attribute referer.



38
39
40
# File 'app/services/spam/spam_params.rb', line 38

def referer
  @referer
end

#spam_log_idObject (readonly)

Returns the value of attribute spam_log_id.



38
39
40
# File 'app/services/spam/spam_params.rb', line 38

def spam_log_id
  @spam_log_id
end

#user_agentObject (readonly)

Returns the value of attribute user_agent.



38
39
40
# File 'app/services/spam/spam_params.rb', line 38

def user_agent
  @user_agent
end

Class Method Details

.new_from_request(request:) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'app/services/spam/spam_params.rb', line 27

def self.new_from_request(request:)
  self.normalize_grape_request_headers(request: request)
  self.new(
    captcha_response: request.headers['X-GitLab-Captcha-Response'],
    spam_log_id: request.headers['X-GitLab-Spam-Log-Id'],
    ip_address: request.env['action_dispatch.remote_ip'].to_s,
    user_agent: request.env['HTTP_USER_AGENT'],
    referer: request.env['HTTP_REFERER']
  )
end

.normalize_grape_request_headers(request:) ⇒ Object



57
58
59
60
61
62
63
64
# File 'app/services/spam/spam_params.rb', line 57

def self.normalize_grape_request_headers(request:)
  # If needed, make a normalized copy of Grape headers with the case of 'GitLab' (with an
  # uppercase 'L') instead of 'Gitlab' (with a lowercase 'l'), because Grape header helper keys
  # are "coerced into a capitalized kebab case". See https://github.com/ruby-grape/grape#request
  %w[X-Gitlab-Captcha-Response X-Gitlab-Spam-Log-Id].each do |header|
    request.headers[header.gsub('Gitlab', 'GitLab')] = request.headers[header] if request.headers.key?(header)
  end
end

Instance Method Details

#==(other) ⇒ Object



48
49
50
51
52
53
54
55
# File 'app/services/spam/spam_params.rb', line 48

def ==(other)
  other.class <= self.class &&
    other.captcha_response == captcha_response &&
    other.spam_log_id == spam_log_id &&
    other.ip_address == ip_address &&
    other.user_agent == user_agent &&
    other.referer == referer
end