contrast-agent 5.3.0 → 6.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (306) hide show
  1. checksums.yaml +4 -4
  2. data/.simplecov +1 -1
  3. data/Rakefile +1 -1
  4. data/ext/build_funchook.rb +3 -3
  5. data/ext/cs__assess_array/cs__assess_array.c +7 -0
  6. data/ext/cs__assess_basic_object/cs__assess_basic_object.c +24 -6
  7. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.c +1 -1
  8. data/ext/cs__assess_hash/cs__assess_hash.c +3 -4
  9. data/ext/cs__assess_kernel/cs__assess_kernel.c +1 -2
  10. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +26 -12
  11. data/ext/cs__assess_module/cs__assess_module.c +1 -1
  12. data/ext/cs__assess_regexp/cs__assess_regexp.c +15 -2
  13. data/ext/cs__assess_regexp/cs__assess_regexp.h +2 -0
  14. data/ext/cs__assess_string/cs__assess_string.c +21 -1
  15. data/ext/cs__assess_test/cs__assess_test.h +9 -0
  16. data/ext/cs__assess_test/cs__assess_tests.c +22 -0
  17. data/ext/cs__assess_test/extconf.rb +5 -0
  18. data/ext/cs__common/cs__common.c +113 -11
  19. data/ext/cs__common/cs__common.h +29 -5
  20. data/ext/cs__contrast_patch/cs__contrast_patch.c +55 -44
  21. data/ext/cs__os_information/cs__os_information.c +13 -10
  22. data/ext/cs__scope/cs__scope.c +146 -97
  23. data/ext/cs__tests/cs__tests.c +12 -0
  24. data/ext/cs__tests/cs__tests.h +3 -0
  25. data/ext/cs__tests/extconf.rb +5 -0
  26. data/ext/extconf_common.rb +1 -1
  27. data/lib/contrast/agent/assess/contrast_object.rb +16 -16
  28. data/lib/contrast/agent/assess/events/source_event.rb +17 -19
  29. data/lib/contrast/agent/assess/finalizers/hash.rb +4 -2
  30. data/lib/contrast/agent/assess/policy/policy.rb +9 -10
  31. data/lib/contrast/agent/assess/policy/policy_node.rb +58 -36
  32. data/lib/contrast/agent/assess/policy/policy_node_utils.rb +51 -0
  33. data/lib/contrast/agent/assess/policy/policy_scanner.rb +2 -16
  34. data/lib/contrast/agent/assess/policy/preshift.rb +8 -2
  35. data/lib/contrast/agent/assess/policy/propagation_method.rb +48 -14
  36. data/lib/contrast/agent/assess/policy/propagation_node.rb +2 -3
  37. data/lib/contrast/agent/assess/policy/propagator/base.rb +1 -1
  38. data/lib/contrast/agent/assess/policy/propagator/buffer.rb +119 -0
  39. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +1 -1
  40. data/lib/contrast/agent/assess/policy/propagator/keep.rb +19 -4
  41. data/lib/contrast/agent/assess/policy/propagator/remove.rb +18 -2
  42. data/lib/contrast/agent/assess/policy/propagator/splat.rb +17 -3
  43. data/lib/contrast/agent/assess/policy/propagator/split.rb +17 -21
  44. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +1 -1
  45. data/lib/contrast/agent/assess/policy/propagator/substitution_utils.rb +1 -1
  46. data/lib/contrast/agent/assess/policy/propagator/trim.rb +2 -2
  47. data/lib/contrast/agent/assess/policy/propagator.rb +1 -0
  48. data/lib/contrast/agent/assess/policy/source_method.rb +7 -7
  49. data/lib/contrast/agent/assess/policy/source_node.rb +1 -1
  50. data/lib/contrast/agent/assess/policy/trigger_method.rb +11 -17
  51. data/lib/contrast/agent/assess/policy/trigger_node.rb +16 -16
  52. data/lib/contrast/agent/assess/policy/trigger_validation/redos_validator.rb +1 -1
  53. data/lib/contrast/agent/assess/property/evented.rb +2 -2
  54. data/lib/contrast/agent/assess/property/tagged.rb +3 -3
  55. data/lib/contrast/agent/assess/rule/provider/hardcoded_key.rb +6 -8
  56. data/lib/contrast/agent/assess/rule/provider/hardcoded_password.rb +6 -7
  57. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +12 -7
  58. data/lib/contrast/agent/assess/rule/response/auto_complete_rule.rb +1 -1
  59. data/lib/contrast/agent/assess/rule/response/base_rule.rb +13 -6
  60. data/lib/contrast/agent/assess/rule/response/body_rule.rb +3 -3
  61. data/lib/contrast/agent/assess/rule/response/cache_control_header_rule.rb +66 -43
  62. data/lib/contrast/agent/assess/rule/response/click_jacking_header_rule.rb +4 -4
  63. data/lib/contrast/agent/assess/rule/response/csp_header_insecure_rule.rb +6 -6
  64. data/lib/contrast/agent/assess/rule/response/csp_header_missing_rule.rb +4 -4
  65. data/lib/contrast/agent/assess/rule/response/hsts_header_rule.rb +4 -4
  66. data/lib/contrast/agent/assess/rule/response/parameters_pollution_rule.rb +1 -1
  67. data/lib/contrast/agent/assess/rule/response/x_content_type_header_rule.rb +4 -4
  68. data/lib/contrast/agent/assess/rule/response/x_xss_protection_header_rule.rb +3 -4
  69. data/lib/contrast/agent/assess/tag.rb +13 -14
  70. data/lib/contrast/agent/at_exit_hook.rb +12 -1
  71. data/lib/contrast/agent/deadzone/policy/deadzone_node.rb +0 -7
  72. data/lib/contrast/agent/deadzone/policy/policy.rb +0 -6
  73. data/lib/contrast/agent/exclusion_matcher.rb +3 -3
  74. data/lib/contrast/agent/inventory/database_config.rb +10 -3
  75. data/lib/contrast/agent/middleware.rb +10 -5
  76. data/lib/contrast/agent/patching/policy/after_load_patch.rb +3 -5
  77. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +2 -2
  78. data/lib/contrast/agent/patching/policy/method_policy_extend.rb +4 -4
  79. data/lib/contrast/agent/patching/policy/patch.rb +20 -19
  80. data/lib/contrast/agent/patching/policy/patch_status.rb +10 -3
  81. data/lib/contrast/agent/patching/policy/patcher.rb +4 -4
  82. data/lib/contrast/agent/patching/policy/policy.rb +13 -15
  83. data/lib/contrast/agent/patching/policy/policy_node.rb +32 -21
  84. data/lib/contrast/agent/patching/policy/trigger_node.rb +1 -1
  85. data/lib/contrast/agent/protect/exploitable_collection.rb +38 -0
  86. data/lib/contrast/agent/protect/input_analyzer/input_analyzer.rb +132 -75
  87. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +4 -3
  88. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +3 -3
  89. data/lib/contrast/agent/protect/policy/applies_xxe_rule.rb +1 -1
  90. data/lib/contrast/agent/protect/policy/rule_applicator.rb +4 -4
  91. data/lib/contrast/agent/protect/rule/base.rb +53 -9
  92. data/lib/contrast/agent/protect/rule/base_service.rb +31 -12
  93. data/lib/contrast/agent/protect/rule/cmd_injection.rb +23 -3
  94. data/lib/contrast/agent/protect/rule/cmdi/cmdi_input_classification.rb +83 -0
  95. data/lib/contrast/agent/protect/rule/cmdi/cmdi_worth_watching.rb +64 -0
  96. data/lib/contrast/agent/protect/rule/default_scanner.rb +2 -1
  97. data/lib/contrast/agent/protect/rule/deserialization.rb +18 -7
  98. data/lib/contrast/agent/protect/rule/http_method_tampering/http_method_tampering_input_classification.rb +96 -0
  99. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +72 -46
  100. data/lib/contrast/agent/protect/rule/no_sqli/no_sqli_input_classification.rb +231 -0
  101. data/lib/contrast/agent/protect/rule/no_sqli.rb +28 -2
  102. data/lib/contrast/agent/protect/rule/path_traversal.rb +13 -3
  103. data/lib/contrast/agent/protect/rule/sqli/sqli_input_classification.rb +18 -54
  104. data/lib/contrast/agent/protect/rule/sqli/sqli_worth_watching.rb +2 -5
  105. data/lib/contrast/agent/protect/rule/sqli.rb +16 -23
  106. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_input_classification.rb +82 -0
  107. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_matcher.rb +45 -0
  108. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +42 -0
  109. data/lib/contrast/agent/protect/rule/xss.rb +17 -0
  110. data/lib/contrast/agent/protect/rule/xxe/entity_wrapper.rb +14 -13
  111. data/lib/contrast/agent/protect/rule/xxe.rb +25 -3
  112. data/lib/contrast/agent/reaction_processor.rb +1 -1
  113. data/lib/contrast/agent/reporting/attack_result/attack_result.rb +63 -0
  114. data/lib/contrast/agent/reporting/attack_result/rasp_rule_sample.rb +52 -0
  115. data/lib/contrast/agent/reporting/attack_result/response_type.rb +29 -0
  116. data/lib/contrast/agent/reporting/attack_result/user_input.rb +87 -0
  117. data/lib/contrast/agent/reporting/masker/masker.rb +243 -0
  118. data/lib/contrast/agent/reporting/masker/masker_utils.rb +62 -0
  119. data/lib/contrast/agent/reporting/report.rb +2 -0
  120. data/lib/contrast/agent/reporting/reporter.rb +29 -22
  121. data/lib/contrast/agent/reporting/reporter_heartbeat.rb +49 -0
  122. data/lib/contrast/agent/reporting/reporting_events/agent_startup.rb +34 -0
  123. data/lib/contrast/agent/reporting/reporting_events/application_activity.rb +51 -0
  124. data/lib/contrast/agent/reporting/reporting_events/application_defend_activity.rb +96 -0
  125. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_activity.rb +70 -0
  126. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample.rb +182 -0
  127. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample_activity.rb +56 -0
  128. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample_stack.rb +22 -0
  129. data/lib/contrast/agent/reporting/reporting_events/application_defend_attacker_activity.rb +70 -0
  130. data/lib/contrast/agent/reporting/reporting_events/application_inventory.rb +13 -5
  131. data/lib/contrast/agent/reporting/reporting_events/application_inventory_activity.rb +60 -0
  132. data/lib/contrast/agent/reporting/reporting_events/application_reporting_event.rb +27 -0
  133. data/lib/contrast/agent/reporting/reporting_events/application_startup.rb +44 -0
  134. data/lib/contrast/agent/reporting/reporting_events/application_startup_instrumentation.rb +27 -0
  135. data/lib/contrast/agent/reporting/reporting_events/application_update.rb +7 -12
  136. data/lib/contrast/agent/reporting/reporting_events/discovered_route.rb +1 -1
  137. data/lib/contrast/agent/reporting/reporting_events/finding.rb +10 -4
  138. data/lib/contrast/agent/reporting/reporting_events/finding_event.rb +2 -4
  139. data/lib/contrast/agent/reporting/reporting_events/finding_event_object.rb +3 -3
  140. data/lib/contrast/agent/reporting/reporting_events/library_usage_observation.rb +5 -5
  141. data/lib/contrast/agent/reporting/reporting_events/observed_library_usage.rb +6 -2
  142. data/lib/contrast/agent/reporting/reporting_events/observed_route.rb +16 -12
  143. data/lib/contrast/agent/reporting/reporting_events/poll.rb +6 -2
  144. data/lib/contrast/agent/reporting/reporting_events/preflight.rb +10 -8
  145. data/lib/contrast/agent/reporting/reporting_events/preflight_message.rb +8 -11
  146. data/lib/contrast/agent/reporting/reporting_events/reporting_event.rb +2 -1
  147. data/lib/contrast/agent/reporting/reporting_events/route_coverage.rb +8 -6
  148. data/lib/contrast/agent/reporting/reporting_events/server_activity.rb +12 -20
  149. data/lib/contrast/agent/reporting/reporting_events/server_reporting_event.rb +27 -0
  150. data/lib/contrast/agent/reporting/reporting_utilities/audit.rb +17 -27
  151. data/lib/contrast/agent/reporting/reporting_utilities/build_preflight.rb +38 -0
  152. data/lib/contrast/agent/reporting/reporting_utilities/dtm_message.rb +8 -0
  153. data/lib/contrast/agent/reporting/reporting_utilities/endpoints.rb +6 -0
  154. data/lib/contrast/agent/reporting/reporting_utilities/headers.rb +1 -2
  155. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +29 -13
  156. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client_utils.rb +64 -76
  157. data/lib/contrast/agent/reporting/reporting_utilities/reporting_storage.rb +1 -1
  158. data/lib/contrast/agent/reporting/reporting_utilities/response.rb +17 -7
  159. data/lib/contrast/agent/reporting/reporting_utilities/response_extractor.rb +100 -0
  160. data/lib/contrast/agent/reporting/reporting_utilities/response_handler.rb +75 -13
  161. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_mode.rb +63 -0
  162. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_utils.rb +163 -122
  163. data/lib/contrast/agent/reporting/settings/application_settings.rb +10 -1
  164. data/lib/contrast/agent/reporting/settings/assess.rb +5 -5
  165. data/lib/contrast/agent/reporting/settings/assess_server_feature.rb +7 -35
  166. data/lib/contrast/agent/reporting/settings/exclusions.rb +3 -3
  167. data/lib/contrast/agent/reporting/settings/protect.rb +21 -6
  168. data/lib/contrast/agent/reporting/settings/protect_server_feature.rb +6 -6
  169. data/lib/contrast/agent/reporting/settings/reaction.rb +3 -3
  170. data/lib/contrast/agent/reporting/settings/sampling.rb +36 -0
  171. data/lib/contrast/agent/reporting/settings/sensitive_data_masking.rb +110 -0
  172. data/lib/contrast/agent/reporting/settings/sensitive_data_masking_rule.rb +58 -0
  173. data/lib/contrast/agent/reporting/settings/server_features.rb +2 -2
  174. data/lib/contrast/agent/request.rb +5 -5
  175. data/lib/contrast/agent/request_context.rb +25 -21
  176. data/lib/contrast/agent/request_context_extend.rb +12 -25
  177. data/lib/contrast/agent/request_handler.rb +7 -3
  178. data/lib/contrast/agent/response.rb +2 -0
  179. data/lib/contrast/agent/rule_set.rb +2 -2
  180. data/lib/contrast/agent/scope.rb +1 -1
  181. data/lib/contrast/agent/service_heartbeat.rb +6 -48
  182. data/lib/contrast/agent/static_analysis.rb +1 -1
  183. data/lib/contrast/agent/telemetry/base.rb +155 -0
  184. data/lib/contrast/agent/telemetry/events/event.rb +35 -0
  185. data/lib/contrast/agent/telemetry/events/exceptions/obfuscate.rb +119 -0
  186. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_base.rb +59 -0
  187. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_event.rb +44 -0
  188. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message.rb +115 -0
  189. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message_exception.rb +83 -0
  190. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_stack_frame.rb +64 -0
  191. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exceptions.rb +20 -0
  192. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exceptions_report.rb +30 -0
  193. data/lib/contrast/agent/telemetry/events/metric_event.rb +28 -0
  194. data/lib/contrast/agent/telemetry/events/startup_metrics_event.rb +123 -0
  195. data/lib/contrast/agent/thread_watcher.rb +52 -68
  196. data/lib/contrast/agent/version.rb +1 -1
  197. data/lib/contrast/agent/worker_thread.rb +8 -0
  198. data/lib/contrast/agent.rb +4 -3
  199. data/lib/contrast/api/communication/messaging_queue.rb +29 -12
  200. data/lib/contrast/api/communication/response_processor.rb +7 -10
  201. data/lib/contrast/api/communication/service_lifecycle.rb +1 -1
  202. data/lib/contrast/api/communication/socket.rb +1 -1
  203. data/lib/contrast/api/communication/socket_client.rb +1 -1
  204. data/lib/contrast/api/communication/speedracer.rb +3 -3
  205. data/lib/contrast/api/decorators/activity.rb +33 -0
  206. data/lib/contrast/api/decorators/address.rb +1 -1
  207. data/lib/contrast/api/decorators/agent_startup.rb +10 -9
  208. data/lib/contrast/api/decorators/application_settings.rb +1 -1
  209. data/lib/contrast/api/decorators/application_startup.rb +4 -4
  210. data/lib/contrast/api/decorators/http_request.rb +1 -1
  211. data/lib/contrast/api/decorators/response_type.rb +17 -0
  212. data/lib/contrast/api/decorators.rb +1 -0
  213. data/lib/contrast/components/agent.rb +1 -1
  214. data/lib/contrast/components/app_context.rb +0 -4
  215. data/lib/contrast/components/assess.rb +14 -0
  216. data/lib/contrast/components/base.rb +1 -1
  217. data/lib/contrast/components/config.rb +19 -28
  218. data/lib/contrast/components/contrast_service.rb +13 -1
  219. data/lib/contrast/components/protect.rb +2 -2
  220. data/lib/contrast/components/sampling.rb +8 -12
  221. data/lib/contrast/components/settings.rb +151 -19
  222. data/lib/contrast/config/agent_configuration.rb +34 -41
  223. data/lib/contrast/config/api_configuration.rb +16 -75
  224. data/lib/contrast/config/api_proxy_configuration.rb +9 -48
  225. data/lib/contrast/config/application_configuration.rb +24 -95
  226. data/lib/contrast/config/assess_configuration.rb +21 -76
  227. data/lib/contrast/config/assess_rules_configuration.rb +13 -38
  228. data/lib/contrast/config/base_configuration.rb +11 -76
  229. data/lib/contrast/config/certification_configuration.rb +15 -68
  230. data/lib/contrast/config/exception_configuration.rb +15 -59
  231. data/lib/contrast/config/heap_dump_configuration.rb +19 -73
  232. data/lib/contrast/config/inventory_configuration.rb +11 -55
  233. data/lib/contrast/config/logger_configuration.rb +8 -41
  234. data/lib/contrast/config/protect_configuration.rb +23 -10
  235. data/lib/contrast/config/protect_rule_configuration.rb +23 -37
  236. data/lib/contrast/config/protect_rules_configuration.rb +39 -43
  237. data/lib/contrast/config/request_audit_configuration.rb +16 -55
  238. data/lib/contrast/config/root_configuration.rb +71 -14
  239. data/lib/contrast/config/ruby_configuration.rb +14 -47
  240. data/lib/contrast/config/sampling_configuration.rb +12 -65
  241. data/lib/contrast/config/server_configuration.rb +13 -45
  242. data/lib/contrast/config/service_configuration.rb +18 -54
  243. data/lib/contrast/configuration.rb +25 -17
  244. data/lib/contrast/extension/assess/array.rb +1 -1
  245. data/lib/contrast/extension/assess/erb.rb +1 -1
  246. data/lib/contrast/extension/assess/marshal.rb +1 -1
  247. data/lib/contrast/extension/assess/string.rb +20 -1
  248. data/lib/contrast/extension/extension.rb +2 -2
  249. data/lib/contrast/extension/module.rb +0 -1
  250. data/lib/contrast/framework/base_support.rb +8 -8
  251. data/lib/contrast/framework/grape/support.rb +3 -3
  252. data/lib/contrast/framework/manager.rb +7 -7
  253. data/lib/contrast/framework/manager_extend.rb +1 -1
  254. data/lib/contrast/framework/rack/patch/session_cookie.rb +1 -1
  255. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +14 -3
  256. data/lib/contrast/framework/rails/patch/assess_configuration.rb +3 -3
  257. data/lib/contrast/framework/rails/patch/rails_application_configuration.rb +1 -1
  258. data/lib/contrast/framework/rails/patch/support.rb +14 -46
  259. data/lib/contrast/framework/rails/support.rb +2 -2
  260. data/lib/contrast/framework/sinatra/support.rb +1 -1
  261. data/lib/contrast/logger/aliased_logging.rb +94 -0
  262. data/lib/contrast/logger/application.rb +1 -5
  263. data/lib/contrast/logger/cef_log.rb +15 -15
  264. data/lib/contrast/logger/format.rb +1 -1
  265. data/lib/contrast/logger/log.rb +8 -8
  266. data/lib/contrast/tasks/config.rb +100 -4
  267. data/lib/contrast/tasks/service.rb +2 -2
  268. data/lib/contrast/utils/assess/object_store.rb +36 -0
  269. data/lib/contrast/utils/assess/propagation_method_utils.rb +6 -0
  270. data/lib/contrast/utils/assess/tracking_util.rb +4 -4
  271. data/lib/contrast/utils/class_util.rb +9 -22
  272. data/lib/contrast/utils/findings.rb +3 -3
  273. data/lib/contrast/utils/hash_digest.rb +6 -7
  274. data/lib/contrast/utils/head_dump_utils_extend.rb +1 -1
  275. data/lib/contrast/utils/input_classification.rb +73 -0
  276. data/lib/contrast/utils/invalid_configuration_util.rb +2 -2
  277. data/lib/contrast/utils/log_utils.rb +7 -5
  278. data/lib/contrast/utils/lru_cache.rb +1 -1
  279. data/lib/contrast/utils/metrics_hash.rb +1 -1
  280. data/lib/contrast/utils/middleware_utils.rb +15 -14
  281. data/lib/contrast/utils/net_http_base.rb +5 -5
  282. data/lib/contrast/utils/object_share.rb +2 -1
  283. data/lib/contrast/utils/os.rb +1 -6
  284. data/lib/contrast/utils/patching/policy/patch_utils.rb +6 -7
  285. data/lib/contrast/utils/request_utils.rb +2 -2
  286. data/lib/contrast/utils/response_utils.rb +18 -33
  287. data/lib/contrast/utils/sha256_builder.rb +4 -4
  288. data/lib/contrast/utils/stack_trace_utils.rb +31 -13
  289. data/lib/contrast/utils/telemetry.rb +22 -7
  290. data/lib/contrast/utils/telemetry_client.rb +28 -16
  291. data/lib/contrast/utils/telemetry_hash.rb +41 -0
  292. data/lib/contrast/utils/telemetry_identifier.rb +18 -3
  293. data/lib/contrast/utils/timer.rb +1 -1
  294. data/lib/contrast.rb +9 -0
  295. data/resources/assess/policy.json +99 -1
  296. data/resources/deadzone/policy.json +0 -86
  297. data/ruby-agent.gemspec +10 -9
  298. data/service_executables/VERSION +1 -1
  299. data/service_executables/linux/contrast-service +0 -0
  300. data/service_executables/mac/contrast-service +0 -0
  301. metadata +99 -29
  302. data/lib/contrast/agent/metric_telemetry_event.rb +0 -26
  303. data/lib/contrast/agent/startup_metrics_telemetry_event.rb +0 -121
  304. data/lib/contrast/agent/telemetry.rb +0 -137
  305. data/lib/contrast/agent/telemetry_event.rb +0 -33
  306. data/lib/contrast/utils/exclude_key.rb +0 -20
@@ -1,51 +1,149 @@
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
48
- Contrast::SETTINGS.update_from_application_settings(response) if response.application_settings
143
+ return unless response
144
+
145
+ ::Contrast::SETTINGS.update_from_server_features(response) if response.server_features
146
+ ::Contrast::SETTINGS.update_from_application_settings(response) if response.application_settings
49
147
  end
50
148
 
51
149
  # Process the given Reactions from the application settings based on what
@@ -54,23 +152,18 @@ module Contrast
54
152
  #
55
153
  # @param response [Contrast::Agent::Reporting::Response]
56
154
  def update_reaction response
57
- return unless response.application_settings.reactions&.any?
155
+ return unless response.application_settings&.reactions&.any?
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,99 +222,27 @@ 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
113
- extract_assess response_data, res
114
- extract_protect response_data, res
115
- extract_exclusions response_data, res
116
- extract_reactions response_data, res
225
+ res = Contrast::Agent::Reporting::Response.application_response
226
+ extract_assess(response_data, res)
227
+ extract_protect(response_data, res)
228
+ extract_exclusions(response_data, res)
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
124
- extract_assess_server_features response_data, res
125
- extract_protect_server_features response_data, res
126
- extract_protect_lists response_data, res
237
+ res = Contrast::Agent::Reporting::Response.server_response
238
+ extract_assess_server_features(response_data, res)
239
+ extract_protect_server_features(response_data, res)
240
+ extract_protect_lists(response_data, res)
127
241
  res.server_features.log_level = response_data[:logLevel]
128
242
  res.server_features.log_file = response_data[:logFile]
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>] {
@@ -52,7 +61,7 @@ module Contrast
52
61
  # operation [String] What to do in response to this reaction.[NOOP, DISABLE] }
53
62
  # @return [Array<Contrast::Agent::Reporting::Settings::Reaction>]
54
63
  def reactions= reactions
55
- return unless reactions.is_a? Array
64
+ return unless reactions.is_a?(Array)
56
65
 
57
66
  @_reactions = []
58
67
  reactions.each do |r|
@@ -12,7 +12,7 @@ module Contrast
12
12
  class Assess
13
13
  # Assess rules to disable for this application.
14
14
  #
15
- # @return disabled_rules [Array<AssessRuleID>] Array with string rule_ids
15
+ # @return disabled_rules [Array<String>] Array with string rule_ids
16
16
  def disabled_rules
17
17
  @_disabled_rules ||= []
18
18
  end
@@ -20,15 +20,15 @@ module Contrast
20
20
  # @param disabled_rules [Array] with AssessRuleID strings
21
21
  # @return disabled_rules [Array<AssessRuleID>] Array with string rule_ids
22
22
  def disabled_rules= disabled_rules
23
- @_disabled_rules = disabled_rules if disabled_rules.is_a? Array
23
+ @_disabled_rules = disabled_rules if disabled_rules.is_a?(Array)
24
24
  end
25
25
 
26
26
  # The id of a session on TeamServer under which this session of this application should report
27
27
  # Overrides that set by application.session_id (should match if provided).
28
28
  #
29
- # @return session_id [String]
29
+ # @return session_id [String, nil]
30
30
  def session_id
31
- @_session_id ||= Contrast::Utils::ObjectShare::EMPTY_STRING
31
+ @_session_id
32
32
  end
33
33
 
34
34
  # Set the session_id
@@ -36,7 +36,7 @@ module Contrast
36
36
  # @param session_id [String]
37
37
  # @return session_id [String]
38
38
  def session_id= session_id
39
- @_session_id = session_id if session_id.is_a? String
39
+ @_session_id = session_id if session_id.is_a?(String) && !session_id.blank?
40
40
  end
41
41
  end
42
42
  end
@@ -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.
@@ -100,7 +72,7 @@ module Contrast
100
72
  # tags [Array[String]]
101
73
  # uuid [String]
102
74
  def sanitizers= sanitizers
103
- @_sanitizers = sanitizers if sanitizers.is_a? Array
75
+ @_sanitizers = sanitizers if sanitizers.is_a?(Array)
104
76
  end
105
77
 
106
78
  # The validators defined by the user for use by the agent on this server for this organization.
@@ -127,7 +99,7 @@ module Contrast
127
99
  # uuid [String]
128
100
  # }
129
101
  def validators= validators
130
- @_validators = validators if validators.is_a? Array
102
+ @_validators = validators if validators.is_a?(Array)
131
103
  end
132
104
  end
133
105
  end
@@ -36,7 +36,7 @@ module Contrast
36
36
  # denylist [String] The call, if in the stack, should result in the agent not taking action.
37
37
  # }
38
38
  def code_exclusions= code_exclusions
39
- @_code_exclusions = code_exclusions if code_exclusions.is_a? Array
39
+ @_code_exclusions = code_exclusions if code_exclusions.is_a?(Array)
40
40
  end
41
41
 
42
42
  # Cases where rules should be excluded if violated from a given input.
@@ -75,7 +75,7 @@ module Contrast
75
75
  # type [String] The type of the input [COOKIE, PARAMETER, HEADER, BODY, QUERYSTRING]
76
76
  # }
77
77
  def input_exclusions= input_exclusions
78
- @_input_exclusions = input_exclusions if input_exclusions.is_a? Array
78
+ @_input_exclusions = input_exclusions if input_exclusions.is_a?(Array)
79
79
  end
80
80
 
81
81
  # A case where rules should be excluded if violated during a call to a given URL.
@@ -114,7 +114,7 @@ module Contrast
114
114
  # type [String] The type of the input [COOKIE, PARAMETER, HEADER, BODY, QUERYSTRING]
115
115
  # }
116
116
  def url_exclusions= url_exclusions
117
- @_url_exclusions = url_exclusions if url_exclusions.is_a? Array
117
+ @_url_exclusions = url_exclusions if url_exclusions.is_a?(Array)
118
118
  end
119
119
  end
120
120
  end
@@ -11,7 +11,7 @@ module Contrast
11
11
  # Application level settings for the Protect featureset
12
12
  class Protect
13
13
  # block at perimeter needs to be check against the blockAtEntry boolean value
14
- PROTECT_RULES_MODE = %w[OFF BLOCKING MONITORING].cs__freeze
14
+ PROTECT_RULES_MODE = %w[OFF MONITORING BLOCKING].cs__freeze
15
15
  # The settings for each protect rule for this application
16
16
  #
17
17
  # @return protection_rules [Array<protectRule>] protectRule: {
@@ -33,7 +33,7 @@ module Contrast
33
33
  # id [String] The id of a rule in Contrast.
34
34
  # mode [String] The mode that this rule should run in. [OFF, MONITORING, BLOCKING] }
35
35
  def protection_rules= protection_rules
36
- @_protection_rules = protection_rules if protection_rules.is_a? Array
36
+ @_protection_rules = protection_rules if protection_rules.is_a?(Array)
37
37
  end
38
38
 
39
39
  # The virtual patches to apply for this application
@@ -66,19 +66,34 @@ module Contrast
66
66
  # urls [Array] The urls that must be present in the request to result in the request being blocked.
67
67
  # uuids [String] The UUID of the Virtual Patch }
68
68
  def virtual_patches= virtual_patches
69
- @_virtual_patches = virtual_patches if virtual_patches.is_a? Array
69
+ @_virtual_patches = virtual_patches if virtual_patches.is_a?(Array)
70
70
  end
71
71
 
72
72
  # Converts settings into Agent Settings understandable hash {RULE_ID => MODE}
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|
80
- PROTECT_RULES_MODE.include? rule[:mode]
81
- modes_by_id[rule[:id]] = rule[:mode]
80
+ setting_mode = rule[:mode]
81
+ next unless PROTECT_RULES_MODE.include?(setting_mode)
82
+
83
+ api_mode = case setting_mode
84
+ when PROTECT_RULES_MODE[1]
85
+ ::Contrast::Api::Settings::ProtectionRule::Mode::MONITORED
86
+ when PROTECT_RULES_MODE[2]
87
+ if rule[:blockAtEntry]
88
+ ::Contrast::Api::Settings::ProtectionRule::Mode::BLOCK_AT_PERIMETER
89
+ else
90
+ ::Contrast::Api::Settings::ProtectionRule::Mode::BLOCKED
91
+ end
92
+ else
93
+ ::Contrast::Api::Settings::ProtectionRule::Mode::NO_ACTION
94
+ end
95
+
96
+ modes_by_id[rule[:id]] = api_mode
82
97
  end
83
98
  modes_by_id
84
99
  end
@@ -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.
@@ -66,7 +66,7 @@ module Contrast
66
66
  # name [String] The user defined name of the filter.
67
67
  # uuid [String] The identifier of the filter as defined by TeamServer.
68
68
  def ip_allowlist= allowlist
69
- @_ip_allowlist = allowlist if allowlist.is_a? Array
69
+ @_ip_allowlist = allowlist if allowlist.is_a?(Array)
70
70
  end
71
71
 
72
72
  # The IP addresses for which to disable protection.
@@ -94,7 +94,7 @@ module Contrast
94
94
  # name [String] The user defined name of the filter.
95
95
  # uuid [String] The identifier of the filter as defined by TeamServer.
96
96
  def ip_denylist= denylist
97
- @_ip_denylist = denylist if denylist.is_a? Array
97
+ @_ip_denylist = denylist if denylist.is_a?(Array)
98
98
  end
99
99
 
100
100
  # All of the apis to add new logging calls to the application at runtime.
@@ -134,7 +134,7 @@ module Contrast
134
134
  # type [String] The type of log message to tenerate. Audit as 0, Security as 2.
135
135
  # [ AUDIT, ERROR, SECURITY ]
136
136
  def log_enchancers= log_enchancers
137
- @_log_enchancers = log_enchancers if log_enchancers.is_a? Array
137
+ @_log_enchancers = log_enchancers if log_enchancers.is_a?(Array)
138
138
  end
139
139
 
140
140
  # The keywords and patterns required for the input analysis of each rule with that capability.
@@ -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.
@@ -234,7 +234,7 @@ module Contrast
234
234
  # syslogSeverityProbed [String]
235
235
  # syslogSeveritySuspicous [String]
236
236
  def syslog= log
237
- @_syslog = log if log.is_a? Hash
237
+ @_syslog = log if log.is_a?(Hash)
238
238
  end
239
239
  end
240
240
  end
@@ -19,9 +19,9 @@ module Contrast
19
19
  # @param message [String] A message to log when receiving this reaction.
20
20
  # @param operation [String] What to do in response to this reaction.[NOOP, DISABLE]
21
21
  def initialize level, operation, message
22
- @level = level if LEVELS.include? level
23
- @operation = operation if OPERATIONS.include? operation
24
- @message = message if message.is_a? String
22
+ @level = level if LEVELS.include?(level)
23
+ @operation = operation if OPERATIONS.include?(operation)
24
+ @message = message if message.is_a?(String)
25
25
  end
26
26
  end
27
27
  end