contrast-agent 6.9.0 → 6.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (94) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -1
  3. data/lib/contrast/agent/assess/rule/response/body_rule.rb +1 -1
  4. data/lib/contrast/agent/middleware.rb +4 -2
  5. data/lib/contrast/agent/protect/input_analyzer/input_analyzer.rb +76 -83
  6. data/lib/contrast/agent/protect/input_analyzer/worth_watching_analyzer.rb +40 -35
  7. data/lib/contrast/agent/protect/policy/applies_command_injection_rule.rb +2 -0
  8. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +6 -3
  9. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +3 -0
  10. data/lib/contrast/agent/protect/policy/applies_sqli_rule.rb +3 -0
  11. data/lib/contrast/agent/protect/policy/rule_applicator.rb +12 -0
  12. data/lib/contrast/agent/protect/rule/base.rb +19 -5
  13. data/lib/contrast/agent/protect/rule/base_service.rb +6 -0
  14. data/lib/contrast/agent/protect/rule/bot_blocker/bot_blocker_input_classification.rb +1 -1
  15. data/lib/contrast/agent/protect/rule/bot_blocker.rb +8 -0
  16. data/lib/contrast/agent/protect/rule/cmdi/cmdi_base_rule.rb +8 -0
  17. data/lib/contrast/agent/protect/rule/deserialization.rb +2 -2
  18. data/lib/contrast/agent/protect/rule/no_sqli.rb +24 -2
  19. data/lib/contrast/agent/protect/rule/path_traversal/path_traversal_input_classification.rb +1 -1
  20. data/lib/contrast/agent/protect/rule/path_traversal.rb +8 -0
  21. data/lib/contrast/agent/protect/rule/sqli/sqli_input_classification.rb +0 -1
  22. data/lib/contrast/agent/protect/rule/sqli.rb +6 -10
  23. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_input_classification.rb +6 -2
  24. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +20 -0
  25. data/lib/contrast/agent/protect/rule/xss/reflected_xss_input_classification.rb +1 -1
  26. data/lib/contrast/agent/protect/rule/xss.rb +8 -0
  27. data/lib/contrast/agent/protect/rule/xxe.rb +2 -2
  28. data/lib/contrast/agent/protect/rule.rb +0 -3
  29. data/lib/contrast/agent/reporting/attack_result/user_input.rb +0 -1
  30. data/lib/contrast/agent/reporting/details/details.rb +0 -1
  31. data/lib/contrast/agent/reporting/input_analysis/input_analysis.rb +12 -0
  32. data/lib/contrast/agent/reporting/report.rb +1 -0
  33. data/lib/contrast/agent/reporting/reporter.rb +11 -10
  34. data/lib/contrast/agent/reporting/reporting_events/application_activity.rb +4 -5
  35. data/lib/contrast/agent/reporting/reporting_events/application_defend_activity.rb +13 -1
  36. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_activity.rb +20 -5
  37. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample.rb +0 -1
  38. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample_activity.rb +5 -0
  39. data/lib/contrast/agent/reporting/reporting_events/application_defend_attacker_activity.rb +10 -1
  40. data/lib/contrast/agent/reporting/reporting_events/application_inventory.rb +2 -1
  41. data/lib/contrast/agent/reporting/reporting_events/application_reporting_event.rb +10 -0
  42. data/lib/contrast/agent/reporting/reporting_events/application_settings.rb +40 -0
  43. data/lib/contrast/agent/reporting/reporting_utilities/ng_response_extractor.rb +137 -0
  44. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +12 -4
  45. data/lib/contrast/agent/reporting/reporting_utilities/response_extractor.rb +100 -107
  46. data/lib/contrast/agent/reporting/reporting_utilities/response_handler.rb +5 -4
  47. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_utils.rb +97 -63
  48. data/lib/contrast/agent/reporting/reporting_workers/application_server_worker.rb +46 -0
  49. data/lib/contrast/agent/reporting/reporting_workers/reporter_heartbeat.rb +51 -0
  50. data/lib/contrast/agent/reporting/reporting_workers/reporting_workers.rb +14 -0
  51. data/lib/contrast/agent/reporting/reporting_workers/server_settings_worker.rb +46 -0
  52. data/lib/contrast/agent/reporting/settings/assess.rb +14 -1
  53. data/lib/contrast/agent/reporting/settings/assess_rule.rb +18 -0
  54. data/lib/contrast/agent/reporting/settings/helpers.rb +4 -2
  55. data/lib/contrast/agent/reporting/settings/protect.rb +17 -12
  56. data/lib/contrast/agent/reporting/settings/protect_rule.rb +18 -0
  57. data/lib/contrast/agent/reporting/settings/protect_server_feature.rb +1 -1
  58. data/lib/contrast/agent/reporting/settings/sensitive_data_masking.rb +1 -1
  59. data/lib/contrast/agent/reporting/settings/virtual_patch.rb +56 -0
  60. data/lib/contrast/agent/reporting/settings/virtual_patch_condition.rb +47 -0
  61. data/lib/contrast/agent/request_context_extend.rb +20 -0
  62. data/lib/contrast/agent/telemetry/base.rb +11 -10
  63. data/lib/contrast/agent/telemetry/events/exceptions/obfuscate.rb +108 -103
  64. data/lib/contrast/agent/telemetry/events/startup_metrics_event.rb +1 -1
  65. data/lib/contrast/agent/thread_watcher.rb +16 -10
  66. data/lib/contrast/agent/version.rb +1 -1
  67. data/lib/contrast/agent.rb +12 -0
  68. data/lib/contrast/agent_lib/api/init.rb +1 -7
  69. data/lib/contrast/agent_lib/api/input_tracing.rb +2 -4
  70. data/lib/contrast/agent_lib/interface.rb +1 -16
  71. data/lib/contrast/agent_lib/interface_base.rb +52 -39
  72. data/lib/contrast/agent_lib/return_types/eval_result.rb +2 -2
  73. data/lib/contrast/components/assess.rb +26 -4
  74. data/lib/contrast/components/polling.rb +4 -1
  75. data/lib/contrast/components/settings.rb +46 -3
  76. data/lib/contrast/config/config.rb +2 -2
  77. data/lib/contrast/config/protect_rule_configuration.rb +1 -1
  78. data/lib/contrast/config/protect_rules_configuration.rb +1 -1
  79. data/lib/contrast/extension/assess/array.rb +3 -3
  80. data/lib/contrast/extension/assess/regexp.rb +2 -2
  81. data/lib/contrast/logger/aliased_logging.rb +48 -15
  82. data/lib/contrast/utils/input_classification_base.rb +21 -4
  83. data/lib/contrast/utils/routes_sent.rb +2 -2
  84. data/lib/contrast/utils/telemetry.rb +1 -1
  85. data/lib/contrast/utils/telemetry_client.rb +1 -1
  86. data/resources/protect/policy.json +8 -0
  87. data/ruby-agent.gemspec +1 -1
  88. metadata +28 -18
  89. data/lib/contrast/agent/protect/rule/http_method_tampering/http_method_tampering_input_classification.rb +0 -96
  90. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +0 -83
  91. data/lib/contrast/agent/reporting/details/http_method_tempering_details.rb +0 -27
  92. data/lib/contrast/agent/reporting/reporter_heartbeat.rb +0 -47
  93. data/lib/contrast/agent/reporting/server_settings_worker.rb +0 -44
  94. data/lib/contrast/agent_lib/api/method_tempering.rb +0 -29
@@ -34,7 +34,7 @@ module Contrast
34
34
  input_eval = Contrast::AGENT_LIB.eval_input(value,
35
35
  convert_input_type(input_type),
36
36
  Contrast::AGENT_LIB.rule_set[rule_id],
37
- Contrast::AGENT_LIB.eval_option[:WORTHWATCHING])
37
+ Contrast::AGENT_LIB.eval_option[:PREFER_WORTH_WATCHING])
38
38
 
39
39
  ia_result = new_ia_result(rule_id, input_type, request.path, value)
40
40
  score = input_eval&.score || 0
@@ -8,6 +8,7 @@ require 'contrast/agent/reporting/input_analysis/score_level'
8
8
  require 'contrast/utils/stack_trace_utils'
9
9
  require 'contrast/agent/reporting/details/path_traversal_details'
10
10
  require 'contrast/agent/reporting/details/path_traversal_semantic_analysis_details'
11
+ require 'contrast/agent/protect/rule/path_traversal/path_traversal_input_classification'
11
12
  require 'contrast/utils/string_utils'
12
13
 
13
14
  module Contrast
@@ -52,6 +53,13 @@ module Contrast
52
53
  APPLICABLE_USER_INPUTS
53
54
  end
54
55
 
56
+ # Path Traversal input classification
57
+ #
58
+ # @return [module<Contrast::Agent::Protect::Rule::PathTraversalInputClassification>]
59
+ def classification
60
+ @_classification ||= Contrast::Agent::Protect::Rule::PathTraversalInputClassification.cs__freeze
61
+ end
62
+
55
63
  def infilter context, method, path
56
64
  return unless infilter?(context)
57
65
 
@@ -3,7 +3,6 @@
3
3
 
4
4
  require 'contrast/utils/object_share'
5
5
  require 'contrast/agent/reporting/input_analysis/input_type'
6
- require 'contrast/agent/protect/rule/sqli'
7
6
  require 'contrast/agent/reporting/input_analysis/score_level'
8
7
  require 'contrast/agent/protect/input_analyzer/input_analyzer'
9
8
  require 'contrast/utils/input_classification_base'
@@ -7,6 +7,7 @@ require 'contrast/agent/protect/rule/sql_sample_builder'
7
7
  require 'contrast/agent/reporting/input_analysis/input_type'
8
8
  require 'contrast/agent/protect/rule/sqli/sqli_base_rule'
9
9
  require 'contrast/agent/protect/rule/sqli/sqli_semantic/sqli_dangerous_functions'
10
+ require 'contrast/agent/protect/rule/sqli/sqli_input_classification'
10
11
 
11
12
  module Contrast
12
13
  module Agent
@@ -45,16 +46,11 @@ module Contrast
45
46
  APPLICABLE_USER_INPUTS
46
47
  end
47
48
 
48
- def infilter context, database, query_string
49
- return unless infilter?(context)
50
-
51
- result = find_attacker(context, query_string, database: database)
52
- return unless result
53
-
54
- append_to_activity(context, result)
55
-
56
- cef_logging(result, :successful_attack, value: query_string)
57
- raise(Contrast::SecurityException.new(self, BLOCK_MESSAGE)) if blocked?
49
+ # SQLI input classification
50
+ #
51
+ # @return [module<Contrast::Agent::Protect::Rule::SqliInputClassification>]
52
+ def classification
53
+ @_classification ||= Contrast::Agent::Protect::Rule::SqliInputClassification.cs__freeze
58
54
  end
59
55
 
60
56
  # Allows for the InputAnalysis from Agent Library to be extracted early
@@ -43,11 +43,15 @@ module Contrast
43
43
  else
44
44
  ia_result.score_level = IGNORE
45
45
  end
46
- ia_result.key = if input_type == MULTIPART_FIELD_NAME
46
+ ia_result.key = case input_type
47
+ when MULTIPART_FIELD_NAME
47
48
  Contrast::Agent::Protect::InputAnalyzer::DISPOSITION_FILENAME
48
- else
49
+ when MULTIPART_NAME
49
50
  Contrast::Agent::Protect::InputAnalyzer::DISPOSITION_NAME
51
+ else
52
+ Contrast::Utils::ObjectShare::EMPTY_STRING
50
53
  end
54
+
51
55
  ia_result
52
56
  end
53
57
  end
@@ -4,6 +4,7 @@
4
4
  require 'contrast/agent/protect/rule/base_service'
5
5
  require 'contrast/agent/reporting/input_analysis/input_type'
6
6
  require 'contrast/agent/reporting/input_analysis/score_level'
7
+ require 'contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_input_classification'
7
8
 
8
9
  module Contrast
9
10
  module Agent
@@ -30,6 +31,25 @@ module Contrast
30
31
  def block_message
31
32
  BLOCK_MESSAGE
32
33
  end
34
+
35
+ # Unsafe File Upload input classification
36
+ #
37
+ # @return [module<Contrast::Agent::Protect::Rule::UnsafeFileUploadInputClassification>]
38
+ def classification
39
+ @_classification ||= Contrast::Agent::Protect::Rule::UnsafeFileUploadInputClassification.cs__freeze
40
+ end
41
+
42
+ private
43
+
44
+ # @param context [Contrast::Agent::RequestContext]
45
+ # @return [Boolean]
46
+ def prefilter? context
47
+ return false unless context
48
+ return false unless enabled?
49
+ return false if protect_excluded_by_code?
50
+
51
+ true
52
+ end
33
53
  end
34
54
  end
35
55
  end
@@ -33,7 +33,7 @@ module Contrast
33
33
  convert_input_type(input_type),
34
34
  Contrast::AGENT_LIB.rule_set[rule_id],
35
35
  Contrast::AGENT_LIB.
36
- eval_option[:WORTHWATCHING])
36
+ eval_option[:PREFER_WORTH_WATCHING])
37
37
 
38
38
  score = input_eval&.score || 0
39
39
  ia_result = new_ia_result(rule_id, input_type, request.path, value)
@@ -2,6 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/agent/protect/rule/base_service'
5
+ require 'contrast/agent/protect/rule/xss/reflected_xss_input_classification'
5
6
  require 'contrast/agent/reporting/input_analysis/input_type'
6
7
 
7
8
  module Contrast
@@ -28,6 +29,13 @@ module Contrast
28
29
  BLOCK_MESSAGE
29
30
  end
30
31
 
32
+ # XSS Upload input classification
33
+ #
34
+ # @return [module<Contrast::Agent::Protect::Rule::ReflectedXssInputClassification>]
35
+ def classification
36
+ @_classification ||= Contrast::Agent::Protect::Rule::ReflectedXssInputClassification.cs__freeze
37
+ end
38
+
31
39
  def stream_safe?
32
40
  false
33
41
  end
@@ -1,7 +1,7 @@
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/agent/protect/rule/base'
4
+ require 'contrast/agent/protect/rule/base_service'
5
5
  require 'contrast/agent/reporting/details/xxe_details'
6
6
  require 'contrast/agent/reporting/details/xxe_match'
7
7
  require 'contrast/agent/reporting/details/xxe_wrapper'
@@ -14,7 +14,7 @@ module Contrast
14
14
  module Rule
15
15
  # Implementation of the XXE Protect Rule used to evaluate XML calls for exploit
16
16
  # of unsafe external entity resolution.
17
- class Xxe < Contrast::Agent::Protect::Rule::Base
17
+ class Xxe < Contrast::Agent::Protect::Rule::BaseService
18
18
  include Contrast::Components::Logger::InstanceMethods
19
19
  INPUT_NAME = 'XML Prolog'
20
20
 
@@ -48,8 +48,5 @@ require 'contrast/agent/protect/rule/deserialization'
48
48
  require 'contrast/agent/protect/rule/no_sqli'
49
49
  require 'contrast/agent/protect/rule/no_sqli/mongo_no_sql_scanner'
50
50
 
51
- # The classes required for Http Method Tampering
52
- require 'contrast/agent/protect/rule/http_method_tampering'
53
-
54
51
  # The classes required for Unsafe File Upload
55
52
  require 'contrast/agent/protect/rule/unsafe_file_upload'
@@ -3,7 +3,6 @@
3
3
 
4
4
  require 'contrast/utils/object_share'
5
5
  require 'contrast/agent/reporting/input_analysis/input_type'
6
- require 'contrast/agent/request_context'
7
6
 
8
7
  module Contrast
9
8
  module Agent
@@ -3,7 +3,6 @@
3
3
 
4
4
  require 'contrast/agent/reporting/details/bot_blocker_details'
5
5
  require 'contrast/agent/reporting/details/cmd_injection_details'
6
- require 'contrast/agent/reporting/details/http_method_tempering_details'
7
6
  require 'contrast/agent/reporting/details/no_sqli_details'
8
7
  require 'contrast/agent/reporting/details/path_traversal_details'
9
8
  require 'contrast/agent/reporting/details/protect_rule_details'
@@ -9,6 +9,18 @@ module Contrast
9
9
  module Reporting
10
10
  # This class will do ia analysis for our protect rules
11
11
  class InputAnalysis
12
+ def inputs
13
+ @_inputs
14
+ end
15
+
16
+ def inputs= extracted_inputs
17
+ @_inputs = extracted_inputs
18
+ end
19
+
20
+ def triggered_rules
21
+ @_triggered_rules ||= []
22
+ end
23
+
12
24
  # result from input analysis
13
25
  #
14
26
  # @return @_results [Array<Contrast::Agent::Reporting::Settings::InputAnalysisResult>]
@@ -27,5 +27,6 @@ require 'contrast/agent/reporting/reporting_events/route_coverage'
27
27
  require 'contrast/agent/reporting/reporting_events/observed_library_usage'
28
28
  require 'contrast/agent/reporting/reporting_events/poll'
29
29
  require 'contrast/agent/reporting/reporting_events/server_settings'
30
+ require 'contrast/agent/reporting/reporting_events/application_settings'
30
31
  # Sensitive data masking
31
32
  require 'contrast/agent/reporting/masker/masker'
@@ -40,13 +40,13 @@ module Contrast
40
40
 
41
41
  client.startup!(connection)
42
42
  @_thread = Contrast::Agent::Thread.new do
43
- logger.debug('Starting background Reporter thread.')
43
+ logger.debug('[Reporter] Starting background Reporter thread.')
44
44
  loop do
45
45
  next unless connected?
46
46
 
47
47
  process_event(queue.pop)
48
48
  rescue StandardError => e
49
- logger.debug('Reporter thread could not process because of:', e)
49
+ logger.debug('[Reporter] thread could not process because of:', e)
50
50
  end
51
51
  end
52
52
  end
@@ -67,7 +67,7 @@ module Contrast
67
67
  # @param event [Contrast::Agent::Reporting::ReportingEvent]
68
68
  def send_event event
69
69
  if ::Contrast::AGENT.disabled?
70
- logger.warn('Attempted to queue event with Agent disabled', caller: caller, event: event)
70
+ logger.warn('[Reporter] Attempted to queue event with Agent disabled', caller: caller, event: event)
71
71
  return
72
72
  end
73
73
  return unless event
@@ -89,14 +89,16 @@ module Contrast
89
89
  # @return [Net::HTTPResponse, nil]
90
90
  def send_event_immediately event
91
91
  if ::Contrast::AGENT.disabled?
92
- logger.warn('Reporter attempted to send event immediately with Agent disabled', caller: caller, event: event)
92
+ logger.warn('[Reporter] attempted to send event immediately with Agent disabled',
93
+ caller: caller,
94
+ event: event)
93
95
  return
94
96
  end
95
97
  return unless event
96
98
 
97
99
  client.send_event(event, connection)
98
100
  rescue StandardError => e
99
- logger.error('Could not send message to TeamServer from Reporter queue.', e)
101
+ logger.error('[Reporter] Could not send message to TeamServer from reporting queue.', e)
100
102
  end
101
103
 
102
104
  def delete_queue!
@@ -126,7 +128,7 @@ module Contrast
126
128
  def connected?
127
129
  return true if client && connection
128
130
 
129
- logger.debug('No client/connection; sleeping', client: client, connection: connection)
131
+ logger.debug('[Reporter] No client/connection; sleeping...', client: client, connection: connection)
130
132
  sleep(5)
131
133
  false
132
134
  end
@@ -136,14 +138,14 @@ module Contrast
136
138
  client.send_event(event, connection)
137
139
  handle_resend(event) if client.mode.status == client.mode.resending
138
140
  rescue StandardError => e
139
- logger.error('Could not send message to TeamServer from Reporter queue.', e)
141
+ logger.error('[Reporter] Could not send message to TeamServer from reporting queue.', e)
140
142
  end
141
143
 
142
144
  # @return [Contrast::Agent::Telemetry::TelemetryException::Event]
143
145
  def queue_limit_telemetry_event
144
146
  message_exception = Contrast::Agent::Telemetry::TelemetryException::MessageException.build(
145
147
  'String',
146
- "Maximum queue size (#{ MAX_QUEUE_SIZE }) reached for reporting events", nil, stack_frame)
148
+ "[Reporter] Maximum queue size (#{ MAX_QUEUE_SIZE }) reached for reporting events", nil, stack_frame)
147
149
  message = Contrast::Agent::Telemetry::TelemetryException::Message.build({}, [message_exception])
148
150
  Contrast::Agent::Telemetry::TelemetryException::Event.new(message)
149
151
  end
@@ -153,8 +155,7 @@ module Contrast
153
155
  stack_frame_type = if stack_trace.nil? || stack_trace[1].nil?
154
156
  'none'
155
157
  else
156
- Contrast::Agent::Telemetry::TelemetryException::Obfuscate.obfuscate_type(
157
- stack_trace[1].path.delete_prefix(Dir.pwd))
158
+ stack_trace[1].path.delete_prefix(Dir.pwd)
158
159
  end
159
160
 
160
161
  stack_frame_function = stack_trace.nil? || stack_trace[1].nil? ? 'none' : stack_trace[1].label
@@ -14,6 +14,7 @@ module Contrast
14
14
  # system to report
15
15
  class ApplicationActivity < Contrast::Agent::Reporting::ApplicationReportingEvent
16
16
  include Contrast::Agent::Reporting::ResponseType
17
+ include Contrast::Components::Logger::InstanceMethods
17
18
 
18
19
  # @return [Integer]
19
20
  attr_accessor :query_count
@@ -55,8 +56,8 @@ module Contrast
55
56
 
56
57
  def to_controlled_hash
57
58
  hsh = { lastUpdate: since_last_update }
58
- hsh[:defend] = @_defend&.to_controlled_hash if @_defend
59
- hsh[:inventory] = @_inventory&.to_controlled_hash if @_inventory
59
+ hsh[:defend] = defend&.to_controlled_hash
60
+ hsh[:inventory] = inventory&.to_controlled_hash
60
61
  hsh
61
62
  end
62
63
 
@@ -94,9 +95,7 @@ module Contrast
94
95
  #
95
96
  # @return [Array<[Contrast::Agent::Reporting::ApplicationDefendAttackerActivity]>]
96
97
  def attack_results
97
- results = []
98
- defend.attackers.each { |a| results << a.protection_rules.values }
99
- results
98
+ defend.attackers.map { |a| a.protection_rules.values }
100
99
  end
101
100
 
102
101
  # This is primary used for attaching new data and merging existing
@@ -13,22 +13,34 @@ module Contrast
13
13
  # @return [Array<Contrast::Agent::Reporting::ApplicationDefendAttackerActivity>]
14
14
  attr_reader :attackers
15
15
 
16
+ # @return [Boolean]
17
+ attr_reader :existing_attacker_activity
18
+
16
19
  def initialize
17
20
  @attackers = []
21
+ @existing_attacker_activity = false
18
22
  @event_type = :application_defend_activity
19
23
  end
20
24
 
21
25
  def to_controlled_hash
26
+ validate
22
27
  {
23
28
  attackers: attackers.map(&:to_controlled_hash)
24
29
  }
25
30
  end
26
31
 
32
+ def validate
33
+ raise(ArgumentError, 'Attackers data must be populated') if existing_attacker_activity && attackers.empty?
34
+ end
35
+
27
36
  # @param attack_result [Contrast::Agent::Reporting::AttackResult]
28
37
  def attach_data attack_result
38
+ return unless attack_result&.cs__is_a?(Contrast::Agent::Reporting::AttackResult)
39
+
29
40
  attacker_activity = Contrast::Agent::Reporting::ApplicationDefendAttackerActivity.new
30
41
  attacker_activity.attach_data(attack_result)
31
42
 
43
+ @existing_attacker_activity = true
32
44
  if (existing_attacker_activity = find_existing_attacker_activity(attacker_activity))
33
45
  attach_existing(existing_attacker_activity, attacker_activity, attack_result.rule_id)
34
46
  else
@@ -37,7 +49,7 @@ module Contrast
37
49
  end
38
50
 
39
51
  # Find an existing attacker if it matches on source details
40
- # @param attacker_activity [Contrast::Agent::Reporting::ApplicationDefendAttackerActivity]
52
+ # @param new_attacker_activity [Contrast::Agent::Reporting::ApplicationDefendAttackerActivity]
41
53
  def find_existing_attacker_activity new_attacker_activity
42
54
  attackers.find do |existing|
43
55
  existing.source_forwarded_for == new_attacker_activity.source_forwarded_for &&
@@ -9,6 +9,7 @@ module Contrast
9
9
  module Reporting
10
10
  # This is the new ApplicationDefendAttackActivity class which will include the attacks sent by the source.
11
11
  class ApplicationDefendAttackActivity
12
+ include Contrast::Components::Logger::InstanceMethods
12
13
  # @return [Contrast::Agent::Reporting::ApplicationDefendAttackSampleActivity]
13
14
  attr_accessor :blocked
14
15
  # @return [Contrast::Agent::Reporting::ApplicationDefendAttackSampleActivity]
@@ -27,15 +28,29 @@ module Contrast
27
28
  end
28
29
 
29
30
  def to_controlled_hash
31
+ blocked_hash = blocked&.to_controlled_hash || Contrast::Utils::ObjectShare::EMPTY_HASH
32
+ exploited_hash = exploited&.to_controlled_hash || Contrast::Utils::ObjectShare::EMPTY_HASH
33
+ ineffective_hash = ineffective&.to_controlled_hash || Contrast::Utils::ObjectShare::EMPTY_HASH
34
+ suspicious_hash = suspicious&.to_controlled_hash || Contrast::Utils::ObjectShare::EMPTY_HASH
35
+ validate(blocked_hash, exploited_hash, ineffective_hash, suspicious_hash)
36
+
30
37
  {
31
38
  startTime: @start_time,
32
- blocked: @blocked&.to_controlled_hash || Contrast::Utils::ObjectShare::EMPTY_HASH,
33
- exploited: @exploited&.to_controlled_hash || Contrast::Utils::ObjectShare::EMPTY_HASH,
34
- ineffective: @ineffective&.to_controlled_hash || Contrast::Utils::ObjectShare::EMPTY_HASH,
35
- suspicious: @suspicious&.to_controlled_hash || Contrast::Utils::ObjectShare::EMPTY_HASH
39
+ blocked: blocked_hash,
40
+ exploited: exploited_hash,
41
+ ineffective: ineffective_hash,
42
+ suspicious: suspicious_hash
36
43
  }
37
44
  end
38
45
 
46
+ def validate blocked_hash, exploited_hash, ineffective_hash, suspicious_hash
47
+ return unless [blocked_hash, exploited_hash, ineffective_hash, suspicious_hash].all?(&:empty?)
48
+
49
+ raise(ArgumentError, 'Start Time for is not presented') unless start_time
50
+
51
+ raise(ArgumentError, 'At least one of the samples must be populated')
52
+ end
53
+
39
54
  # @param attack_result [Contrast::Agent::Reporting::AttackResult]
40
55
  # @return [Contrast::Agent::Reporting::Defend::AttackSampleActivity]
41
56
  def attach_data attack_result
@@ -57,7 +72,7 @@ module Contrast
57
72
  end
58
73
 
59
74
  def start_time
60
- Contrast::Agent::REQUEST_TRACKER.current&.timer&.start_ms
75
+ Contrast::Agent::REQUEST_TRACKER.current&.timer&.start_ms || 0
61
76
  end
62
77
  end
63
78
  end
@@ -3,7 +3,6 @@
3
3
 
4
4
  require 'contrast/agent/protect/rule/cmd_injection'
5
5
  require 'contrast/agent/protect/rule/deserialization'
6
- require 'contrast/agent/protect/rule/http_method_tampering'
7
6
  require 'contrast/agent/protect/rule/no_sqli'
8
7
  require 'contrast/agent/reporting/attack_result/user_input'
9
8
  require 'contrast/agent/reporting/attack_result/response_type'
@@ -24,6 +24,7 @@ module Contrast
24
24
  end
25
25
 
26
26
  def to_controlled_hash
27
+ validate
27
28
  {
28
29
  attackTimeMap: time_map,
29
30
  samples: samples.map(&:to_controlled_hash),
@@ -32,6 +33,10 @@ module Contrast
32
33
  }
33
34
  end
34
35
 
36
+ def validate
37
+ raise(ArgumentError, 'Start Time is not presented') unless @start_time
38
+ end
39
+
35
40
  # @param attack_result [Contrast::Agent::Reporting::AttackResult]
36
41
  def attach_data attack_result
37
42
  attack_result.samples.each do |attack_sample|
@@ -11,6 +11,7 @@ module Contrast
11
11
  # This is the new AttackerActivity class which will includes the attacker information discovered during this
12
12
  # activity period.
13
13
  class ApplicationDefendAttackerActivity
14
+ include Contrast::Components::Logger::InstanceMethods
14
15
  # @return [Hash<String,Contrast::Agent::Reporting::ApplicationDefendAttackActivity>] map of rule-id to violated
15
16
  # samples for that rule
16
17
  attr_accessor :protection_rules
@@ -32,8 +33,11 @@ module Contrast
32
33
  end
33
34
 
34
35
  def to_controlled_hash
36
+ processed_rules = process_protection_rules
37
+ validate(processed_rules)
38
+
35
39
  {
36
- protectionRules: process_protection_rules,
40
+ protectionRules: processed_rules,
37
41
  source: {
38
42
  ip: source_ip,
39
43
  xForwardedFor: source_forwarded_for
@@ -41,6 +45,11 @@ module Contrast
41
45
  }
42
46
  end
43
47
 
48
+ def validate processed_rules
49
+ raise(ArgumentError, 'Protection Rules are not presented') if processed_rules.empty?
50
+ raise(ArgumentError, 'Source is not presented') unless source_ip
51
+ end
52
+
44
53
  # @param attack_result [Contrast::Agent::Reporting::AttackResult]
45
54
  def attach_data attack_result
46
55
  @protection_rules[attack_result.rule_id] = Contrast::Agent::Reporting::ApplicationDefendAttackActivity.new.
@@ -22,7 +22,7 @@ module Contrast
22
22
  @event_method = :POST
23
23
  @event_endpoint = Contrast::Agent::Reporting::Endpoints.application_inventory
24
24
  # The API spec limits us to 500 routes, so we'll enforce that here to not hold on to superfulous data.
25
- @routes = Contrast::Agent.framework_manager.find_route_discovery_data.take(500)
25
+ @routes = Contrast::Agent.framework_manager.find_route_discovery_data&.take(500)
26
26
  super
27
27
  end
28
28
 
@@ -33,6 +33,7 @@ module Contrast
33
33
  def to_controlled_hash
34
34
  {
35
35
  session_id: ::Contrast::ASSESS.session_id,
36
+ # documentation lists routes as deprecated from the agent_ng_endpoints.yml
36
37
  routes: routes.map(&:to_controlled_hash)
37
38
  }
38
39
  end
@@ -17,10 +17,20 @@ module Contrast
17
17
  # The timestamp field is a bit of a misnomer. It's really the time, in ms, since the settings for this
18
18
  # application have been updated. If I've never updated, then it's been 0ms since then.
19
19
  #
20
+ #
20
21
  # @return [Integer]
21
22
  def since_last_update
22
23
  (update_time = Contrast::SETTINGS.last_app_update_ms) ? Contrast::Utils::Timer.now_ms - update_time : 0
23
24
  end
25
+
26
+ # Human readable last time update for header set. Set to 0 if the agent is just starting and have not received
27
+ # the latest header from TS.
28
+ #
29
+ #
30
+ # @return [String]
31
+ def since_last_update_httpdate
32
+ Contrast::SETTINGS.app_settings_last_httpdate || Contrast::Utils::Timer.ms_to_httpdate(0)
33
+ end
24
34
  end
25
35
  end
26
36
  end
@@ -0,0 +1,40 @@
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_reporting_event'
5
+ require 'contrast/agent/reporting/reporting_utilities/endpoints'
6
+ require 'contrast/utils/timer'
7
+
8
+ module Contrast
9
+ module Agent
10
+ module Reporting
11
+ # This class will initialize a GET request to be send to TS. The application settings endpoint is the way
12
+ # the Agent receives application sittings
13
+ class ApplicationSettings < Contrast::Agent::Reporting::ApplicationReportingEvent
14
+ def initialize
15
+ @event_method = :GET
16
+ @event_endpoint = Contrast::Agent::Reporting::Endpoints.application_settings
17
+ super
18
+ end
19
+
20
+ def file_name
21
+ 'application-settings'
22
+ end
23
+
24
+ # Attach the last server settings received timestamp to the request as it is required.
25
+ #
26
+ # If-Modified-Since: <day-name>, <day> <month> <year> <hour>:<minute>:<second> GMT
27
+ # @param request [Net::HTTPRequest]
28
+ def attach_headers request
29
+ request['If-Modified-Since'] = since_last_update_httpdate
30
+ end
31
+
32
+ # @return [Hash]
33
+ # @raise [ArgumentError]
34
+ def to_controlled_hash
35
+ {}
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end