contrast-agent 5.2.0 → 6.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (228) 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 +1 -2
  7. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +26 -12
  8. data/ext/cs__assess_module/cs__assess_module.c +1 -1
  9. data/ext/cs__assess_regexp/cs__assess_regexp.c +15 -2
  10. data/ext/cs__assess_regexp/cs__assess_regexp.h +2 -0
  11. data/ext/cs__assess_string/cs__assess_string.c +21 -1
  12. data/ext/cs__assess_test/cs__assess_test.h +9 -0
  13. data/ext/cs__assess_test/cs__assess_tests.c +22 -0
  14. data/ext/cs__assess_test/extconf.rb +5 -0
  15. data/ext/cs__common/cs__common.c +113 -11
  16. data/ext/cs__common/cs__common.h +29 -5
  17. data/ext/cs__contrast_patch/cs__contrast_patch.c +55 -44
  18. data/ext/cs__os_information/cs__os_information.c +13 -10
  19. data/ext/cs__scope/cs__scope.c +146 -97
  20. data/ext/cs__tests/cs__tests.c +12 -0
  21. data/ext/cs__tests/cs__tests.h +3 -0
  22. data/ext/cs__tests/extconf.rb +5 -0
  23. data/lib/contrast/agent/assess/contrast_object.rb +16 -16
  24. data/lib/contrast/agent/assess/events/source_event.rb +17 -19
  25. data/lib/contrast/agent/assess/finalizers/hash.rb +2 -0
  26. data/lib/contrast/agent/assess/policy/policy_node.rb +50 -27
  27. data/lib/contrast/agent/assess/policy/policy_node_utils.rb +51 -0
  28. data/lib/contrast/agent/assess/policy/policy_scanner.rb +2 -16
  29. data/lib/contrast/agent/assess/policy/preshift.rb +8 -2
  30. data/lib/contrast/agent/assess/policy/propagation_method.rb +47 -13
  31. data/lib/contrast/agent/assess/policy/propagator/buffer.rb +118 -0
  32. data/lib/contrast/agent/assess/policy/propagator/keep.rb +19 -4
  33. data/lib/contrast/agent/assess/policy/propagator/remove.rb +18 -2
  34. data/lib/contrast/agent/assess/policy/propagator/splat.rb +17 -3
  35. data/lib/contrast/agent/assess/policy/propagator/split.rb +15 -19
  36. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +1 -1
  37. data/lib/contrast/agent/assess/policy/propagator/substitution_utils.rb +1 -1
  38. data/lib/contrast/agent/assess/policy/propagator/trim.rb +1 -1
  39. data/lib/contrast/agent/assess/policy/propagator.rb +1 -0
  40. data/lib/contrast/agent/assess/policy/source_method.rb +7 -7
  41. data/lib/contrast/agent/assess/policy/trigger_method.rb +4 -10
  42. data/lib/contrast/agent/assess/property/tagged.rb +1 -1
  43. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +7 -2
  44. data/lib/contrast/agent/assess/rule/response/auto_complete_rule.rb +1 -1
  45. data/lib/contrast/agent/assess/rule/response/base_rule.rb +11 -3
  46. data/lib/contrast/agent/assess/rule/response/body_rule.rb +3 -3
  47. data/lib/contrast/agent/assess/rule/response/cache_control_header_rule.rb +60 -36
  48. data/lib/contrast/agent/assess/rule/response/framework/rails_support.rb +3 -3
  49. data/lib/contrast/agent/assess/rule/response/parameters_pollution_rule.rb +1 -1
  50. data/lib/contrast/agent/assess/rule/response/x_xss_protection_header_rule.rb +1 -2
  51. data/lib/contrast/agent/at_exit_hook.rb +1 -1
  52. data/lib/contrast/agent/deadzone/policy/deadzone_node.rb +0 -7
  53. data/lib/contrast/agent/deadzone/policy/policy.rb +0 -6
  54. data/lib/contrast/agent/exclusion_matcher.rb +3 -3
  55. data/lib/contrast/agent/inventory/database_config.rb +10 -3
  56. data/lib/contrast/agent/middleware.rb +5 -3
  57. data/lib/contrast/agent/patching/policy/after_load_patch.rb +0 -2
  58. data/lib/contrast/agent/patching/policy/patch.rb +13 -12
  59. data/lib/contrast/agent/patching/policy/patcher.rb +4 -4
  60. data/lib/contrast/agent/patching/policy/policy_node.rb +15 -2
  61. data/lib/contrast/agent/protect/exploitable_collection.rb +38 -0
  62. data/lib/contrast/agent/protect/input_analyzer/input_analyzer.rb +66 -9
  63. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +2 -1
  64. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +2 -2
  65. data/lib/contrast/agent/protect/rule/base.rb +37 -5
  66. data/lib/contrast/agent/protect/rule/base_service.rb +3 -1
  67. data/lib/contrast/agent/protect/rule/cmd_injection.rb +13 -0
  68. data/lib/contrast/agent/protect/rule/cmdi/cmdi_input_classification.rb +83 -0
  69. data/lib/contrast/agent/protect/rule/cmdi/cmdi_worth_watching.rb +64 -0
  70. data/lib/contrast/agent/protect/rule/http_method_tampering/http_method_tampering_input_classification.rb +96 -0
  71. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +8 -0
  72. data/lib/contrast/agent/protect/rule/no_sqli/no_sqli_input_classification.rb +231 -0
  73. data/lib/contrast/agent/protect/rule/no_sqli.rb +27 -0
  74. data/lib/contrast/agent/protect/rule/sqli/sqli_input_classification.rb +18 -54
  75. data/lib/contrast/agent/protect/rule/sqli/sqli_worth_watching.rb +1 -4
  76. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_input_classification.rb +82 -0
  77. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_matcher.rb +45 -0
  78. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +42 -0
  79. data/lib/contrast/agent/reporting/attack_result/attack_result.rb +63 -0
  80. data/lib/contrast/agent/reporting/attack_result/rasp_rule_sample.rb +52 -0
  81. data/lib/contrast/agent/reporting/attack_result/response_type.rb +29 -0
  82. data/lib/contrast/agent/reporting/attack_result/user_input.rb +87 -0
  83. data/lib/contrast/agent/reporting/masker/masker.rb +243 -0
  84. data/lib/contrast/agent/reporting/masker/masker_utils.rb +62 -0
  85. data/lib/contrast/agent/reporting/report.rb +2 -0
  86. data/lib/contrast/agent/reporting/reporter.rb +29 -22
  87. data/lib/contrast/agent/reporting/reporter_heartbeat.rb +49 -0
  88. data/lib/contrast/agent/reporting/reporting_events/agent_startup.rb +34 -0
  89. data/lib/contrast/agent/reporting/reporting_events/application_activity.rb +53 -0
  90. data/lib/contrast/agent/reporting/reporting_events/application_defend_activity.rb +48 -0
  91. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_activity.rb +64 -0
  92. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample.rb +70 -0
  93. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample_activity.rb +57 -0
  94. data/lib/contrast/agent/reporting/reporting_events/application_defend_attacker_activity.rb +56 -0
  95. data/lib/contrast/agent/reporting/reporting_events/application_inventory.rb +12 -4
  96. data/lib/contrast/agent/reporting/reporting_events/application_inventory_activity.rb +58 -0
  97. data/lib/contrast/agent/reporting/reporting_events/application_reporting_event.rb +27 -0
  98. data/lib/contrast/agent/reporting/reporting_events/application_startup.rb +50 -0
  99. data/lib/contrast/agent/reporting/reporting_events/application_startup_instrumentation.rb +27 -0
  100. data/lib/contrast/agent/reporting/reporting_events/application_update.rb +7 -12
  101. data/lib/contrast/agent/reporting/reporting_events/finding.rb +10 -4
  102. data/lib/contrast/agent/reporting/reporting_events/finding_event.rb +2 -4
  103. data/lib/contrast/agent/reporting/reporting_events/finding_event_object.rb +3 -3
  104. data/lib/contrast/agent/reporting/reporting_events/library_usage_observation.rb +5 -5
  105. data/lib/contrast/agent/reporting/reporting_events/observed_library_usage.rb +6 -2
  106. data/lib/contrast/agent/reporting/reporting_events/observed_route.rb +16 -12
  107. data/lib/contrast/agent/reporting/reporting_events/poll.rb +6 -2
  108. data/lib/contrast/agent/reporting/reporting_events/preflight.rb +10 -8
  109. data/lib/contrast/agent/reporting/reporting_events/preflight_message.rb +8 -11
  110. data/lib/contrast/agent/reporting/reporting_events/reporting_event.rb +2 -1
  111. data/lib/contrast/agent/reporting/reporting_events/route_coverage.rb +8 -6
  112. data/lib/contrast/agent/reporting/reporting_events/server_activity.rb +12 -20
  113. data/lib/contrast/agent/reporting/reporting_events/server_reporting_event.rb +27 -0
  114. data/lib/contrast/agent/reporting/reporting_utilities/audit.rb +17 -27
  115. data/lib/contrast/agent/reporting/reporting_utilities/build_preflight.rb +38 -0
  116. data/lib/contrast/agent/reporting/reporting_utilities/dtm_message.rb +8 -0
  117. data/lib/contrast/agent/reporting/reporting_utilities/endpoints.rb +6 -0
  118. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +23 -7
  119. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client_utils.rb +64 -76
  120. data/lib/contrast/agent/reporting/reporting_utilities/reporting_storage.rb +1 -1
  121. data/lib/contrast/agent/reporting/reporting_utilities/response.rb +17 -7
  122. data/lib/contrast/agent/reporting/reporting_utilities/response_extractor.rb +100 -0
  123. data/lib/contrast/agent/reporting/reporting_utilities/response_handler.rb +75 -13
  124. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_mode.rb +63 -0
  125. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_utils.rb +154 -113
  126. data/lib/contrast/agent/reporting/settings/application_settings.rb +9 -0
  127. data/lib/contrast/agent/reporting/settings/assess_server_feature.rb +5 -33
  128. data/lib/contrast/agent/reporting/settings/protect.rb +1 -1
  129. data/lib/contrast/agent/reporting/settings/protect_server_feature.rb +2 -2
  130. data/lib/contrast/agent/reporting/settings/sampling.rb +36 -0
  131. data/lib/contrast/agent/reporting/settings/sensitive_data_masking.rb +110 -0
  132. data/lib/contrast/agent/reporting/settings/sensitive_data_masking_rule.rb +58 -0
  133. data/lib/contrast/agent/request.rb +3 -3
  134. data/lib/contrast/agent/request_context.rb +1 -1
  135. data/lib/contrast/agent/request_context_extend.rb +2 -2
  136. data/lib/contrast/agent/request_handler.rb +7 -3
  137. data/lib/contrast/agent/response.rb +2 -0
  138. data/lib/contrast/agent/service_heartbeat.rb +6 -48
  139. data/lib/contrast/agent/static_analysis.rb +1 -1
  140. data/lib/contrast/agent/telemetry/base.rb +151 -0
  141. data/lib/contrast/agent/telemetry/events/event.rb +35 -0
  142. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_base.rb +59 -0
  143. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_event.rb +44 -0
  144. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message.rb +115 -0
  145. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message_exception.rb +83 -0
  146. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_stack_frame.rb +64 -0
  147. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exceptions.rb +20 -0
  148. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exceptions_report.rb +32 -0
  149. data/lib/contrast/agent/telemetry/events/metric_event.rb +28 -0
  150. data/lib/contrast/agent/telemetry/events/startup_metrics_event.rb +123 -0
  151. data/lib/contrast/agent/thread_watcher.rb +52 -68
  152. data/lib/contrast/agent/version.rb +1 -1
  153. data/lib/contrast/agent/worker_thread.rb +8 -0
  154. data/lib/contrast/agent.rb +4 -3
  155. data/lib/contrast/api/communication/messaging_queue.rb +28 -11
  156. data/lib/contrast/api/communication/response_processor.rb +7 -10
  157. data/lib/contrast/api/communication/speedracer.rb +1 -1
  158. data/lib/contrast/api/decorators/activity.rb +33 -0
  159. data/lib/contrast/api/decorators/address.rb +1 -1
  160. data/lib/contrast/api/decorators/http_request.rb +1 -1
  161. data/lib/contrast/api/decorators/response_type.rb +30 -0
  162. data/lib/contrast/api/decorators.rb +1 -0
  163. data/lib/contrast/components/app_context.rb +0 -4
  164. data/lib/contrast/components/assess.rb +14 -0
  165. data/lib/contrast/components/config.rb +13 -22
  166. data/lib/contrast/components/contrast_service.rb +9 -0
  167. data/lib/contrast/components/protect.rb +2 -2
  168. data/lib/contrast/components/sampling.rb +7 -11
  169. data/lib/contrast/components/settings.rb +116 -8
  170. data/lib/contrast/config/agent_configuration.rb +34 -41
  171. data/lib/contrast/config/api_configuration.rb +16 -75
  172. data/lib/contrast/config/api_proxy_configuration.rb +9 -48
  173. data/lib/contrast/config/application_configuration.rb +24 -95
  174. data/lib/contrast/config/assess_configuration.rb +21 -76
  175. data/lib/contrast/config/assess_rules_configuration.rb +13 -38
  176. data/lib/contrast/config/base_configuration.rb +11 -76
  177. data/lib/contrast/config/certification_configuration.rb +15 -68
  178. data/lib/contrast/config/exception_configuration.rb +15 -59
  179. data/lib/contrast/config/heap_dump_configuration.rb +19 -73
  180. data/lib/contrast/config/inventory_configuration.rb +11 -55
  181. data/lib/contrast/config/logger_configuration.rb +8 -41
  182. data/lib/contrast/config/protect_configuration.rb +23 -10
  183. data/lib/contrast/config/protect_rule_configuration.rb +23 -37
  184. data/lib/contrast/config/protect_rules_configuration.rb +39 -43
  185. data/lib/contrast/config/request_audit_configuration.rb +16 -55
  186. data/lib/contrast/config/root_configuration.rb +70 -13
  187. data/lib/contrast/config/ruby_configuration.rb +14 -47
  188. data/lib/contrast/config/sampling_configuration.rb +12 -65
  189. data/lib/contrast/config/server_configuration.rb +13 -45
  190. data/lib/contrast/config/service_configuration.rb +36 -17
  191. data/lib/contrast/configuration.rb +21 -13
  192. data/lib/contrast/extension/assess/string.rb +20 -1
  193. data/lib/contrast/extension/module.rb +0 -1
  194. data/lib/contrast/framework/manager.rb +2 -2
  195. data/lib/contrast/framework/rails/patch/support.rb +13 -45
  196. data/lib/contrast/logger/aliased_logging.rb +87 -0
  197. data/lib/contrast/logger/application.rb +1 -5
  198. data/lib/contrast/logger/cef_log.rb +1 -1
  199. data/lib/contrast/tasks/config.rb +100 -4
  200. data/lib/contrast/utils/assess/object_store.rb +36 -0
  201. data/lib/contrast/utils/assess/propagation_method_utils.rb +6 -0
  202. data/lib/contrast/utils/class_util.rb +5 -18
  203. data/lib/contrast/utils/input_classification.rb +73 -0
  204. data/lib/contrast/utils/invalid_configuration_util.rb +1 -1
  205. data/lib/contrast/utils/log_utils.rb +3 -1
  206. data/lib/contrast/utils/middleware_utils.rb +10 -9
  207. data/lib/contrast/utils/net_http_base.rb +1 -1
  208. data/lib/contrast/utils/object_share.rb +2 -1
  209. data/lib/contrast/utils/os.rb +0 -5
  210. data/lib/contrast/utils/patching/policy/patch_utils.rb +4 -5
  211. data/lib/contrast/utils/response_utils.rb +18 -33
  212. data/lib/contrast/utils/telemetry.rb +20 -2
  213. data/lib/contrast/utils/telemetry_client.rb +23 -11
  214. data/lib/contrast/utils/telemetry_hash.rb +41 -0
  215. data/lib/contrast/utils/telemetry_identifier.rb +16 -1
  216. data/lib/contrast.rb +9 -0
  217. data/resources/assess/policy.json +98 -0
  218. data/resources/deadzone/policy.json +0 -86
  219. data/ruby-agent.gemspec +10 -9
  220. data/service_executables/VERSION +1 -1
  221. data/service_executables/linux/contrast-service +0 -0
  222. data/service_executables/mac/contrast-service +0 -0
  223. metadata +98 -30
  224. data/lib/contrast/agent/metric_telemetry_event.rb +0 -26
  225. data/lib/contrast/agent/startup_metrics_telemetry_event.rb +0 -121
  226. data/lib/contrast/agent/telemetry.rb +0 -137
  227. data/lib/contrast/agent/telemetry_event.rb +0 -33
  228. data/lib/contrast/utils/exclude_key.rb +0 -20
@@ -1,50 +1,148 @@
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/reporting/reporting_utilities/response_extractor'
5
+
4
6
  module Contrast
5
7
  module Agent
6
8
  module Reporting
7
9
  # This module holds utilities required by the reporting service response handler
8
10
  module ResponseHandlerUtils
9
- ANALYZE_WHEN = '200'
10
- SLEEP_WHEN = %w[401 408 500].cs__freeze
11
+ include Contrast::Agent::Reporting::ResponseExtractor
12
+
13
+ ANALYZE_WHEN = %w[200 204].cs__freeze
14
+ ERROR_CODES = {
15
+ message_not_sent: '400',
16
+ access_forbidden: '401',
17
+ access_forbidden_no_action: '403',
18
+ application_do_not_exist: '404',
19
+ unprocessable_entity: '422',
20
+ too_many_requests: '429'
21
+ }.cs__freeze
22
+ APP_NON_EXISTENT_MSG = 'Application does not exist! Either it has not been created or has '\
23
+ 'been deleted or archived. '\
24
+ 'Disabling permanently.'
25
+ SUSPEND_MSG = 'Reporter is temporarily suspended.'
26
+ UNSUCCESSFULLY_RECEIVED_MSG = 'The Reporter was unable to send message.'
27
+ FORBIDDEN_MSG = 'Access was forbidden for current Report because the request authentication '\
28
+ 'information was not provided'
29
+ FORBIDDEN_NO_ACTION_MSG = 'Report access was forbidden because the supplied credentials failed '\
30
+ 'to authenticate the Agent'
31
+ UNPROCESSABLE_ENTITY_MSG = 'Reporter received Unprocessable Entity response. Disabling permanently.'
32
+ RETRY_AFTER_MSG = "There are too many requests of this type being sent by this Agent. #{ SUSPEND_MSG }"
11
33
 
12
34
  private
13
35
 
14
36
  # check if response code is valid before analyze it
15
37
  #
16
- # @param response_code [Integer]
38
+ # @param response [Net::HTTP::Response, nil]
17
39
  # @return [Boolean]
18
- def analyze_response_code? response_code
19
- # 200 Successful recording of the server record on the database. FeatureSet successfully returned
40
+ def analyze_response? response
41
+ # Code descriptions:
42
+ # 200:
43
+ # Message successfully received and there are new settings
44
+ # 204:
45
+ # Message successfully received and it's up to Contrast Server to decide what is done with the data.
46
+ # 304:
47
+ # Message successfully received and there are no new settings. Use your current ones.
48
+ #
49
+ # ERRORS:
50
+ # 400:
51
+ # Message unsuccessfully received. The Contrast Server was unable to process the message properly.
20
52
  # 401:
21
- # If any authentication errors occur. Agents should enter a suspended mode
22
- # (no reporting) and attempt to reconnect in 15 minutes.
23
- # 408:
24
- # If the FeatureSet from TeamServer cannot be constructed in time.
25
- # Agents should enter a suspended mode (no reporting) and attempt to reconnect in 15 minutes.
26
- # 500:
27
- # If any server error occurs. Agents should enter a suspended mode
28
- # (no reporting) and attempt to reconnect in 15 minutes.
29
- # 204, 304
30
- # Successful update of the server record on the database.
31
- # No FeatureSet update since the last time an activity was sent.
32
- @_sleep = true if SLEEP_WHEN.include? response_code
33
- return true if ANALYZE_WHEN.include? response_code
34
-
53
+ # Access was forbidden because the request authentication information was not provided.
54
+ # headers:
55
+ # www-Authenticate => Indicate that the API-Key and Authorization header are both required,
56
+ # in the standard format per RFC 2616
57
+ # 403:
58
+ # Access was forbidden because the supplied credentials failed to authenticate the Agent.
59
+ # 404:
60
+ # The application does not exist - either it has not been created or has been deleted or archived.
61
+ # If possible, the Agent should no longer analyze this application. For those Agents with multiple
62
+ # applications in a single process, at a minimum, cease reporting about this application.
63
+ # 422:
64
+ # Unprocessable Entity - The application startup is rejected because some piece of data is incorrect.
65
+ # The session_id could reference a non-existent session or the metadata (not session_metadata) could
66
+ # fail a constraint check. TeamServer should indicate this is an error message which the Agent should
67
+ # log. The Agent should no longer analyze this application.
68
+ # {
69
+ # "error": "string"
70
+ # }
71
+ # 429:
72
+ # There are too many requests of this type being sent by this Agent. Back off for the time listed in
73
+ # the Retry-After header. In this case, it is on the Agent to determine if it is safe to hold onto the
74
+ # data to attempt to send again or if it needs to be dropped. The Contrast Server can choose what to do
75
+ # with message from improperly throttled Agents, including dropping them.
76
+ # header:
77
+ # Retry-After => how long, in seconds, to wait before attempting to send another request to this endpoint,
78
+ # in the standard format per RFC 2616
79
+ # used for in observed routes message.
80
+ return false unless response && (response_code = response&.code)
81
+ return true if ANALYZE_WHEN.include?(response_code)
82
+
83
+ handle_error(response) if ERROR_CODES.value?(response_code)
84
+ # There was error, so analyze the Error and nothing more.
35
85
  false
36
86
  end
37
87
 
88
+ # Analyze the headers of the response code. They have information about the
89
+ # retry timeout and some response bodies contains error messages.
90
+ #
91
+ # @param response [String] the response code from Net::HTTPResponse, which is obnoxiousy a String, not an
92
+ # Integer
93
+ # @param message [String] Message to log.
94
+ # @param mode [Symbol, nil]
95
+ def handle_response_errors response, message, mode
96
+ # Set the current mode status.
97
+ @_mode.status = mode
98
+ ready_after, error_message, auth_error = extract_response_info response
99
+ # log, suspend, disable:
100
+ if mode == @_mode.running
101
+ log_debug_msg message,
102
+ response: response.__id__,
103
+ request: Contrast::Agent::REQUEST_TRACKER.current&.request&.type,
104
+ error_message: error_message || 'none',
105
+ auth_error: auth_error || 'none'
106
+ end
107
+ suspend_reporting message, ready_after, error_message if mode == @_mode.resending
108
+ return unless mode == @_mode.disabled
109
+
110
+ stop_reporting(message, application: Contrast::APP_CONTEXT.app_name, error_message: error_message)
111
+ rescue StandardError => e
112
+ logger.debug('Could not handle Response error information', error: e)
113
+ end
114
+
115
+ # Extract what we've received.
116
+ #
117
+ # @param response [Net::HTTP::Response, nil]
118
+ # @return [Array<String, Integer>] all collected error info.
119
+ def extract_response_info response
120
+ # Extract what we got from the response:
121
+ ready_after = response['Ready-After'] if response.to_hash.keys.map(&:downcase).include?('ready-after')
122
+ if response.to_hash.keys.map(&:downcase).include?('www-authenticate')
123
+ auth_error = response['www-Authenticate']
124
+ end
125
+ error_message = response.message
126
+ [ready_after.to_i, error_message, auth_error]
127
+ end
128
+
129
+ # Cease reporting about this application
130
+ #
131
+ # @param message [String] Message to log
132
+ # @param info_hash [Hash] information about the context to log.
133
+ def stop_reporting message, info_hash
134
+ Contrast::Agent.reporter&.stop!
135
+ log_debug_msg(message, info_hash)
136
+ ::Contrast::AGENT.disable!
137
+ end
138
+
38
139
  # Applies the settings from the TS response
39
140
  #
40
141
  # @param response [Contrast::Agent::Reporting::Response]
41
142
  def update_agent_settings response
42
- if response.server_features
43
- log_file = response.server_features.log_file
44
- log_level = response.server_features.log_level
45
- Contrast::Logger::Log.instance.update(log_file, log_level) if log_file && log_level
46
- Contrast::SETTINGS.update_from_server_features(response)
47
- end
143
+ return unless response
144
+
145
+ Contrast::SETTINGS.update_from_server_features(response) if response.server_features
48
146
  Contrast::SETTINGS.update_from_application_settings(response) if response.application_settings
49
147
  end
50
148
 
@@ -58,19 +156,14 @@ module Contrast
58
156
 
59
157
  response.application_settings.reactions.each do |reaction|
60
158
  # The enums are all uppercase, we need to downcase them before attempting to log.
61
- level = if reaction.level.nil?
62
- :error
63
- else
64
- reaction.level.downcase
65
- end
66
-
159
+ level = reaction.level.nil? ? :error : reaction.level.downcase
67
160
  logger.with_level(level, reaction.message) if reaction.message
68
161
 
69
162
  case reaction.operation
70
- when Contrast::Agent::Reporting::Settings::Reaction::OPERATIONS.second
163
+ when Contrast::Agent::Reporting::Settings::Reaction::OPERATIONS[1]
71
164
  # DISABLED
72
- Contrast::Agent::DisableReaction.run reaction, level
73
- when Contrast::Agent::Reporting::Settings::Reaction::OPERATIONS.first
165
+ Contrast::Agent::DisableReaction.run(reaction, level)
166
+ when Contrast::Agent::Reporting::Settings::Reaction::OPERATIONS[0]
74
167
  # NOOP
75
168
  else
76
169
  logger.warn('ReactionProcessor received a reaction with an unknown operation',
@@ -79,6 +172,24 @@ module Contrast
79
172
  end
80
173
  end
81
174
 
175
+ # This can't go in the Settings component because protect and assess depend on settings
176
+ # I don't think it should go into contrast_service because that only handles connection specific data.
177
+ #
178
+ # @param response [Contrast::Agent::Reporting::Response]
179
+ def update_ruleset response
180
+ logger.info('Updating features from TeamServer')
181
+ return unless response&.server_features || response&.application_settings
182
+ return unless ::Contrast::AGENT.enabled?
183
+
184
+ logger.trace_with_time('Rebuilding rule modes from TeamServer') do
185
+ ::Contrast::SETTINGS.build_protect_rules if ::Contrast::PROTECT.enabled?
186
+ ::Contrast::AGENT.reset_ruleset
187
+ logger.info('Current rule settings:')
188
+ ::Contrast::PROTECT.rules.each { |k, v| logger.info('Protect Rule mode set', rule: k, mode: v.mode) }
189
+ logger.info('Disabled Assess Rules', rules: ::Contrast::ASSESS.disabled_rules)
190
+ end
191
+ end
192
+
82
193
  # Converts response from Net to Reporting Response object
83
194
  #
84
195
  # @param response [Net::HTTP::Response, nil]
@@ -94,12 +205,14 @@ module Contrast
94
205
  # check if response contains application settings or Feature settings
95
206
  if response_data[:settings]
96
207
  # the response contains ApplicationSettings
97
- logger.trace('Agent: Received updated application settings')
98
- build_application_settings response_data
208
+ app_settings = build_application_settings(response_data)
209
+ logger.trace('Agent: Received updated application settings', raw: response_data, processed: app_settings)
210
+ app_settings
99
211
  else
100
212
  # the response contains FeatureSettings
101
- logger.trace('Agent: Received updated server features')
102
- build_feature_settings response_data
213
+ server_features = build_feature_settings(response_data)
214
+ logger.trace('Agent: Received updated application settings', raw: response_data, processed: server_features)
215
+ server_features
103
216
  end
104
217
  rescue StandardError => e
105
218
  logger.error('Unable to convert response', e)
@@ -109,18 +222,19 @@ module Contrast
109
222
  # @param response_data [Hash]
110
223
  # @return res [Contrast::Agent::Reporting::Response]
111
224
  def build_application_settings response_data
112
- res = Contrast::Agent::Reporting::Response.new
225
+ res = Contrast::Agent::Reporting::Response.application_response
113
226
  extract_assess response_data, res
114
227
  extract_protect response_data, res
115
228
  extract_exclusions response_data, res
116
229
  extract_reactions response_data, res
230
+ extract_sensitive_data_policy response_data, res
117
231
  res
118
232
  end
119
233
 
120
234
  # @param response_data [Hash]
121
235
  # @return res [Contrast::Agent::Reporting::Response]
122
236
  def build_feature_settings response_data
123
- res = Contrast::Agent::Reporting::Response.new
237
+ res = Contrast::Agent::Reporting::Response.server_response
124
238
  extract_assess_server_features response_data, res
125
239
  extract_protect_server_features response_data, res
126
240
  extract_protect_lists response_data, res
@@ -129,79 +243,6 @@ module Contrast
129
243
  res.server_features.telemetry = response_data[:telemetry]
130
244
  res
131
245
  end
132
-
133
- # @param response_data [Hash]
134
- # @return res [Contrast::Agent::Reporting::Response]
135
- def extract_assess response_data, res
136
- assessments = response_data[:settings][:assessment]
137
- return unless assessments
138
-
139
- res.application_settings.assess.disabled_rules = assessments[:disabledRules]
140
- res.application_settings.assess.session_id = assessments[:session_id]
141
- end
142
-
143
- # @param response_data [Hash]
144
- # @return res [Contrast::Agent::Reporting::Response]
145
- def extract_protect response_data, res
146
- protect = response_data[:settings][:defend]
147
- return unless protect
148
-
149
- res.application_settings.protect.protection_rules = protect[:protectionRules]
150
- res.application_settings.protect.virtual_patches = protect[:virtualPatches]
151
- end
152
-
153
- # @param response_data [Hash]
154
- # @return res [Contrast::Agent::Reporting::Response]
155
- def extract_exclusions response_data, res
156
- exclusions = response_data[:settings][:exceptions]
157
- return unless exclusions
158
-
159
- res.application_settings.exclusions.code_exclusions = exclusions[:codeExceptions]
160
- res.application_settings.exclusions.input_exclusions = exclusions[:inputExceptions]
161
- res.application_settings.exclusions.url_exclusions = exclusions[:urlExceptions]
162
- end
163
-
164
- # @param response_data [Hash]
165
- # @return res [Contrast::Agent::Reporting::Response]
166
- def extract_reactions response_data, res
167
- res.application_settings.reactions = response_data[:settings][:reactions]
168
- end
169
-
170
- # @param response_data [Hash]
171
- # @return res [Contrast::Agent::Reporting::Response]
172
- def extract_assess_server_features response_data, res
173
- assess = response_data[:assessment]
174
- return unless assess
175
-
176
- res.server_features.assess.enabled = assess[:enabled]
177
- res.server_features.assess.disabled_rules = assess[:disabledRules]
178
- res.server_features.assess.sampling = assess[:sampling]
179
- res.server_features.assess.sanitizers = assess[:sanitizers]
180
- res.server_features.assess.validators = assess[:validators]
181
- end
182
-
183
- # @param response_data [Hash]
184
- # @return res [Contrast::Agent::Reporting::Response]
185
- def extract_protect_server_features response_data, res
186
- protect = response_data[:defend]
187
- return unless protect
188
-
189
- res.server_features.protect.enabled = protect[:enabled]
190
- res.server_features.protect.bot_blocker = protect[:bot_blocker]
191
- res.server_features.protect.syslog = protect[:syslog]
192
- end
193
-
194
- # @param response_data [Hash]
195
- # @return res [Contrast::Agent::Reporting::Response]
196
- def extract_protect_lists response_data, res
197
- protect = response_data[:defend]
198
- return unless protect
199
-
200
- res.server_features.protect.ip_allowlist = protect[:ipAllowlist]
201
- res.server_features.protect.ip_denylist = protect[:ipDenyList]
202
- res.server_features.protect.log_enchancers = protect[:logEnhancers]
203
- res.server_features.protect.rule_definition_list = protect[:ruleDefinitionList]
204
- end
205
246
  end
206
247
  end
207
248
  end
@@ -5,6 +5,7 @@ require 'contrast/agent/reporting/settings/assess'
5
5
  require 'contrast/agent/reporting/settings/protect'
6
6
  require 'contrast/agent/reporting/settings/exclusions'
7
7
  require 'contrast/agent/reporting/settings/reaction'
8
+ require 'contrast/agent/reporting/settings/sensitive_data_masking'
8
9
 
9
10
  module Contrast
10
11
  module Agent
@@ -43,6 +44,14 @@ module Contrast
43
44
  @_reactions
44
45
  end
45
46
 
47
+ # This object will hold the masking rules send from TS.
48
+ #
49
+ # @return sensitive_data_masking [Contrast::Agent::Reporting::Settings::SensitiveDataMasking] this object
50
+ # includes mask_http_body flag and Array set of rules with keywords in them.
51
+ def sensitive_data_masking
52
+ @_sensitive_data_masking ||= Contrast::Agent::Reporting::Settings::SensitiveDataMasking.new
53
+ end
54
+
46
55
  # Set the reaction
47
56
  #
48
57
  # @param reactions [Array<Reaction>] {
@@ -2,6 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/utils/object_share'
5
+ require 'contrast/agent/reporting/settings/sampling'
5
6
 
6
7
  module Contrast
7
8
  module Agent
@@ -11,19 +12,6 @@ module Contrast
11
12
  # Application level settings for the Assess featureset.
12
13
  # Used for the FeatureSet TS response
13
14
  class AssessServerFeature
14
- # Assess rules to disable for this application.
15
- #
16
- # @return disabled_rules [Array<AssessRuleID>] Array with string rule_ids
17
- def disabled_rules
18
- @_disabled_rules ||= []
19
- end
20
-
21
- # @param disabled_rules [Array<AssessRuleID>] with AssessRuleID strings
22
- # @return disabled_rules [Array<AssessRuleID>] Array with string rule_ids
23
- def disabled_rules= disabled_rules
24
- @_disabled_rules = disabled_rules if disabled_rules.is_a? Array
25
- end
26
-
27
15
  # Indicate if the assess feature set is enabled for this server or not.
28
16
  #
29
17
  # @return enabled [Boolean]
@@ -41,17 +29,9 @@ module Contrast
41
29
 
42
30
  # Used to control the sampling feature in the agent.
43
31
  #
44
- # @return sampling [Hash<AssessSampling>] Hash of AssessSampling: {
45
- # baseline [Integer] The number of baseline requests to take before switching to sampling
46
- # for the window.
47
- # enabled [Boolean] If the sampling feature should be used or not.
48
- # frequency [Integer] The number of requests to skip before observing during the sampling
49
- # window after the baseline.
50
- # responseFrequency [Integer]
51
- # window [Integer]
52
- # }
32
+ # @return [Contrast::Agent::Reporting::Settings::Sampling]
53
33
  def sampling
54
- @_sampling ||= {}
34
+ @_sampling ||= Contrast::Agent::Reporting::Settings::Sampling.new({})
55
35
  end
56
36
 
57
37
  # set sampling
@@ -65,17 +45,9 @@ module Contrast
65
45
  # responseFrequency [Integer]
66
46
  # window [Integer]
67
47
  # }
68
- # @return sampling [Hash<AssessSampling>] Hash of AssessSampling: {
69
- # baseline [Integer] The number of baseline requests to take before switching to sampling
70
- # for the window.
71
- # enabled [Boolean] If the sampling feature should be used or not.
72
- # frequency [Integer] The number of requests to skip before observing during the sampling
73
- # window after the baseline.
74
- # responseFrequency [Integer]
75
- # window [Integer]
76
- # }
48
+ # @return [Contrast::Agent::Reporting::Settings::Sampling]
77
49
  def sampling= sampling
78
- @_sampling = sampling if sampling.is_a? Hash
50
+ @_sampling = Contrast::Agent::Reporting::Settings::Sampling.new(sampling) if sampling.is_a? Hash
79
51
  end
80
52
 
81
53
  # The sanitizers defined by the user for use by the agent on this server for this organization.
@@ -73,7 +73,7 @@ module Contrast
73
73
  #
74
74
  # @return rules [Hash<RULE_ID => MODE>, nil] Hash with rule_id as key and mode as value
75
75
  def protection_rules_to_settings_hash
76
- return if protection_rules.empty?
76
+ return {} if protection_rules.empty?
77
77
 
78
78
  modes_by_id = {}
79
79
  protection_rules.each do |rule|
@@ -23,7 +23,7 @@ module Contrast
23
23
  # @param enabled [Boolean]
24
24
  # @return enabled [Boolean]
25
25
  def enabled= enabled
26
- @_enabled = enabled if !!enabled == enabled
26
+ @_enabled = enabled
27
27
  end
28
28
 
29
29
  # Indicate if the bot protection feature set is enabled for this server or not.
@@ -191,7 +191,7 @@ module Contrast
191
191
  # value [String] }
192
192
  # }
193
193
  def rule_definition_list= list
194
- @_rule_definition_list = list.is_a? Array
194
+ @_rule_definition_list = list if list.is_a? Array
195
195
  end
196
196
 
197
197
  # Controls for the syslogging feature in the agent.
@@ -0,0 +1,36 @@
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/utils/object_share'
5
+
6
+ module Contrast
7
+ module Agent
8
+ module Reporting
9
+ module Settings
10
+ # Sampling controls for the Assess Features provided by TeamServer
11
+ class Sampling
12
+ # @return [Integer] The number of baseline requests to take before switching to sampling for the window.
13
+ attr_reader :baseline
14
+ # @return [Boolean] If the sampling feature should be used or not.
15
+ attr_reader :enabled
16
+ # @return [Integer] The number of requests to skip before observing during the sampling window after the
17
+ # baseline.
18
+ attr_reader :request_frequency
19
+ # @return [Integer] The number of responses to skip before observing during the sampling window after the
20
+ # baseline.
21
+ attr_reader :response_frequency
22
+ # @return [Integer] The length of time for which the sample period is valid, in ms.
23
+ attr_reader :window_ms
24
+
25
+ def initialize hsh
26
+ @baseline = hsh[:baseline]
27
+ @enabled = hsh[:enabled]
28
+ @request_frequency = hsh[:frequency]
29
+ @response_frequency = hsh[:responseFrequency]
30
+ @window_ms = hsh[:window]
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,110 @@
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/settings/sensitive_data_masking_rule'
5
+
6
+ module Contrast
7
+ module Agent
8
+ module Reporting
9
+ # This module will hold all the settings from the TS responce
10
+ module Settings
11
+ # Protect level settings for the sensitive_data_masking_policy.
12
+ # Configuration of the masking parameters will be delivered dynamically from TeamServer
13
+ # during an ApplicationSettings response from requests to TS endpoints that return that
14
+ # structure.
15
+ # https://contrast.atlassian.net/wiki/spaces/~699189087/pages/807960614/Sensitive+Data+Masking+Design
16
+ class SensitiveDataMasking
17
+ # Policy flag to enable the use of masking on request body
18
+ # Here is set to defaults to true.
19
+ #
20
+ # @return true | false
21
+ def mask_http_body?
22
+ @_mask_http_body
23
+ end
24
+
25
+ # Set the flag for request body masking
26
+ #
27
+ # @param bool [Boolean]
28
+ # @return true | false
29
+ def mask_http_body= bool
30
+ @_mask_http_body = bool.nil? ? true : !!bool
31
+ end
32
+
33
+ # Policy flag to enable the use of masking on attack vector.
34
+ # Here is set to defaults to false.
35
+ #
36
+ # @return true | false
37
+ def mask_attack_vector?
38
+ @_mask_attack_vector
39
+ end
40
+
41
+ # Set the flag for using masking on attack vector
42
+ #
43
+ # @param bool [Boolean]
44
+ # @return true | false
45
+ def mask_attack_vector= bool
46
+ @_mask_attack_vector = !!bool
47
+ end
48
+
49
+ # Rules to follow when using the masking
50
+ #
51
+ # @return rules [Array<Contrast::Agent::Reporting::Settings::SensitiveDataMaskingRule>, []]
52
+ def rules
53
+ @_rules ||= Contrast::Agent::Reporting::Settings::RulesArray.new
54
+ end
55
+
56
+ # Assign rules array
57
+ #
58
+ # @param rules [Array<Contrast::Agent::Reporting::Settings::SensitiveDataMaskingRule>]
59
+ # @return rules [Array<Contrast::Agent::Reporting::Settings::SensitiveDataMaskingRule>, []]
60
+ def rules= rules
61
+ @_rules = rules if rules_array? rules
62
+ end
63
+
64
+ # Build rules from hash
65
+ #
66
+ # @param settings_rules [Hash] Response settings under Settings/sensitive_data_masking_policy/rules
67
+ # @return rules [Array<Contrast::Agent::Reporting::Settings::SensitiveDataMaskingRule>, nil
68
+ def build_rules_form_settings settings_rules
69
+ return unless settings_rules || settings_rules.empty?
70
+
71
+ settings_rules.each do |rule|
72
+ instance = Contrast::Agent::Reporting::Settings::SensitiveDataMaskingRule.new
73
+ instance.rule_id = rule[:id]
74
+ instance.keywords = rule[:keywords]
75
+ rules << instance
76
+ end
77
+ rules
78
+ end
79
+
80
+ private
81
+
82
+ # Determine if parameter is array of Rules.
83
+ #
84
+ # @param array [Array<Contrast::Agent::Reporting::Settings::SensitiveDataMaskingRule>] Array of keywords.
85
+ # @return true | false
86
+ def rules_array? array
87
+ return false unless array.is_a?(Array)
88
+
89
+ array.all?(Contrast::Agent::Reporting::Settings::SensitiveDataMaskingRule)
90
+ end
91
+ end
92
+
93
+ # Simple validation class for Rules Array.
94
+ class RulesArray < Array
95
+ # Do not push anything except Rules instances.
96
+ # :<< method is called on object directly so this
97
+ # will make sure the data pushed is as expected.
98
+ #
99
+ # @param item [Contrast::Agent::Reporting::Settings::SensitiveDataMaskingRule]
100
+ # @return itself [Array<Contrast::Agent::Reporting::Settings::SensitiveDataMaskingRule>]
101
+ def << item
102
+ return itself unless item.instance_of? Contrast::Agent::Reporting::Settings::SensitiveDataMaskingRule
103
+
104
+ super
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,58 @@
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/utils/object_share'
5
+
6
+ module Contrast
7
+ module Agent
8
+ module Reporting
9
+ # This module will hold all the settings from the TS responce
10
+ module Settings
11
+ # Protect level settings for the sensitive_data_masking_policy Rule entry.
12
+ class SensitiveDataMaskingRule
13
+ # Id for this rule.
14
+ #
15
+ # @return rule_id [String] Name of parameter to mask.
16
+ def rule_id
17
+ @_rule_id ||= Contrast::Utils::ObjectShare::EMPTY_STRING
18
+ end
19
+
20
+ # Set the rule name.
21
+ #
22
+ # @param id [String] Name of parameter to mask.
23
+ # @return rule_id [String]
24
+ def rule_id= id
25
+ @_rule_id = id.to_s
26
+ end
27
+
28
+ # Array of keywords to mask values for the rule_id.
29
+ #
30
+ # @return keywords [Array<String>] set of keywords
31
+ def keywords
32
+ @_keywords ||= []
33
+ end
34
+
35
+ # Set keywords array.
36
+ #
37
+ # @param words_array [Array<String>]
38
+ # @return keywords [Array<String>] set of keywords
39
+ def keywords= words_array
40
+ @_keywords = words_array if string_array? words_array
41
+ end
42
+
43
+ private
44
+
45
+ # Determine if a array is array of strings.
46
+ #
47
+ # @param array [Array<String] Array of keywords.
48
+ # @return true | false
49
+ def string_array? array
50
+ return false unless array.is_a?(Array) && array.any?
51
+
52
+ array.all? String
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end