contrast-agent 7.1.0 → 7.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (97) hide show
  1. checksums.yaml +4 -4
  2. data/ext/extconf_common.rb +88 -14
  3. data/lib/contrast/agent/assess/policy/source_method.rb +13 -4
  4. data/lib/contrast/agent/assess/policy/trigger_method.rb +12 -18
  5. data/lib/contrast/agent/excluder/excluder.rb +64 -31
  6. data/lib/contrast/agent/protect/input_analyzer/input_analyzer.rb +62 -23
  7. data/lib/contrast/agent/protect/input_analyzer/worth_watching_analyzer.rb +37 -4
  8. data/lib/contrast/agent/protect/rule/base.rb +9 -7
  9. data/lib/contrast/agent/protect/rule/bot_blocker/bot_blocker.rb +1 -1
  10. data/lib/contrast/agent/protect/rule/bot_blocker/bot_blocker_input_classification.rb +29 -13
  11. data/lib/contrast/agent/protect/rule/cmdi/cmdi_backdoors.rb +1 -1
  12. data/lib/contrast/agent/protect/rule/cmdi/cmdi_base_rule.rb +0 -1
  13. data/lib/contrast/agent/protect/rule/cmdi/cmdi_input_classification.rb +2 -2
  14. data/lib/contrast/agent/protect/rule/deserialization/deserialization.rb +2 -2
  15. data/lib/contrast/agent/protect/rule/input_classification/base.rb +191 -0
  16. data/lib/contrast/agent/protect/rule/input_classification/base64_statistic.rb +71 -0
  17. data/lib/contrast/agent/protect/rule/input_classification/cached_result.rb +37 -0
  18. data/lib/contrast/agent/protect/rule/input_classification/encoding.rb +109 -0
  19. data/lib/contrast/agent/protect/rule/input_classification/encoding_rates.rb +47 -0
  20. data/lib/contrast/agent/protect/rule/input_classification/extendable.rb +80 -0
  21. data/lib/contrast/agent/protect/rule/input_classification/lru_cache.rb +198 -0
  22. data/lib/contrast/agent/protect/rule/input_classification/match_rates.rb +66 -0
  23. data/lib/contrast/agent/protect/rule/input_classification/rates.rb +53 -0
  24. data/lib/contrast/agent/protect/rule/input_classification/statistics.rb +115 -0
  25. data/lib/contrast/agent/protect/rule/input_classification/utils.rb +23 -0
  26. data/lib/contrast/agent/protect/rule/no_sqli/no_sqli_input_classification.rb +17 -7
  27. data/lib/contrast/agent/protect/rule/path_traversal/path_traversal_input_classification.rb +18 -15
  28. data/lib/contrast/agent/protect/rule/path_traversal/path_traversal_semantic_security_bypass.rb +1 -1
  29. data/lib/contrast/agent/protect/rule/sqli/sqli_input_classification.rb +2 -2
  30. data/lib/contrast/agent/protect/rule/sqli/sqli_semantic/sqli_dangerous_functions.rb +1 -1
  31. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_input_classification.rb +18 -15
  32. data/lib/contrast/agent/protect/rule/utils/filters.rb +6 -6
  33. data/lib/contrast/agent/protect/rule/xss/reflected_xss_input_classification.rb +19 -17
  34. data/lib/contrast/agent/protect/rule/xxe/xxe.rb +1 -1
  35. data/lib/contrast/agent/reporting/attack_result/attack_result.rb +6 -0
  36. data/lib/contrast/agent/reporting/client/interface.rb +132 -0
  37. data/lib/contrast/agent/reporting/client/interface_base.rb +27 -0
  38. data/lib/contrast/agent/reporting/connection_status.rb +0 -1
  39. data/lib/contrast/agent/reporting/input_analysis/input_analysis.rb +2 -7
  40. data/lib/contrast/agent/reporting/input_analysis/input_analysis_result.rb +17 -4
  41. data/lib/contrast/agent/reporting/input_analysis/input_type.rb +33 -1
  42. data/lib/contrast/agent/reporting/masker/masker_utils.rb +1 -1
  43. data/lib/contrast/agent/reporting/reporter.rb +11 -26
  44. data/lib/contrast/agent/reporting/reporting_events/application_defend_activity.rb +1 -0
  45. data/lib/contrast/agent/reporting/reporting_events/application_defend_attacker_activity.rb +1 -0
  46. data/lib/contrast/agent/reporting/reporting_events/discovered_route.rb +1 -1
  47. data/lib/contrast/agent/reporting/reporting_utilities/audit.rb +10 -3
  48. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +47 -6
  49. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client_utils.rb +41 -32
  50. data/lib/contrast/agent/reporting/reporting_utilities/resend.rb +144 -0
  51. data/lib/contrast/agent/reporting/reporting_utilities/response_handler.rb +35 -13
  52. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_mode.rb +14 -1
  53. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_utils.rb +11 -11
  54. data/lib/contrast/agent/request/request.rb +27 -12
  55. data/lib/contrast/agent/telemetry/base.rb +44 -19
  56. data/lib/contrast/agent/telemetry/base64_hash.rb +55 -0
  57. data/lib/contrast/agent/telemetry/cache_hash.rb +55 -0
  58. data/lib/contrast/agent/telemetry/client.rb +10 -2
  59. data/lib/contrast/agent/telemetry/exception/obfuscate.rb +97 -0
  60. data/lib/contrast/agent/telemetry/exception.rb +1 -0
  61. data/lib/contrast/agent/telemetry/{hash.rb → exception_hash.rb} +1 -1
  62. data/lib/contrast/agent/telemetry/input_analysis_cache_event.rb +27 -0
  63. data/lib/contrast/agent/telemetry/input_analysis_encoding_event.rb +26 -0
  64. data/lib/contrast/agent/telemetry/input_analysis_event.rb +91 -0
  65. data/lib/contrast/agent/telemetry/metric_event.rb +12 -0
  66. data/lib/contrast/agent/telemetry/startup_metrics_event.rb +0 -8
  67. data/lib/contrast/agent/version.rb +1 -1
  68. data/lib/contrast/components/config/sources.rb +6 -5
  69. data/lib/contrast/components/config.rb +4 -4
  70. data/lib/contrast/components/protect.rb +11 -1
  71. data/lib/contrast/components/sampling.rb +15 -10
  72. data/lib/contrast/components/settings.rb +9 -0
  73. data/lib/contrast/config/diagnostics/environment_variables.rb +3 -1
  74. data/lib/contrast/config/diagnostics/source_config_value.rb +5 -1
  75. data/lib/contrast/config/diagnostics/tools.rb +4 -4
  76. data/lib/contrast/config/validate.rb +2 -2
  77. data/lib/contrast/config/yaml_file.rb +8 -0
  78. data/lib/contrast/configuration.rb +11 -19
  79. data/lib/contrast/framework/grape/support.rb +1 -2
  80. data/lib/contrast/framework/manager.rb +17 -8
  81. data/lib/contrast/framework/rack/support.rb +99 -1
  82. data/lib/contrast/framework/rails/support.rb +4 -2
  83. data/lib/contrast/framework/sinatra/support.rb +1 -2
  84. data/lib/contrast/logger/aliased_logging.rb +18 -9
  85. data/lib/contrast/utils/assess/event_limit_utils.rb +13 -13
  86. data/lib/contrast/utils/hash_utils.rb +21 -2
  87. data/lib/contrast/utils/metrics_hash.rb +1 -1
  88. data/lib/contrast/utils/object_share.rb +2 -1
  89. data/lib/contrast/utils/request_utils.rb +14 -0
  90. data/lib/contrast/utils/response_utils.rb +12 -0
  91. data/lib/contrast/utils/timer.rb +2 -0
  92. data/lib/contrast.rb +9 -2
  93. data/resources/assess/policy.json +11 -0
  94. data/ruby-agent.gemspec +1 -1
  95. metadata +25 -7
  96. data/lib/contrast/agent/reporting/input_analysis/details/bot_blocker_details.rb +0 -27
  97. data/lib/contrast/utils/input_classification_base.rb +0 -169
@@ -5,7 +5,10 @@ require 'contrast/agent/thread/worker_thread'
5
5
  require 'contrast/agent/reporting/input_analysis/input_analysis_result'
6
6
  require 'contrast/agent/reporting/input_analysis/score_level'
7
7
  require 'contrast/agent/reporting/reporting_events/application_activity'
8
- require 'contrast/utils/input_classification_base'
8
+ require 'contrast/agent/protect/rule/input_classification/base'
9
+ require 'contrast/agent/telemetry/input_analysis_cache_event'
10
+ require 'contrast/agent/telemetry/input_analysis_encoding_event'
11
+ require 'contrast/utils/reporting/application_activity_batch_utils'
9
12
 
10
13
  module Contrast
11
14
  module Agent
@@ -15,7 +18,8 @@ module Contrast
15
18
  # Currently only includes: cmd_injection & sqli_injection rules
16
19
  class WorthWatchingInputAnalyzer < WorkerThread
17
20
  include Timeout
18
- include Contrast::Agent::Protect::Rule::InputClassificationBase
21
+ include Contrast::Agent::Protect::Rule::InputClassification::Base
22
+ include Contrast::Utils::Reporting::ApplicationActivityBatchUtils
19
23
 
20
24
  QUEUE_SIZE = 1000.cs__freeze
21
25
  AGENTLIB_TIMEOUT = 5.cs__freeze
@@ -48,8 +52,10 @@ module Contrast
48
52
  activity.attach_defend(attack_result)
49
53
  report = true
50
54
  end
51
- Contrast::Agent::Reporting::Masker.mask(activity)
52
- Contrast::Agent.reporter.send_event(activity) if report
55
+ report_activity(activity) if report
56
+ # Handle reporting of IA Cache statistics:
57
+ enqueue_cache_event(stored_ia.request)
58
+ enqueue_encoding_event(stored_ia.request)
53
59
  rescue StandardError => e
54
60
  logger.error('[WorthWatchingAnalyzer] thread could not process result because of:', e)
55
61
  end
@@ -73,6 +79,27 @@ module Contrast
73
79
 
74
80
  private
75
81
 
82
+ # After we have finished with all IA results, we need to send the cache statistics to Telemetry.
83
+ # Now the request cycle is finished and we can send the cache statistics.
84
+ #
85
+ # @param request [Contrast::Agent::Request] stored request.
86
+ def enqueue_cache_event request
87
+ return unless Contrast::Agent::Telemetry::Base.enabled?
88
+
89
+ Contrast::TELEMETRY_IA_CACHE[request.__id__] = Contrast::Agent::Protect::InputAnalyzer.
90
+ lru_cache.statistics.to_events.dup
91
+ Contrast::Agent::Protect::InputAnalyzer.lru_cache.clear_statistics
92
+ end
93
+
94
+ def enqueue_encoding_event request
95
+ return unless Contrast::Agent::Telemetry::Base.enabled?
96
+ return unless Contrast::PROTECT.normalize_base64?
97
+
98
+ Contrast::TELEMETRY_BASE64_HASH[request.__id__] = Contrast::Agent::Protect::InputAnalyzer.
99
+ base64_statistic.to_events.dup
100
+ Contrast::Agent::Protect::InputAnalyzer.base64_statistic.clear
101
+ end
102
+
76
103
  # This method will build the attack results from the saved ia.
77
104
  #
78
105
  # @param input_analysis [Contrast::Agent::Reporting::InputAnalysis]
@@ -114,6 +141,12 @@ module Contrast
114
141
  @_queue ||= Queue.new
115
142
  end
116
143
 
144
+ def report_activity activity
145
+ logger.debug('[WorthWatchingAnalyzer] preparing to send activity batch')
146
+ add_activity_to_batch(activity)
147
+ report_batch
148
+ end
149
+
117
150
  def delete_queue!
118
151
  @_queue&.clear
119
152
  @_queue&.close
@@ -63,6 +63,10 @@ module Contrast
63
63
  RULE_NAME
64
64
  end
65
65
 
66
+ # Should return the short name.
67
+ #
68
+ # @return [String]
69
+
66
70
  # Should return list of all sub_rules.
67
71
  # Extend for each main rule any sub-rules.
68
72
  #
@@ -166,17 +170,15 @@ module Contrast
166
170
  # Check if the protect rules is excluded by url from the exclusion rules for this application.
167
171
  #
168
172
  # @param rule_id [String]
169
- # @param request_path [String] Current request path
170
- def protect_excluded_by_url? rule_id, request_path
171
- Contrast::SETTINGS.excluder.protect_excluded_by_url?(rule_id, request_path)
173
+ def protect_excluded_by_url? rule_id
174
+ Contrast::SETTINGS.excluder.protect_excluded_by_url?(rule_id)
172
175
  end
173
176
 
174
177
  # Check if the protect rules is excluded by input from the exclusion rules for this application.
175
178
  #
176
179
  # @param results [Array<Contrast::Agent::Reporting::InputAnalysis>]
177
- # @param request_path [String] Current request path
178
- def protect_excluded_by_input? results, request_path
179
- Contrast::SETTINGS.excluder.protect_excluded_by_input?(results, request_path)
180
+ def protect_excluded_by_input? results
181
+ Contrast::SETTINGS.excluder.protect_excluded_by_input?(results)
180
182
  end
181
183
 
182
184
  # Allows for the InputAnalysis from Agent Library to be extracted early
@@ -330,7 +332,7 @@ module Contrast
330
332
  # @param context [Contrast::Agent::RequestContext]
331
333
  # @return [Array<Contrast::Agent::Reporting::InputAnalysis>]
332
334
  def gather_ia_results context
333
- return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless context&.agent_input_analysis&.results
335
+ return [] unless context&.agent_input_analysis&.results
334
336
 
335
337
  context.agent_input_analysis.results.select do |ia_result|
336
338
  ia_result.rule_id == rule_name && ia_result.score_level != Contrast::Agent::Reporting::ScoreLevel::IGNORE
@@ -77,7 +77,7 @@ module Contrast
77
77
  # @return [Contrast::Agent::Reporting::RaspRuleSample]
78
78
  def build_sample context, ia_result, _candidate_string, **_kwargs
79
79
  sample = build_base_sample(context, ia_result)
80
- sample.details = Contrast::Agent::Reporting::BotBlockerDetails.new
80
+ sample.details = Contrast::Agent::Reporting::Details::BotBlockerDetails.new
81
81
  sample.details.bot = ia_result.value
82
82
  sample.details.user_agent = context&.request&.user_agent
83
83
  sample
@@ -3,8 +3,8 @@
3
3
 
4
4
  require 'contrast/agent/reporting/input_analysis/input_type'
5
5
  require 'contrast/agent/reporting/input_analysis/score_level'
6
- require 'contrast/agent/reporting/input_analysis/details/bot_blocker_details'
7
- require 'contrast/utils/input_classification_base'
6
+ require 'contrast/agent/reporting/details/bot_blocker_details'
7
+ require 'contrast/agent/protect/rule/input_classification/base'
8
8
  require 'contrast/utils/object_share'
9
9
 
10
10
  module Contrast
@@ -20,7 +20,7 @@ module Contrast
20
20
  BOT_BLOCKER_MATCH = 'bot-blocker-input-tracing-v1'
21
21
 
22
22
  class << self
23
- include InputClassificationBase
23
+ include Contrast::Agent::Protect::Rule::InputClassification::Base
24
24
 
25
25
  # Input Classification stage is done to determine if an user input is
26
26
  # DEFINITEATTACK or to be ignored.
@@ -45,6 +45,7 @@ module Contrast
45
45
  input_analysis
46
46
  rescue StandardError => e
47
47
  logger.debug("An Error was recorded in the input classification of the #{ rule_id }", error: e)
48
+ nil
48
49
  end
49
50
 
50
51
  private
@@ -57,29 +58,44 @@ module Contrast
57
58
  # @param input_type [Contrast::Agent::Reporting::InputType] The type of the user input.
58
59
  # @param value [String, Array<String>] the value of the input.
59
60
  #
60
- # @return res [Contrast::Agent::Reporting::InputAnalysisResult]
61
+ # @return res [Contrast::Agent::Reporting::InputAnalysisResult, nil]
61
62
  def create_new_input_result request, rule_id, input_type, value
62
63
  return unless request.headers.key(value) == USER_AGENT
63
- return unless Contrast::AGENT_LIB
64
64
 
65
- # If there is no match this would return nil.
66
- header_eval = Contrast::AGENT_LIB.eval_header(AGENT_LIB_HEADER_NAME,
67
- value,
68
- Contrast::AGENT_LIB.rule_set[rule_id],
69
- Contrast::AGENT_LIB.eval_option[:NONE])
65
+ super(request, rule_id, input_type, value)
66
+ end
67
+
68
+ # Creates new instance of AgentLib evaluation result with direct call to AgentLib.
69
+ #
70
+ # @param rule_id [String] The name of the Protect Rule.
71
+ # @param _input_type [Contrast::Agent::Reporting::InputType] The type of the user input.
72
+ # @param value [String, Array<String>] the value of the input.
73
+ def build_input_eval rule_id, _input_type, value
74
+ Contrast::AGENT_LIB.eval_header(AGENT_LIB_HEADER_NAME,
75
+ value,
76
+ Contrast::AGENT_LIB.rule_set[rule_id],
77
+ Contrast::AGENT_LIB.eval_option[:NONE])
78
+ end
70
79
 
80
+ # Creates specific result from the AgentLib evaluation.
81
+ #
82
+ # @param rule_id [String] The name of the Protect Rule.
83
+ # @param input_type [Contrast::Agent::Reporting::InputType] The type of the user input.
84
+ # @param value [String, Array<String>] the value of the input.
85
+ # @param request [Contrast::Agent::Request] the current request context.
86
+ # @param input_eval [Contrast::AgentLib::EvalResult] the result of the input evaluation.
87
+ def build_ia_result rule_id, input_type, value, request, input_eval
71
88
  ia_result = new_ia_result(rule_id, input_type, request.path, value)
72
- score = header_eval&.score || 0
89
+ score = input_eval&.score || 0
73
90
  if score >= THRESHOLD
74
91
  ia_result.score_level = DEFINITEATTACK
75
92
  ia_result.ids << BOT_BLOCKER_MATCH
76
- ia_result.details = Contrast::Agent::Reporting::BotBlockerDetails.new
93
+ ia_result.details = Contrast::Agent::Reporting::Details::BotBlockerDetails.new
77
94
  # details:
78
95
  add_details(ia_result, value)
79
96
  else
80
97
  ia_result.score_level = IGNORE
81
98
  end
82
- add_needed_key(request, ia_result, input_type, value)
83
99
  ia_result
84
100
  end
85
101
 
@@ -37,7 +37,7 @@ module Contrast
37
37
  # @raise [Contrast::SecurityException] if the rule mode ise set
38
38
  # to BLOCK and valid cdmi is detected.
39
39
  def infilter context, classname, method, command
40
- return if protect_excluded_by_url?(rule_name, context.request.path)
40
+ return if protect_excluded_by_url?(rule_name)
41
41
  return unless backdoors_match?(command)
42
42
  return unless (result = build_attack_with_match(context, nil, nil, command,
43
43
  **{ classname: classname, method: method }))
@@ -43,7 +43,6 @@ module Contrast
43
43
  # to BLOCK and valid cdmi is detected.
44
44
  def infilter context, classname, method, command
45
45
  return unless infilter?(command)
46
- return if protect_excluded_by_url?(rule_name, context.request.path)
47
46
  return unless (result = build_violation(context, command))
48
47
 
49
48
  append_to_activity(context, result)
@@ -4,7 +4,7 @@
4
4
  require 'contrast/agent/protect/rule/cmdi/cmd_injection'
5
5
  require 'contrast/agent/reporting/input_analysis/score_level'
6
6
  require 'contrast/agent/protect/input_analyzer/input_analyzer'
7
- require 'contrast/utils/input_classification_base'
7
+ require 'contrast/agent/protect/rule/input_classification/base'
8
8
  require 'contrast/components/logger'
9
9
 
10
10
  module Contrast
@@ -17,7 +17,7 @@ module Contrast
17
17
  module CmdiInputClassification
18
18
  WORTHWATCHING_MATCH = 'cmdi-worth-watching-v2'.cs__freeze
19
19
  class << self
20
- include InputClassificationBase
20
+ include Contrast::Agent::Protect::Rule::InputClassification::Base
21
21
  include Contrast::Components::Logger::InstanceMethods
22
22
  end
23
23
  end
@@ -58,9 +58,9 @@ module Contrast
58
58
  # Per the spec, this rule applies regardless of input. Only the mode
59
59
  # of the rule and code exclusions apply at this point.
60
60
  # @return [Boolean] should the rule apply to this call.
61
- def infilter? context
61
+ def infilter?_context
62
62
  return false unless enabled?
63
- return false if protect_excluded_by_url?(rule_name, context.request.path)
63
+ return false if protect_excluded_by_url?(rule_name)
64
64
 
65
65
  true
66
66
  end
@@ -0,0 +1,191 @@
1
+ # Copyright (c) 2023 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ require 'contrast/utils/object_share'
5
+ require 'contrast/agent/protect/input_analyzer/input_analyzer'
6
+ require 'contrast/agent/protect/rule/input_classification/extendable'
7
+ require 'contrast/agent/protect/rule/input_classification/encoding'
8
+ require 'contrast/components/logger'
9
+
10
+ module Contrast
11
+ module Agent
12
+ module Protect
13
+ module Rule
14
+ module InputClassification
15
+ # This module will include all the similar information for all input classifications
16
+ # between different rules
17
+ module Base
18
+ UNKNOWN_KEY = 'unknown'
19
+ include Contrast::Components::Logger::InstanceMethods
20
+ include Contrast::Agent::Protect::Rule::InputClassification::Extendable
21
+ include Contrast::Agent::Protect::Rule::InputClassification::Encoding
22
+
23
+ KEYS_NEEDED = [
24
+ COOKIE_VALUE, PARAMETER_VALUE, HEADER, JSON_VALUE, MULTIPART_VALUE, XML_VALUE, DWR_VALUE
25
+ ].cs__freeze
26
+
27
+ BASE64_INPUT_TYPES = [BODY, COOKIE_VALUE, HEADER, PARAMETER_VALUE, MULTIPART_VALUE, XML_VALUE].cs__freeze
28
+
29
+ class << self
30
+ include Contrast::Components::Logger::InstanceMethods
31
+ include Contrast::Agent::Reporting::InputType
32
+
33
+ # Finds key value and type based on input type and value.
34
+ # @param request [Contrast::Agent::Request] the current request context.
35
+ # @param input_type [Contrast::Agent::Reporting::InputType] The type of the user input.
36
+ # @param value [String, Array<String>] the value of the input.
37
+ # @return [Array<(String, Contrast::Agent::Reporting::InputType)>] key and key type.
38
+ def find_key request, input_type, value
39
+ # TODO: RUBY-99999 Add handling for multipart, json and if any missing types.
40
+ case input_type
41
+ when COOKIE_VALUE
42
+ [request.cookies.key(value), Contrast::Agent::Reporting::InputType::COOKIE_NAME]
43
+ when PARAMETER_VALUE, URL_PARAMETER
44
+ [request.parameters.key(value), Contrast::Agent::Reporting::InputType::PARAMETER_NAME]
45
+ when HEADER
46
+ [request.headers.key(value), Contrast::Agent::Reporting::InputType::HEADER]
47
+ when UNKNOWN
48
+ [UNKNOWN_KEY, Contrast::Agent::Reporting::InputType::UNKNOWN]
49
+ else
50
+ [nil, nil]
51
+ end
52
+ rescue StandardError => e
53
+ logger.warn('[InputAnalyzer] Could not find proper key for input traced value', message: e)
54
+ [nil, nil]
55
+ end
56
+
57
+ # Some input types are not yet supported from the AgentLib.
58
+ # This will convert the type to the closet possible if viable,
59
+ # so that the input tracing could be done.
60
+ #
61
+ # @param input_type [Contrast::Agent::Reporting::InputType] The type of the user input.
62
+ # @return [Integer<Contrast::AgentLib::Interface::INPUT_SET>]
63
+ def convert_input_type input_type
64
+ case input_type
65
+ when URI, URL_PARAMETER
66
+ Contrast::AGENT_LIB.input_set[:URI_PATH]
67
+ when BODY, DWR_VALUE, SOCKET, UNDEFINED_TYPE, UNKNOWN, REQUEST, QUERYSTRING
68
+ Contrast::AGENT_LIB.input_set[:PARAMETER_VALUE]
69
+ when HEADER
70
+ Contrast::AGENT_LIB.input_set[:HEADER_VALUE]
71
+ when MULTIPART_VALUE, MULTIPART_FIELD_NAME
72
+ Contrast::AGENT_LIB.input_set[:MULTIPART_NAME]
73
+ when JSON_ARRAYED_VALUE
74
+ Contrast::AGENT_LIB.input_set[:JSON_KEY]
75
+ when PARAMETER_NAME
76
+ Contrast::AGENT_LIB.input_set[:PARAMETER_KEY]
77
+ else
78
+ Contrast::AGENT_LIB.input_set[input_type]
79
+ end
80
+ rescue StandardError => e
81
+ logger.debug('[InputAnalyzer] Protect Input classification could not determine input type,
82
+ falling back to default',
83
+ error: e)
84
+ Contrast::AGENT_LIB.input_set[:PARAMETER_VALUE]
85
+ end
86
+ end
87
+
88
+ # Input Classification stage is done to determine if an user input is
89
+ # DEFINITEATTACK or to be ignored.
90
+ #
91
+ # @param rule_id [String] Name of the protect rule.
92
+ # @param input_type [Symbol, Contrast::Agent::Reporting::InputType] The type of the user input.
93
+ # @param value [String, Array<String>] the value of the input.
94
+ # @param input_analysis [Contrast::Agent::Reporting::InputAnalysis] Holds all the results from the
95
+ # agent analysis from the current
96
+ # Request.
97
+ # @return ia [Contrast::Agent::Reporting::InputAnalysis, nil] with updated results.
98
+ def classify rule_id, input_type, value, input_analysis
99
+ return unless (rule = Contrast::PROTECT.rule(rule_id))
100
+ return unless rule.applicable_user_inputs.include?(input_type)
101
+ return unless input_analysis.request
102
+
103
+ Array(value).each do |val|
104
+ Array(val).each do |v|
105
+ next unless v
106
+
107
+ result = create_new_input_result(input_analysis.request, rule.rule_name, input_type, v)
108
+ append_result(input_analysis, result)
109
+ end
110
+ end
111
+
112
+ input_analysis
113
+ rescue StandardError => e
114
+ logger.debug("An Error was recorded in the input classification of the #{ rule_id }", error: e)
115
+ nil
116
+ end
117
+
118
+ # This methods checks if input is value that matches a key in the input.
119
+ #
120
+ # @param request [Contrast::Agent::Request] the current request context.
121
+ # @param ia_result [Contrast::Agent::Reporting::InputAnalysisResult] result to be updated.
122
+ # @param input_type [Contrast::Agent::Reporting::InputType] The type of the user input.
123
+ # @param value [String, Array<String>] the value of the input.
124
+ #
125
+ # @return result [Array<String, Symbol>] updated with key result.
126
+ def add_needed_key request, ia_result, input_type, value
127
+ ia_result.key, ia_result.key_type = Contrast::Agent::Protect::Rule::InputClassification::Base.
128
+ find_key(request, input_type, value)
129
+ end
130
+
131
+ private
132
+
133
+ # Appends result to the InputAnalysis.
134
+ #
135
+ # @param ia_analysis [Contrast::Agent::Reporting::InputAnalysis] the current input analysis.
136
+ # @param result [Contrast::Agent::Reporting::InputAnalysisResult] result to be appended.
137
+ # @return [Contrast::Agent::Reporting::InputAnalysis] the input analysis with the appended result.
138
+ def append_result ia_analysis, result
139
+ ia_analysis.results << result if result
140
+ ia_analysis
141
+ end
142
+
143
+ # Do not override this method, it will hold base operations, instead overwrite methods called inside
144
+ # of this method.
145
+ # This methods checks if input is tagged WORTHWATCHING or IGNORE matches value with it's
146
+ # key if needed and Creates new instance of InputAnalysisResult.
147
+ #
148
+ # @param request [Contrast::Agent::Request] the current request context.
149
+ # @param rule_id [String] The name of the Protect Rule.
150
+ # @param input_type [Contrast::Agent::Reporting::InputType] The type of the user input.
151
+ # @param value [String, Array<String>] the value of the input.
152
+ #
153
+ # @return res [Contrast::Agent::Reporting::InputAnalysisResult, nil]
154
+ def create_new_input_result request, rule_id, input_type, value
155
+ return unless Contrast::AGENT_LIB
156
+
157
+ # Cache retrieve
158
+ cached = Contrast::Agent::Protect::InputAnalyzer.lru_cache.lookout(rule_id, value, input_type, request)
159
+ return cached.result if cached.cs__is_a?(Contrast::Agent::Protect::InputClassification::CachedResult)
160
+
161
+ # Input evaluation
162
+ input_eval = build_input_eval(rule_id, input_type, base64_decode_input(value, input_type))
163
+ ia_result = build_ia_result(rule_id, input_type, value, request, input_eval)
164
+ return unless ia_result
165
+
166
+ add_needed_key(request, ia_result, input_type, value) if KEYS_NEEDED.include?(input_type)
167
+ # Input evaluation end
168
+
169
+ # Cache save. Cache must be saved after the input evaluation is completed.
170
+ Contrast::Agent::Protect::InputAnalyzer.lru_cache.save(rule_id, ia_result, request)
171
+ ia_result
172
+ end
173
+
174
+ # Decodes the value for the given input type.
175
+ # Applies to BODY, COOKIE_VALUE, HEADER, PARAMETER_VALUE, MULTIPART_VALUE, XML_VALUE
176
+ #
177
+ # @param value [String]
178
+ # @param input_type [Symbol]
179
+ # @return input [String]
180
+ def base64_decode_input value, input_type
181
+ return value unless Contrast::PROTECT.normalize_base64?
182
+ return value unless BASE64_INPUT_TYPES.include?(input_type)
183
+
184
+ cs__decode64(value, input_type)
185
+ end
186
+ end
187
+ end
188
+ end
189
+ end
190
+ end
191
+ end
@@ -0,0 +1,71 @@
1
+ # Copyright (c) 2023 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ require 'contrast/agent/protect/rule/input_classification/encoding_rates'
5
+ require 'contrast/agent/telemetry/input_analysis_encoding_event'
6
+ require 'contrast/components/logger'
7
+
8
+ module Contrast
9
+ module Agent
10
+ module Protect
11
+ module Rule
12
+ module InputClassification
13
+ # This class will safe all the information for the Base64 decoding matches per input type.
14
+ class Base64Statistic
15
+ include Contrast::Components::Logger::InstanceMethods
16
+
17
+ # @return [Hash<Contrast::Agent::Protect::Rule::InputClassification::EncodingRates>]
18
+ attr_reader :data
19
+
20
+ # Capacity for request context life cycle.
21
+ CAPACITY = 1000
22
+
23
+ def initialize
24
+ @data = {}
25
+ end
26
+
27
+ # Add a match for the given input type.
28
+ #
29
+ # @param input_type [Symbol]
30
+ def match! input_type
31
+ return @data[input_type]&.increase_match_base64 if @data[input_type]
32
+
33
+ @data[input_type] = Contrast::Agent::Protect::Rule::InputClassification::EncodingRates.new(input_type)
34
+ @data[input_type].increase_match_base64
35
+ end
36
+
37
+ # Add a mismatch for the given input type.
38
+ #
39
+ # @param input_type [Symbol]
40
+ def mismatch! input_type
41
+ return @data[input_type]&.increase_mismatch_base64 if @data[input_type]
42
+
43
+ @data[input_type] = Contrast::Agent::Protect::Rule::InputClassification::EncodingRates.new(input_type)
44
+ @data[input_type].increase_mismatch_base64
45
+ end
46
+
47
+ # Clears statistic data.
48
+ def clear
49
+ @data.clear
50
+ end
51
+
52
+ # @return [Array<Contrast::Agent::Telemetry::InputAnalysisCacheEvent>] the events to be sent.
53
+ def to_events
54
+ events = []
55
+ data.each do |_input_type, encoding_rate|
56
+ event = Contrast::Agent::Telemetry::InputAnalysisEncodingEvent.new(nil, encoding_rate)
57
+ next if event.empty?
58
+
59
+ events << event
60
+ end
61
+ events
62
+ rescue StandardError => e
63
+ logger.error("[Telemetry] Error while creating events: #{ e }", stacktrace: e.backtrace)
64
+ []
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,37 @@
1
+ # Copyright (c) 2023 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ require 'contrast/utils/duck_utils'
5
+ require 'contrast/agent/reporting/input_analysis/input_analysis_result'
6
+
7
+ module Contrast
8
+ module Agent
9
+ module Protect
10
+ module InputClassification
11
+ # This Class with store the input classification results for a given user input.
12
+ class CachedResult
13
+ # @return [String]
14
+ attr_reader :result
15
+ # @return [Integer]
16
+ attr_reader :request_id
17
+
18
+ # Initialize Input Classification Cached Result
19
+ #
20
+ # @param result [Contrast::Agent::Reporting::InputAnalysisResult]
21
+ # @param request_id [Integer] the id of current request.
22
+ def initialize result, request_id
23
+ @result = result.dup if result&.cs__is_a?(Contrast::Agent::Reporting::InputAnalysisResult)
24
+ @request_id = request_id
25
+ end
26
+
27
+ # Check if the input classification result is empty.
28
+ #
29
+ # @return [Boolean]
30
+ def empty?
31
+ Contrast::Utils::DuckUtils.empty_duck?(@result) && Contrast::Utils::DuckUtils.empty_duck?(@request_id)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end