contrast-agent 5.2.0 → 6.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (228) hide show
  1. checksums.yaml +4 -4
  2. data/ext/cs__assess_array/cs__assess_array.c +7 -0
  3. data/ext/cs__assess_basic_object/cs__assess_basic_object.c +19 -5
  4. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.c +1 -1
  5. data/ext/cs__assess_hash/cs__assess_hash.c +3 -4
  6. data/ext/cs__assess_kernel/cs__assess_kernel.c +1 -2
  7. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +26 -12
  8. data/ext/cs__assess_module/cs__assess_module.c +1 -1
  9. data/ext/cs__assess_regexp/cs__assess_regexp.c +15 -2
  10. data/ext/cs__assess_regexp/cs__assess_regexp.h +2 -0
  11. data/ext/cs__assess_string/cs__assess_string.c +21 -1
  12. data/ext/cs__assess_test/cs__assess_test.h +9 -0
  13. data/ext/cs__assess_test/cs__assess_tests.c +22 -0
  14. data/ext/cs__assess_test/extconf.rb +5 -0
  15. data/ext/cs__common/cs__common.c +113 -11
  16. data/ext/cs__common/cs__common.h +29 -5
  17. data/ext/cs__contrast_patch/cs__contrast_patch.c +55 -44
  18. data/ext/cs__os_information/cs__os_information.c +13 -10
  19. data/ext/cs__scope/cs__scope.c +146 -97
  20. data/ext/cs__tests/cs__tests.c +12 -0
  21. data/ext/cs__tests/cs__tests.h +3 -0
  22. data/ext/cs__tests/extconf.rb +5 -0
  23. data/lib/contrast/agent/assess/contrast_object.rb +16 -16
  24. data/lib/contrast/agent/assess/events/source_event.rb +17 -19
  25. data/lib/contrast/agent/assess/finalizers/hash.rb +2 -0
  26. data/lib/contrast/agent/assess/policy/policy_node.rb +50 -27
  27. data/lib/contrast/agent/assess/policy/policy_node_utils.rb +51 -0
  28. data/lib/contrast/agent/assess/policy/policy_scanner.rb +2 -16
  29. data/lib/contrast/agent/assess/policy/preshift.rb +8 -2
  30. data/lib/contrast/agent/assess/policy/propagation_method.rb +47 -13
  31. data/lib/contrast/agent/assess/policy/propagator/buffer.rb +118 -0
  32. data/lib/contrast/agent/assess/policy/propagator/keep.rb +19 -4
  33. data/lib/contrast/agent/assess/policy/propagator/remove.rb +18 -2
  34. data/lib/contrast/agent/assess/policy/propagator/splat.rb +17 -3
  35. data/lib/contrast/agent/assess/policy/propagator/split.rb +15 -19
  36. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +1 -1
  37. data/lib/contrast/agent/assess/policy/propagator/substitution_utils.rb +1 -1
  38. data/lib/contrast/agent/assess/policy/propagator/trim.rb +1 -1
  39. data/lib/contrast/agent/assess/policy/propagator.rb +1 -0
  40. data/lib/contrast/agent/assess/policy/source_method.rb +7 -7
  41. data/lib/contrast/agent/assess/policy/trigger_method.rb +4 -10
  42. data/lib/contrast/agent/assess/property/tagged.rb +1 -1
  43. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +7 -2
  44. data/lib/contrast/agent/assess/rule/response/auto_complete_rule.rb +1 -1
  45. data/lib/contrast/agent/assess/rule/response/base_rule.rb +11 -3
  46. data/lib/contrast/agent/assess/rule/response/body_rule.rb +3 -3
  47. data/lib/contrast/agent/assess/rule/response/cache_control_header_rule.rb +60 -36
  48. data/lib/contrast/agent/assess/rule/response/framework/rails_support.rb +3 -3
  49. data/lib/contrast/agent/assess/rule/response/parameters_pollution_rule.rb +1 -1
  50. data/lib/contrast/agent/assess/rule/response/x_xss_protection_header_rule.rb +1 -2
  51. data/lib/contrast/agent/at_exit_hook.rb +1 -1
  52. data/lib/contrast/agent/deadzone/policy/deadzone_node.rb +0 -7
  53. data/lib/contrast/agent/deadzone/policy/policy.rb +0 -6
  54. data/lib/contrast/agent/exclusion_matcher.rb +3 -3
  55. data/lib/contrast/agent/inventory/database_config.rb +10 -3
  56. data/lib/contrast/agent/middleware.rb +5 -3
  57. data/lib/contrast/agent/patching/policy/after_load_patch.rb +0 -2
  58. data/lib/contrast/agent/patching/policy/patch.rb +13 -12
  59. data/lib/contrast/agent/patching/policy/patcher.rb +4 -4
  60. data/lib/contrast/agent/patching/policy/policy_node.rb +15 -2
  61. data/lib/contrast/agent/protect/exploitable_collection.rb +38 -0
  62. data/lib/contrast/agent/protect/input_analyzer/input_analyzer.rb +66 -9
  63. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +2 -1
  64. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +2 -2
  65. data/lib/contrast/agent/protect/rule/base.rb +37 -5
  66. data/lib/contrast/agent/protect/rule/base_service.rb +3 -1
  67. data/lib/contrast/agent/protect/rule/cmd_injection.rb +13 -0
  68. data/lib/contrast/agent/protect/rule/cmdi/cmdi_input_classification.rb +83 -0
  69. data/lib/contrast/agent/protect/rule/cmdi/cmdi_worth_watching.rb +64 -0
  70. data/lib/contrast/agent/protect/rule/http_method_tampering/http_method_tampering_input_classification.rb +96 -0
  71. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +8 -0
  72. data/lib/contrast/agent/protect/rule/no_sqli/no_sqli_input_classification.rb +231 -0
  73. data/lib/contrast/agent/protect/rule/no_sqli.rb +27 -0
  74. data/lib/contrast/agent/protect/rule/sqli/sqli_input_classification.rb +18 -54
  75. data/lib/contrast/agent/protect/rule/sqli/sqli_worth_watching.rb +1 -4
  76. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_input_classification.rb +82 -0
  77. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_matcher.rb +45 -0
  78. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +42 -0
  79. data/lib/contrast/agent/reporting/attack_result/attack_result.rb +63 -0
  80. data/lib/contrast/agent/reporting/attack_result/rasp_rule_sample.rb +52 -0
  81. data/lib/contrast/agent/reporting/attack_result/response_type.rb +29 -0
  82. data/lib/contrast/agent/reporting/attack_result/user_input.rb +87 -0
  83. data/lib/contrast/agent/reporting/masker/masker.rb +243 -0
  84. data/lib/contrast/agent/reporting/masker/masker_utils.rb +62 -0
  85. data/lib/contrast/agent/reporting/report.rb +2 -0
  86. data/lib/contrast/agent/reporting/reporter.rb +29 -22
  87. data/lib/contrast/agent/reporting/reporter_heartbeat.rb +49 -0
  88. data/lib/contrast/agent/reporting/reporting_events/agent_startup.rb +34 -0
  89. data/lib/contrast/agent/reporting/reporting_events/application_activity.rb +53 -0
  90. data/lib/contrast/agent/reporting/reporting_events/application_defend_activity.rb +48 -0
  91. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_activity.rb +64 -0
  92. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample.rb +70 -0
  93. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample_activity.rb +57 -0
  94. data/lib/contrast/agent/reporting/reporting_events/application_defend_attacker_activity.rb +56 -0
  95. data/lib/contrast/agent/reporting/reporting_events/application_inventory.rb +12 -4
  96. data/lib/contrast/agent/reporting/reporting_events/application_inventory_activity.rb +58 -0
  97. data/lib/contrast/agent/reporting/reporting_events/application_reporting_event.rb +27 -0
  98. data/lib/contrast/agent/reporting/reporting_events/application_startup.rb +50 -0
  99. data/lib/contrast/agent/reporting/reporting_events/application_startup_instrumentation.rb +27 -0
  100. data/lib/contrast/agent/reporting/reporting_events/application_update.rb +7 -12
  101. data/lib/contrast/agent/reporting/reporting_events/finding.rb +10 -4
  102. data/lib/contrast/agent/reporting/reporting_events/finding_event.rb +2 -4
  103. data/lib/contrast/agent/reporting/reporting_events/finding_event_object.rb +3 -3
  104. data/lib/contrast/agent/reporting/reporting_events/library_usage_observation.rb +5 -5
  105. data/lib/contrast/agent/reporting/reporting_events/observed_library_usage.rb +6 -2
  106. data/lib/contrast/agent/reporting/reporting_events/observed_route.rb +16 -12
  107. data/lib/contrast/agent/reporting/reporting_events/poll.rb +6 -2
  108. data/lib/contrast/agent/reporting/reporting_events/preflight.rb +10 -8
  109. data/lib/contrast/agent/reporting/reporting_events/preflight_message.rb +8 -11
  110. data/lib/contrast/agent/reporting/reporting_events/reporting_event.rb +2 -1
  111. data/lib/contrast/agent/reporting/reporting_events/route_coverage.rb +8 -6
  112. data/lib/contrast/agent/reporting/reporting_events/server_activity.rb +12 -20
  113. data/lib/contrast/agent/reporting/reporting_events/server_reporting_event.rb +27 -0
  114. data/lib/contrast/agent/reporting/reporting_utilities/audit.rb +17 -27
  115. data/lib/contrast/agent/reporting/reporting_utilities/build_preflight.rb +38 -0
  116. data/lib/contrast/agent/reporting/reporting_utilities/dtm_message.rb +8 -0
  117. data/lib/contrast/agent/reporting/reporting_utilities/endpoints.rb +6 -0
  118. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +23 -7
  119. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client_utils.rb +64 -76
  120. data/lib/contrast/agent/reporting/reporting_utilities/reporting_storage.rb +1 -1
  121. data/lib/contrast/agent/reporting/reporting_utilities/response.rb +17 -7
  122. data/lib/contrast/agent/reporting/reporting_utilities/response_extractor.rb +100 -0
  123. data/lib/contrast/agent/reporting/reporting_utilities/response_handler.rb +75 -13
  124. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_mode.rb +63 -0
  125. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_utils.rb +154 -113
  126. data/lib/contrast/agent/reporting/settings/application_settings.rb +9 -0
  127. data/lib/contrast/agent/reporting/settings/assess_server_feature.rb +5 -33
  128. data/lib/contrast/agent/reporting/settings/protect.rb +1 -1
  129. data/lib/contrast/agent/reporting/settings/protect_server_feature.rb +2 -2
  130. data/lib/contrast/agent/reporting/settings/sampling.rb +36 -0
  131. data/lib/contrast/agent/reporting/settings/sensitive_data_masking.rb +110 -0
  132. data/lib/contrast/agent/reporting/settings/sensitive_data_masking_rule.rb +58 -0
  133. data/lib/contrast/agent/request.rb +3 -3
  134. data/lib/contrast/agent/request_context.rb +1 -1
  135. data/lib/contrast/agent/request_context_extend.rb +2 -2
  136. data/lib/contrast/agent/request_handler.rb +7 -3
  137. data/lib/contrast/agent/response.rb +2 -0
  138. data/lib/contrast/agent/service_heartbeat.rb +6 -48
  139. data/lib/contrast/agent/static_analysis.rb +1 -1
  140. data/lib/contrast/agent/telemetry/base.rb +151 -0
  141. data/lib/contrast/agent/telemetry/events/event.rb +35 -0
  142. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_base.rb +59 -0
  143. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_event.rb +44 -0
  144. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message.rb +115 -0
  145. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message_exception.rb +83 -0
  146. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_stack_frame.rb +64 -0
  147. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exceptions.rb +20 -0
  148. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exceptions_report.rb +32 -0
  149. data/lib/contrast/agent/telemetry/events/metric_event.rb +28 -0
  150. data/lib/contrast/agent/telemetry/events/startup_metrics_event.rb +123 -0
  151. data/lib/contrast/agent/thread_watcher.rb +52 -68
  152. data/lib/contrast/agent/version.rb +1 -1
  153. data/lib/contrast/agent/worker_thread.rb +8 -0
  154. data/lib/contrast/agent.rb +4 -3
  155. data/lib/contrast/api/communication/messaging_queue.rb +28 -11
  156. data/lib/contrast/api/communication/response_processor.rb +7 -10
  157. data/lib/contrast/api/communication/speedracer.rb +1 -1
  158. data/lib/contrast/api/decorators/activity.rb +33 -0
  159. data/lib/contrast/api/decorators/address.rb +1 -1
  160. data/lib/contrast/api/decorators/http_request.rb +1 -1
  161. data/lib/contrast/api/decorators/response_type.rb +30 -0
  162. data/lib/contrast/api/decorators.rb +1 -0
  163. data/lib/contrast/components/app_context.rb +0 -4
  164. data/lib/contrast/components/assess.rb +14 -0
  165. data/lib/contrast/components/config.rb +13 -22
  166. data/lib/contrast/components/contrast_service.rb +9 -0
  167. data/lib/contrast/components/protect.rb +2 -2
  168. data/lib/contrast/components/sampling.rb +7 -11
  169. data/lib/contrast/components/settings.rb +116 -8
  170. data/lib/contrast/config/agent_configuration.rb +34 -41
  171. data/lib/contrast/config/api_configuration.rb +16 -75
  172. data/lib/contrast/config/api_proxy_configuration.rb +9 -48
  173. data/lib/contrast/config/application_configuration.rb +24 -95
  174. data/lib/contrast/config/assess_configuration.rb +21 -76
  175. data/lib/contrast/config/assess_rules_configuration.rb +13 -38
  176. data/lib/contrast/config/base_configuration.rb +11 -76
  177. data/lib/contrast/config/certification_configuration.rb +15 -68
  178. data/lib/contrast/config/exception_configuration.rb +15 -59
  179. data/lib/contrast/config/heap_dump_configuration.rb +19 -73
  180. data/lib/contrast/config/inventory_configuration.rb +11 -55
  181. data/lib/contrast/config/logger_configuration.rb +8 -41
  182. data/lib/contrast/config/protect_configuration.rb +23 -10
  183. data/lib/contrast/config/protect_rule_configuration.rb +23 -37
  184. data/lib/contrast/config/protect_rules_configuration.rb +39 -43
  185. data/lib/contrast/config/request_audit_configuration.rb +16 -55
  186. data/lib/contrast/config/root_configuration.rb +70 -13
  187. data/lib/contrast/config/ruby_configuration.rb +14 -47
  188. data/lib/contrast/config/sampling_configuration.rb +12 -65
  189. data/lib/contrast/config/server_configuration.rb +13 -45
  190. data/lib/contrast/config/service_configuration.rb +36 -17
  191. data/lib/contrast/configuration.rb +21 -13
  192. data/lib/contrast/extension/assess/string.rb +20 -1
  193. data/lib/contrast/extension/module.rb +0 -1
  194. data/lib/contrast/framework/manager.rb +2 -2
  195. data/lib/contrast/framework/rails/patch/support.rb +13 -45
  196. data/lib/contrast/logger/aliased_logging.rb +87 -0
  197. data/lib/contrast/logger/application.rb +1 -5
  198. data/lib/contrast/logger/cef_log.rb +1 -1
  199. data/lib/contrast/tasks/config.rb +100 -4
  200. data/lib/contrast/utils/assess/object_store.rb +36 -0
  201. data/lib/contrast/utils/assess/propagation_method_utils.rb +6 -0
  202. data/lib/contrast/utils/class_util.rb +5 -18
  203. data/lib/contrast/utils/input_classification.rb +73 -0
  204. data/lib/contrast/utils/invalid_configuration_util.rb +1 -1
  205. data/lib/contrast/utils/log_utils.rb +3 -1
  206. data/lib/contrast/utils/middleware_utils.rb +10 -9
  207. data/lib/contrast/utils/net_http_base.rb +1 -1
  208. data/lib/contrast/utils/object_share.rb +2 -1
  209. data/lib/contrast/utils/os.rb +0 -5
  210. data/lib/contrast/utils/patching/policy/patch_utils.rb +4 -5
  211. data/lib/contrast/utils/response_utils.rb +18 -33
  212. data/lib/contrast/utils/telemetry.rb +20 -2
  213. data/lib/contrast/utils/telemetry_client.rb +23 -11
  214. data/lib/contrast/utils/telemetry_hash.rb +41 -0
  215. data/lib/contrast/utils/telemetry_identifier.rb +16 -1
  216. data/lib/contrast.rb +9 -0
  217. data/resources/assess/policy.json +98 -0
  218. data/resources/deadzone/policy.json +0 -86
  219. data/ruby-agent.gemspec +10 -9
  220. data/service_executables/VERSION +1 -1
  221. data/service_executables/linux/contrast-service +0 -0
  222. data/service_executables/mac/contrast-service +0 -0
  223. metadata +98 -30
  224. data/lib/contrast/agent/metric_telemetry_event.rb +0 -26
  225. data/lib/contrast/agent/startup_metrics_telemetry_event.rb +0 -121
  226. data/lib/contrast/agent/telemetry.rb +0 -137
  227. data/lib/contrast/agent/telemetry_event.rb +0 -33
  228. data/lib/contrast/utils/exclude_key.rb +0 -20
@@ -41,7 +41,7 @@ module Contrast
41
41
  #
42
42
  # @param object [Contrast::Agent::Assess::ContrastObject, nil]
43
43
  def attach_data object, truncate
44
- @hash = object&.object.__id__
44
+ @hash = object ? object.tracked_object_id : nil.__id__
45
45
  @tracked = !!object&.tracked?
46
46
  @value = reportable_value(object&.object, truncate)
47
47
  end
@@ -75,9 +75,9 @@ module Contrast
75
75
  #
76
76
  # @param value [String, nil] the contrast_string of the object this represents.
77
77
  # @param truncate [Boolean] if the string should be truncated or not.
78
- # @return [String]
78
+ # @return [String] The strict_encode64 value of the String sent to TeamServer to represent this object.
79
79
  def reportable_value value, truncate
80
- return Contrast::Utils::ObjectShare::NIL_STRING unless value
80
+ return Contrast::Utils::ObjectShare::NIL_64_STRING unless value
81
81
 
82
82
  if truncate && value.length > TRUNCATION_LENGTH
83
83
  tmp = []
@@ -4,12 +4,12 @@
4
4
  module Contrast
5
5
  module Agent
6
6
  module Reporting
7
- #
8
- # @attr_accessor id - [String] - Hash of library as identified by the agent
9
- # @attr_accessor names Array[String] - List of file paths that have been loaded out of or executed by the library
10
- #
7
+ # The usage, meaning loaded files, of a library seen during this request
11
8
  class LibraryUsageObservation
12
- attr_accessor :id, :names
9
+ # @param [String] Sha256Sum of library as identified by the agent
10
+ attr_accessor :id
11
+ # @param [Array<String>] List of file paths that have been loaded out of or executed by the library
12
+ attr_reader :names
13
13
 
14
14
  class << self
15
15
  # Convert a DTM for SpeedRacer to an Event for TeamServer.
@@ -1,7 +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/agent/reporting/reporting_events/reporting_event'
4
+ require 'contrast/agent/reporting/reporting_events/application_reporting_event'
5
5
  require 'contrast/agent/reporting/reporting_events/library_usage_observation'
6
6
 
7
7
  module Contrast
@@ -11,7 +11,7 @@ module Contrast
11
11
  #
12
12
  # @attr_reader observations - Array[Contrast::Agent::Reporting::LibraryUsageObservation]
13
13
  # - Hash of LibraryUsageObservations
14
- class ObservedLibraryUsage < Contrast::Agent::Reporting::ReportingEvent
14
+ class ObservedLibraryUsage < Contrast::Agent::Reporting::ApplicationReportingEvent
15
15
  attr_reader :observations
16
16
 
17
17
  class << self
@@ -32,6 +32,10 @@ module Contrast
32
32
  super
33
33
  end
34
34
 
35
+ def file_name
36
+ 'library-observation'
37
+ end
38
+
35
39
  def to_controlled_hash
36
40
  validate
37
41
  { observations: @observations.map(&:to_controlled_hash) }
@@ -3,7 +3,7 @@
3
3
 
4
4
  require 'json'
5
5
  require 'contrast/components/logger'
6
- require 'contrast/agent/reporting/reporting_events/reporting_event'
6
+ require 'contrast/agent/reporting/reporting_events/application_reporting_event'
7
7
  require 'contrast/utils/object_share'
8
8
 
9
9
  module Contrast
@@ -15,16 +15,16 @@ module Contrast
15
15
  # externally accessible endpoints within the application, as registered to the application framework. This also
16
16
  # includes the literal URL and HTTP Verb used to invoke them, as they must have been called at this point to be
17
17
  # recorded.
18
- #
19
- # @attr_accessor signature [String] the method signature used to uniquely identify the coverage report.
20
- # @attr_accessor url [String] the normalized URL used to access the method in the route.
21
- # @attr_accessor verb [String] the HTTP Verb used to access the method in the route.
22
- # @attr_accessor agent_session_id_value [Integer] the session of this process, as determined from TeamServer on
23
- # application create.
24
- # @attr_accessor sources [Array<Contrast::Agent::Reporting::TraceEventSource>] the sources of user input accessed
25
- # during this request. Used for remediation determinations in TeamServer.
26
- class ObservedRoute < Contrast::Agent::Reporting::ReportingEvent
27
- attr_accessor :sources, :agent_session_id_value, :signature, :verb, :url
18
+ class ObservedRoute < Contrast::Agent::Reporting::ApplicationReportingEvent
19
+ # @param [String] the method signature used to uniquely identify the coverage report.
20
+ attr_accessor :signature
21
+ # @param [String] the normalized URL used to access the method in the route.
22
+ attr_accessor :url
23
+ # @param [String] the HTTP Verb used to access the method in the route.
24
+ attr_accessor :verb
25
+ # @param [Array<Contrast::Agent::Reporting::TraceEventSource>] the sources of user input accessed during this
26
+ # request. Used for remediation determinations in TeamServer.
27
+ attr_reader :sources
28
28
 
29
29
  def initialize
30
30
  @event_endpoint = Contrast::Agent::Reporting::Endpoints.observed_route
@@ -35,6 +35,10 @@ module Contrast
35
35
  super()
36
36
  end
37
37
 
38
+ def file_name
39
+ 'routes-observed'
40
+ end
41
+
38
42
  # Convert the instance variables on the class, and other information, into the identifiers required for
39
43
  # TeamServer to process the JSON form of this message.
40
44
  #
@@ -44,7 +48,7 @@ module Contrast
44
48
  validate
45
49
  rc_hash = {
46
50
  session_id: @agent_session_id_value,
47
- sources: @sources,
51
+ sources: @sources.map(&:to_controlled_hash),
48
52
  signature: @signature,
49
53
  verb: @verb,
50
54
  url: @url
@@ -1,19 +1,23 @@
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_events/reporting_event'
4
+ require 'contrast/agent/reporting/reporting_events/application_reporting_event'
5
5
 
6
6
  module Contrast
7
7
  module Agent
8
8
  module Reporting
9
9
  # This is the new Poll class for the Heartbeat Service.
10
- class Poll < Contrast::Agent::Reporting::ReportingEvent
10
+ class Poll < Contrast::Agent::Reporting::ApplicationReportingEvent
11
11
  def initialize
12
12
  @event_type = :heartbeat
13
13
  @event_endpoint = Contrast::Agent::Reporting::Endpoints.heartbeat
14
14
  super
15
15
  end
16
16
 
17
+ def file_name
18
+ 'applications-heartbeat'
19
+ end
20
+
17
21
  # This is commented out as I am not aware if we're supposed to send some information and/or parse it to hash
18
22
  # In https://github.com/Contrast-Security-Inc/contrast-service/blob/next/reporting/tsreporter.go
19
23
  #
@@ -1,7 +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/agent/reporting/reporting_events/reporting_event'
4
+ require 'contrast/agent/reporting/reporting_events/application_reporting_event'
5
5
 
6
6
  module Contrast
7
7
  module Agent
@@ -10,12 +10,10 @@ module Contrast
10
10
  # to report a Preflight message to TeamServer. This message represents the identifying information of a
11
11
  # collection of Findings/Traces, which TeamServer will use to determine if it requires the full information of
12
12
  # any of the Findings/Traces to be reported.
13
- #
14
- # For our purposes, we'll only ever send one message per request; however, we must use an array to conform to the
15
- # specification.
16
- #
17
- # @attr_reader messages [Array<Contrast::Agent::Reporting::PreflightMessage>]
18
- class Preflight < Contrast::Agent::Reporting::ReportingEvent
13
+ class Preflight < Contrast::Agent::Reporting::ApplicationReportingEvent
14
+ # For our purposes, we'll only ever send one message per request; however,
15
+ # we must use an array to conform to the specification.
16
+ # @return messages [Array<Contrast::Agent::Reporting::PreflightMessage>]
19
17
  attr_reader :messages
20
18
 
21
19
  def initialize
@@ -25,13 +23,17 @@ module Contrast
25
23
  super
26
24
  end
27
25
 
26
+ def file_name
27
+ 'preflight'
28
+ end
29
+
28
30
  # Convert the instance variables on the class, and other information, into the identifiers required for
29
31
  # TeamServer to process the JSON form of this message.
30
32
  #
31
33
  # @return [Hash]
32
34
  # @raise [ArgumentError]
33
35
  def to_controlled_hash
34
- { messages: @messages }
36
+ { messages: @messages.map(&:to_controlled_hash) }
35
37
  end
36
38
  end
37
39
  end
@@ -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/components/assess'
4
5
  require 'contrast/components/logger'
5
6
  require 'contrast/agent/reporting/reporting_events/reporting_event'
6
7
 
@@ -11,13 +12,13 @@ module Contrast
11
12
  # system to report a single message, part of the main Preflight, to TeamServer. This message represents the
12
13
  # identifying information of a Finding/Trace, which TeamServer will use to determine if it requires the full
13
14
  # information of the Finding/Trace to be reported.
14
- #
15
- # @attr_accessor data [String] the message identifier; rule_id,hash
16
- # @attr_accessor hash_code [String]
17
- # @attr_reader routes [Array<Contrast::Agent::Reporting::RouteDiscovery] the route that triggered the finding
18
- # to which this preflight applies.
19
15
  class PreflightMessage
20
- attr_accessor :data, :hash_code
16
+ # @return [String] the message identifier; rule_id,hash
17
+ attr_accessor :data
18
+ # @return [String] CRC checksum of the finding to which this message pertains
19
+ attr_accessor :hash_code
20
+ # @return [Array<Contrast::Agent::Reporting::RouteDiscovery] the route that triggered the finding to which this
21
+ # preflight applies.
21
22
  attr_reader :routes
22
23
 
23
24
  # The type of this event, as understood by TeamServer. While TRACE and APPUPDATE are both technically valid, we
@@ -29,7 +30,7 @@ module Contrast
29
30
  @app_name = ::Contrast::APP_CONTEXT.app_name
30
31
  @app_version = ::Contrast::APP_CONTEXT.app_version
31
32
  @routes = []
32
- @agent_session_id_value = Contrast::APP_CONTEXT.session_id
33
+ @agent_session_id_value = ::Contrast::ASSESS.session_id
33
34
  end
34
35
 
35
36
  # Convert the instance variables on the class, and other information, into the identifiers required for
@@ -53,16 +54,12 @@ module Contrast
53
54
 
54
55
  def validate
55
56
  raise(ArgumentError, "#{ cs__class } did not have a proper data. Unable to continue.") unless data
56
- raise(ArgumentError, "#{ cs__class } did not have a proper routes. Unable to continue.") if routes.empty?
57
57
  unless @app_name
58
58
  raise(ArgumentError, "#{ cs__class } did not have a proper application name. Unable to continue.")
59
59
  end
60
60
  unless @app_language
61
61
  raise(ArgumentError, "#{ cs__class } did not have a proper application language. Unable to continue.")
62
62
  end
63
- unless @app_version
64
- raise(ArgumentError, "#{ cs__class } did not have a proper application version. Unable to continue.")
65
- end
66
63
  unless @agent_session_id_value
67
64
  raise(ArgumentError, "#{ cs__class } did not have a proper session id. Unable to continue.")
68
65
  end
@@ -4,6 +4,7 @@
4
4
  require 'contrast/components/logger'
5
5
  require 'contrast/utils/assess/trigger_method_utils'
6
6
  require 'contrast/agent/reporting/reporting_utilities/endpoints'
7
+ require 'contrast/components/assess'
7
8
 
8
9
  module Contrast
9
10
  module Agent
@@ -19,7 +20,7 @@ module Contrast
19
20
  attr_reader :event_method
20
21
 
21
22
  def initialize
22
- @agent_session_id_value = '0' # TODO: RUBY-1514 Contrast::APP_CONTEXT.session_id once we have app start
23
+ @agent_session_id_value = ::Contrast::ASSESS.session_id
23
24
  @event_endpoint ||= nil
24
25
  @event_method ||= :POST
25
26
  end
@@ -15,13 +15,15 @@ module Contrast
15
15
  # externally accessible endpoints within the application, as registered to the application framework. This also
16
16
  # includes the literal URL and HTTP Verb used to invoke them, as they must have been called at this point to be
17
17
  # recorded.
18
- #
19
- # @attr_accessor route [String] the method signature used to uniquely identify the coverage report.
20
- # @attr_accessor url [String] the normalized URL used to access the method in the route.
21
- # @attr_accessor verb [String] the HTTP Verb used to access the method in the route.
22
- # @attr_accessor count [Integer] the number of times this was hit; no longer needed as it can be derived.
23
18
  class RouteCoverage
24
- attr_accessor :route, :url, :verb, :count
19
+ # @param [String] the method signature used to uniquely identify the coverage report.
20
+ attr_accessor :route
21
+ # @param [String] the normalized URL used to access the method in the route.
22
+ attr_accessor :url
23
+ # @param [String] the HTTP Verb used to access the method in the route.
24
+ attr_accessor :verb
25
+ # @param [Integer] the number of times this was hit; no longer needed as it can be derived.
26
+ attr_reader :count
25
27
 
26
28
  def initialize
27
29
  @route = Contrast::Utils::ObjectShare::EMPTY_STRING
@@ -1,18 +1,17 @@
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_events/reporting_event'
4
+ require 'contrast/agent/reporting/reporting_events/server_reporting_event'
5
5
 
6
6
  module Contrast
7
7
  module Agent
8
8
  module Reporting
9
- # This class will initialize empty ServerActivity body to be send to TS.
10
- # The server activity endpoint records actions taken on the serveror process as a whole.
11
- # It currently only reports the number of times a defensive action was taken and is most
12
- # likely not populated by most agents. The main purpose of sending this message is for its
13
- # response, which contains any updated server feature settings from TeamServer.
14
- # The new Server Settings endpoint should let us remove this.
15
- class ServerActivity < Contrast::Agent::Reporting::ReportingEvent
9
+ # This class will initialize empty ServerActivity body to be send to TS. The server activity endpoint records
10
+ # actions taken on the server or process as a whole. It currently only reports the number of times a defensive
11
+ # action was taken and is most likely not populated by most agents. The main purpose of sending this message is
12
+ # for its response, which contains any updated server feature settings from TeamServer. The new Server Settings
13
+ # endpoint should let us remove this.
14
+ class ServerActivity < Contrast::Agent::Reporting::ServerReportingEvent
16
15
  class << self
17
16
  # @param _server_activity_dtm [Contrast::Api::Dtm::ServerActivity]
18
17
  # @return [Contrast::Agent::Reporting::ServerActivity]
@@ -27,24 +26,17 @@ module Contrast
27
26
  super
28
27
  end
29
28
 
29
+ def file_name
30
+ 'server-activity'
31
+ end
32
+
30
33
  # Convert the instance variables on the class, and other information, into the identifiers required for
31
34
  # TeamServer to process the JSON form of this message.
32
35
  #
33
36
  # @return [Hash]
34
37
  # @raise [ArgumentError]
35
38
  def to_controlled_hash
36
- { lastUpdate: timestamp }
37
- end
38
-
39
- private
40
-
41
- # The timestamp field is a bit of a misnomer. It's really the time, in ms, since the settings for this
42
- # server have been updated.
43
- #
44
- # @return [Integer]
45
- def timestamp
46
- Contrast::Utils::Timer.now_ms -
47
- (Contrast::Agent.reporter&.client&.response_handler&.last_server_update_ms || 0)
39
+ { lastUpdate: since_last_update }
48
40
  end
49
41
  end
50
42
  end
@@ -0,0 +1,27 @@
1
+ # Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ require 'contrast/agent/reporting/reporting_events/reporting_event'
5
+ require 'contrast/utils/timer'
6
+
7
+ module Contrast
8
+ module Agent
9
+ module Reporting
10
+ # This is the new ServerReportingEvent class which will include all the needed and mutual information for those
11
+ # events which correspond to Servers, such as Servers Activity.
12
+ #
13
+ # @abstract
14
+ class ServerReportingEvent < Contrast::Agent::Reporting::ReportingEvent
15
+ protected
16
+
17
+ # The timestamp field is a bit of a misnomer. It's really the time, in ms, since the settings for this
18
+ # application have been updated. If I've never updated, then it's been 0ms since then.
19
+ #
20
+ # @return [Integer]
21
+ def since_last_update
22
+ (update_time = Contrast::SETTINGS.last_server_update_ms) ? Contrast::Utils::Timer.now_ms - update_time : 0
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -22,61 +22,51 @@ module Contrast
22
22
  # This method will be handling the auditing of the requests and responses we send to SpeedRacer. If the audit
23
23
  # feature is enabled, we'll log to file the request and/or response protobuf objects.
24
24
  #
25
- # @param event [Contrast::Api::Dtm|Contrast::Agent::Reporting::ReportingEvent] One of the DTMs valid for the
26
- # event field of Contrast::Api::Dtm::Message|Contrast::Agent::Reporting::ReportingEvent
27
- # @param response_data [Contrast::Api::Settings::AgentSettings,nil]
25
+ # @param event [Contrast::Agent::Reporting::ReportingEvent] One of the DTMs valid for the
26
+ # event field of Contrast::Agent::Reporting::ReportingEvent
27
+ # @param response_data [Net::HTTP::Response]
28
28
  def audit_event event, response_data = nil
29
29
  return unless ::Contrast::API.request_audit_requests? || ::Contrast::API.request_audit_responses?
30
30
 
31
- type = event.cs__respond_to?(:file_name) ? event.file_name : event.cs__class.cs__name.to_s.downcase
32
- if ::Contrast::API.request_audit_requests?
33
- data = if event.cs__class < Contrast::Agent::Reporting::ReportingEvent
34
- event.to_controlled_hash.to_json
35
- else # TODO: RUBY-1438 -- remove
36
- event.to_s
37
- end
38
- log_data :request, type, data if data
39
- end
31
+ file_name = event.cs__respond_to?(:file_name) ? event.file_name : event.cs__class.cs__name.to_s.downcase
32
+ data = event.to_controlled_hash.to_json
33
+ log_data(:request, file_name, data) if data
40
34
  return unless ::Contrast::API.request_audit_responses?
41
35
 
42
- data = response_data.to_s || event.http_response.try(:body) || 'There is no available response'
43
- log_data :response, type, data
36
+ data = response_data&.body || 'There is no available response'
37
+ log_data(:response, file_name, data)
44
38
  end
45
39
 
46
40
  private
47
41
 
48
42
  # This method will proceed with passing the data with to the writing method
49
43
  # @param type [Symbol] This is the type of the file /:request, :response/
50
- # @param data_type[String] DTM type String representation
44
+ # @param file_name[String] file_name to log
51
45
  # @param data[String] String representation if the logged data
52
- def log_data type, data_type, data = nil
46
+ def log_data type, file_name, data = nil
53
47
  return unless enabled?
54
48
  return unless Contrast::CONTRAST_SERVICE.use_agent_communication?
55
49
 
56
- write_to_file type, data_type, data
50
+ logger.debug('logging to file', file_name: file_name) # TODO: RUBY-99999 DO NOT COMMIT THIS
51
+ write_to_file(type, file_name, data)
57
52
  end
58
53
 
59
54
  # This method will be actually writing to the file
60
55
  # @param type [Symbol] This is the type of the file /:request, :response/
61
- # @param data_type [String] Data type /Activity/Finding../
56
+ # @param event_name [String] the type portion of the file to which to write
62
57
  # @param data [any] The data to be written to the file
63
- def write_to_file type, data_type, data = nil
58
+ def write_to_file type, event_name, data = nil
64
59
  time = Time.now.to_i
65
60
  destination = type == :request ? path_for_requests : path_for_responses
66
61
  # If the feature is disabled or we have yet to create the directory structure, then we could have a nil
67
62
  # destination. In that case, take no action
68
63
  return unless destination
69
64
 
70
- filename = "#{ time }-#{ Thread.current.object_id }-#{ data_type.gsub('::', '_') }-teamserver.json"
65
+ filename = "#{ time }_#{ Thread.current.object_id }_#{ event_name.gsub('::', '-') }_teamserver_#{ type }.json"
71
66
  filepath = File.join(destination, filename)
67
+ logger.debug('Writing to file', eventname: event_name, filename: filename, filepath: filepath)
72
68
  # Here is use append mode, because of a slightly possibility of overwriting an existing file
73
- File.open(filepath, 'a') do |f|
74
- if data_type.include?('reporting')
75
- f.write(Contrast::Utils::StringUtils.force_utf8(data))
76
- else
77
- f.write({ data_type: Contrast::Utils::StringUtils.force_utf8(data) }.to_json)
78
- end
79
- end
69
+ File.open(filepath, 'a') { |f| f.write(Contrast::Utils::StringUtils.force_utf8(data)) }
80
70
  rescue StandardError => e
81
71
  logger.warn('Saving audit failed', e: e)
82
72
  end
@@ -0,0 +1,38 @@
1
+ # Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ require 'contrast/agent/reporting/reporting_events/preflight_message'
5
+ require 'contrast/agent/reporting/reporting_events/preflight'
6
+ require 'contrast/agent/reporting/reporting_events/route_discovery'
7
+ require 'contrast/agent/reporting/reporting_utilities/reporting_storage'
8
+
9
+ module Contrast
10
+ module Agent
11
+ module Reporting
12
+ # This module will handle the building of new preflights prior to
13
+ # sending them.
14
+ module BuildPreflight
15
+ class << self
16
+ # @param finding [Contrast::Agent::Reporting::Finding]
17
+ # @param request [Contrast::Agent::Request] current request
18
+ def build finding, request
19
+ return unless finding
20
+
21
+ # save the current finding
22
+ Contrast::Agent::Reporting::ReportingStorage[finding.hash_code] = finding
23
+
24
+ new_preflight = Contrast::Agent::Reporting::Preflight.new
25
+ new_preflight_message = Contrast::Agent::Reporting::PreflightMessage.new
26
+ if request&.route
27
+ new_preflight_message.routes << Contrast::Agent::Reporting::RouteDiscovery.convert(request.route)
28
+ end
29
+ new_preflight_message.hash_code = finding.hash_code
30
+ new_preflight_message.data = "#{ finding.rule_id },#{ finding.hash_code }"
31
+ new_preflight.messages << new_preflight_message
32
+ new_preflight
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -2,6 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/agent/reporting/reporting_events/server_activity'
5
+ require 'contrast/agent/reporting/reporting_events/application_activity'
5
6
  require 'contrast/api/dtm.pb'
6
7
 
7
8
  module Contrast
@@ -43,6 +44,12 @@ module Contrast
43
44
  dtm.cs__is_a?(Contrast::Api::Dtm::Finding)
44
45
  end
45
46
 
47
+ # @param dtm [Contrast::Api::Dtm::Finding,Object]
48
+ # @return [Boolean]
49
+ def activity? dtm
50
+ dtm.cs__is_a?(Contrast::Api::Dtm::Activity)
51
+ end
52
+
46
53
  # Converts DTM message to Reporting Event for those messages that have conversion methods crated. We use this
47
54
  # as we work to move away from requiring the Service.
48
55
  #
@@ -57,6 +64,7 @@ module Contrast
57
64
  return Contrast::Agent::Reporting::ObservedLibraryUsage.convert(dtm) if library_usage?(dtm)
58
65
  return Contrast::Agent::Reporting::ApplicationUpdate.convert(dtm) if application_update?(dtm)
59
66
  return Contrast::Agent::Reporting::Finding.convert(dtm) if finding?(dtm)
67
+ return Contrast::Agent::Reporting::ApplicationActivity.convert(dtm) if activity?(dtm)
60
68
 
61
69
  nil
62
70
  end
@@ -43,6 +43,12 @@ module Contrast
43
43
  end
44
44
  end
45
45
 
46
+ def application_activity
47
+ with_rescue do
48
+ NG_ENDPOINTS[:application_activity].cs__freeze
49
+ end
50
+ end
51
+
46
52
  # @return [String,nil]
47
53
  def library_usage
48
54
  with_rescue do
@@ -30,7 +30,7 @@ module Contrast
30
30
  # @return [Net::HTTP, nil] Return open connection or nil
31
31
  def initialize_connection
32
32
  # for this client we would use proxy and custom certificate file if available
33
- super(SERVICE_NAME, Contrast::API.api_url, true, true)
33
+ super(SERVICE_NAME, Contrast::API.api_url, use_proxy: true, use_custom_cert: true)
34
34
  end
35
35
 
36
36
  def initialize
@@ -50,20 +50,24 @@ module Contrast
50
50
 
51
51
  # Check event type and send it to appropriate TS endpoint
52
52
  #
53
- # @param event [Contrast::Api::Dtm,Contrast::Agent::Reporting::ReportingEvent] One of the DTMs valid
54
- # for the event field of Contrast::Api::Dtm::Message|Contrast::Api::Dtm::Activity
53
+ # @param event [Contrast::Agent::Reporting::ReportingEvent] The event to send to TeamServer. Really a
54
+ # child of the ReportingEvent rather than a literal one.
55
55
  # @param connection [Net::HTTP] open connection
56
56
  # @param send_immediately [Boolean] flag for the logger
57
57
  # @return response [Net::HTTP::Response, nil] response from TS if no response
58
- def send_event event, connection, send_immediately = false
58
+ def send_event event, connection, send_immediately: false
59
59
  return unless Contrast::Agent::Reporter.enabled?
60
60
  return unless connection
61
61
 
62
- response = send_events(event, connection)
63
- log_send_event event if send_immediately
62
+ log_send_event(event) if send_immediately
63
+ request = build_request(event)
64
+ response = connection.request(request)
65
+ audit&.audit_event(event, response) if ::Contrast::API.request_audit_enable?
66
+ process_settings_response(response)
67
+ process_preflight_response(event, response, connection)
64
68
  response
65
69
  rescue StandardError => e
66
- handle_error event, e
70
+ handle_error(event, e)
67
71
  end
68
72
 
69
73
  def status
@@ -78,6 +82,18 @@ module Contrast
78
82
  response_handler.sleep?
79
83
  end
80
84
 
85
+ def timeout
86
+ response_handler.timeout
87
+ end
88
+
89
+ def mode
90
+ response_handler.mode
91
+ end
92
+
93
+ def reset_mode
94
+ response_handler.reset_mode
95
+ end
96
+
81
97
  def wake_up
82
98
  response_handler.wake_up
83
99
  end