Class: Contrast::Agent::Protect::Rule::InputClassification::Base64Statistic

Inherits:
Object
  • Object
show all
Includes:
Components::Logger::InstanceMethods
Defined in:
lib/contrast/agent/protect/rule/input_classification/base64_statistic.rb

Overview

This class will safe all the information for the Base64 decoding matches per input type.

Constant Summary collapse

CAPACITY =

Capacity for request context life cycle.

1000

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Components::Logger::InstanceMethods

#cef_logger, #logger

Constructor Details

#initializeBase64Statistic

Returns a new instance of Base64Statistic.



23
24
25
# File 'lib/contrast/agent/protect/rule/input_classification/base64_statistic.rb', line 23

def initialize
  @data = {}
end

Instance Attribute Details

#dataHash<Contrast::Agent::Protect::Rule::InputClassification::EncodingRates> (readonly)



18
19
20
# File 'lib/contrast/agent/protect/rule/input_classification/base64_statistic.rb', line 18

def data
  @data
end

Instance Method Details

#clearObject

Clears statistic data.



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

def clear
  @data.clear
end

#match!(input_type) ⇒ Object

Add a match for the given input type.

Parameters:

  • input_type (Symbol)


30
31
32
33
34
35
# File 'lib/contrast/agent/protect/rule/input_classification/base64_statistic.rb', line 30

def match! input_type
  return @data[input_type]&.increase_match_base64 if @data[input_type]

  @data[input_type] = Contrast::Agent::Protect::Rule::InputClassification::EncodingRates.new(input_type)
  @data[input_type].increase_match_base64
end

#mismatch!(input_type) ⇒ Object

Add a mismatch for the given input type.

Parameters:

  • input_type (Symbol)


40
41
42
43
44
45
# File 'lib/contrast/agent/protect/rule/input_classification/base64_statistic.rb', line 40

def mismatch! input_type
  return @data[input_type]&.increase_mismatch_base64 if @data[input_type]

  @data[input_type] = Contrast::Agent::Protect::Rule::InputClassification::EncodingRates.new(input_type)
  @data[input_type].increase_mismatch_base64
end

#to_eventsArray<Contrast::Agent::Telemetry::InputAnalysisCacheEvent>

Returns the events to be sent.

Returns:



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/contrast/agent/protect/rule/input_classification/base64_statistic.rb', line 53

def to_events
  events = []
  data.each do |_input_type, encoding_rate|
    event = Contrast::Agent::Telemetry::InputAnalysisEncodingEvent.new(nil, encoding_rate)
    next if event.empty?

    events << event
  end
  events
rescue StandardError => e
  logger.error("[Telemetry] Error while creating events: #{ e }", stacktrace: e.backtrace)
  []
end