contrast-agent 6.8.0 → 6.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (154) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -1
  3. data/lib/contrast/agent/assess/policy/trigger_method.rb +1 -1
  4. data/lib/contrast/agent/assess/property/evented.rb +11 -11
  5. data/lib/contrast/agent/assess/rule/response/body_rule.rb +1 -1
  6. data/lib/contrast/agent/assess.rb +0 -1
  7. data/lib/contrast/agent/excluder.rb +1 -1
  8. data/lib/contrast/agent/middleware.rb +12 -4
  9. data/lib/contrast/agent/protect/input_analyzer/input_analyzer.rb +76 -83
  10. data/lib/contrast/agent/protect/input_analyzer/worth_watching_analyzer.rb +121 -0
  11. data/lib/contrast/agent/protect/policy/applies_command_injection_rule.rb +2 -0
  12. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +6 -3
  13. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +3 -0
  14. data/lib/contrast/agent/protect/policy/applies_sqli_rule.rb +3 -0
  15. data/lib/contrast/agent/protect/policy/rule_applicator.rb +12 -0
  16. data/lib/contrast/agent/protect/rule/base.rb +21 -7
  17. data/lib/contrast/agent/protect/rule/base_service.rb +6 -0
  18. data/lib/contrast/agent/protect/rule/bot_blocker/bot_blocker_input_classification.rb +1 -1
  19. data/lib/contrast/agent/protect/rule/bot_blocker.rb +9 -1
  20. data/lib/contrast/agent/protect/rule/cmd_injection.rb +8 -7
  21. data/lib/contrast/agent/protect/rule/cmdi/cmdi_base_rule.rb +8 -0
  22. data/lib/contrast/agent/protect/rule/cmdi/cmdi_chained_command.rb +0 -5
  23. data/lib/contrast/agent/protect/rule/cmdi/cmdi_dangerous_path.rb +0 -5
  24. data/lib/contrast/agent/protect/rule/deserialization.rb +2 -2
  25. data/lib/contrast/agent/protect/rule/no_sqli.rb +24 -2
  26. data/lib/contrast/agent/protect/rule/path_traversal/path_traversal_input_classification.rb +1 -1
  27. data/lib/contrast/agent/protect/rule/path_traversal.rb +12 -3
  28. data/lib/contrast/agent/protect/rule/sqli/sqli_input_classification.rb +0 -1
  29. data/lib/contrast/agent/protect/rule/sqli.rb +10 -13
  30. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_input_classification.rb +6 -2
  31. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +20 -0
  32. data/lib/contrast/agent/protect/rule/xss/reflected_xss_input_classification.rb +1 -1
  33. data/lib/contrast/agent/protect/rule/xss.rb +9 -0
  34. data/lib/contrast/agent/protect/rule/xxe.rb +2 -2
  35. data/lib/contrast/agent/protect/rule.rb +0 -3
  36. data/lib/contrast/agent/reporting/attack_result/rasp_rule_sample.rb +1 -1
  37. data/lib/contrast/agent/reporting/attack_result/user_input.rb +0 -1
  38. data/lib/contrast/agent/reporting/details/details.rb +0 -1
  39. data/lib/contrast/agent/reporting/input_analysis/input_analysis.rb +12 -0
  40. data/lib/contrast/agent/reporting/report.rb +2 -0
  41. data/lib/contrast/agent/reporting/reporter.rb +42 -7
  42. data/lib/contrast/agent/reporting/reporting_events/application_activity.rb +5 -6
  43. data/lib/contrast/agent/reporting/reporting_events/application_defend_activity.rb +24 -7
  44. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_activity.rb +20 -5
  45. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample.rb +0 -1
  46. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample_activity.rb +5 -0
  47. data/lib/contrast/agent/reporting/reporting_events/application_defend_attacker_activity.rb +10 -1
  48. data/lib/contrast/agent/reporting/reporting_events/application_inventory.rb +2 -1
  49. data/lib/contrast/agent/reporting/reporting_events/application_inventory_activity.rb +6 -1
  50. data/lib/contrast/agent/reporting/reporting_events/application_reporting_event.rb +10 -0
  51. data/lib/contrast/agent/reporting/reporting_events/application_settings.rb +40 -0
  52. data/lib/contrast/agent/reporting/reporting_events/finding.rb +2 -2
  53. data/lib/contrast/agent/reporting/reporting_events/finding_event.rb +239 -93
  54. data/lib/contrast/agent/reporting/reporting_events/finding_event_signature.rb +10 -23
  55. data/lib/contrast/agent/reporting/reporting_events/finding_event_source.rb +10 -9
  56. data/lib/contrast/agent/reporting/reporting_events/observed_route.rb +12 -0
  57. data/lib/contrast/agent/reporting/reporting_events/server_reporting_event.rb +8 -0
  58. data/lib/contrast/agent/reporting/reporting_events/server_settings.rb +40 -0
  59. data/lib/contrast/agent/reporting/reporting_utilities/endpoints.rb +6 -0
  60. data/lib/contrast/agent/reporting/reporting_utilities/ng_response_extractor.rb +137 -0
  61. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +52 -2
  62. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client_utils.rb +8 -4
  63. data/lib/contrast/agent/reporting/reporting_utilities/response_extractor.rb +105 -58
  64. data/lib/contrast/agent/reporting/reporting_utilities/response_handler.rb +9 -7
  65. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_utils.rb +143 -49
  66. data/lib/contrast/agent/reporting/reporting_workers/application_server_worker.rb +46 -0
  67. data/lib/contrast/agent/reporting/reporting_workers/reporter_heartbeat.rb +51 -0
  68. data/lib/contrast/agent/reporting/reporting_workers/reporting_workers.rb +14 -0
  69. data/lib/contrast/agent/reporting/reporting_workers/server_settings_worker.rb +46 -0
  70. data/lib/contrast/agent/reporting/settings/assess.rb +14 -1
  71. data/lib/contrast/agent/reporting/settings/assess_rule.rb +18 -0
  72. data/lib/contrast/agent/reporting/settings/assess_server_feature.rb +14 -2
  73. data/lib/contrast/agent/reporting/settings/helpers.rb +9 -0
  74. data/lib/contrast/agent/reporting/settings/protect.rb +17 -12
  75. data/lib/contrast/agent/reporting/settings/protect_rule.rb +18 -0
  76. data/lib/contrast/agent/reporting/settings/protect_server_feature.rb +40 -3
  77. data/lib/contrast/agent/reporting/settings/rule_definition.rb +3 -0
  78. data/lib/contrast/agent/reporting/settings/security_logger.rb +77 -0
  79. data/lib/contrast/agent/reporting/settings/sensitive_data_masking.rb +1 -1
  80. data/lib/contrast/agent/reporting/settings/server_features.rb +9 -0
  81. data/lib/contrast/agent/reporting/settings/syslog.rb +34 -5
  82. data/lib/contrast/agent/reporting/settings/virtual_patch.rb +56 -0
  83. data/lib/contrast/agent/reporting/settings/virtual_patch_condition.rb +47 -0
  84. data/lib/contrast/agent/request.rb +1 -0
  85. data/lib/contrast/agent/request_context_extend.rb +20 -0
  86. data/lib/contrast/agent/request_handler.rb +5 -10
  87. data/lib/contrast/agent/telemetry/base.rb +11 -10
  88. data/lib/contrast/agent/telemetry/events/exceptions/obfuscate.rb +108 -103
  89. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_event.rb +1 -1
  90. data/lib/contrast/agent/telemetry/events/startup_metrics_event.rb +1 -1
  91. data/lib/contrast/agent/thread_watcher.rb +46 -6
  92. data/lib/contrast/agent/version.rb +1 -1
  93. data/lib/contrast/agent.rb +18 -0
  94. data/lib/contrast/agent_lib/api/init.rb +1 -7
  95. data/lib/contrast/agent_lib/api/input_tracing.rb +2 -4
  96. data/lib/contrast/agent_lib/interface.rb +1 -16
  97. data/lib/contrast/agent_lib/interface_base.rb +52 -39
  98. data/lib/contrast/agent_lib/return_types/eval_result.rb +2 -2
  99. data/lib/contrast/api/communication/connection_status.rb +15 -0
  100. data/lib/contrast/components/agent.rb +34 -0
  101. data/lib/contrast/components/api.rb +23 -0
  102. data/lib/contrast/components/app_context.rb +23 -3
  103. data/lib/contrast/components/assess.rb +60 -8
  104. data/lib/contrast/components/assess_rules.rb +18 -0
  105. data/lib/contrast/components/base.rb +40 -0
  106. data/lib/contrast/components/config/sources.rb +95 -0
  107. data/lib/contrast/components/config.rb +18 -1
  108. data/lib/contrast/components/heap_dump.rb +10 -0
  109. data/lib/contrast/components/inventory.rb +15 -2
  110. data/lib/contrast/components/logger.rb +18 -0
  111. data/lib/contrast/components/polling.rb +39 -0
  112. data/lib/contrast/components/protect.rb +48 -1
  113. data/lib/contrast/components/ruby_component.rb +15 -0
  114. data/lib/contrast/components/sampling.rb +70 -13
  115. data/lib/contrast/components/security_logger.rb +13 -0
  116. data/lib/contrast/components/settings.rb +120 -10
  117. data/lib/contrast/config/certification_configuration.rb +14 -0
  118. data/lib/contrast/config/config.rb +46 -0
  119. data/lib/contrast/config/diagnostics.rb +114 -0
  120. data/lib/contrast/config/diagnostics_tools.rb +98 -0
  121. data/lib/contrast/config/effective_config.rb +65 -0
  122. data/lib/contrast/config/effective_config_value.rb +32 -0
  123. data/lib/contrast/config/exception_configuration.rb +12 -0
  124. data/lib/contrast/config/protect_rule_configuration.rb +2 -2
  125. data/lib/contrast/config/protect_rules_configuration.rb +7 -6
  126. data/lib/contrast/config/request_audit_configuration.rb +13 -0
  127. data/lib/contrast/config/server_configuration.rb +41 -2
  128. data/lib/contrast/configuration.rb +28 -2
  129. data/lib/contrast/extension/assess/array.rb +3 -3
  130. data/lib/contrast/extension/assess/erb.rb +1 -1
  131. data/lib/contrast/extension/assess/regexp.rb +2 -2
  132. data/lib/contrast/logger/aliased_logging.rb +48 -15
  133. data/lib/contrast/utils/assess/event_limit_utils.rb +31 -9
  134. data/lib/contrast/utils/assess/trigger_method_utils.rb +5 -4
  135. data/lib/contrast/utils/hash_digest.rb +2 -2
  136. data/lib/contrast/utils/input_classification_base.rb +21 -5
  137. data/lib/contrast/utils/reporting/application_activity_batch_utils.rb +81 -0
  138. data/lib/contrast/utils/routes_sent.rb +60 -0
  139. data/lib/contrast/utils/telemetry.rb +1 -1
  140. data/lib/contrast/utils/telemetry_client.rb +2 -3
  141. data/lib/contrast/utils/timer.rb +16 -0
  142. data/lib/contrast.rb +3 -1
  143. data/resources/protect/policy.json +8 -0
  144. data/ruby-agent.gemspec +6 -2
  145. metadata +43 -24
  146. data/lib/contrast/agent/assess/contrast_event.rb +0 -157
  147. data/lib/contrast/agent/assess/events/event_factory.rb +0 -34
  148. data/lib/contrast/agent/assess/events/source_event.rb +0 -46
  149. data/lib/contrast/agent/protect/rule/http_method_tampering/http_method_tampering_input_classification.rb +0 -96
  150. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +0 -83
  151. data/lib/contrast/agent/reporting/details/http_method_tempering_details.rb +0 -27
  152. data/lib/contrast/agent/reporting/reporter_heartbeat.rb +0 -53
  153. data/lib/contrast/agent/reporting/reporting_events/server_activity.rb +0 -36
  154. data/lib/contrast/agent_lib/api/method_tempering.rb +0 -29
@@ -41,31 +41,39 @@ module Contrast
41
41
 
42
42
  private
43
43
 
44
+ # @param type [ALIASED_FATAL, ALIASED_ERROR, ALIASED_WARN] the type of error, used to indicate the function used
45
+ # for logging
46
+ # @param message [String] the exception message
47
+ # @param exception [Exception] The exception or error
48
+ # @param data [Object] Any structured data
44
49
  def build_exception type, message = nil, exception = nil, data = nil
45
- stack_trace = get_stack_trace(type)
46
- stack_frame_type = Contrast::Agent::Telemetry::TelemetryException::Obfuscate.obfuscate_type(
47
- stack_trace[1].path.delete_prefix(Dir.pwd))
48
- stack_frame_function = stack_trace[1].label
50
+ stack_trace = wrapped_caller_locations
51
+ caller_idx = stack_trace&.find_index { |stack| stack.to_s.include?(type) } || 0
52
+ # The caller_stack is the method in which the error occurred, so has to be above this method
53
+ caller_idx += 1
54
+ caller_frame = stack_trace[caller_idx]
55
+ stack_frame_type = caller_frame.path.delete_prefix(Dir.pwd)
56
+ stack_frame_function = caller_frame.label
49
57
  key = "#{ stack_frame_type }|#{ stack_frame_function }|#{ message }"
50
- if TELEMETRY_EXCEPTIONS[key]
51
- TELEMETRY_EXCEPTIONS.increment(key)
58
+ if Contrast::TELEMETRY_EXCEPTIONS[key]
59
+ Contrast::TELEMETRY_EXCEPTIONS.increment(key)
52
60
  return
53
61
  end
54
62
 
55
- return if TELEMETRY_EXCEPTIONS.exception_limit?
56
-
57
- message_exception_type = Contrast::Agent::Telemetry::TelemetryException::Obfuscate.obfuscate_exception_type(
58
- exception ? exception.cs__class.to_s : stack_frame_type.split('/').last)
63
+ return if Contrast::TELEMETRY_EXCEPTIONS.exception_limit?
59
64
 
65
+ message_exception_type = exception ? exception.cs__class.to_s : stack_frame_type.split('/').last
60
66
  event_message = create_message(stack_frame_function,
61
67
  stack_frame_type, message_exception_type,
62
68
  data, exception,
63
69
  message)
70
+ build_stack(event_message, stack_trace, caller_idx)
64
71
  TELEMETRY_EXCEPTIONS[key] = event_message
65
72
  rescue StandardError => e
66
- debug('Unable to report exception to telemetry', e)
73
+ debug('[Telemetry] Unable to report exception', e)
67
74
  end
68
75
 
76
+ # @return [Contrast::Agent::Telemetry::TelemetryException::Event]
69
77
  def create_message stack_frame_function, stack_frame_type, message_exception_type, data, exception, message
70
78
  message_for_exception = if exception
71
79
  exception.cs__respond_to?(:message) ? exception.message : exception
@@ -89,12 +97,37 @@ module Contrast
89
97
  message = Contrast::Agent::Telemetry::TelemetryException::Message.build(tags, [message_exception])
90
98
  Contrast::Agent::Telemetry::TelemetryException::Event.new(message)
91
99
  rescue ArgumentError => e
92
- debug('TelemetryException failed from aliased logging with: ', e)
100
+ debug('[Telemetry] TelemetryException failed from aliased logging with: ', e)
101
+ end
102
+
103
+ # Convert the given caller_stack from the event into the exception StackFrame format for reporting, appending it
104
+ # to the Exception wrapped in the Event.
105
+ #
106
+ # @param event_message [Contrast::Agent::Telemetry::TelemetryException::Event]
107
+ # @param caller_stack [(Array<Thread::Backtrace::Location>, nil]
108
+ # @param caller_idx [Integer] the starting location for the exception, which allows filtering out the logger code
109
+ # from the stack
110
+ def build_stack event_message, caller_stack, caller_idx = 0
111
+ return unless caller_stack
112
+
113
+ event_exception = event_message.exceptions[0]
114
+ event_exception_message = event_exception.exceptions[0]
115
+
116
+ caller_stack.each_with_index do |caller, idx|
117
+ next unless idx > caller_idx
118
+
119
+ stack_frame =
120
+ Contrast::Agent::Telemetry::TelemetryException::StackFrame.build(caller.label,
121
+ caller.path.delete_prefix(Dir.pwd))
122
+ event_exception_message.push(stack_frame)
123
+ end
93
124
  end
94
125
 
95
- def get_stack_trace type
96
- start = caller_locations&.find_index { |stack| stack.to_s.include?(type) }
97
- start ? caller_locations(start + 1, 20) : caller_locations(20, 20)
126
+ # This is purely a wrapper around caller_locations used for testing
127
+ #
128
+ # @return [Array<Thread::Backtrace::Location>, nil]
129
+ def wrapped_caller_locations
130
+ caller_locations
98
131
  end
99
132
  end
100
133
  end
@@ -12,18 +12,19 @@ module Contrast
12
12
  module EventLimitUtils
13
13
  include Contrast::Components::Logger::InstanceMethods
14
14
  # Checks to see if the event limit for the policy type has been met or exceeded
15
- # @param method_policy [Contrast::Agent::Patching::Policy::MethodPolicy] method to check for event limit
16
- def event_limit? method_policy
15
+ # @param policy [Contrast::Agent::Patching::Policy::MethodPolicy,
16
+ # Contrast::Agent::Patching::Policy::TriggerNode] method to check for event limit
17
+ def event_limit? policy
17
18
  return false unless (context = Contrast::Agent::REQUEST_TRACKER.current)
18
19
 
19
- if method_policy.source_node
20
+ if policy.source_node
20
21
  max = ::Contrast::ASSESS.max_context_source_events
21
- return at_limit?(method_policy, context.source_event_count, max)
22
-
22
+ return at_limit?(policy, context.source_event_count, max, context)
23
23
  end
24
- if method_policy.propagation_node
24
+
25
+ if policy.propagation_node
25
26
  max = ::Contrast::ASSESS.max_propagation_events
26
- return at_limit?(method_policy, context.propagation_event_count, max)
27
+ return at_limit?(policy, context.propagation_event_count, max, context)
27
28
  end
28
29
 
29
30
  false # policy does not have limit
@@ -65,8 +66,10 @@ module Contrast
65
66
  private
66
67
 
67
68
  # helper method to check limit and log when necessary
68
- def at_limit? method_policy, current_count, event_max
69
+ def at_limit? method_policy, current_count, event_max, context
69
70
  if current_count == event_max
71
+ return if event_limit_counts.key?(get_event_limit_key(method_policy, context))
72
+
70
73
  logger.warn('Event Limit Reached:',
71
74
  {
72
75
  count: current_count,
@@ -76,16 +79,21 @@ module Contrast
76
79
  })
77
80
  # increment to be over count for logging purposes
78
81
  increment_event_count(method_policy)
82
+ increment_event_limit_logs(method_policy, context)
83
+
79
84
  return true
80
85
  elsif current_count > event_max
81
- # increment to be over count for logging purposes
82
86
  increment_event_count(method_policy)
87
+ return if event_limit_counts.key?(get_event_limit_key(method_policy, context))
88
+
89
+ # increment to be over count for logging purposes
83
90
  logger.warn('Event Limit Exceeded:',
84
91
  {
85
92
  count: current_count,
86
93
  policy: method_policy.method_name,
87
94
  node: method_policy
88
95
  })
96
+ increment_event_limit_logs(method_policy, context)
89
97
  return true
90
98
  end
91
99
  false
@@ -95,6 +103,20 @@ module Contrast
95
103
  @_rule_counts ||= Hash.new { |h, k| h[k] = 0 }
96
104
  end
97
105
 
106
+ def event_limit_counts
107
+ @_event_limit_counts ||= Hash.new { |h, k| h[k] = 0 }
108
+ end
109
+
110
+ def get_event_limit_key method_policy, context
111
+ "#{ method_policy.method_name }_#{ context.request.__id__ }"
112
+ end
113
+
114
+ def increment_event_limit_logs method_policy, context
115
+ event_limit_counter = get_event_limit_key(method_policy, context)
116
+
117
+ event_limit_counts[event_limit_counter] += 1
118
+ end
119
+
98
120
  # the time threshold for which to track rule counts resets when now >= threshold_time_limit
99
121
  # @return [Integer]
100
122
  def threshold_time_limit
@@ -1,12 +1,15 @@
1
1
  # Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
+ require 'contrast/utils/assess/event_limit_utils'
5
+
4
6
  module Contrast
5
7
  module Utils
6
8
  module Assess
7
9
  # This module will include all methods for some internal validations/appliers in the TriggerMethod module
8
10
  # and some other module methods from the same place, so we can ease the main module
9
11
  module TriggerMethodUtils
12
+ extend Contrast::Utils::Assess::EventLimitUtils
10
13
  # A request is reportable if it is not from ActionController::Live
11
14
  #
12
15
  # @param env [Hash] the env of the Request
@@ -33,10 +36,10 @@ module Contrast
33
36
 
34
37
  # Finds the first request along the left most tree of parent events
35
38
  #
36
- # @param event [Contrast::Agent::Assess::ContrastEvent|Contrast::Agent::Assess::Events::SourceEvent]
39
+ # @param event [Contrast::Agent::Reporting::FindingEvent]
37
40
  # @return [Contrast::Agent::Request, nil]
38
41
  def find_event_request event
39
- return event.request if event.cs__is_a?(Contrast::Agent::Assess::Events::SourceEvent)
42
+ return event.request if event&.source_type
40
43
 
41
44
  idx = 0
42
45
  while idx <= event.parent_events.length
@@ -44,9 +47,7 @@ module Contrast
44
47
  return found if found
45
48
 
46
49
  idx += 1
47
- return event.request if event.request
48
50
  end
49
- return unless event.cs__is_a?(Contrast::Agent::Assess::Events::SourceEvent)
50
51
 
51
52
  event.request
52
53
  end
@@ -61,8 +61,8 @@ module Contrast
61
61
  def update_on_sources events
62
62
  events.each do |event|
63
63
  event.event_sources.each do |source|
64
- update(source.type)
65
- update(source.name) # rubocop:disable Security/Module/Name
64
+ update(source.source_type)
65
+ update(source.source_name)
66
66
  end
67
67
  end
68
68
  end
@@ -43,7 +43,7 @@ module Contrast
43
43
  next unless v
44
44
 
45
45
  result = create_new_input_result(input_analysis.request, rule.rule_name, input_type, v)
46
- input_analysis.results << result unless result.nil?
46
+ append_result(input_analysis, result)
47
47
  end
48
48
  end
49
49
 
@@ -51,6 +51,7 @@ module Contrast
51
51
  rescue StandardError => e
52
52
  logger.debug("An Error was recorded in the input classification of the #{ rule_id }")
53
53
  logger.debug(e)
54
+ nil
54
55
  end
55
56
 
56
57
  # Creates new isntance of InputAnalysisResult with basic info.
@@ -103,16 +104,24 @@ module Contrast
103
104
  # @return [Integer<Contrast::AgentLib::Interface::INPUT_SET>]
104
105
  def convert_input_type input_type
105
106
  case input_type
106
- when BODY, DWR_VALUE
107
+ when URI, URL_PARAMETER
108
+ Contrast::AGENT_LIB.input_set[:URI_PATH]
109
+ when BODY, DWR_VALUE, SOCKET, UNDEFINED_TYPE, UNKNOWN, REQUEST, QUERYSTRING
107
110
  Contrast::AGENT_LIB.input_set[:PARAMETER_VALUE]
108
111
  when HEADER
109
112
  Contrast::AGENT_LIB.input_set[:HEADER_VALUE]
110
113
  when MULTIPART_VALUE, MULTIPART_FIELD_NAME
111
114
  Contrast::AGENT_LIB.input_set[:MULTIPART_NAME]
115
+ when JSON_ARRAYED_VALUE
116
+ Contrast::AGENT_LIB.input_set[:JSON_KEY]
117
+ when PARAMETER_NAME
118
+ Contrast::AGENT_LIB.input_set[:PARAMETER_KEY]
112
119
  else
113
120
  Contrast::AGENT_LIB.input_set[input_type]
114
121
  end
115
- rescue StandardError
122
+ rescue StandardError => e
123
+ logger.debug('Protect Input classification could not determine input type, falling back to default',
124
+ error: e)
116
125
  Contrast::AGENT_LIB.input_set[:PARAMETER_VALUE]
117
126
  end
118
127
 
@@ -133,8 +142,7 @@ module Contrast
133
142
  input_eval = Contrast::AGENT_LIB.eval_input(value,
134
143
  convert_input_type(input_type),
135
144
  Contrast::AGENT_LIB.rule_set[rule_id],
136
- Contrast::AGENT_LIB.
137
- eval_option[:WORTHWATCHING])
145
+ Contrast::AGENT_LIB.eval_option[:PREFER_WORTH_WATCHING])
138
146
 
139
147
  ia_result = new_ia_result(rule_id, input_type, request.path, value)
140
148
  score = input_eval&.score || 0
@@ -149,6 +157,14 @@ module Contrast
149
157
  add_needed_key(request, ia_result, input_type, value) if KEYS_NEEDED.include?(input_type)
150
158
  ia_result
151
159
  end
160
+
161
+ def append_result ia_analysis, result
162
+ unless result.nil?
163
+ ia_analysis.results << result
164
+ ia_analysis.triggered_rules << result.rule_id unless ia_analysis.triggered_rules.include?(result.rule_id)
165
+ end
166
+ ia_analysis
167
+ end
152
168
  end
153
169
  end
154
170
  end
@@ -0,0 +1,81 @@
1
+ # Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ require 'contrast/agent/reporting/reporting_events/application_activity'
5
+
6
+ module Contrast
7
+ module Utils
8
+ module Reporting
9
+ # ApplicationActivityBatchUtils handles batching and reporting of ApplicationActivity events to TeamServer at a
10
+ # set interval
11
+ module ApplicationActivityBatchUtils
12
+ DEFAULT_REPORTING_INTERVAL_MS = 30_000.cs__freeze
13
+
14
+ # @return [Integer] time when activity batch was created in ms
15
+ attr_reader :batch_age
16
+
17
+ # Merge a ApplicationActivity into the ApplicationActivityBatch
18
+ # @param activity [Contrast::Agent::Reporting::ApplicationActivity] from a RequestContext
19
+ def add_activity_to_batch activity
20
+ return unless activity
21
+
22
+ activity_batch.query_count += activity.query_count
23
+ activity_batch.routes << activity.routes
24
+ activity_batch.routes.flatten!
25
+ merge_attackers(activity)
26
+ activity_batch.attach_inventory(activity.inventory) unless activity.inventory.empty?
27
+ end
28
+
29
+ # If the batch can be reported, mask the data and add it to the reporting queue, then reset the activity_batch
30
+ def report_batch
31
+ return unless report_batch?
32
+ return unless (reporter = Contrast::Agent.reporter)
33
+
34
+ Contrast::Agent::Reporting::Masker.mask(activity_batch)
35
+ reporter&.send_event(activity_batch)
36
+ reset_activity_batch
37
+ end
38
+
39
+ # @return Contrast::Agent::Reporting::ApplicationActivity
40
+ def activity_batch
41
+ return @activity_batch unless @activity_batch.nil?
42
+
43
+ reset_activity_batch
44
+ @activity_batch
45
+ end
46
+
47
+ private
48
+
49
+ def merge_attackers activity
50
+ return if activity.defend.attackers.empty?
51
+
52
+ activity.defend.attackers.each do |attacker|
53
+ if (existing = activity_batch.defend.find_existing_attacker_activity(attacker))
54
+ attacker.protection_rules.each_key do |key|
55
+ activity_batch.defend.attach_existing(existing, attacker, key)
56
+ end
57
+ else
58
+ activity_batch.defend.attackers << attacker
59
+ end
60
+ end
61
+ end
62
+
63
+ # resets the activity_batch object and it's batch_age
64
+ def reset_activity_batch
65
+ @batch_age = Contrast::Utils::Timer.now_ms
66
+ @activity_batch = Contrast::Agent::Reporting::ApplicationActivity.new
67
+ end
68
+
69
+ # @return [Boolean] if the age of the batch is outside the reporting interval
70
+ def report_batch?
71
+ reporting_interval <= (Contrast::Utils::Timer.now_ms - batch_age)
72
+ end
73
+
74
+ # @return [Integer] interval to report to TeamServer in seconds
75
+ def reporting_interval
76
+ Contrast::AGENT.polling.batch_reporting_interval_ms.to_i || DEFAULT_REPORTING_INTERVAL_MS
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,60 @@
1
+ # Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ # require 'contrast/components/logger'
5
+ # require 'contrast/agent/telemetry/events/exceptions/telemetry_exception_event'
6
+
7
+ module Contrast
8
+ module Utils
9
+ # This is the RoutesSent class, which determines whether observed routes can be sent to TeamServer.
10
+ # Routes that have not been seen (according to the cache) can be sent, as well as any route that
11
+ # # has been seen but not within the time limit.
12
+ class RoutesSent
13
+ # include Contrast::Components::Logger::InstanceMethods
14
+ ROUTES_LIMIT = 500
15
+ TIME_LIMIT_IN_SECONDS = 60
16
+
17
+ attr_accessor :cache
18
+
19
+ def initialize
20
+ @cache = {}
21
+ end
22
+
23
+ # Determine whether the provided route can be sent to TeamServer.
24
+ #
25
+ # @param route [Contrast::Agent::Reporting::ObservedRoute] the route
26
+ # @return [boolean]
27
+ def sendable? route
28
+ route_hash = route.hash_id
29
+
30
+ # If hash doesn't exist in @cache...
31
+ # - Add hash to @cache (with Time.now)
32
+ # - Clear oldest entries (if more than ROUTES_LIMIT)
33
+ # - Return *true*
34
+ unless cache.key?(route_hash)
35
+ cache[route_hash] = Time.now
36
+ remove_oldest_entries!
37
+ return true
38
+ end
39
+
40
+ # If hash exists in @cache...
41
+ # - Return *true* if more than a minute since time recorded for hash
42
+ # - Return *false* if not than a minute since time recorded for hash
43
+ return false unless Time.now.to_i - cache.fetch(route_hash, 0).to_i > TIME_LIMIT_IN_SECONDS
44
+
45
+ cache[route_hash] = Time.now
46
+ true
47
+ end
48
+
49
+ private
50
+
51
+ def remove_oldest_entries!
52
+ return if cache.size < ROUTES_LIMIT
53
+
54
+ route_hashes = cache.sort_by { |_, v| -v.tv_nsec }.
55
+ to_h.keys.slice(0, ROUTES_LIMIT)
56
+ @cache = cache.slice(*route_hashes)
57
+ end
58
+ end
59
+ end
60
+ end
@@ -9,7 +9,7 @@ module Contrast
9
9
  module Utils
10
10
  # Tools for supporting the Telemetry feature
11
11
  module Telemetry
12
- DIR = '/ect/contrast/ruby-agent/'.cs__freeze
12
+ DIR = '/etc/contrast/ruby-agent/'.cs__freeze
13
13
  FILE = '.telemetry'.cs__freeze
14
14
  CURRENT_DIR = Dir.pwd.cs__freeze
15
15
  CONFIG_DIR = CURRENT_DIR + '/config/contrast/'.cs__freeze
@@ -97,10 +97,9 @@ module Contrast
97
97
  # @param event [Contrast::Agent::Telemetry::Event, Array<Contrast::Agent::Telemetry::TelemetryException::Event>]
98
98
  # @return [String] - JSON
99
99
  def get_event_json event
100
- hsh = [event.to_controlled_hash]
101
- hsh.to_json
100
+ Array(event.to_controlled_hash).to_json
102
101
  rescue Exception => e # rubocop:disable Lint/RescueException
103
- logger.error('Unable to convert TelemetryEvent to JSON string', e, hsh)
102
+ logger.error('[Telemetry] Unable to convert TelemetryEvent to JSON string', e, hsh)
104
103
  raise(e)
105
104
  end
106
105
  end
@@ -22,6 +22,22 @@ module Contrast
22
22
  def self.now_ms
23
23
  (Time.now.to_f * 1000).to_i
24
24
  end
25
+
26
+ # Return current time in iso8601 format.
27
+ #
28
+ # @return[String]
29
+ def self.time_now
30
+ Time.now.utc.iso8601(7)
31
+ end
32
+
33
+ # Converts time given in ms format form TS to HttpDate.
34
+ # Returns time format for If-Modified-Since: <day-name>, <day> <month> <year> <hour>:<minute>:<second> GMT
35
+ # Note: The Time class treats GMT (Greenwich Mean Time) and UTC (Coordinated Universal Time) as equivalent.
36
+ #
37
+ # @param time [Integer] time in ms.
38
+ def self.ms_to_httpdate time
39
+ Time.at(time / 1000).httpdate unless time.nil?
40
+ end
25
41
  end
26
42
  end
27
43
  end
data/lib/contrast.rb CHANGED
@@ -60,12 +60,13 @@ require 'contrast/components/protect'
60
60
  require 'contrast/components/sampling'
61
61
  require 'contrast/components/scope'
62
62
  require 'contrast/components/settings'
63
+ require 'contrast/utils/routes_sent'
63
64
  require 'contrast/utils/telemetry_hash'
64
65
  require 'contrast/utils/telemetry'
65
66
  require 'contrast/agent/telemetry/events/exceptions/telemetry_exception_event'
66
67
  require 'contrast/agent_lib/interface'
67
68
 
68
- module Contrast
69
+ module Contrast # :nodoc:
69
70
  CONFIG = Contrast::Components::Config::Interface.new
70
71
  SCOPE = Contrast::Components::Scope::Interface.new
71
72
  API = CONFIG.api
@@ -81,6 +82,7 @@ end
81
82
 
82
83
  module Contrast
83
84
  TELEMETRY_EXCEPTIONS = (Contrast::Utils::TelemetryHash.new if Contrast::Utils::Telemetry.exceptions_enabled?)
85
+ ROUTES_SENT = Contrast::Utils::RoutesSent.new
84
86
  end
85
87
 
86
88
  # This needs to be required very early, after component interfaces, and before instrumentation attempts
@@ -128,6 +128,14 @@
128
128
  "database": "MongoDB"
129
129
  }
130
130
  },{
131
+ "class_name": "Mongo::Collection",
132
+ "instance_method": true,
133
+ "method_visibility": "public",
134
+ "method_name": "find",
135
+ "properties": {
136
+ "database": "MongoDB"
137
+ }
138
+ },{
131
139
  "class_name": "Moped::Node",
132
140
  "method_name": "read",
133
141
  "instance_method": true,
data/ruby-agent.gemspec CHANGED
@@ -101,7 +101,11 @@ end
101
101
 
102
102
  # Dependencies not mocked out during RSpec that we test real code of, beyond just frameworks.
103
103
  def self.add_tested_gems spec
104
- spec.add_development_dependency 'async'
104
+ if RUBY_VERSION < '3.0.0'
105
+ spec.add_development_dependency 'async', '~> 1.30.3'
106
+ else
107
+ spec.add_development_dependency 'async'
108
+ end
105
109
  spec.add_development_dependency 'execjs'
106
110
  spec.add_development_dependency 'rhino'
107
111
  spec.add_development_dependency 'sqlite3'
@@ -117,7 +121,7 @@ end
117
121
  def self.add_dependencies spec
118
122
  spec.add_dependency 'ougai', '>= 1.8', '< 3.0.0'
119
123
  spec.add_dependency 'rack', '~> 2.0'
120
- spec.add_dependency 'contrast-agent-lib', '~> 0.1.0'
124
+ spec.add_dependency 'contrast-agent-lib', '~> 0.1.0', '>= 0.1.3'
121
125
  spec.add_dependency 'ffi', '~> 1.0'
122
126
  end
123
127