Class: Sqreen::Rules::NotFoundCB

Inherits:
RuleCB show all
Defined in:
lib/sqreen/rules/not_found_cb.rb

Constant Summary collapse

IGNORED_EXTENSIONS =
['.css', '.gif', '.jpg', '.jpeg', '.png', '.svg', '.ico', '.webp', '.pdf', '.woff'].freeze

Constants inherited from RuleCB

RuleCB::DEFAULT_PAYLOAD

Constants included from CallCountable

CallCountable::COUNT_CALLS, CallCountable::FAILING, CallCountable::POST, CallCountable::PRE

Constants inherited from CB

CB::DEFAULT_PRIORITY

Instance Attribute Summary

Attributes inherited from RuleCB

#block, #payload_tpl, #test

Attributes included from CallCountable

#call_count_interval, #call_counts

Attributes inherited from FrameworkCB

#framework

Attributes inherited from CB

#klass, #method, #overtimeable

Instance Method Summary collapse

Methods inherited from RuleCB

#advise_action, #initialize, #overtime!, #priority, #record_event, #record_exception, #rule_name, #rulespack_id

Methods included from CallCountable

#count_callback_calls, #failing_with_count, #post_with_count, #pre_with_count

Methods included from Conditionable

#condition_callbacks, #failing_with_conditions, #post_with_conditions, #pre_with_conditions

Methods inherited from FrameworkCB

#record_observation, #whitelisted?

Methods inherited from CB

#failing?, #framework, #initialize, #overtime!, #post?, #pre?, #priority, #to_s, #whitelisted?

Constructor Details

This class inherits a constructor from Sqreen::Rules::RuleCB

Instance Method Details

#extension?(path, extensions) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
78
# File 'lib/sqreen/rules/not_found_cb.rb', line 72

def extension?(path, extensions)
  return false if path.nil?

  candidate = File.extname(path).downcase

  extensions.include?(candidate)
end

#path_from_variables(script_name, path_info, override) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/sqreen/rules/not_found_cb.rb', line 58

def path_from_variables(script_name, path_info, override)
  path = script_name

  if path.nil?
    path = override || path_info
  elsif override
    path += override
  elsif path_info
    path += path_info
  end

  path
end

#post(rv, _inst, args, _budget = nil, &_block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sqreen/rules/not_found_cb.rb', line 14

def post(rv, _inst, args, _budget = nil, &_block)
  return if rv[0].to_i != 404

  env         = args[0]
  ua          = env['HTTP_USER_AGENT']
  script_name = env['SCRIPT_NAME']
  path_info   = env['PATH_INFO']
  verb        = env['REQUEST_METHOD']
  host        = env['SERVER_NAME']
  override    = env['action_dispatch.original_path']
  exception   = env['action_dispatch.exception']

  record_from_env(ua, script_name, path_info, verb, override, host, exception)

  nil
end

#record_from_env(ua, script_name, path_info, verb, override, host, exception) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sqreen/rules/not_found_cb.rb', line 31

def record_from_env(ua, script_name, path_info, verb, override, host, exception)
  path = path_from_variables(script_name, path_info, override)

  return if extension?(path, IGNORED_EXTENSIONS)

  if !override && exception && !exception.to_s.empty?
    record_from_exception({ 'ua' => ua, 'verb' => verb, 'host' => host, 'script_name' => script_name, 'path_info' => path_info }, exception.exception)
  end

  record_event({ 'path' => path, 'ua' => ua, 'verb' => verb, 'host' => host })
end

#record_from_exception(payload, exception) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/sqreen/rules/not_found_cb.rb', line 43

def record_from_exception(payload, exception)
  message = exception.to_s

  if message && !message.empty?
    override = message =~ /No route matches\s+\[[a-z]+\]\s+"(.*)"/i && $1
  end
  payload['path'] = path_from_variables(payload['script_name'], payload['path_info'], override)
  return if extension?(payload['path'], IGNORED_EXTENSIONS)

  record = payload.reject { |k, v| v.nil? || ['path_info', 'script_name'].include?(k) }
  payload.delete('path') # remove added claim

  record_event(record)
end