contrast-agent 5.1.0 → 6.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (218) hide show
  1. checksums.yaml +4 -4
  2. data/ext/cs__assess_array/cs__assess_array.c +7 -0
  3. data/ext/cs__assess_basic_object/cs__assess_basic_object.c +19 -5
  4. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.c +1 -1
  5. data/ext/cs__assess_hash/cs__assess_hash.c +3 -4
  6. data/ext/cs__assess_kernel/cs__assess_kernel.c +7 -5
  7. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +26 -12
  8. data/ext/cs__assess_module/cs__assess_module.c +7 -7
  9. data/ext/cs__assess_string/cs__assess_string.c +13 -1
  10. data/ext/cs__common/cs__common.c +16 -11
  11. data/ext/cs__common/cs__common.h +1 -0
  12. data/ext/cs__contrast_patch/cs__contrast_patch.c +100 -64
  13. data/ext/cs__contrast_patch/cs__contrast_patch.h +2 -0
  14. data/ext/cs__os_information/cs__os_information.c +13 -10
  15. data/ext/cs__scope/cs__scope.c +796 -0
  16. data/ext/cs__scope/cs__scope.h +88 -0
  17. data/ext/cs__scope/extconf.rb +5 -0
  18. data/lib/contrast/agent/assess/contrast_event.rb +20 -13
  19. data/lib/contrast/agent/assess/contrast_object.rb +4 -1
  20. data/lib/contrast/agent/assess/finalizers/hash.rb +2 -0
  21. data/lib/contrast/agent/assess/policy/policy_node.rb +50 -27
  22. data/lib/contrast/agent/assess/policy/policy_node_utils.rb +51 -0
  23. data/lib/contrast/agent/assess/policy/preshift.rb +8 -2
  24. data/lib/contrast/agent/assess/policy/propagation_method.rb +47 -13
  25. data/lib/contrast/agent/assess/policy/propagation_node.rb +2 -5
  26. data/lib/contrast/agent/assess/policy/propagator/buffer.rb +118 -0
  27. data/lib/contrast/agent/assess/policy/propagator/keep.rb +19 -4
  28. data/lib/contrast/agent/assess/policy/propagator/match_data.rb +2 -0
  29. data/lib/contrast/agent/assess/policy/propagator/remove.rb +18 -2
  30. data/lib/contrast/agent/assess/policy/propagator/splat.rb +17 -3
  31. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +1 -1
  32. data/lib/contrast/agent/assess/policy/propagator/substitution_utils.rb +1 -1
  33. data/lib/contrast/agent/assess/policy/propagator/trim.rb +1 -1
  34. data/lib/contrast/agent/assess/policy/propagator.rb +1 -0
  35. data/lib/contrast/agent/assess/policy/source_method.rb +7 -7
  36. data/lib/contrast/agent/assess/policy/trigger_method.rb +6 -1
  37. data/lib/contrast/agent/assess/property/tagged.rb +1 -1
  38. data/lib/contrast/agent/assess/rule/response/{autocomplete_rule.rb → auto_complete_rule.rb} +4 -3
  39. data/lib/contrast/agent/assess/rule/response/base_rule.rb +12 -79
  40. data/lib/contrast/agent/assess/rule/response/body_rule.rb +109 -0
  41. data/lib/contrast/agent/assess/rule/response/cache_control_header_rule.rb +157 -0
  42. data/lib/contrast/agent/assess/rule/response/click_jacking_header_rule.rb +26 -0
  43. data/lib/contrast/agent/assess/rule/response/csp_header_insecure_rule.rb +14 -15
  44. data/lib/contrast/agent/assess/rule/response/csp_header_missing_rule.rb +5 -25
  45. data/lib/contrast/agent/assess/rule/response/framework/rails_support.rb +29 -0
  46. data/lib/contrast/agent/assess/rule/response/header_rule.rb +70 -0
  47. data/lib/contrast/agent/assess/rule/response/hsts_header_rule.rb +12 -36
  48. data/lib/contrast/agent/assess/rule/response/parameters_pollution_rule.rb +2 -1
  49. data/lib/contrast/agent/assess/rule/response/x_content_type_header_rule.rb +26 -0
  50. data/lib/contrast/agent/assess/rule/response/x_xss_protection_header_rule.rb +35 -0
  51. data/lib/contrast/agent/deadzone/policy/deadzone_node.rb +0 -7
  52. data/lib/contrast/agent/deadzone/policy/policy.rb +0 -6
  53. data/lib/contrast/agent/exclusion_matcher.rb +3 -3
  54. data/lib/contrast/agent/middleware.rb +4 -1
  55. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +1 -3
  56. data/lib/contrast/agent/patching/policy/patch.rb +2 -6
  57. data/lib/contrast/agent/patching/policy/patcher.rb +4 -4
  58. data/lib/contrast/agent/patching/policy/policy_node.rb +15 -2
  59. data/lib/contrast/agent/protect/exploitable_collection.rb +38 -0
  60. data/lib/contrast/agent/protect/input_analyzer/input_analyzer.rb +147 -0
  61. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +2 -1
  62. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +2 -2
  63. data/lib/contrast/agent/protect/rule/base.rb +61 -2
  64. data/lib/contrast/agent/protect/rule/base_service.rb +12 -1
  65. data/lib/contrast/agent/protect/rule/cmd_injection.rb +15 -0
  66. data/lib/contrast/agent/protect/rule/cmdi/cmdi_input_classification.rb +83 -0
  67. data/lib/contrast/agent/protect/rule/cmdi/cmdi_worth_watching.rb +64 -0
  68. data/lib/contrast/agent/protect/rule/deserialization.rb +6 -0
  69. data/lib/contrast/agent/protect/rule/http_method_tampering/http_method_tampering_input_classification.rb +96 -0
  70. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +13 -1
  71. data/lib/contrast/agent/protect/rule/no_sqli/no_sqli_input_classification.rb +231 -0
  72. data/lib/contrast/agent/protect/rule/no_sqli.rb +28 -0
  73. data/lib/contrast/agent/protect/rule/path_traversal.rb +1 -0
  74. data/lib/contrast/agent/protect/rule/sqli/sqli_input_classification.rb +88 -0
  75. data/lib/contrast/agent/protect/rule/sqli/sqli_worth_watching.rb +118 -0
  76. data/lib/contrast/agent/protect/rule/sqli.rb +33 -0
  77. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_input_classification.rb +82 -0
  78. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_matcher.rb +45 -0
  79. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +42 -0
  80. data/lib/contrast/agent/protect/rule/xxe.rb +4 -0
  81. data/lib/contrast/agent/reporting/attack_result/attack_result.rb +63 -0
  82. data/lib/contrast/agent/reporting/attack_result/rasp_rule_sample.rb +52 -0
  83. data/lib/contrast/agent/reporting/attack_result/response_type.rb +29 -0
  84. data/lib/contrast/agent/reporting/attack_result/user_input.rb +87 -0
  85. data/lib/contrast/agent/reporting/input_analysis/input_analysis.rb +44 -0
  86. data/lib/contrast/agent/reporting/input_analysis/input_analysis_result.rb +115 -0
  87. data/lib/contrast/agent/reporting/input_analysis/input_type.rb +44 -0
  88. data/lib/contrast/agent/reporting/input_analysis/score_level.rb +21 -0
  89. data/lib/contrast/agent/reporting/masker/masker.rb +246 -0
  90. data/lib/contrast/agent/reporting/masker/masker_utils.rb +58 -0
  91. data/lib/contrast/agent/reporting/report.rb +3 -0
  92. data/lib/contrast/agent/reporting/reporter.rb +31 -12
  93. data/lib/contrast/agent/reporting/reporting_events/agent_startup.rb +30 -0
  94. data/lib/contrast/agent/reporting/reporting_events/application_inventory.rb +7 -3
  95. data/lib/contrast/agent/reporting/reporting_events/application_startup.rb +40 -0
  96. data/lib/contrast/agent/reporting/reporting_events/application_startup_instrumentation.rb +27 -0
  97. data/lib/contrast/agent/reporting/reporting_events/finding.rb +69 -36
  98. data/lib/contrast/agent/reporting/reporting_events/finding_event.rb +88 -59
  99. data/lib/contrast/agent/reporting/reporting_events/{finding_object.rb → finding_event_object.rb} +24 -20
  100. data/lib/contrast/agent/reporting/reporting_events/finding_event_parent_object.rb +39 -0
  101. data/lib/contrast/agent/reporting/reporting_events/finding_event_property.rb +40 -0
  102. data/lib/contrast/agent/reporting/reporting_events/{finding_signature.rb → finding_event_signature.rb} +29 -24
  103. data/lib/contrast/agent/reporting/reporting_events/finding_event_source.rb +12 -8
  104. data/lib/contrast/agent/reporting/reporting_events/{finding_stack.rb → finding_event_stack.rb} +23 -19
  105. data/lib/contrast/agent/reporting/reporting_events/{finding_taint_range.rb → finding_event_taint_range.rb} +17 -15
  106. data/lib/contrast/agent/reporting/reporting_events/finding_request.rb +26 -53
  107. data/lib/contrast/agent/reporting/reporting_events/library_usage_observation.rb +5 -5
  108. data/lib/contrast/agent/reporting/reporting_events/observed_route.rb +9 -9
  109. data/lib/contrast/agent/reporting/reporting_events/poll.rb +29 -0
  110. data/lib/contrast/agent/reporting/reporting_events/preflight_message.rb +2 -1
  111. data/lib/contrast/agent/reporting/reporting_events/reporting_event.rb +6 -4
  112. data/lib/contrast/agent/reporting/reporting_events/route_coverage.rb +8 -6
  113. data/lib/contrast/agent/reporting/reporting_events/route_discovery.rb +1 -0
  114. data/lib/contrast/agent/reporting/reporting_events/server_activity.rb +1 -1
  115. data/lib/contrast/agent/reporting/reporting_utilities/audit.rb +10 -3
  116. data/lib/contrast/agent/reporting/reporting_utilities/endpoints.rb +0 -1
  117. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +17 -5
  118. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client_utils.rb +54 -45
  119. data/lib/contrast/agent/reporting/reporting_utilities/reporting_storage.rb +1 -1
  120. data/lib/contrast/agent/reporting/reporting_utilities/response_extractor.rb +97 -0
  121. data/lib/contrast/agent/reporting/reporting_utilities/response_handler.rb +69 -7
  122. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_mode.rb +63 -0
  123. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_utils.rb +123 -85
  124. data/lib/contrast/agent/reporting/settings/application_settings.rb +9 -0
  125. data/lib/contrast/agent/reporting/settings/assess_server_feature.rb +5 -33
  126. data/lib/contrast/agent/reporting/settings/protect_server_feature.rb +1 -1
  127. data/lib/contrast/agent/reporting/settings/sampling.rb +36 -0
  128. data/lib/contrast/agent/reporting/settings/sensitive_data_masking.rb +110 -0
  129. data/lib/contrast/agent/reporting/settings/sensitive_data_masking_rule.rb +58 -0
  130. data/lib/contrast/agent/request_context.rb +7 -2
  131. data/lib/contrast/agent/request_context_extend.rb +85 -21
  132. data/lib/contrast/agent/request_handler.rb +4 -0
  133. data/lib/contrast/agent/scope.rb +102 -107
  134. data/lib/contrast/agent/service_heartbeat.rb +45 -2
  135. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_base.rb +51 -0
  136. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_event.rb +36 -0
  137. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message.rb +97 -0
  138. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message_exception.rb +65 -0
  139. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_stack_frame.rb +47 -0
  140. data/lib/contrast/agent/{metric_telemetry_event.rb → telemetry/events/metric_telemetry_event.rb} +1 -1
  141. data/lib/contrast/agent/{startup_metrics_telemetry_event.rb → telemetry/events/startup_metrics_telemetry_event.rb} +3 -3
  142. data/lib/contrast/agent/{telemetry_event.rb → telemetry/events/telemetry_event.rb} +1 -1
  143. data/lib/contrast/agent/{telemetry.rb → telemetry/telemetry.rb} +32 -19
  144. data/lib/contrast/agent/thread_watcher.rb +1 -1
  145. data/lib/contrast/agent/version.rb +1 -1
  146. data/lib/contrast/agent.rb +3 -0
  147. data/lib/contrast/api/communication/speedracer.rb +1 -1
  148. data/lib/contrast/api/decorators/address.rb +1 -1
  149. data/lib/contrast/api/decorators/bot_blocker.rb +37 -0
  150. data/lib/contrast/api/decorators/ip_denylist.rb +37 -0
  151. data/lib/contrast/api/decorators/rasp_rule_sample.rb +29 -0
  152. data/lib/contrast/api/decorators/response_type.rb +30 -0
  153. data/lib/contrast/api/decorators/user_input.rb +11 -1
  154. data/lib/contrast/api/decorators/virtual_patch.rb +34 -0
  155. data/lib/contrast/api/decorators.rb +1 -0
  156. data/lib/contrast/components/app_context.rb +0 -4
  157. data/lib/contrast/components/assess.rb +14 -0
  158. data/lib/contrast/components/logger.rb +5 -0
  159. data/lib/contrast/components/protect.rb +6 -4
  160. data/lib/contrast/components/sampling.rb +7 -11
  161. data/lib/contrast/components/scope.rb +98 -91
  162. data/lib/contrast/components/settings.rb +106 -8
  163. data/lib/contrast/config/agent_configuration.rb +41 -12
  164. data/lib/contrast/config/api_configuration.rb +37 -12
  165. data/lib/contrast/config/api_proxy_configuration.rb +12 -3
  166. data/lib/contrast/config/application_configuration.rb +38 -14
  167. data/lib/contrast/config/assess_configuration.rb +47 -12
  168. data/lib/contrast/config/assess_rules_configuration.rb +15 -3
  169. data/lib/contrast/config/base_configuration.rb +18 -50
  170. data/lib/contrast/config/certification_configuration.rb +17 -3
  171. data/lib/contrast/config/exception_configuration.rb +14 -3
  172. data/lib/contrast/config/heap_dump_configuration.rb +43 -17
  173. data/lib/contrast/config/inventory_configuration.rb +17 -3
  174. data/lib/contrast/config/logger_configuration.rb +10 -3
  175. data/lib/contrast/config/protect_configuration.rb +17 -7
  176. data/lib/contrast/config/protect_rule_configuration.rb +17 -8
  177. data/lib/contrast/config/protect_rules_configuration.rb +115 -17
  178. data/lib/contrast/config/request_audit_configuration.rb +26 -3
  179. data/lib/contrast/config/root_configuration.rb +52 -12
  180. data/lib/contrast/config/ruby_configuration.rb +60 -22
  181. data/lib/contrast/config/sampling_configuration.rb +19 -9
  182. data/lib/contrast/config/server_configuration.rb +19 -10
  183. data/lib/contrast/config/service_configuration.rb +27 -11
  184. data/lib/contrast/configuration.rb +5 -3
  185. data/lib/contrast/extension/assess/string.rb +20 -1
  186. data/lib/contrast/extension/module.rb +0 -1
  187. data/lib/contrast/framework/manager.rb +2 -2
  188. data/lib/contrast/logger/application.rb +1 -1
  189. data/lib/contrast/logger/cef_log.rb +151 -0
  190. data/lib/contrast/tasks/config.rb +90 -3
  191. data/lib/contrast/utils/assess/object_store.rb +36 -0
  192. data/lib/contrast/utils/assess/propagation_method_utils.rb +6 -0
  193. data/lib/contrast/utils/class_util.rb +3 -12
  194. data/lib/contrast/utils/hash_digest.rb +14 -6
  195. data/lib/contrast/utils/input_classification.rb +73 -0
  196. data/lib/contrast/utils/log_utils.rb +114 -0
  197. data/lib/contrast/utils/middleware_utils.rb +9 -9
  198. data/lib/contrast/utils/net_http_base.rb +13 -10
  199. data/lib/contrast/utils/object_share.rb +2 -1
  200. data/lib/contrast/utils/os.rb +0 -5
  201. data/lib/contrast/utils/patching/policy/patch_utils.rb +4 -9
  202. data/lib/contrast/utils/response_utils.rb +18 -33
  203. data/lib/contrast/utils/telemetry.rb +1 -1
  204. data/lib/contrast/utils/telemetry_client.rb +1 -1
  205. data/lib/contrast/utils/telemetry_identifier.rb +1 -1
  206. data/lib/contrast.rb +4 -3
  207. data/resources/assess/policy.json +98 -0
  208. data/resources/deadzone/policy.json +0 -86
  209. data/ruby-agent.gemspec +9 -8
  210. data/service_executables/VERSION +1 -1
  211. data/service_executables/linux/contrast-service +0 -0
  212. data/service_executables/mac/contrast-service +0 -0
  213. metadata +103 -38
  214. data/lib/contrast/agent/assess/rule/response/cachecontrol_rule.rb +0 -184
  215. data/lib/contrast/agent/assess/rule/response/clickjacking_rule.rb +0 -66
  216. data/lib/contrast/agent/assess/rule/response/x_content_type_rule.rb +0 -52
  217. data/lib/contrast/agent/assess/rule/response/x_xss_protection_rule.rb +0 -53
  218. data/lib/contrast/extension/kernel.rb +0 -54
@@ -3,6 +3,7 @@
3
3
 
4
4
  require 'contrast/components/logger'
5
5
  require 'contrast/components/scope'
6
+ require 'contrast/agent/reporting/reporting_events/application_startup'
6
7
  require 'contrast/agent/reporting/reporting_utilities/reporter_client'
7
8
  require 'contrast/agent/reporting/reporting_utilities/endpoints'
8
9
 
@@ -15,8 +16,41 @@ module Contrast
15
16
  include Contrast::Components::Scope::InstanceMethods
16
17
  include Contrast::Agent::Reporting::Endpoints
17
18
 
19
+ # List the events that need to be sent when agent starts up
20
+ STARTUP_EVENTS = [
21
+ Contrast::Agent::Reporting::ApplicationStartup,
22
+ Contrast::Agent::Reporting::AgentStartup
23
+ ].cs__freeze
24
+
25
+ # @param event [Contrast::Agent::Reporting::Preflight] the preflight we handle here
26
+ # @param response [Net::HTTP::Response,nil] The response we handle and read from
27
+ # @param connection [Net::HTTP] open connection
28
+ def handle_response event, response, connection
29
+ return unless event && response && connection
30
+ return unless event.cs__is_a?(Contrast::Agent::Reporting::Preflight)
31
+
32
+ preflight_message = event.messages[0]
33
+ # for handling multiple findings
34
+ # we'll only extract the indexes without *
35
+ # findings_to_return = response.body.split(',').delete_if { |el| el.include?('*') }
36
+ # after that we'll do some magic and return them the same way we do for corresponding_finding
37
+ corresponding_finding = Contrast::Agent::Reporting::ReportingStorage.delete(preflight_message.hash_code)
38
+ return unless corresponding_finding
39
+
40
+ audit&.audit_event(corresponding_finding, response) if ::Contrast::API.request_audit_enable?
41
+ send_event(corresponding_finding, connection, send_immediately: true)
42
+ nil
43
+ rescue StandardError => e
44
+ logger.error('Unable to handle response', e)
45
+ end
46
+
18
47
  private
19
48
 
49
+ # TODO: RUBY-1466 find better home for this?
50
+ def audit
51
+ @_audit ||= Contrast::Agent::Reporting::Audit.new
52
+ end
53
+
20
54
  # This method will build headers of the request required for TS communication
21
55
  #
22
56
  # @param request [Net::HTTPRequest]
@@ -65,38 +99,28 @@ module Contrast
65
99
  # Handles response processing and sets status
66
100
  #
67
101
  # @param response [Net::HTTP::Response]
68
- # @return response [Net::HTTP::Response]
69
102
  def process_response response
70
- response_handler.process response
103
+ response_handler.process(response)
71
104
  logger.debug('Successfully sent startup messages to service.')
72
105
  status.success!
73
- response
74
106
  end
75
107
 
76
108
  # build the request headers and assign endpoint
77
109
  #
78
- # @param event event [Contrast::Api::Dtm::AgentStartup, Contrast::Agent::Reporting::ReportingEvent]
110
+ # @param event event [Contrast::Agent::Reporting::ReportingEvent]
79
111
  # One of the DTMs valid for the event field of Contrast::Api::Dtm::Message|Contrast::Api::Dtm::Activity
80
112
  # @return [Net::HTTP::Post,Net::HTTP::Put]
81
113
  def build_request event
82
114
  with_contrast_scope do
83
- request = if event.cs__is_a?(Contrast::Agent::Reporting::ReportingEvent)
84
- case event.event_method
85
- when :PUT
86
- Net::HTTP::Put.new(event.event_endpoint)
87
- when :POST
88
- Net::HTTP::Post.new(event.event_endpoint)
89
- end
90
- else # TODO: RUBY-1438 -- remove
91
- Net::HTTP::Put.new(Contrast::Agent::Reporting::Endpoints::NG_ENDPOINTS[:agent_startup])
115
+ request = case event.event_method
116
+ when :PUT
117
+ Net::HTTP::Put.new(event.event_endpoint)
118
+ else # :POST
119
+ Net::HTTP::Post.new(event.event_endpoint)
92
120
  end
93
121
  build_headers(request)
94
- if event.cs__is_a?(Contrast::Agent::Reporting::ReportingEvent)
95
- event.attach_headers(request)
96
- request.body = event.to_controlled_hash.to_json
97
- else
98
- request.body = event.to_json
99
- end
122
+ event.attach_headers(request)
123
+ request.body = event.to_controlled_hash.to_json
100
124
  request
101
125
  end
102
126
  end
@@ -104,15 +128,17 @@ module Contrast
104
128
  # Send Agent Startup event
105
129
  #
106
130
  # @param connection [Net::HTTP] open connection
107
- # @return response [Net::HTTP::Response] response from TS
108
131
  def send_agent_startup connection
109
132
  logger.debug('Preparing to send startup messages')
110
- startup = Contrast::APP_CONTEXT.build_agent_startup_message
111
- request = build_request(startup)
112
- response = connection.request(request)
113
- process_response(response)
114
- rescue StandardError => e
115
- handle_error(startup, e)
133
+
134
+ STARTUP_EVENTS.each do |event|
135
+ startup_event = event.new
136
+ request = build_request(startup_event)
137
+ response = connection.request(request)
138
+ process_response(response)
139
+ rescue StandardError => e
140
+ handle_error(startup_event, e)
141
+ end
116
142
  end
117
143
 
118
144
  # Sent different events to different endpoints
@@ -120,34 +146,17 @@ module Contrast
120
146
  # @param event [Contrast::Api::Dtm,Contrast::Agent::Reporting::ReportingEvent] Main reporting event, all events
121
147
  # inherit it
122
148
  # @param connection [Net::HTTP] open connection
149
+ # @return [Net::HTTP::Response]
123
150
  def send_events event, connection
124
151
  request = build_request(event)
125
152
  response = connection.request(request)
126
153
  process_response(response)
154
+ response
127
155
  rescue StandardError => e
128
156
  handle_error(event, e)
129
157
  end
130
158
 
131
159
  # Eventually here we'll handle more response types and etc
132
-
133
- # @param event [Contrast::Agent::Reporting::Preflight] the preflight we handle here
134
- # @param response [Net::HTTP::Response,nil] The response we handle and read from
135
- # @param connection [Net::HTTP] open connection
136
- def handle_response event, response, connection
137
- return unless event || response || connection
138
- return unless event.cs__is_a?(Contrast::Agent::Reporting::Preflight)
139
-
140
- preflight_message = event.messages[0]
141
- # for handling multiple findings
142
- # we'll only extract the indexes without *
143
- # findings_to_return = response.body.split(',').delete_if { |el| el.include?('*') }
144
- # after that we'll do some magic and return them the same way we do for corresponding_finding
145
- corresponding_finding = Contrast::Agent::Reporting::ReportingStorage.delete(preflight_message.hash_code)
146
- return unless corresponding_finding
147
-
148
- send_event corresponding_finding, connection, true
149
- nil
150
- end
151
160
  end
152
161
  end
153
162
  end
@@ -53,7 +53,7 @@ module Contrast
53
53
  end
54
54
 
55
55
  # @param rule_id [String] the rule_id
56
- # @return [Hash, nil] return array with key and value of the pair
56
+ # @return [Array, nil] return array with key and value of the pair
57
57
  def find_by_rule_id rule_id
58
58
  return unless rule_id
59
59
 
@@ -0,0 +1,97 @@
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
+ module Contrast
5
+ module Agent
6
+ module Reporting
7
+ # This module will hold the methods for TS response conversion to settings objects.
8
+ module ResponseExtractor
9
+ private
10
+
11
+ # @param response_data [Hash]
12
+ # @param res [Contrast::Agent::Reporting::Response]
13
+ def extract_assess response_data, res
14
+ assessments = response_data[:settings][:assessment]
15
+ return unless assessments
16
+
17
+ res.application_settings.assess.disabled_rules = assessments[:disabledRules]
18
+ res.application_settings.assess.session_id = assessments[:sessionId]
19
+ end
20
+
21
+ # @param response_data [Hash]
22
+ # @param res [Contrast::Agent::Reporting::Response]
23
+ def extract_protect response_data, res
24
+ protect = response_data[:settings][:defend]
25
+ return unless protect
26
+
27
+ res.application_settings.protect.protection_rules = protect[:protectionRules]
28
+ res.application_settings.protect.virtual_patches = protect[:virtualPatches]
29
+ end
30
+
31
+ # @param response_data [Hash]
32
+ # @param res [Contrast::Agent::Reporting::Response]
33
+ def extract_exclusions response_data, res
34
+ exclusions = response_data[:settings][:exceptions]
35
+ return unless exclusions
36
+
37
+ res.application_settings.exclusions.code_exclusions = exclusions[:codeExceptions]
38
+ res.application_settings.exclusions.input_exclusions = exclusions[:inputExceptions]
39
+ res.application_settings.exclusions.url_exclusions = exclusions[:urlExceptions]
40
+ end
41
+
42
+ # @param response_data [Hash]
43
+ # @param res [Contrast::Agent::Reporting::Response]
44
+ def extract_reactions response_data, res
45
+ res.application_settings.reactions = response_data[:settings][:reactions]
46
+ end
47
+
48
+ # @param response_data [Hash]
49
+ # @param res [Contrast::Agent::Reporting::Response]
50
+ def extract_assess_server_features response_data, res
51
+ assess = response_data[:assessment]
52
+ return unless assess
53
+
54
+ res.server_features.assess.enabled = assess[:enabled]
55
+ res.server_features.assess.sampling = assess[:sampling]
56
+ res.server_features.assess.sanitizers = assess[:sanitizers]
57
+ res.server_features.assess.validators = assess[:validators]
58
+ end
59
+
60
+ # @param response_data [Hash]
61
+ # @param res [Contrast::Agent::Reporting::Response]
62
+ def extract_protect_server_features response_data, res
63
+ protect = response_data[:defend]
64
+ return unless protect
65
+
66
+ res.server_features.protect.enabled = protect[:enabled]
67
+ res.server_features.protect.bot_blocker = protect[:bot_blocker]
68
+ res.server_features.protect.syslog = protect[:syslog]
69
+ end
70
+
71
+ # @param response_data [Hash]
72
+ # @param res [Contrast::Agent::Reporting::Response]
73
+ def extract_protect_lists response_data, res
74
+ protect = response_data[:defend]
75
+ return unless protect
76
+
77
+ res.server_features.protect.ip_allowlist = protect[:ipAllowlist]
78
+ res.server_features.protect.ip_denylist = protect[:ipDenyList]
79
+ res.server_features.protect.log_enchancers = protect[:logEnhancers]
80
+ res.server_features.protect.rule_definition_list = protect[:ruleDefinitionList]
81
+ end
82
+
83
+ # Here we extract the rules and state for the sensitive data masking policy
84
+ # Received from TS.
85
+ #
86
+ # @param response_data [Hash]
87
+ # @param res [Contrast::Agent::Reporting::Response]
88
+ def extract_sensitive_data_policy response_data, res
89
+ sensitive_data = response_data[:settings][:sensitive_data_masking_policy]
90
+ res.application_settings.sensitive_data_masking.mask_http_body = sensitive_data[:mask_http_body]
91
+ res.application_settings.sensitive_data_masking.mask_attack_vector = sensitive_data[:mask_attack_vector]
92
+ res.application_settings.sensitive_data_masking.build_rules_form_settings sensitive_data[:rules]
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
@@ -5,6 +5,7 @@ require 'contrast/api/communication/response_processor'
5
5
  require 'contrast/api/decorators/application_settings'
6
6
  require 'contrast/agent/reporting/reporting_utilities/response'
7
7
  require 'contrast/agent/reporting/reporting_utilities/response_handler_utils'
8
+ require 'contrast/agent/reporting/reporting_utilities/response_handler_mode'
8
9
  require 'contrast/api/decorators/server_features'
9
10
  require 'contrast/components/logger'
10
11
  require 'json'
@@ -16,41 +17,102 @@ module Contrast
16
17
  class ResponseHandler < Contrast::Api::Communication::ResponseProcessor
17
18
  include Contrast::Components::Logger::InstanceMethods
18
19
  include Contrast::Agent::Reporting::ResponseHandlerUtils
20
+ # 15 min
21
+ TIMEOUT = 900.cs__freeze
22
+
19
23
  # Process the response from TS
20
24
  #
21
25
  # @param response [Net::HTTP::Response, nil]
22
26
  # @return response [Net::HTTP::Response, nil]
23
27
  def process response
24
28
  logger.debug('Reporter Received a response')
25
- return unless analyze_response_code? response&.code
29
+ return unless analyze_response? response
26
30
 
27
- # handle the response body and obtain server_features or app_settings
31
+ # Handle the response body and obtain server_features or app_settings
28
32
  report_response = convert_response response
29
33
 
30
34
  return unless report_response
31
35
 
32
36
  update_agent_settings report_response
33
37
  update_reaction report_response
34
- # this one is called from super needs to be placed here after protobuf
35
- update_features report_response.server_features, report_response.application_settings
38
+ # This one is called from super needs to be placed here after protobuf end of life.
39
+ update_features(report_response.server_features, report_response.application_settings)
36
40
  logger.trace('Agent settings updated in response to Service', protect_on: ::Contrast::PROTECT.enabled?,
37
41
  assess_on: ::Contrast::ASSESS.enabled?)
38
42
  response
39
43
  rescue StandardError => e
40
- logger.error(e.message)
44
+ logger.error('Unable to process response from TeamServer', e)
41
45
  nil
42
46
  end
43
47
 
44
- # if sleep is true puts reporting service to sleep
48
+ # If sleep is true puts reporting service to sleep.
45
49
  def sleep?
46
50
  @_sleep = wake_up if @_sleep.nil?
47
51
  @_sleep
48
52
  end
49
53
 
50
- # wakes the reporting service
54
+ # For how long the agent should wait until retry
55
+ # to send message
56
+ #
57
+ # @return timeout [Integer] the time for reporter
58
+ # to be suspended.
59
+ def timeout
60
+ @_timeout ||= TIMEOUT
61
+ end
62
+
63
+ def mode
64
+ @_mode ||= Contrast::Agent::Reporting::ResponseHandlerMode.new
65
+ end
66
+
67
+ # Wakes the reporting service
51
68
  def wake_up
52
69
  @_sleep = false
53
70
  end
71
+
72
+ private
73
+
74
+ # Handles the errors code received from TS and takes appropriate action.
75
+ # If we are here the response.code is an error that needs handling [4XX]
76
+ #
77
+ # @param response [Net::HTTP::Response]
78
+ def handle_error response
79
+ case response&.code
80
+ when ERROR_CODES[:message_not_sent]
81
+ handle_response_errors response, UNSUCCESSFULLY_RECEIVED_MSG, mode.running
82
+ when ERROR_CODES[:access_forbidden]
83
+ handle_response_errors response, FORBIDDEN_MSG, mode.running
84
+ when ERROR_CODES[:access_forbidden_no_action]
85
+ handle_response_errors response, FORBIDDEN_NO_ACTION_MSG, mode.running
86
+ when ERROR_CODES[:application_do_not_exist]
87
+ handle_response_errors response, APP_NON_EXISTENT_MSG, mode.disabled
88
+ when ERROR_CODES[:unprocessable_entity]
89
+ handle_response_errors response, UNPROCESSABLE_ENTITY_MSG, mode.disabled
90
+ when ERROR_CODES[:too_many_requests]
91
+ handle_response_errors response, RETRY_AFTER_MSG, mode.resending
92
+ else
93
+ logger.debug('Response Error code could not be processed')
94
+ end
95
+ end
96
+
97
+ # Suspend the reporter and try again in time. If not set
98
+ # the timeout is set to 15 min As default:
99
+ #
100
+ # @param message [String] Message to log.
101
+ # @param timeout [Integer,nil] The timeout to wait and retry after.
102
+ # @param error_message [String, nil] Error message if any received.
103
+ def suspend_reporting message, timeout, error_message
104
+ @_timeout = timeout || Contrast::Agent::Reporting::ResponseHandler::TIMEOUT
105
+ log_debug_msg message, timeout: @_timeout, error_message: error_message || 'none'
106
+ @_sleep = true
107
+ end
108
+
109
+ # Log what we've received.
110
+ #
111
+ # @param message [String] Message to log
112
+ # @param info_hash [Hash] information about the context to log.
113
+ def log_debug_msg message, info_hash
114
+ logger.debug(message, info_hash)
115
+ end
54
116
  end
55
117
  end
56
118
  end
@@ -0,0 +1,63 @@
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
+ module Contrast
5
+ module Agent
6
+ module Reporting
7
+ # This class will hold the mode state in which the reporter will send
8
+ # event ot TS.
9
+ class ResponseHandlerMode
10
+ # Reader for the running mode type.
11
+ #
12
+ # @return [Symbol] Reporter's client and Response Handler are running
13
+ # and responses are being processed.
14
+ attr_reader :running
15
+ # Reader for the disabled mode type.
16
+ #
17
+ # @return [Symbol] Reporter's client and Response Handler are disabled
18
+ # due to received Error. The Reporting is disabled for this application.
19
+ attr_reader :disabled
20
+ # Reader for the resending/retry mode type.
21
+ #
22
+ # @return [Symbol] Reporter's client and Response Handler are suspended
23
+ # for a TS received amount of time and will try to resend the previous
24
+ # request after that.
25
+ attr_reader :resending
26
+ # Reader for all supported modes.
27
+ #
28
+ # @return [Array<Symbol>] Reporter's client and Response Handler all
29
+ # available modes of operation.
30
+ attr_reader :modes
31
+
32
+ def initialize
33
+ @running = :running
34
+ @disabled = :disabled
35
+ @resending = :resending
36
+ @modes = [@running, @disabled, @resending].cs__freeze
37
+ end
38
+
39
+ # Current mode.
40
+ #
41
+ # @return [Symbol] Reporter's client and Response Handler
42
+ # current mode of operation.
43
+ def status
44
+ @_status ||= running
45
+ end
46
+
47
+ # Set current mode.
48
+ #
49
+ # @return [Symbol] Reporter's client and Response Handler
50
+ # current mode of operation.
51
+ def status= mode
52
+ @_status = mode if mode.cs__is_a?(Symbol) && modes.include?(mode)
53
+ end
54
+
55
+ # Reset mode
56
+ #
57
+ def reset_mode
58
+ @_status = running
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end