Class: Contrast::Agent::Protect::Rule::InputClassification::LRUCache

Inherits:
Utils::LRUCache show all
Includes:
Utils, Components::Logger::InstanceMethods
Defined in:
lib/contrast/agent/protect/rule/input_classification/lru_cache.rb

Overview

This Class with store the input classification results for a given user input. Among the most used inputs are the headers values for session_id and path values.

Constant Summary collapse

RESULTS_CAPACITY =

Protect rules will always be fixed number, on other hand the number of inputs will grow, we need to limit the number of inputs to be cached.

20

Instance Method Summary collapse

Methods included from Utils

#safe_extract

Methods included from Components::Logger::InstanceMethods

#cef_logger, #logger

Methods inherited from Utils::LRUCache

#[], #[]=, #key?, #keys, #values

Constructor Details

#initialize(capacity = 10) ⇒ LRUCache

Initialize the LRU Cache.

instance it will never reach outside of the number of supported Protect rules.

Parameters:

  • capacity (Integer) (defaults to: 10)

    the maximum number of elements to store in the cache. For this



31
32
33
# File 'lib/contrast/agent/protect/rule/input_classification/lru_cache.rb', line 31

def initialize capacity = 10
  super(capacity)
end

Instance Method Details

#clearObject

Clear the cache and statistics.



53
54
55
56
# File 'lib/contrast/agent/protect/rule/input_classification/lru_cache.rb', line 53

def clear
  with_mutex { @cache.clear }
  clear_statistics
end

#clear_statisticsObject

Clear only the statistics.



59
60
61
# File 'lib/contrast/agent/protect/rule/input_classification/lru_cache.rb', line 59

def clear_statistics
  with_mutex { statistics.send(:clear) }
end

#empty?Boolean

Check if the cache is empty.

Returns:

  • (Boolean)


66
67
68
# File 'lib/contrast/agent/protect/rule/input_classification/lru_cache.rb', line 66

def empty?
  Contrast::Utils::DuckUtils.empty_duck?(@cache)
end

#lookout(rule_id, input, input_type, request) ⇒ Contrast::Agent::Protect::InputClassification::CachedResult?

Check if the input is cached and returns it if so and record the statistics for the required input.

Parameters:

  • rule_id (String)

    the Protect rule name.

  • input (String)

    the user input.

  • input_type (Symbol)

    Type of the input

  • request (Contrast::Agent::Request)

    the current request.

Returns:



77
78
79
# File 'lib/contrast/agent/protect/rule/input_classification/lru_cache.rb', line 77

def lookout rule_id, input, input_type, request
  with_mutex { _loockout(rule_id, input, input_type, request) }
end

#mutexObject



35
36
37
# File 'lib/contrast/agent/protect/rule/input_classification/lru_cache.rb', line 35

def mutex
  @_mutex ||= Mutex.new
end

#save(rule_id, result, request) ⇒ Object

Save the input classification result for a given user input.

Parameters:



87
88
89
# File 'lib/contrast/agent/protect/rule/input_classification/lru_cache.rb', line 87

def save rule_id, result, request
  with_mutex { _save(rule_id, result, request) }
end

#statisticsContrast::Agent::Protect::Rule::InputClassification::Statistics

Capacity of the statistics will always be the number of rule_id Protect supports.



48
49
50
# File 'lib/contrast/agent/protect/rule/input_classification/lru_cache.rb', line 48

def statistics
  @_statistics ||= Contrast::Agent::Protect::Rule::InputClassification::Statistics.new
end

#with_mutex(&block) ⇒ Object



39
40
41
42
43
# File 'lib/contrast/agent/protect/rule/input_classification/lru_cache.rb', line 39

def with_mutex &block
  return_type = mutex.synchronize(&block)
ensure
  return_type
end