Class: Sqreen::Rules::CustomErrorCB

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

Overview

Display sqreen presence

Constant Summary

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 collapse

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, #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, #overtime!, #post?, #pre?, #priority, #to_s, #whitelisted?

Constructor Details

#initialize(klass, method, rule_hash) ⇒ CustomErrorCB

Returns a new instance of CustomErrorCB.



14
15
16
17
18
19
20
21
22
23
# File 'lib/sqreen/rules/custom_error_cb.rb', line 14

def initialize(klass, method, rule_hash)
  @redirect_url = nil
  @status_code = nil
  super(klass, method, rule_hash)
  if @data.nil? || @data['values'].empty?
    raise Sqreen::Exception, 'No data'
  end
  configure_custom_error(@data['values'][0])
  @overtimeable = false
end

Instance Attribute Details

#redirect_urlObject (readonly)

Returns the value of attribute redirect_url.



13
14
15
# File 'lib/sqreen/rules/custom_error_cb.rb', line 13

def redirect_url
  @redirect_url
end

#status_codeObject (readonly)

Returns the value of attribute status_code.



13
14
15
# File 'lib/sqreen/rules/custom_error_cb.rb', line 13

def status_code
  @status_code
end

Instance Method Details

#configure_custom_error(custom_error) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sqreen/rules/custom_error_cb.rb', line 25

def configure_custom_error(custom_error)
  case custom_error['type']
  when 'custom_error_page' then
    @status_code = custom_error['status_code'].to_i
  when 'redirection' then
    @redirect_url = custom_error['redirection_url']
    @status_code = custom_error.fetch('status_code', 303).to_i
  else
    raise Sqreen::Exception, "No custom error #{custom_error['type']}"
  end
end

#failing(except, _inst, _args, _budget = nil, &_block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sqreen/rules/custom_error_cb.rb', line 37

def failing(except, _inst, _args, _budget = nil, &_block)
  oexcept = nil
  if except.respond_to?(:original_exception)
    oexcept = except.original_exception
  end
  if !except.is_a?(Sqreen::AttackBlocked) &&
     !oexcept.is_a?(Sqreen::AttackBlocked)
    return advise_action(nil)
  end
  if @redirect_url
    advise_action(:override, :new_return_value => respond_redirect)
  else
    advise_action(:override, :new_return_value => respond_page)
  end
end

#respond_pageObject



57
58
59
60
61
62
63
64
# File 'lib/sqreen/rules/custom_error_cb.rb', line 57

def respond_page
  @page ||= File.open(File.join(File.dirname(__FILE__), '../attack_detected.html'), 'rb', &:read)
  headers = {
    'Content-Type' => 'text/html',
    'Content-Length' => @page.size.to_s,
  }
  [@status_code, headers, [@page]]
end

#respond_redirectObject



53
54
55
# File 'lib/sqreen/rules/custom_error_cb.rb', line 53

def respond_redirect
  [@status_code, { 'Location' => @redirect_url }, ['']]
end