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
@@ -3,6 +3,7 @@
3
3
 
4
4
  require 'socket'
5
5
  require 'contrast/agent/version'
6
+ require 'contrast/logger/aliased_logging'
6
7
 
7
8
  module Contrast
8
9
  module Utils
@@ -37,6 +38,7 @@ module Contrast
37
38
  logger.extend(Contrast::Logger::Application)
38
39
  logger.extend(Contrast::Logger::Request)
39
40
  logger.extend(Contrast::Logger::Time)
41
+ logger.extend(Contrast::Logger::AliasedLogging) if Contrast::Utils::Telemetry.exceptions_enabled?
40
42
  end
41
43
 
42
44
  # Determine the valid path to which to log, given the precedence of config > settings > default.
@@ -62,13 +64,13 @@ module Contrast
62
64
  # Log once when the path is invalid. We'll change to this path, so no
63
65
  # need to log again.
64
66
  if previous_path != DEFAULT_NAME
65
- $stdout.puts "[!] Unable to write to '#{ path }'. Writing to default log '#{ DEFAULT_NAME }' instead."
67
+ $stdout.puts("[!] Unable to write to '#{ path }'. Writing to default log '#{ DEFAULT_NAME }' instead.")
66
68
  end
67
69
  DEFAULT_NAME
68
70
  else
69
71
  # Log once when the path is invalid. We'll change to this path, so no
70
72
  # need to log again.
71
- $stdout.puts "[!] Unable to write to '#{ path }'. Writing to standard out instead."
73
+ $stdout.puts("[!] Unable to write to '#{ path }'. Writing to standard out instead.")
72
74
  STDOUT_STR
73
75
  end
74
76
  end
@@ -139,7 +141,7 @@ module Contrast
139
141
  end
140
142
  logger.progname = PROGNAME
141
143
  logger.level = level_const
142
- change_logger_formatter logger
144
+ change_logger_formatter(logger)
143
145
  logger
144
146
  end
145
147
 
@@ -160,7 +162,7 @@ module Contrast
160
162
  message << msg[0]
161
163
  message << severity
162
164
  message << extract_metadata(msg[1], msg[2])
163
- message.join('|') + "\n"
165
+ "#{ message.join('|') }\n"
164
166
  end
165
167
  end
166
168
 
@@ -181,7 +183,7 @@ module Contrast
181
183
  DEFAULT_METADATA
182
184
  end
183
185
  app_name = ::Contrast::APP_CONTEXT.app_name
184
- attach_request_and_sender_info message, sender_info
186
+ attach_request_and_sender_info(message, sender_info)
185
187
  message << "request=#{ context.request.url } "
186
188
  message << "requestMethod=#{ request_method } "
187
189
  message << "app=#{ app_name } "
@@ -6,7 +6,7 @@ module Contrast
6
6
  # A LRU(Least Recently Used) Cache store.
7
7
  class LRUCache
8
8
  def initialize capacity = 500
9
- raise StandardError 'Capacity must be bigger than 0' if capacity <= 0
9
+ raise(StandardError('Capacity must be bigger than 0')) if capacity <= 0
10
10
 
11
11
  @capacity = capacity
12
12
  @cache = {}
@@ -29,7 +29,7 @@ module Contrast
29
29
  value_val = value.dup
30
30
  key_val.strip! if key_val.cs__is_a?(String)
31
31
  value_val.strip! if value_val.cs__is_a?(String)
32
- return unless valid_pair? key_val, value_val
32
+ return unless valid_pair?(key_val, value_val)
33
33
 
34
34
  key_val.downcase!
35
35
  key_val.strip!
@@ -8,6 +8,13 @@ module Contrast
8
8
  module MiddlewareUtils
9
9
  private
10
10
 
11
+ # TODO: RUBY-1609 update this part of the method for Ruby Version and Year
12
+ LANGUAGE_DEPRECATION_VERSION = '2.7'
13
+ LANGUAGE_DEPRECATION_YEAR = '2023'
14
+ LANGUAGE_DEPRECATION_WARNING =
15
+ "[Contrast Security] [DEPRECATION] Support for Ruby #{ LANGUAGE_DEPRECATION_VERSION } will be removed in "\
16
+ "April #{ LANGUAGE_DEPRECATION_YEAR }. Please contact Customer Support prior if you require continued support."
17
+
11
18
  def setup_agent
12
19
  ::Contrast::SETTINGS.reset_state
13
20
 
@@ -30,12 +37,12 @@ module Contrast
30
37
  def handle_exception exception
31
38
  if security_exception?(exception)
32
39
  exception_control = ::Contrast::AGENT.exception_control
33
- raise exception unless exception_control[:enable]
40
+ raise(exception) unless exception_control[:enable]
34
41
 
35
42
  [exception_control[:status], {}, [exception_control[:message]]]
36
43
  else
37
44
  logger.debug('Re-throwing original error', exception)
38
- raise exception
45
+ raise(exception)
39
46
  end
40
47
  end
41
48
 
@@ -51,25 +58,19 @@ module Contrast
51
58
  # deprecated and soon to be removed functionality. This method handles doing that by leveraging the standard
52
59
  # Kernel#warn approach
53
60
  def inform_deprecations
54
- # Ruby 2.6 will be dropped in april of 2022. We'll begin the deprecation warnings
55
- # now so that customers have time to reach out if they'll be impacted.
56
- # TODO: RUBY-1188 remove this part of the method, leaving it empty if there are no other deprecations, when we
57
- # drop 2.6 support.
58
- return unless RUBY_VERSION < '2.7.0'
59
-
60
- Kernel.warn('[Contrast Security] [DEPRECATION] Support for Ruby 2.6 will be removed in April 2022. '\
61
- 'Please contact Customer Support prior if you require continued support.')
61
+ # Warn customers that they're on a version that's losing support in the next year.
62
+ Kernel.warn(LANGUAGE_DEPRECATION_WARNING) if RUBY_VERSION.start_with?(LANGUAGE_DEPRECATION_VERSION)
62
63
  end
63
64
 
64
65
  # Displays Telemetry disclaimer if Telemetry is enabled.
65
66
  # if .telemetry file doesn't exist we create one and then show the disclaimer.
66
67
  # if the file already exists we do nothing.
67
68
  def telemetry_disclaimer
68
- return unless Contrast::Agent::Telemetry.enabled?
69
+ return unless Contrast::Agent::Telemetry::Base.enabled?
69
70
  return unless Contrast::Utils::Telemetry.create_telemetry_file
70
71
 
71
- logger.info Contrast::Utils::Telemetry.disclaimer
72
- $stdout.print Contrast::Utils::Telemetry.disclaimer
72
+ logger.info(Contrast::Utils::Telemetry.disclaimer)
73
+ $stdout.print(Contrast::Utils::Telemetry.disclaimer)
73
74
  true
74
75
  end
75
76
 
@@ -79,7 +80,7 @@ module Contrast
79
80
  end
80
81
  rescue Contrast::SecurityException => e
81
82
  logger.trace('Security Exception raised during application lifecycle to prevent an attack', e)
82
- raise e
83
+ raise(e)
83
84
  end
84
85
  end
85
86
  end
@@ -22,7 +22,7 @@ module Contrast
22
22
  # @param use_custom_cert [Boolean] flag used to indicate whether the client is to use
23
23
  # self signed certificates provided by config [default = false]
24
24
  # @return [Net::HTTP, nil] Return open connection or nil
25
- def initialize_connection service_name, url, use_proxy = false, use_custom_cert = false
25
+ def initialize_connection service_name, url, use_proxy: false, use_custom_cert: false
26
26
  return unless url
27
27
 
28
28
  addr = URI(url)
@@ -31,14 +31,14 @@ module Contrast
31
31
  return if addr.scheme != 'https' && !addr.host.to_s.include?('localhost') # TODO: RUBY-99999 allow http w/ localhost # rubocop:disable Layout/LineLength
32
32
 
33
33
  proxy_addr = URI(Contrast::API.proxy_url) if proxy_enabled?
34
- net_http_client = initialize_client addr, proxy_addr, use_proxy, use_custom_cert
34
+ net_http_client = initialize_client(addr, proxy_addr, use_proxy, use_custom_cert)
35
35
  return if net_http_client.nil?
36
36
 
37
37
  net_http_client.start
38
38
  return unless net_http_client.started?
39
39
 
40
40
  logger.debug("Starting #{ service_name } connection test")
41
- return unless connection_verified? net_http_client
41
+ return unless connection_verified?(net_http_client)
42
42
 
43
43
  logger.debug('Client verified', service: service_name, url: url)
44
44
  net_http_client
@@ -63,7 +63,7 @@ module Contrast
63
63
  response = client.request(Net::HTTP::Get.new(client.address))
64
64
  verify_cert = client.address.to_s.include?('localhost') ||
65
65
  OpenSSL::SSL.verify_certificate_identity(client.peer_cert, client.address)
66
- resolved = resolved? client.address, ipaddr
66
+ resolved = resolved?(client.address, ipaddr)
67
67
  @_connection_verified = if resolved && response && verify_cert
68
68
  true
69
69
  else
@@ -91,7 +91,7 @@ module Contrast
91
91
  def resolved? address, ipaddr
92
92
  return @_resolved unless @_resolved.nil?
93
93
 
94
- @_resolved = if (addresses = Resolv.getaddresses address)
94
+ @_resolved = if (addresses = Resolv.getaddresses(address))
95
95
  addresses.any? { |addr| addr.include?(ipaddr) }
96
96
  else
97
97
  false
@@ -23,6 +23,7 @@ module Contrast
23
23
  HTTPS_START = 'https:'
24
24
  NEW_LINE = "\n"
25
25
  NIL_STRING = 'nil'
26
+ NIL_64_STRING = 'bmls'
26
27
  PERIOD = '.'
27
28
  POUND_SIGN = '#'
28
29
  QUESTION_MARK = '?'
@@ -38,6 +39,7 @@ module Contrast
38
39
  COLON_SLASH_SLASH = '://'
39
40
  DOLLAR_SIGN = '$'
40
41
  CARROT = '^'
42
+ BANG = '!'
41
43
 
42
44
  WRITE_FLAG = 'w'
43
45
 
@@ -47,7 +49,6 @@ module Contrast
47
49
  CACHE = 'cache'
48
50
 
49
51
  CONTRAST_PATCHED_METHOD_START = 'cs__patched_'
50
- DOUBLE_COLON = '::'
51
52
 
52
53
  EMPTY_ARRAY = [].freeze
53
54
  EMPTY_HASH = {}.freeze
@@ -42,7 +42,7 @@ module Contrast
42
42
  end
43
43
 
44
44
  def mac?
45
- @_mac = RUBY_PLATFORM.include? 'darwin' if @_mac.nil?
45
+ @_mac = RUBY_PLATFORM.include?('darwin') if @_mac.nil?
46
46
  @_mac
47
47
  end
48
48
 
@@ -53,11 +53,6 @@ module Contrast
53
53
  def linux?
54
54
  (unix? and !mac?)
55
55
  end
56
-
57
- def jruby?
58
- @_jruby = RUBY_ENGINE == 'jruby' if @_jruby.nil?
59
- @_jruby
60
- end
61
56
  end
62
57
  end
63
58
  end
@@ -35,10 +35,9 @@ module Contrast
35
35
  # Given a method, return a symbol in the format
36
36
  # <method_start>_unbound_<method_name>
37
37
  def build_unbound_method_name patcher_method
38
- (Contrast::Utils::ObjectShare::CONTRAST_PATCHED_METHOD_START +
39
- 'unbound' +
40
- Contrast::Utils::ObjectShare::UNDERSCORE +
41
- patcher_method.to_s).to_sym
38
+ "#{ Contrast::Utils::ObjectShare::CONTRAST_PATCHED_METHOD_START }unbound"\
39
+ "#{ Contrast::Utils::ObjectShare::UNDERSCORE }"\
40
+ "#{ patcher_method }".to_sym
42
41
  end
43
42
 
44
43
  # ===== PATCH APPLIERS =====
@@ -59,7 +58,7 @@ module Contrast
59
58
  rescue Contrast::SecurityException => e
60
59
  # We were told to block something, so we gotta. Don't catch this one, let it get back to our Middleware or
61
60
  # even all the way out to the framework
62
- raise e
61
+ raise(e)
63
62
  rescue StandardError => e
64
63
  # Anything else was our bad and we gotta catch that to allow for normal application flow
65
64
  logger.error('Unable to apply pre patch to method.', e)
@@ -138,7 +137,7 @@ module Contrast
138
137
  if method_policy.source_node
139
138
  # If we were given a frozen return, and it was the target of a source, and we have frozen sources enabled,
140
139
  # we'll need to replace the return. Note, this is not the default case.
141
- source_ret = Contrast::Agent::Assess::Policy::SourceMethod.source_patchers(method_policy, object, ret, args)
140
+ source_ret = Contrast::Agent::Assess::Policy::SourceMethod.apply_source(method_policy, object, ret, args)
142
141
  end
143
142
  if method_policy.propagation_node
144
143
  propagated_ret = Contrast::Agent::Assess::Policy::PropagationMethod.apply_propagation(
@@ -156,7 +155,7 @@ module Contrast
156
155
  rescue Exception => e # rubocop:disable Lint/RescueException
157
156
  logger.error('Unable to assess method call.', e)
158
157
  handle_return(propagated_ret, source_ret, ret)
159
- raise e
158
+ raise(e)
160
159
  end
161
160
 
162
161
  # Generic invocation of the Inventory or Protect patch which apply to the given method.
@@ -31,7 +31,7 @@ module Contrast
31
31
  k = Contrast::Utils::StringUtils.force_utf8(k)
32
32
  nested_prefix = prefix.nil? ? k : "#{ prefix }[#{ k }]"
33
33
  hash[k] = Contrast::Utils::ObjectShare::EMPTY_STRING
34
- hash.merge! normalize_params(v, prefix: nested_prefix)
34
+ hash.merge!(normalize_params(v, prefix: nested_prefix))
35
35
  end
36
36
  res[prefix] = Contrast::Utils::ObjectShare::EMPTY_STRING if prefix
37
37
  res
@@ -39,7 +39,7 @@ module Contrast
39
39
  idx = 0
40
40
  res = {}
41
41
  while idx < val.length
42
- res.merge! normalize_params(val[idx], prefix: "#{ prefix }[#{ idx }]")
42
+ res.merge!(normalize_params(val[idx], prefix: "#{ prefix }[#{ idx }]"))
43
43
  idx += 1
44
44
  end
45
45
  res[prefix] = Contrast::Utils::ObjectShare::EMPTY_STRING if prefix
@@ -36,39 +36,24 @@ module Contrast
36
36
  return unless body
37
37
  return if defined?(Rack::File) && body.is_a?(Rack::File)
38
38
 
39
- case body
40
- when Rack::BodyProxy
41
- handle_rack_body_proxy(body)
42
- when ActionDispatch::Response::RackBody
43
- extract_body(body.body) if defined?(ActionDispatch::Response::RackBody)
44
- when Rack::Response
45
- extract_body(body.body)
46
- when Contrast::Utils::DuckUtils.quacks_to?(body, :each)
47
- acc = []
48
- body.each { |tmp| acc << read_or_string(tmp) }
49
- acc.compact.join(Contrast::Utils::ObjectShare::NEW_LINE)
50
- when ActionView::OutputBuffer
51
- # https://stackoverflow.com/questions/15654676/how-to-convert-activesupportsafebuffer-to-string
52
- body.to_str
53
- else
54
- read_or_string(body)
55
- end
56
- # if body.is_a?(Rack::BodyProxy)
57
- # handle_rack_body_proxy(body)
58
- # elsif (defined?(ActionDispatch::Response::RackBody) && body.is_a?(ActionDispatch::Response::RackBody)) ||
59
- # body.is_a?(Rack::Response)
60
- #
61
- # extract_body(body.body)
62
- # elsif Contrast::Utils::DuckUtils.quacks_to?(body, :each)
63
- # acc = []
64
- # body.each { |tmp| acc << read_or_string(tmp) }
65
- # acc.compact.join(Contrast::Utils::ObjectShare::NEW_LINE)
66
- # elsif ActionView::OutputBuffer
67
- # # https://stackoverflow.com/questions/15654676/how-to-convert-activesupportsafebuffer-to-string
68
- # body.to_str
69
- # else
70
- # read_or_string(body)
71
- # end
39
+ return handle_rack_body_proxy(body) if body.is_a?(Rack::BodyProxy)
40
+ return extract_body(body.body) if sub_extractable?(body)
41
+ return enumerable_text_from(body) if Contrast::Utils::DuckUtils.quacks_to?(body, :each)
42
+ # https://stackoverflow.com/questions/15654676/how-to-convert-activesupportsafebuffer-to-string
43
+ return body.to_str if body.is_a?(ActionView::OutputBuffer)
44
+
45
+ read_or_string(body)
46
+ end
47
+
48
+ def sub_extractable? body
49
+ (defined?(ActionDispatch::Response::RackBody) && body.is_a?(ActionDispatch::Response::RackBody)) ||
50
+ body.is_a?(Rack::Response)
51
+ end
52
+
53
+ def enumerable_text_from body
54
+ entries = body.map { |entry| read_or_string(entry) }
55
+ entries.compact!
56
+ entries.join(Contrast::Utils::ObjectShare::NEW_LINE)
72
57
  end
73
58
 
74
59
  def handle_rack_body_proxy body
@@ -9,16 +9,16 @@ module Contrast
9
9
  # Utility methods for building and caching hashes from strings
10
10
  class Sha256Builder
11
11
  include Singleton
12
+ # Return a list of "source" files. Ignore metadata files as well as files
13
+ # in the spec, test or exe directories. Could potentially miss important
14
+ # files but good enough for the time being.
15
+ PATHS = '{lib,bin,ext}/**/*.{rb,c,h,c++,cpp,java,rs}'
12
16
 
13
17
  def initialize
14
18
  @sha256_cache = {}
15
19
  @gem_hash_cache = {}
16
20
  end
17
21
 
18
- # Return a list of "source" files. Ignore metadata files as well as files
19
- # in the spec, test or exe directories. Could potentially miss important
20
- # files but good enough for the time being.
21
- PATHS = '{lib,bin,ext}/**/*.{rb,c,h,c++,cpp,java,rs}'
22
22
  def files path
23
23
  if Dir.exist?(path)
24
24
  Dir.glob(File.join(path, PATHS))
@@ -2,6 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/utils/object_share'
5
+ require 'contrast/agent/reporting/reporting_events/application_defend_attack_sample_stack'
5
6
  require 'contrast/api'
6
7
 
7
8
  module Contrast
@@ -35,21 +36,18 @@ module Contrast
35
36
  # StackTraceElement for TeamServer to display, excluding any Contrast
36
37
  # code found.
37
38
  #
38
- # @return [Array<Contrast::Api::Dtm::StackTraceElement]
39
+ # @return [Array<Contrast::Api::Dtm::StackTraceElement>]
39
40
  def build_protect_stack_array
40
- stack = caller(2, 20)
41
- return [] unless stack
41
+ build_protect_stack(Contrast::Api::Dtm::StackTraceElement)
42
+ end
42
43
 
43
- stack = reject_caller_entries(stack)
44
- i = 0
45
- stack.map! do |entry|
46
- element = Contrast::Api::Dtm::StackTraceElement.new
47
- element = fill_protect_element(element, entry, i)
48
- i += 1
49
- element
50
- end
51
- stack.compact!
52
- stack
44
+ # Call and translate a caller_locations array to an array of
45
+ # StackTraceElement for TeamServer to display, excluding any Contrast
46
+ # code found.
47
+ #
48
+ # @return [Array<Contrast::Agent::Reporting::ApplicationDefendAttackSampleStack>]
49
+ def build_protect_report_stack_array
50
+ build_protect_stack(Contrast::Agent::Reporting::ApplicationDefendAttackSampleStack)
53
51
  end
54
52
 
55
53
  # Translate a caller array to an array of TraceStacks for TeamServer to
@@ -86,6 +84,26 @@ module Contrast
86
84
  end
87
85
  end
88
86
 
87
+ # @param clazz [Class] Contrast::Api::Dtm::StackTraceElement or
88
+ # Contrast::Agent::Reporting::ApplicationDefendAttackSampleStack
89
+ # @return [Array<Contrast::Agent::Reporting::ApplicationDefendAttackSampleStack|
90
+ # Contrast::Api::Dtm::StackTraceElement>]
91
+ def build_protect_stack clazz
92
+ stack = caller(3, 21)
93
+ return [] unless stack
94
+
95
+ stack = reject_caller_entries(stack)
96
+ i = 0
97
+ stack.map! do |entry|
98
+ element = clazz.new
99
+ element = fill_protect_element(element, entry, i)
100
+ i += 1
101
+ element
102
+ end
103
+ stack.compact!
104
+ stack
105
+ end
106
+
89
107
  # In Contrast UI - there are many ways to render the stacktraces. For
90
108
  # Protect, there are two ways we're concerned about, one for the first
91
109
  # element and then all others.
@@ -1,8 +1,9 @@
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/telemetry'
4
+ require 'contrast/agent/telemetry/base'
5
5
  require 'contrast/utils/telemetry_identifier'
6
+ require 'contrast/config/env_variables'
6
7
 
7
8
  module Contrast
8
9
  module Utils
@@ -30,14 +31,21 @@ module Contrast
30
31
  # @return[Boolean, nil] true if file is created, false if file already exist
31
32
  # and nil if Telemetry is disabled or on unsupported OS.
32
33
  def self.create_telemetry_file
33
- write_mark_file DIR, FILE, CONFIG_DIR
34
+ write_mark_file(DIR, FILE, CONFIG_DIR)
34
35
  end
35
36
 
36
37
  def self.disclaimer
37
38
  @_disclaimer = MESSAGE[:disclaimer]
38
39
  end
39
40
 
41
+ def self.exceptions_enabled?
42
+ @_exceptions_enabled = telemetry_exceptions_enabled? if @_exceptions_enabled.nil?
43
+ @_exceptions_enabled
44
+ end
45
+
40
46
  class << self
47
+ include Contrast::Config::EnvVariables
48
+
41
49
  private
42
50
 
43
51
  # Create the mark file
@@ -48,7 +56,7 @@ module Contrast
48
56
  # @return[Boolean, nil] true if file is created, false if file already exist
49
57
  # and nil if Telemetry is disabled or on unsupported OS.
50
58
  def write_mark_file dir, file, config_dir
51
- return unless Contrast::Agent::Telemetry.enabled?
59
+ return unless Contrast::Agent::Telemetry::Base.enabled?
52
60
  return if Contrast::Utils::OS.windows?
53
61
 
54
62
  @dir = dir
@@ -56,7 +64,7 @@ module Contrast
56
64
  # nor can we write to the "root" directory ( / ). This results in Errno::EROFS exception.
57
65
  # So for the MacOS we would use the config directory of the instrumented application.
58
66
  @dir = config_dir if Contrast::Utils::OS.mac?
59
- return false if File.file? @dir + file
67
+ return false if File.file?(@dir + file)
60
68
  return true if touch_marker(@dir, file)
61
69
 
62
70
  # If we don't have permission to write to root directory use config instead
@@ -67,12 +75,19 @@ module Contrast
67
75
  #
68
76
  # @return[Boolean] true if success, false if fails
69
77
  def touch_marker dir, file
70
- FileUtils.mkdir_p dir unless Dir.exist? dir
71
- FileUtils.touch dir + file
72
- File.file? dir + file
78
+ FileUtils.mkdir_p(dir) unless Dir.exist?(dir)
79
+ FileUtils.touch(dir + file)
80
+ File.file?(dir + file)
73
81
  rescue StandardError => _e
74
82
  false
75
83
  end
84
+
85
+ def telemetry_exceptions_enabled?
86
+ opts_out_telemetry = return_value(:telemetry_opt_outs).to_s
87
+ return false if opts_out_telemetry.casecmp?('true') || opts_out_telemetry == '1'
88
+
89
+ true
90
+ end
76
91
  end
77
92
  end
78
93
  end
@@ -12,9 +12,10 @@ module Contrast
12
12
  module Utils
13
13
  # This module creates a Net::HTTP client and initiates a connection to the provided result
14
14
  class TelemetryClient < NetHttpBase
15
+ include Contrast::Components::Logger::InstanceMethods
15
16
  ENDPOINT = 'api/v1/telemetry/metrics' # /TelemetryEvent.path
17
+ EXCEPTIONS = 'api/v1/telemetry/exceptions' # /TelemetryExceptions::Event.path
16
18
  SERVICE_NAME = 'Telemetry'
17
- include Contrast::Components::Logger::InstanceMethods
18
19
  # This method initializes the Net::HTTP client we'll need. it will validate
19
20
  # the connection and make the first request. If connection is valid and response
20
21
  # is available then the open connection is returned.
@@ -22,36 +23,41 @@ module Contrast
22
23
  # @param url [String]
23
24
  # @return [Net::HTTP, nil] Return open connection or nil
24
25
  def initialize_connection url
25
- super(SERVICE_NAME, url, false, false)
26
+ super(SERVICE_NAME, url, use_proxy: false, use_custom_cert: false)
26
27
  end
27
28
 
28
29
  # This method will be responsible for building the request. Because the telemetry collector expects to receive
29
30
  # multiple events in a single request, we must always wrap the event in an array, even if there is only one.
30
31
  #
31
- # @param event [Contrast::Agent::TelemetryEvent,Contrast::Agent::StartupMetricsTelemetryEvent]
32
+ # @param event [Contrast::Agent::Telemetry::Event, Array<Contrast::Agent::Telemetry::TelemetryException::Event>]
32
33
  # @return [Net::HTTP::Post]
33
34
  def build_request event
34
- return unless valid_event? event
35
+ return unless valid_event?(event)
36
+
37
+ string_body = if Array(event).all?(Contrast::Agent::Telemetry::TelemetryException::Event)
38
+ event.map(&:to_controlled_hash).flatten!
39
+ else
40
+ [event.to_hash]
41
+ end
35
42
 
36
- string_body = [event.to_hash].to_json
37
43
  header = {
38
44
  'User-Agent' => "<#{ Contrast::Utils::ObjectShare::RUBY }>-<#{ Contrast::Agent::VERSION }>",
39
45
  'Content-Type' => 'application/json'
40
46
  }
41
- request = Net::HTTP::Post.new(build_path(event.path), header)
42
- request.body = string_body
47
+ request = Net::HTTP::Post.new(build_path(event), header)
48
+ request.body = string_body.to_json
43
49
  request
44
50
  end
45
51
 
46
52
  # This method will create the actual request and send it
47
- # @param event[Contrast::Agent::TelemetryEvent]
53
+ # @param event[Contrast::Agent::Telemetry::Event]
48
54
  # @param connection[Net::HTTP]
49
55
  def send_request event, connection
50
56
  return if connection.nil? || event.nil?
51
- return unless valid_event? event
57
+ return unless valid_event?(event)
52
58
 
53
- req = build_request event
54
- connection.request req
59
+ req = build_request(event)
60
+ connection.request(req)
55
61
  end
56
62
 
57
63
  # This method will handle the response from the tenant
@@ -68,10 +74,13 @@ module Contrast
68
74
  end
69
75
 
70
76
  # This method will be responsible for validating the event
71
- # @param event[Contrast::Agent::TelemetryEvent,Contrast::Agent::StartupMetricsTelemetryEvent]
77
+ # @param event[Contrast::Agent::Telemetry::Event,Contrast::Agent::Telemetry::StartupMetricsEvent,
78
+ # array<Contrast::Agent::Telemetry::TelemetryException::Event>]
72
79
  def valid_event? event
73
- return true if event.cs__is_a?(Contrast::Agent::TelemetryEvent)
74
- return true if event.cs__is_a?(Contrast::Agent::StartupMetricsTelemetryEvent)
80
+ return true if event.cs__is_a?(Contrast::Agent::Telemetry::Event)
81
+ return true if event.cs__is_a?(Contrast::Agent::Telemetry::StartupMetricsEvent)
82
+ # Batch
83
+ return true if Array(event).all?(Contrast::Agent::Telemetry::TelemetryException::Event)
75
84
 
76
85
  false
77
86
  end
@@ -81,9 +90,12 @@ module Contrast
81
90
  # The telemetry instance accepts any path to #{ Contrast::Agent::Telemetry::URL }#{ ENDPOINT }, using the
82
91
  # remainder of the path to segregate messages.
83
92
  #
93
+ # @param event [Contrast::Agent::Telemetry::Event, Contrast::Agent::Telemetry::TelemetryException::Event]
84
94
  # @return [String] the fully qualified path to send the request
85
- def build_path event_path
86
- "#{ Contrast::Agent::Telemetry::URL }#{ ENDPOINT }#{ event_path }"
95
+ def build_path event
96
+ endpoint = Array(event).all?(Contrast::Agent::Telemetry::TelemetryException::Event) ? EXCEPTIONS : ENDPOINT
97
+ path = endpoint == EXCEPTIONS ? Contrast::Agent::Telemetry::TelemetryException::Event.path : event.path
98
+ "#{ Contrast::Agent::Telemetry::Base::URL }#{ endpoint }#{ path }"
87
99
  end
88
100
  end
89
101
  end
@@ -0,0 +1,41 @@
1
+ # Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ require 'contrast/components/logger'
5
+
6
+ module Contrast
7
+ module Utils
8
+ # This is the TelemetryHash, which will take data type, so we can push freely, without worrying about
9
+ # validating the event before that
10
+ class TelemetryHash < Hash
11
+ include Contrast::Components::Logger::InstanceMethods
12
+
13
+ attr_reader :data_type
14
+
15
+ def initialize data_type, *_several_variants
16
+ super()
17
+ @data_type = data_type
18
+ end
19
+
20
+ def []= key, value
21
+ return unless valid_value?(value)
22
+
23
+ super(key, value)
24
+ end
25
+
26
+ def increment key
27
+ self[key].exceptions[0].increment_occurrences
28
+ :incremented!
29
+ end
30
+
31
+ def valid_value? value
32
+ unless value.cs__is_a?(data_type)
33
+ logger.debug('The following key will be omitted', value: value)
34
+ return false
35
+ end
36
+
37
+ true
38
+ end
39
+ end
40
+ end
41
+ end