contrast-agent 6.0.0 → 6.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (260) 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_basic_object/cs__assess_basic_object.c +5 -1
  6. data/ext/cs__assess_regexp/cs__assess_regexp.c +15 -2
  7. data/ext/cs__assess_regexp/cs__assess_regexp.h +2 -0
  8. data/ext/cs__assess_string/cs__assess_string.c +8 -0
  9. data/ext/cs__assess_test/cs__assess_test.h +9 -0
  10. data/ext/cs__assess_test/cs__assess_tests.c +22 -0
  11. data/ext/cs__assess_test/extconf.rb +5 -0
  12. data/ext/cs__common/cs__common.c +101 -0
  13. data/ext/cs__common/cs__common.h +29 -5
  14. data/ext/cs__contrast_patch/cs__contrast_patch.c +1 -1
  15. data/ext/cs__tests/cs__tests.c +12 -0
  16. data/ext/cs__tests/cs__tests.h +3 -0
  17. data/ext/cs__tests/extconf.rb +5 -0
  18. data/ext/extconf_common.rb +1 -1
  19. data/lib/contrast/agent/assess/contrast_object.rb +16 -16
  20. data/lib/contrast/agent/assess/events/source_event.rb +17 -19
  21. data/lib/contrast/agent/assess/finalizers/hash.rb +2 -2
  22. data/lib/contrast/agent/assess/policy/policy.rb +9 -10
  23. data/lib/contrast/agent/assess/policy/policy_node.rb +9 -10
  24. data/lib/contrast/agent/assess/policy/policy_scanner.rb +2 -16
  25. data/lib/contrast/agent/assess/policy/propagation_method.rb +3 -3
  26. data/lib/contrast/agent/assess/policy/propagation_node.rb +2 -3
  27. data/lib/contrast/agent/assess/policy/propagator/base.rb +1 -1
  28. data/lib/contrast/agent/assess/policy/propagator/buffer.rb +2 -1
  29. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +1 -1
  30. data/lib/contrast/agent/assess/policy/propagator/splat.rb +1 -1
  31. data/lib/contrast/agent/assess/policy/propagator/split.rb +17 -21
  32. data/lib/contrast/agent/assess/policy/propagator/trim.rb +1 -1
  33. data/lib/contrast/agent/assess/policy/source_node.rb +1 -1
  34. data/lib/contrast/agent/assess/policy/trigger_method.rb +10 -18
  35. data/lib/contrast/agent/assess/policy/trigger_node.rb +16 -16
  36. data/lib/contrast/agent/assess/policy/trigger_validation/redos_validator.rb +1 -1
  37. data/lib/contrast/agent/assess/property/evented.rb +2 -2
  38. data/lib/contrast/agent/assess/property/tagged.rb +2 -2
  39. data/lib/contrast/agent/assess/rule/provider/hardcoded_key.rb +6 -8
  40. data/lib/contrast/agent/assess/rule/provider/hardcoded_password.rb +6 -7
  41. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +12 -7
  42. data/lib/contrast/agent/assess/rule/response/base_rule.rb +13 -6
  43. data/lib/contrast/agent/assess/rule/response/cache_control_header_rule.rb +66 -43
  44. data/lib/contrast/agent/assess/rule/response/click_jacking_header_rule.rb +4 -4
  45. data/lib/contrast/agent/assess/rule/response/csp_header_insecure_rule.rb +6 -6
  46. data/lib/contrast/agent/assess/rule/response/csp_header_missing_rule.rb +4 -4
  47. data/lib/contrast/agent/assess/rule/response/hsts_header_rule.rb +4 -4
  48. data/lib/contrast/agent/assess/rule/response/x_content_type_header_rule.rb +4 -4
  49. data/lib/contrast/agent/assess/rule/response/x_xss_protection_header_rule.rb +3 -4
  50. data/lib/contrast/agent/assess/tag.rb +13 -14
  51. data/lib/contrast/agent/at_exit_hook.rb +12 -1
  52. data/lib/contrast/agent/inventory/database_config.rb +22 -7
  53. data/lib/contrast/agent/middleware.rb +9 -6
  54. data/lib/contrast/agent/patching/policy/after_load_patch.rb +3 -5
  55. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +2 -2
  56. data/lib/contrast/agent/patching/policy/method_policy_extend.rb +4 -4
  57. data/lib/contrast/agent/patching/policy/patch.rb +20 -19
  58. data/lib/contrast/agent/patching/policy/patch_status.rb +10 -3
  59. data/lib/contrast/agent/patching/policy/patcher.rb +1 -1
  60. data/lib/contrast/agent/patching/policy/policy.rb +13 -15
  61. data/lib/contrast/agent/patching/policy/policy_node.rb +19 -21
  62. data/lib/contrast/agent/patching/policy/trigger_node.rb +1 -1
  63. data/lib/contrast/agent/protect/input_analyzer/input_analyzer.rb +126 -122
  64. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +2 -2
  65. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +1 -1
  66. data/lib/contrast/agent/protect/policy/applies_xxe_rule.rb +1 -1
  67. data/lib/contrast/agent/protect/policy/rule_applicator.rb +4 -4
  68. data/lib/contrast/agent/protect/rule/base.rb +30 -18
  69. data/lib/contrast/agent/protect/rule/base_service.rb +31 -14
  70. data/lib/contrast/agent/protect/rule/cmd_injection.rb +16 -9
  71. data/lib/contrast/agent/protect/rule/cmdi/cmdi_input_classification.rb +3 -3
  72. data/lib/contrast/agent/protect/rule/default_scanner.rb +2 -1
  73. data/lib/contrast/agent/protect/rule/deserialization.rb +18 -7
  74. data/lib/contrast/agent/protect/rule/http_method_tampering/http_method_tampering_input_classification.rb +74 -74
  75. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +71 -53
  76. data/lib/contrast/agent/protect/rule/no_sqli/no_sqli_input_classification.rb +3 -3
  77. data/lib/contrast/agent/protect/rule/no_sqli.rb +15 -16
  78. data/lib/contrast/agent/protect/rule/path_traversal.rb +13 -3
  79. data/lib/contrast/agent/protect/rule/sqli/sqli_input_classification.rb +2 -2
  80. data/lib/contrast/agent/protect/rule/sqli/sqli_worth_watching.rb +1 -1
  81. data/lib/contrast/agent/protect/rule/sqli.rb +16 -23
  82. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_input_classification.rb +61 -61
  83. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_matcher.rb +29 -29
  84. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +32 -32
  85. data/lib/contrast/agent/protect/rule/xss.rb +17 -0
  86. data/lib/contrast/agent/protect/rule/xxe/entity_wrapper.rb +14 -13
  87. data/lib/contrast/agent/protect/rule/xxe.rb +25 -3
  88. data/lib/contrast/agent/reaction_processor.rb +1 -1
  89. data/lib/contrast/agent/reporting/attack_result/rasp_rule_sample.rb +36 -36
  90. data/lib/contrast/agent/reporting/masker/masker.rb +18 -21
  91. data/lib/contrast/agent/reporting/masker/masker_utils.rb +10 -6
  92. data/lib/contrast/agent/reporting/reporter.rb +11 -16
  93. data/lib/contrast/agent/reporting/reporter_heartbeat.rb +49 -0
  94. data/lib/contrast/agent/reporting/reporting_events/agent_startup.rb +6 -2
  95. data/lib/contrast/agent/reporting/reporting_events/application_activity.rb +51 -0
  96. data/lib/contrast/agent/reporting/reporting_events/application_defend_activity.rb +96 -0
  97. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_activity.rb +70 -0
  98. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample.rb +182 -0
  99. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample_activity.rb +56 -0
  100. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample_stack.rb +22 -0
  101. data/lib/contrast/agent/reporting/reporting_events/application_defend_attacker_activity.rb +70 -0
  102. data/lib/contrast/agent/reporting/reporting_events/application_inventory.rb +6 -2
  103. data/lib/contrast/agent/reporting/reporting_events/application_inventory_activity.rb +60 -0
  104. data/lib/contrast/agent/reporting/reporting_events/application_reporting_event.rb +27 -0
  105. data/lib/contrast/agent/reporting/reporting_events/application_startup.rb +15 -11
  106. data/lib/contrast/agent/reporting/reporting_events/application_update.rb +7 -12
  107. data/lib/contrast/agent/reporting/reporting_events/discovered_route.rb +1 -1
  108. data/lib/contrast/agent/reporting/reporting_events/finding.rb +9 -3
  109. data/lib/contrast/agent/reporting/reporting_events/finding_event.rb +2 -4
  110. data/lib/contrast/agent/reporting/reporting_events/finding_event_object.rb +3 -3
  111. data/lib/contrast/agent/reporting/reporting_events/observed_library_usage.rb +6 -2
  112. data/lib/contrast/agent/reporting/reporting_events/observed_route.rb +7 -3
  113. data/lib/contrast/agent/reporting/reporting_events/poll.rb +6 -2
  114. data/lib/contrast/agent/reporting/reporting_events/preflight.rb +10 -8
  115. data/lib/contrast/agent/reporting/reporting_events/preflight_message.rb +6 -10
  116. data/lib/contrast/agent/reporting/reporting_events/server_activity.rb +12 -20
  117. data/lib/contrast/agent/reporting/reporting_events/server_reporting_event.rb +27 -0
  118. data/lib/contrast/agent/reporting/reporting_utilities/audit.rb +17 -27
  119. data/lib/contrast/agent/reporting/reporting_utilities/build_preflight.rb +38 -0
  120. data/lib/contrast/agent/reporting/reporting_utilities/dtm_message.rb +8 -0
  121. data/lib/contrast/agent/reporting/reporting_utilities/endpoints.rb +6 -0
  122. data/lib/contrast/agent/reporting/reporting_utilities/headers.rb +1 -2
  123. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +15 -10
  124. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client_utils.rb +54 -67
  125. data/lib/contrast/agent/reporting/reporting_utilities/response.rb +17 -7
  126. data/lib/contrast/agent/reporting/reporting_utilities/response_extractor.rb +9 -6
  127. data/lib/contrast/agent/reporting/reporting_utilities/response_handler.rb +17 -17
  128. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_utils.rb +47 -32
  129. data/lib/contrast/agent/reporting/settings/application_settings.rb +1 -1
  130. data/lib/contrast/agent/reporting/settings/assess.rb +5 -5
  131. data/lib/contrast/agent/reporting/settings/assess_server_feature.rb +3 -3
  132. data/lib/contrast/agent/reporting/settings/exclusions.rb +3 -3
  133. data/lib/contrast/agent/reporting/settings/protect.rb +21 -6
  134. data/lib/contrast/agent/reporting/settings/protect_server_feature.rb +6 -6
  135. data/lib/contrast/agent/reporting/settings/reaction.rb +3 -3
  136. data/lib/contrast/agent/reporting/settings/sensitive_data_masking.rb +2 -2
  137. data/lib/contrast/agent/reporting/settings/sensitive_data_masking_rule.rb +2 -2
  138. data/lib/contrast/agent/reporting/settings/server_features.rb +2 -2
  139. data/lib/contrast/agent/request.rb +5 -5
  140. data/lib/contrast/agent/request_context.rb +23 -19
  141. data/lib/contrast/agent/request_context_extend.rb +11 -24
  142. data/lib/contrast/agent/request_handler.rb +4 -4
  143. data/lib/contrast/agent/response.rb +2 -0
  144. data/lib/contrast/agent/rule_set.rb +2 -2
  145. data/lib/contrast/agent/scope.rb +1 -1
  146. data/lib/contrast/agent/service_heartbeat.rb +6 -48
  147. data/lib/contrast/agent/static_analysis.rb +1 -1
  148. data/lib/contrast/agent/telemetry/base.rb +155 -0
  149. data/lib/contrast/agent/telemetry/events/event.rb +35 -0
  150. data/lib/contrast/agent/telemetry/events/exceptions/obfuscate.rb +119 -0
  151. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_base.rb +44 -36
  152. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_event.rb +29 -21
  153. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message.rb +91 -73
  154. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message_exception.rb +62 -44
  155. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_stack_frame.rb +50 -33
  156. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exceptions.rb +20 -0
  157. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exceptions_report.rb +30 -0
  158. data/lib/contrast/agent/telemetry/events/metric_event.rb +28 -0
  159. data/lib/contrast/agent/telemetry/events/startup_metrics_event.rb +123 -0
  160. data/lib/contrast/agent/thread_watcher.rb +52 -68
  161. data/lib/contrast/agent/version.rb +1 -1
  162. data/lib/contrast/agent/worker_thread.rb +8 -0
  163. data/lib/contrast/agent.rb +1 -3
  164. data/lib/contrast/api/communication/messaging_queue.rb +29 -12
  165. data/lib/contrast/api/communication/response_processor.rb +7 -10
  166. data/lib/contrast/api/communication/service_lifecycle.rb +1 -1
  167. data/lib/contrast/api/communication/socket.rb +1 -1
  168. data/lib/contrast/api/communication/socket_client.rb +1 -1
  169. data/lib/contrast/api/communication/speedracer.rb +3 -3
  170. data/lib/contrast/api/decorators/activity.rb +33 -0
  171. data/lib/contrast/api/decorators/agent_startup.rb +10 -9
  172. data/lib/contrast/api/decorators/application_settings.rb +1 -1
  173. data/lib/contrast/api/decorators/application_startup.rb +4 -4
  174. data/lib/contrast/api/decorators/http_request.rb +1 -1
  175. data/lib/contrast/api/decorators/response_type.rb +4 -17
  176. data/lib/contrast/components/agent.rb +1 -1
  177. data/lib/contrast/components/base.rb +1 -1
  178. data/lib/contrast/components/config.rb +19 -28
  179. data/lib/contrast/components/contrast_service.rb +13 -1
  180. data/lib/contrast/components/sampling.rb +1 -1
  181. data/lib/contrast/components/settings.rb +58 -24
  182. data/lib/contrast/config/agent_configuration.rb +21 -11
  183. data/lib/contrast/config/api_configuration.rb +12 -8
  184. data/lib/contrast/config/api_proxy_configuration.rb +7 -3
  185. data/lib/contrast/config/application_configuration.rb +15 -11
  186. data/lib/contrast/config/assess_configuration.rb +13 -9
  187. data/lib/contrast/config/assess_rules_configuration.rb +6 -2
  188. data/lib/contrast/config/base_configuration.rb +3 -35
  189. data/lib/contrast/config/certification_configuration.rb +9 -5
  190. data/lib/contrast/config/exception_configuration.rb +10 -7
  191. data/lib/contrast/config/heap_dump_configuration.rb +13 -9
  192. data/lib/contrast/config/inventory_configuration.rb +9 -6
  193. data/lib/contrast/config/logger_configuration.rb +9 -6
  194. data/lib/contrast/config/protect_configuration.rb +9 -6
  195. data/lib/contrast/config/protect_rule_configuration.rb +12 -8
  196. data/lib/contrast/config/protect_rules_configuration.rb +19 -18
  197. data/lib/contrast/config/request_audit_configuration.rb +10 -7
  198. data/lib/contrast/config/root_configuration.rb +29 -12
  199. data/lib/contrast/config/ruby_configuration.rb +14 -11
  200. data/lib/contrast/config/sampling_configuration.rb +11 -8
  201. data/lib/contrast/config/server_configuration.rb +13 -9
  202. data/lib/contrast/config/service_configuration.rb +14 -11
  203. data/lib/contrast/configuration.rb +23 -14
  204. data/lib/contrast/extension/assess/array.rb +1 -1
  205. data/lib/contrast/extension/assess/erb.rb +1 -1
  206. data/lib/contrast/extension/assess/marshal.rb +1 -1
  207. data/lib/contrast/extension/assess/string.rb +1 -1
  208. data/lib/contrast/extension/extension.rb +2 -2
  209. data/lib/contrast/framework/base_support.rb +8 -8
  210. data/lib/contrast/framework/grape/support.rb +3 -3
  211. data/lib/contrast/framework/manager.rb +5 -5
  212. data/lib/contrast/framework/manager_extend.rb +1 -1
  213. data/lib/contrast/framework/rack/patch/session_cookie.rb +1 -1
  214. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +14 -3
  215. data/lib/contrast/framework/rails/patch/assess_configuration.rb +3 -3
  216. data/lib/contrast/framework/rails/patch/rails_application_configuration.rb +1 -1
  217. data/lib/contrast/framework/rails/patch/support.rb +14 -46
  218. data/lib/contrast/framework/rails/support.rb +2 -2
  219. data/lib/contrast/framework/sinatra/support.rb +1 -1
  220. data/lib/contrast/logger/aliased_logging.rb +94 -0
  221. data/lib/contrast/logger/application.rb +0 -4
  222. data/lib/contrast/logger/cef_log.rb +14 -14
  223. data/lib/contrast/logger/format.rb +1 -1
  224. data/lib/contrast/logger/log.rb +8 -8
  225. data/lib/contrast/tasks/config.rb +30 -21
  226. data/lib/contrast/tasks/service.rb +2 -2
  227. data/lib/contrast/utils/assess/tracking_util.rb +4 -4
  228. data/lib/contrast/utils/class_util.rb +6 -10
  229. data/lib/contrast/utils/findings.rb +3 -3
  230. data/lib/contrast/utils/hash_digest.rb +6 -7
  231. data/lib/contrast/utils/head_dump_utils_extend.rb +1 -1
  232. data/lib/contrast/utils/invalid_configuration_util.rb +2 -2
  233. data/lib/contrast/utils/log_utils.rb +6 -4
  234. data/lib/contrast/utils/lru_cache.rb +1 -1
  235. data/lib/contrast/utils/metrics_hash.rb +1 -1
  236. data/lib/contrast/utils/middleware_utils.rb +6 -6
  237. data/lib/contrast/utils/net_http_base.rb +4 -4
  238. data/lib/contrast/utils/object_share.rb +1 -1
  239. data/lib/contrast/utils/os.rb +1 -1
  240. data/lib/contrast/utils/patching/policy/patch_utils.rb +2 -2
  241. data/lib/contrast/utils/request_utils.rb +2 -2
  242. data/lib/contrast/utils/sha256_builder.rb +4 -4
  243. data/lib/contrast/utils/stack_trace_utils.rb +31 -13
  244. data/lib/contrast/utils/telemetry.rb +22 -7
  245. data/lib/contrast/utils/telemetry_client.rb +27 -15
  246. data/lib/contrast/utils/telemetry_hash.rb +41 -0
  247. data/lib/contrast/utils/telemetry_identifier.rb +18 -3
  248. data/lib/contrast/utils/timer.rb +1 -1
  249. data/lib/contrast.rb +9 -0
  250. data/resources/assess/policy.json +1 -1
  251. data/ruby-agent.gemspec +1 -1
  252. data/service_executables/VERSION +1 -1
  253. data/service_executables/linux/contrast-service +0 -0
  254. data/service_executables/mac/contrast-service +0 -0
  255. metadata +41 -16
  256. data/lib/contrast/agent/telemetry/events/metric_telemetry_event.rb +0 -26
  257. data/lib/contrast/agent/telemetry/events/startup_metrics_telemetry_event.rb +0 -121
  258. data/lib/contrast/agent/telemetry/events/telemetry_event.rb +0 -33
  259. data/lib/contrast/agent/telemetry/telemetry.rb +0 -150
  260. data/lib/contrast/utils/exclude_key.rb +0 -20
@@ -1,6 +1,7 @@
1
1
  # Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
+ require 'contrast/api/dtm.pb'
4
5
  require 'contrast/utils/timer'
5
6
  require 'contrast/agent/request'
6
7
  require 'contrast/agent/response'
@@ -16,20 +17,6 @@ module Contrast
16
17
  module Agent
17
18
  # This class acts to encapsulate information about the currently executed request, making it available to the Agent
18
19
  # for the duration of the request in a standardized and normalized format which the Agent understands.
19
- #
20
- # @attr_reader timer [Contrast::Utils::Timer] when the context was created
21
- # @attr_reader logging_hash [Hash] context used to log the request
22
- # @attr_reader speedracer_input_analysis [Contrast::Api::Settings::InputAnalysis] the protect input analysis of
23
- # sources on this request
24
- # @attr_reader request [Contrast::Agent::Request] our wrapper around the Rack::Request for this context
25
- # @attr_reader response [Contrast::Agent::Response] our wrapper aroudn the Rack::Response or Array for this context,
26
- # only available after the application has finished its processing
27
- # @attr_reader activity [Contrast::Api::Dtm::Activity] the application activity found in this request
28
- # @attr_reader server_activity [Contrast::Api::Dtm::ServerActivity] the server activity found in this request
29
- # @attr_reader route [Contrast::Api::Dtm::RouteCoverage] the route, used for findings, of this request
30
- # @attr_reader observed_route [Contrast::Api::Dtm::ObservedRoute] the route, used for coverage, of this request
31
- # @attr_reader new_observed_route [Contrast::Agent::Reporting::ObservedRoute] the route, used for coverage, of this
32
- # request
33
20
  class RequestContext
34
21
  include Contrast::Components::Logger::InstanceMethods
35
22
  include Contrast::Components::Scope::InstanceMethods
@@ -37,10 +24,28 @@ module Contrast
37
24
  include Contrast::Agent::RequestContextExtend
38
25
 
39
26
  EMPTY_INPUT_ANALYSIS_PB = Contrast::Api::Settings::InputAnalysis.new
40
- INPUT_ANALYSIS = Contrast::Agent::Reporting::InputAnalysis.new
41
27
 
42
- attr_reader :activity, :logging_hash, :observed_route, :new_observed_route, :request, :response, :route,
43
- :speedracer_input_analysis, :agent_input_analysis, :server_activity, :timer
28
+ # @return [Contrast::Api::Dtm::Activity] the application activity found in this request
29
+ attr_reader :activity
30
+ # @return [Hash] context used to log the request
31
+ attr_reader :logging_hash
32
+ # @return [Contrast::Agent::Reporting::ObservedRoute] the route, used for coverage, of this request
33
+ attr_reader :new_observed_route
34
+ # @return [Contrast::Api::Dtm::ObservedRoute] the route, used for coverage, of this request
35
+ attr_reader :observed_route
36
+ # @return [Contrast::Agent::Request] our wrapper around the Rack::Request for this context
37
+ attr_reader :request
38
+ # @return [Contrast::Agent::Response] our wrapper around the Rack::Response or Array for this context,
39
+ # only available after the application has finished its processing
40
+ attr_reader :response
41
+ # @return [Contrast::Api::Dtm::RouteCoverage] the route, used for findings, of this request
42
+ attr_reader :route
43
+ # @return [Contrast::Api::Dtm::ServerActivity] the server activity found in this request
44
+ attr_reader :server_activity
45
+ # @return [Contrast::Api::Settings::InputAnalysis] the protect input analysis of sources on this request
46
+ attr_reader :speedracer_input_analysis
47
+ # @return [Contrast::Utils::Timer] when the context was created
48
+ attr_reader :timer
44
49
 
45
50
  def initialize rack_request, app_loaded: true
46
51
  with_contrast_scope do
@@ -61,8 +66,7 @@ module Contrast
61
66
  @speedracer_input_analysis = EMPTY_INPUT_ANALYSIS_PB
62
67
  speedracer_input_analysis.request = request
63
68
 
64
- @agent_input_analysis = INPUT_ANALYSIS
65
- agent_input_analysis.request = request
69
+ # TODO: RUBY-1627
66
70
 
67
71
  # flag to indicate whether the app is fully loaded
68
72
  @app_loaded = !!app_loaded
@@ -76,15 +76,16 @@ module Contrast
76
76
  return false unless ::Contrast::PROTECT.enabled?
77
77
  return false if @do_not_track
78
78
 
79
- service_response = Contrast::Agent.messaging_queue.send_event_immediately(@activity.http_request)
79
+ service_response = Contrast::Agent&.messaging_queue&.send_event_immediately(@activity.http_request)
80
80
  return false unless service_response
81
81
 
82
82
  handle_protect_state(service_response)
83
83
  ia = service_response.input_analysis
84
84
  if ia
85
- service_extract_logging ia
85
+ service_extract_logging(ia)
86
+ # TODO: RUBY-1629
86
87
  # using Agent analysis
87
- initialize_agent_input_analysis request
88
+ # initialize_agent_input_analysis request
88
89
 
89
90
  @speedracer_input_analysis = ia
90
91
  speedracer_input_analysis.request = request
@@ -93,7 +94,7 @@ module Contrast
93
94
  false
94
95
  end
95
96
  rescue Contrast::SecurityException => e
96
- raise e
97
+ raise(e)
97
98
  rescue StandardError => e
98
99
  logger.warn('Unable to extract Contrast Service information from request', e)
99
100
  false
@@ -115,7 +116,7 @@ module Contrast
115
116
  # If Contrast Service has NOT handled the input analysis, handle them here
116
117
  build_attack_results(agent_settings)
117
118
  logger.debug('Contrast Service said to block this request')
118
- raise Contrast::SecurityException.new(nil, (state.security_message || 'Blocking suspicious behavior'))
119
+ raise(Contrast::SecurityException.new(nil, (state.security_message || 'Blocking suspicious behavior')))
119
120
  end
120
121
 
121
122
  # append anything we've learned to the request seen message this is the sum-total of all inventory information
@@ -147,7 +148,7 @@ module Contrast
147
148
 
148
149
  # This here is for things we don't have implemented
149
150
  def log_to_cef
150
- activity.results.each { |attack_result| logging_logic attack_result, attack_result.rule_id.downcase }
151
+ activity.results.each { |attack_result| logging_logic(attack_result, attack_result.rule_id.downcase) }
151
152
  end
152
153
 
153
154
  # @param input_analysis [Contrast::Api::Settings::InputAnalysis]
@@ -172,7 +173,7 @@ module Contrast
172
173
  next unless rule
173
174
 
174
175
  logger.debug(BUILD_ATTACK_LOGGER_MESSAGE, result: ia_result.inspect) if logger.debug?
175
- results_by_rule[rule_id] = attack_result rule, rule_id, ia_result, results_by_rule
176
+ results_by_rule[rule_id] = attack_result(rule, rule_id, ia_result, results_by_rule)
176
177
  end
177
178
 
178
179
  results_by_rule.each_pair do |_, attack_result|
@@ -199,20 +200,6 @@ module Contrast
199
200
  end
200
201
  end
201
202
 
202
- # Sets request to be used with agent and service input analysis.
203
- #
204
- # @param request [Contrast::Agent::Request] our wrapper around the Rack::Request
205
- # for this context
206
- def initialize_agent_input_analysis request
207
- # using Agent analysis
208
- ia = Contrast::Agent::Protect::InputAnalyzer.analyse request
209
- if ia
210
- @agent_input_analysis = ia
211
- else
212
- logger.trace('Analysis from Agent was empty.')
213
- end
214
- end
215
-
216
203
  def logging_logic result, rule_id
217
204
  rules = %w[bot_blocker virtual_patch ip_denylist]
218
205
  return unless rules.include?(rule_id)
@@ -221,14 +208,14 @@ module Contrast
221
208
  outcome = Contrast::Api::Dtm::AttackResult::ResponseType.get_name_by_tag(result.response)
222
209
  case rule_id
223
210
  when /bot_blocker/i
224
- blocker_to_json = Contrast::Api::Dtm::BotBlockerDetails.to_controlled_hash rule_details
211
+ blocker_to_json = Contrast::Api::Dtm::BotBlockerDetails.to_controlled_hash(rule_details)
225
212
  cef_logger.bot_blocking_message(blocker_to_json, outcome)
226
213
  when /virtual_patch/i
227
- virtual_patch_to_json = Contrast::Api::Dtm::VirtualPatchDetails.to_controlled_hash rule_details
214
+ virtual_patch_to_json = Contrast::Api::Dtm::VirtualPatchDetails.to_controlled_hash(rule_details)
228
215
  cef_logger.virtual_patch_message(virtual_patch_to_json, outcome)
229
216
  when /ip_denylist/i
230
217
  sender_ip = extract_sender_ip
231
- ip_denylist_to_json = Contrast::Api::Dtm::IpDenylistDetails.to_controlled_hash rule_details
218
+ ip_denylist_to_json = Contrast::Api::Dtm::IpDenylistDetails.to_controlled_hash(rule_details)
232
219
  return unless sender_ip
233
220
  return unless sender_ip.include?(ip_denylist_to_json[:ip])
234
221
 
@@ -47,21 +47,21 @@ module Contrast
47
47
  # This method is used to send our JSON messages directly to TeamServer at the end of each request. As we move
48
48
  # more endpoints over, this method will take the messages originally sent by #send_actiivty_messages. At the end,
49
49
  # that method should be removed.
50
- # TODO: RUBY-1358
51
- def report_activity
50
+ def report_activity # rubocop:disable Metrics/AbcSize
52
51
  return unless Contrast::Agent::Reporter.enabled?
53
52
 
54
53
  reporter = Contrast::Agent.reporter
55
54
  return unless reporter
56
55
 
57
56
  # Mask Sensitive Data
58
- Contrast::Agent::Reporting::Masker.mask context.activity
57
+ Contrast::Agent::Reporting::Masker.mask(context.activity)
59
58
 
60
59
  Contrast::Agent::Inventory::DependencyUsageAnalysis.instance.generate_library_usage(context.activity)
61
60
  [
62
61
  context.new_observed_route,
63
62
  Contrast::Agent::Reporting::DtmMessage.dtm_to_event(context.server_activity),
64
- Contrast::Agent::Reporting::DtmMessage.dtm_to_event(context.activity.library_usages)
63
+ Contrast::Agent::Reporting::DtmMessage.dtm_to_event(context.activity.library_usages),
64
+ Contrast::Agent::Reporting::DtmMessage.dtm_to_event(context.activity)
65
65
  ].each do |event|
66
66
  reporter.send_event(event)
67
67
  rescue StandardError => e
@@ -22,6 +22,8 @@ module Contrast
22
22
 
23
23
  extend Forwardable
24
24
 
25
+ # @return [Array, Rack::Response] The Rack Response passed by the application & middleware. It can be an Array
26
+ # in format [response_code, header_hash, response_body] or an instance of Rack::Response
25
27
  attr_reader :rack_response
26
28
 
27
29
  def initialize rack_response
@@ -23,7 +23,7 @@ module Contrast
23
23
  end
24
24
  rescue Contrast::SecurityException => e
25
25
  logger.warn('RASP threw security exception in prefilter', e)
26
- raise e
26
+ raise(e)
27
27
  rescue StandardError => e
28
28
  logger.error('Unexpected exception during prefilter', e)
29
29
  end
@@ -39,7 +39,7 @@ module Contrast
39
39
  end
40
40
  rescue Contrast::SecurityException => e
41
41
  logger.warn('RASP threw security exception in postfilter', e)
42
- raise e
42
+ raise(e)
43
43
  rescue StandardError => e
44
44
  logger.error('Unexpected exception during postfilter', e)
45
45
  end
@@ -142,7 +142,7 @@ module Contrast
142
142
  # @param scope_sym [Symbol] scope to check.
143
143
  # @return [Boolean] true | false
144
144
  def valid_scope? scope_sym
145
- Contrast::Agent::Scope::SCOPE_LIST.include? scope_sym
145
+ Contrast::Agent::Scope::SCOPE_LIST.include?(scope_sym)
146
146
  end
147
147
  end
148
148
  end
@@ -7,71 +7,29 @@ require 'contrast/agent/reporting/report'
7
7
 
8
8
  module Contrast
9
9
  module Agent
10
- # The ServiceHeartbeat functions to keep the Contrast Service alive and
11
- # ensure that it maintains this Agent's ApplicationContext.
10
+ # The ServiceHeartbeat functions to keep the Contrast Service alive and ensure that it maintains this Agent's
11
+ # ApplicationContext.
12
12
  class ServiceHeartbeat < WorkerThread
13
13
  include Contrast::Components::Logger::InstanceMethods
14
14
 
15
15
  # Spec recommends 30 seconds, we're going with 15.
16
16
  REFRESH_INTERVAL_SEC = 15
17
17
 
18
- # check if we can report to TS
19
- #
20
- # @return[Boolean] true if bypass is enabled, or false if bypass disabled
21
- def enabled?
22
- @_enabled = Contrast::CONTRAST_SERVICE.use_agent_communication? if @_enabled.nil?
23
- @_enabled
24
- end
25
-
26
- def client
27
- @_client ||= Contrast::Agent::Reporting::ReporterClient.new
28
- end
29
-
30
- def connection
31
- @_connection ||= client.initialize_connection
32
- end
33
-
34
18
  def start_thread!
19
+ return if ::Contrast::CONTRAST_SERVICE.unnecessary?
35
20
  return if running?
36
21
 
37
- report_from_reporter = check_report_provider
38
-
39
22
  @_thread = Contrast::Agent::Thread.new do
40
23
  logger.info('Starting heartbeat thread.')
41
24
  loop do
42
- if report_from_reporter
43
- client.send_event(poll_message, connection)
44
- else
45
- Contrast::Agent.messaging_queue.send_event_eventually(poll_message)
46
- end
47
-
48
- sleep REFRESH_INTERVAL_SEC
25
+ Contrast::Agent.messaging_queue.send_event_eventually(poll_message)
26
+ sleep(REFRESH_INTERVAL_SEC)
49
27
  end
50
28
  end
51
29
  end
52
30
 
53
31
  def poll_message
54
- @_poll_message ||= if enabled? && client
55
- Contrast::Agent::Reporting::Poll.new
56
- else
57
- Contrast::Api::Dtm::Poll.new
58
- end
59
- end
60
-
61
- def check_report_provider
62
- return false unless enabled?
63
- return false unless client && connection
64
-
65
- client.startup!(connection)
66
- true
67
- end
68
-
69
- def send_event provider
70
- if provider
71
- client.send_event(poll_message, connection)
72
- return
73
- end
74
- Contrast::Agent.messaging_queue.send_event_eventually(poll_message)
32
+ @_poll_message ||= Contrast::Api::Dtm::Poll.new
75
33
  end
76
34
  end
77
35
  end
@@ -40,7 +40,7 @@ module Contrast
40
40
  inventory_report = Contrast::Agent::Reporting::ApplicationInventory.convert(app_update_msg)
41
41
  Contrast::Agent.reporter.send_event(inventory_report)
42
42
  else
43
- Contrast::Agent.messaging_queue.send_event_eventually(app_update_msg)
43
+ Contrast::Agent.messaging_queue.send_event_eventually(app_update_msg, force: true)
44
44
  end
45
45
  end
46
46
 
@@ -0,0 +1,155 @@
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/config/env_variables'
5
+ require 'contrast/components/logger'
6
+ require 'contrast/utils/telemetry_client'
7
+ require 'contrast/agent/worker_thread'
8
+ require 'contrast/utils/telemetry'
9
+ require 'contrast/agent/telemetry/events/exceptions/telemetry_exceptions'
10
+ require 'contrast/agent/telemetry/events/exceptions/telemetry_exceptions_report'
11
+
12
+ module Contrast
13
+ module Agent
14
+ module Telemetry
15
+ # This class will initialize and hold everything needed for the telemetry
16
+ class Base < WorkerThread
17
+ include Contrast::Components::Logger::InstanceMethods
18
+ include Contrast::Agent::Telemetry::TelemetryExceptionReport
19
+
20
+ # this is where we will send the data from the agents
21
+ URL = 'https://telemetry.ruby.contrastsecurity.com/'
22
+ # Suggested timeout after each send is to be 3 hours (10800 seconds)
23
+ SUGGESTED_TIMEOUT = 10_800
24
+
25
+ class << self
26
+ include Contrast::Components::Logger::InstanceMethods
27
+ include Contrast::Config::EnvVariables
28
+
29
+ def application_id
30
+ Contrast::Utils::Telemetry::Identifier.application_id
31
+ end
32
+
33
+ def instance_id
34
+ Contrast::Utils::Telemetry::Identifier.instance_id
35
+ end
36
+
37
+ def enabled?
38
+ @_enabled = telemetry_enabled? if @_enabled.nil?
39
+ @_enabled
40
+ end
41
+
42
+ private
43
+
44
+ def telemetry_enabled?
45
+ opt_out_telemetry = return_value(:telemetry_opt_outs).to_s
46
+ return false if opt_out_telemetry.casecmp?('true') || opt_out_telemetry == '1'
47
+
48
+ # In case of connection error, do not create the background thread or queue,
49
+ # as if the opt-out env var was set
50
+ @_client = Contrast::Utils::TelemetryClient.new
51
+ ip_opt_out_telemetry = @_client.initialize_connection(URL)
52
+ if ip_opt_out_telemetry.nil?
53
+ logger.warn("Connection was not established properly!!! \n Telemetry reporting will be disabled!")
54
+ return false
55
+ end
56
+
57
+ true
58
+ end
59
+ end
60
+
61
+ def client
62
+ @_client ||= Contrast::Utils::TelemetryClient.new
63
+ end
64
+
65
+ def connection
66
+ @_connection ||= client.initialize_connection(URL)
67
+ end
68
+
69
+ def error_messages
70
+ @_error_messages ||= []
71
+ end
72
+
73
+ def attempt_to_start?
74
+ unless cs__class.enabled?
75
+ logger.warn('Telemetry service is disabled!')
76
+ return false
77
+ end
78
+
79
+ logger.debug('Attempting to start telemetry thread') unless running?
80
+ true
81
+ end
82
+
83
+ def start_thread!
84
+ return if running?
85
+
86
+ # It is recommended that implementations send a single payload of
87
+ # general metrics every 3 hours, starting from implementation startup.
88
+ @_thread = Contrast::Agent::Thread.new do
89
+ logger.debug('Starting background telemetry thread.')
90
+ loop do
91
+ next unless client && connection
92
+
93
+ # Start pushing exceptions to queue for reporting.
94
+ push_exceptions
95
+ until queue.empty?
96
+ event = queue.pop
97
+ begin
98
+ logger.debug('This is the current processed event', event)
99
+ sleep_time = request_with_response(event)
100
+ if sleep_time
101
+ sleep(sleep_time)
102
+ logger.debug('Retrying to process event', event)
103
+ retry_sleep_time = request_with_response(event)
104
+ sleep(retry_sleep_time) unless retry_sleep_time.nil?
105
+ end
106
+ rescue StandardError => e
107
+ logger.error('Could not send message to service from telemetry queue.', e)
108
+ stop!
109
+ end
110
+ end
111
+ sleep(SUGGESTED_TIMEOUT)
112
+ end
113
+ end
114
+ end
115
+
116
+ def send_event event
117
+ if ::Contrast::AGENT.disabled?
118
+ logger.warn('Attempted to queue event with Agent disabled', caller: caller, event: event)
119
+ return
120
+ end
121
+
122
+ return unless cs__class.enabled?
123
+
124
+ logger.debug('Enqueued event for sending', event_type: event.cs__class)
125
+ queue << event if event
126
+ end
127
+
128
+ def delete_queue!
129
+ @_queue&.clear
130
+ @_queue&.close
131
+ @_queue = nil
132
+ end
133
+
134
+ def stop!
135
+ return unless running?
136
+
137
+ @_enabled = false
138
+ delete_queue!
139
+ super
140
+ end
141
+
142
+ def request_with_response event
143
+ res = client.send_request(event, connection)
144
+ client.handle_response(res)
145
+ end
146
+
147
+ private
148
+
149
+ def queue
150
+ @_queue ||= Queue.new
151
+ end
152
+ end
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,35 @@
1
+ # Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ require 'contrast/utils/metrics_hash'
5
+
6
+ module Contrast
7
+ module Agent
8
+ module Telemetry
9
+ # This class will hold the basic information for a Telemetry Event
10
+ class Event
11
+ include Contrast::Utils
12
+
13
+ attr_reader :tags
14
+
15
+ def initialize
16
+ @tags = MetricsHash.new(String)
17
+ @timestamp = Time.now.iso8601
18
+ end
19
+
20
+ def path
21
+ ''
22
+ end
23
+
24
+ def to_hash **_args
25
+ {
26
+ tags: @tags,
27
+ timestamp: @timestamp,
28
+ instance: Contrast::Agent::Telemetry::Base.instance_id,
29
+ application: Contrast::Agent::Telemetry::Base.application_id
30
+ }
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,119 @@
1
+ # Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ module Contrast
5
+ module Agent
6
+ module Telemetry
7
+ module TelemetryException
8
+ # Here will be all known gems to obfuscate during the building of TelemetryExceptions
9
+ module Obfuscate
10
+ # List of known gems to be third parties not containing any
11
+ # user sensitive data.
12
+ KNOWN_GEMS = %w[
13
+ activesupport redis-activesupport redis builder dry-types rails rails-html-sanitizer rails-observers sinatra
14
+ benchmark-ips bundler climate_control execjs factory_bot debride fake_ftp fasterer flay openssl
15
+ parallel_tests contrast contrast-agent ruby pry-byebug byebug pry resolv ougai protobuf async nokogiri
16
+ benchmark grape-order grape-activerecord newrelic newrelic-grape grape-rails-cache grape-msgpack
17
+ grape-batch rspec rake rubocop grape-jbuilder rack-test rack-protection minitest grape-instrumentation
18
+ minitest-global_expectations rack-proxy rack-attack rack-ssl grape-cancan grape-attack grape-rake-tasks
19
+ grape-route-helpers benchmark-memory grape-rabl grape-kaminari grape-path-helpers grape-swagger-entity
20
+ grape-swagger-rails grape-roar newrelic-rake grapes-wagger-representative grape-forgery_protection
21
+ grape-papertrail grape-app grape-middleware-logger rack-protection rake-compile rhino rspec-benchmark
22
+ rspec_junit_formatter rspec-rails rake-performance rubocop-rails rubocop-rake rubocop-rspec
23
+ ruby-debug-ide simplecov sqlite steep tilt tzinfo-data warning xpath sinatra-activerecord sinatra-param
24
+ sinatra-partial sinatra-flash sinatra-reloader sinatra-sinatra rake_fly rack rack-accept grape grape-entity
25
+ sinatra-advanced-routes sinatra-warden grape_logging grape-swagger sinatra-namespace sinatra-resource
26
+ sinatra-hashfix sinatra-instrumentation sinatra-helpers redis-rack sinatra-support sinatra-assetpack
27
+ sinatra-router sinatra-cross_origin sinatra_more sinatra-jsonp faraday-rack zlib mustermann mustermann-grape
28
+ rspec-expectations rspec-core rspec-mocks rspec-sidekiq
29
+ ].cs__freeze
30
+ KNOWN_ERRORS = %w[
31
+ ActiveModel Error Errors Resolv Rspec ActiveRecord nil NilClass NoMemoryError ScriptError
32
+ LoadError NotImplementedError SyntaxError SecurityError SignalException Interrupt
33
+ StandardError ArgumentError UncaughtThrowError FiberError IOError EOFError IndexError KeyError
34
+ StopIteration LocalJumpError NameError NoMethodError RangeError FloatDomainError RegexpError
35
+ RuntimeError FrozenError SystemCallError ThreadError TypeError ZeroDivisionError SystemExit
36
+ SystemStackError fatal Warning buffer OpenSSL SSL SSLError Errno Timeout Exception EOFError
37
+ ECONNRESET ECONNREFUSED ETIMEDOUT EINVAL ESHUTDOWN EHOSTDOWN EHOSTUNREACH EISCONN
38
+ ECONNABORTED ENETRESET ENETUNREACH Net HTTPBadResponse HTTPHeaderSyntaxError ProtocolError
39
+ Faraday BadRequestError UnauthorizedError ForbiddenError ResourceNotFound ProxyAuthError
40
+ ConflictError UnprocessableEntityError ClientError
41
+ ].cs__freeze
42
+ CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'.cs__freeze
43
+ # This will generate different chars for each agent startup but will be constant
44
+ # for current run and will make identical obfuscation to be compared if given type
45
+ # is the same.
46
+ CYPHER = CHARS.chars.shuffle.join
47
+
48
+ class << self
49
+ # Returns paths for known gems.
50
+ #
51
+ # @return [Array<Regexp>] known paths
52
+ def known_gem_paths
53
+ @_known_gem_paths ||= KNOWN_GEMS.map do |gem_name|
54
+ to_regexp(File.join(
55
+ 'gems', gem_name.tr('-', ''), 'lib'))
56
+ end
57
+ end
58
+
59
+ # Returns known modules names.
60
+ #
61
+ # @return [Array<Regexp>] known modules
62
+ def known_modules
63
+ @_known_modules ||= KNOWN_ERRORS.map { |module_name| to_regexp(module_name) }
64
+ end
65
+
66
+ # Obfuscate a type and replace it with random characters.
67
+ #
68
+ # @param type [String] the StackFrame type to obfuscate
69
+ # @return [String] obfuscated type
70
+ def obfuscate_type type
71
+ return unless type
72
+
73
+ check = type.tr('[^0-9].-', '')
74
+ type = cypher(type) unless match_known(known_gem_paths, check)
75
+ type
76
+ end
77
+
78
+ # Obfuscate a type and replace it with random characters.
79
+ #
80
+ # @param exception_type [String] the exception type to obfuscate
81
+ # @return [String] obfuscated exception type
82
+ def obfuscate_exception_type exception_type
83
+ return unless exception_type
84
+
85
+ exceptions = exception_type.split('::').each do |exception|
86
+ cypher(exception) unless match_known(known_modules, exception)
87
+ end
88
+ exceptions.join('::')
89
+ end
90
+
91
+ private
92
+
93
+ # Transforms string to regexp
94
+ #
95
+ # @param string [String]
96
+ def to_regexp string
97
+ /#{ string }/
98
+ end
99
+
100
+ # Add cypher to path to make it obscure, but unique enough for
101
+ # comparisons. Mutates original or duplicate if frozen string.
102
+ #
103
+ # @param string [String] string to be transformed.
104
+ def cypher string
105
+ string = string.dup if string.cs__frozen?
106
+ string.to_s.tr!(CHARS, CYPHER)
107
+ end
108
+
109
+ # @param known [Array<Regexp>] Array of regexp to match againts
110
+ # @param type [String] type to check
111
+ def match_known known, type
112
+ known.any? { |regexp| type =~ regexp }
113
+ end
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end