contrast-agent 4.11.0 → 4.14.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +4 -4
  2. data/.simplecov +1 -0
  3. data/ext/cs__assess_module/cs__assess_module.c +48 -0
  4. data/ext/cs__assess_module/cs__assess_module.h +7 -0
  5. data/ext/cs__common/cs__common.c +24 -7
  6. data/ext/cs__common/cs__common.h +12 -2
  7. data/ext/cs__contrast_patch/cs__contrast_patch.c +48 -11
  8. data/ext/cs__contrast_patch/cs__contrast_patch.h +5 -2
  9. data/ext/cs__os_information/cs__os_information.c +31 -0
  10. data/ext/cs__os_information/cs__os_information.h +7 -0
  11. data/ext/{cs__protect_kernel → cs__os_information}/extconf.rb +0 -0
  12. data/lib/contrast/agent/assess/contrast_event.rb +1 -1
  13. data/lib/contrast/agent/assess/contrast_object.rb +1 -1
  14. data/lib/contrast/agent/assess/policy/dynamic_source_factory.rb +2 -0
  15. data/lib/contrast/agent/assess/policy/policy_node.rb +6 -6
  16. data/lib/contrast/agent/assess/policy/policy_scanner.rb +5 -0
  17. data/lib/contrast/agent/assess/policy/preshift.rb +19 -6
  18. data/lib/contrast/agent/assess/policy/propagation_method.rb +2 -116
  19. data/lib/contrast/agent/assess/policy/propagation_node.rb +4 -4
  20. data/lib/contrast/agent/assess/policy/propagator/center.rb +1 -1
  21. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +2 -0
  22. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +2 -154
  23. data/lib/contrast/agent/assess/policy/source_method.rb +2 -71
  24. data/lib/contrast/agent/assess/policy/trigger_method.rb +45 -110
  25. data/lib/contrast/agent/assess/policy/trigger_node.rb +62 -21
  26. data/lib/contrast/agent/assess/policy/trigger_validation/xss_validator.rb +1 -1
  27. data/lib/contrast/agent/assess/property/tagged.rb +66 -189
  28. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +40 -6
  29. data/lib/contrast/agent/deadzone/policy/policy.rb +6 -0
  30. data/lib/contrast/agent/inventory/dependency_usage_analysis.rb +1 -0
  31. data/lib/contrast/agent/metric_telemetry_event.rb +26 -0
  32. data/lib/contrast/agent/middleware.rb +14 -62
  33. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +0 -1
  34. data/lib/contrast/agent/patching/policy/method_policy.rb +3 -44
  35. data/lib/contrast/agent/patching/policy/method_policy_extend.rb +111 -0
  36. data/lib/contrast/agent/patching/policy/patch.rb +37 -238
  37. data/lib/contrast/agent/patching/policy/patcher.rb +15 -50
  38. data/lib/contrast/agent/reporting/report.rb +21 -0
  39. data/lib/contrast/agent/reporting/reporter.rb +142 -0
  40. data/lib/contrast/agent/reporting/reporting_events/finding.rb +90 -0
  41. data/lib/contrast/agent/reporting/reporting_events/preflight.rb +25 -0
  42. data/lib/contrast/agent/reporting/reporting_events/preflight_message.rb +56 -0
  43. data/lib/contrast/agent/reporting/reporting_events/reporting_event.rb +37 -0
  44. data/lib/contrast/agent/reporting/reporting_utilities/audit.rb +127 -0
  45. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +168 -0
  46. data/lib/contrast/agent/reporting/reporting_utilities/reporting_storage.rb +66 -0
  47. data/lib/contrast/agent/request.rb +2 -81
  48. data/lib/contrast/agent/request_context.rb +18 -126
  49. data/lib/contrast/agent/request_context_extend.rb +138 -0
  50. data/lib/contrast/agent/request_handler.rb +7 -3
  51. data/lib/contrast/agent/response.rb +2 -73
  52. data/lib/contrast/agent/rule_set.rb +2 -4
  53. data/lib/contrast/agent/startup_metrics_telemetry_event.rb +94 -0
  54. data/lib/contrast/agent/static_analysis.rb +5 -3
  55. data/lib/contrast/agent/telemetry.rb +137 -0
  56. data/lib/contrast/agent/telemetry_event.rb +33 -0
  57. data/lib/contrast/agent/thread_watcher.rb +66 -11
  58. data/lib/contrast/agent/version.rb +1 -1
  59. data/lib/contrast/agent.rb +21 -1
  60. data/lib/contrast/api/communication/connection_status.rb +10 -7
  61. data/lib/contrast/api/communication/messaging_queue.rb +37 -3
  62. data/lib/contrast/api/communication/response_processor.rb +15 -8
  63. data/lib/contrast/api/communication/service_lifecycle.rb +13 -3
  64. data/lib/contrast/api/communication/socket.rb +6 -8
  65. data/lib/contrast/api/communication/socket_client.rb +29 -12
  66. data/lib/contrast/api/communication/speedracer.rb +37 -1
  67. data/lib/contrast/api/communication/tcp_socket.rb +4 -3
  68. data/lib/contrast/api/communication/unix_socket.rb +1 -0
  69. data/lib/contrast/api/decorators/finding.rb +45 -0
  70. data/lib/contrast/components/api.rb +90 -0
  71. data/lib/contrast/components/app_context.rb +10 -41
  72. data/lib/contrast/components/app_context_extend.rb +78 -0
  73. data/lib/contrast/components/assess.rb +7 -0
  74. data/lib/contrast/components/base.rb +23 -0
  75. data/lib/contrast/components/config.rb +92 -13
  76. data/lib/contrast/components/contrast_service.rb +11 -0
  77. data/lib/contrast/components/sampling.rb +2 -2
  78. data/lib/contrast/config/agent_configuration.rb +1 -1
  79. data/lib/contrast/config/api_configuration.rb +27 -0
  80. data/lib/contrast/config/api_proxy_configuration.rb +14 -0
  81. data/lib/contrast/config/application_configuration.rb +2 -3
  82. data/lib/contrast/config/assess_configuration.rb +3 -2
  83. data/lib/contrast/config/base_configuration.rb +17 -28
  84. data/lib/contrast/config/certification_configuration.rb +15 -0
  85. data/lib/contrast/config/env_variables.rb +18 -0
  86. data/lib/contrast/config/heap_dump_configuration.rb +6 -6
  87. data/lib/contrast/config/inventory_configuration.rb +1 -5
  88. data/lib/contrast/config/protect_rule_configuration.rb +1 -1
  89. data/lib/contrast/config/request_audit_configuration.rb +18 -0
  90. data/lib/contrast/config/root_configuration.rb +1 -0
  91. data/lib/contrast/config/ruby_configuration.rb +6 -6
  92. data/lib/contrast/config/service_configuration.rb +2 -2
  93. data/lib/contrast/config.rb +1 -1
  94. data/lib/contrast/configuration.rb +4 -2
  95. data/lib/contrast/extension/assess/array.rb +5 -7
  96. data/lib/contrast/framework/manager.rb +22 -44
  97. data/lib/contrast/framework/manager_extend.rb +50 -0
  98. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +9 -6
  99. data/lib/contrast/framework/rails/patch/support.rb +31 -29
  100. data/lib/contrast/framework/rails/railtie.rb +1 -1
  101. data/lib/contrast/framework/sinatra/support.rb +2 -1
  102. data/lib/contrast/logger/application.rb +4 -0
  103. data/lib/contrast/logger/log.rb +8 -103
  104. data/lib/contrast/utils/assess/propagation_method_utils.rb +129 -0
  105. data/lib/contrast/utils/assess/property/tagged_utils.rb +165 -0
  106. data/lib/contrast/utils/assess/source_method_utils.rb +83 -0
  107. data/lib/contrast/utils/assess/tracking_util.rb +20 -15
  108. data/lib/contrast/utils/assess/trigger_method_utils.rb +138 -0
  109. data/lib/contrast/utils/class_util.rb +65 -54
  110. data/lib/contrast/utils/exclude_key.rb +20 -0
  111. data/lib/contrast/utils/findings.rb +62 -0
  112. data/lib/contrast/utils/hash_digest.rb +10 -73
  113. data/lib/contrast/utils/hash_digest_extend.rb +86 -0
  114. data/lib/contrast/utils/head_dump_utils_extend.rb +74 -0
  115. data/lib/contrast/utils/heap_dump_util.rb +2 -65
  116. data/lib/contrast/utils/invalid_configuration_util.rb +29 -0
  117. data/lib/contrast/utils/io_util.rb +1 -1
  118. data/lib/contrast/utils/log_utils.rb +108 -0
  119. data/lib/contrast/utils/lru_cache.rb +4 -2
  120. data/lib/contrast/utils/metrics_hash.rb +59 -0
  121. data/lib/contrast/utils/middleware_utils.rb +87 -0
  122. data/lib/contrast/utils/net_http_base.rb +158 -0
  123. data/lib/contrast/utils/object_share.rb +1 -0
  124. data/lib/contrast/utils/os.rb +23 -0
  125. data/lib/contrast/utils/patching/policy/patch_utils.rb +232 -0
  126. data/lib/contrast/utils/patching/policy/patcher_utils.rb +54 -0
  127. data/lib/contrast/utils/request_utils.rb +88 -0
  128. data/lib/contrast/utils/response_utils.rb +97 -0
  129. data/lib/contrast/utils/substitution_utils.rb +167 -0
  130. data/lib/contrast/utils/tag_util.rb +9 -9
  131. data/lib/contrast/utils/telemetry.rb +79 -0
  132. data/lib/contrast/utils/telemetry_client.rb +90 -0
  133. data/lib/contrast/utils/telemetry_identifier.rb +130 -0
  134. data/lib/contrast.rb +19 -1
  135. data/resources/assess/policy.json +12 -6
  136. data/resources/deadzone/policy.json +86 -5
  137. data/ruby-agent.gemspec +7 -6
  138. data/service_executables/VERSION +1 -1
  139. data/service_executables/linux/contrast-service +0 -0
  140. data/service_executables/mac/contrast-service +0 -0
  141. metadata +68 -26
  142. data/ext/cs__protect_kernel/cs__protect_kernel.c +0 -47
  143. data/ext/cs__protect_kernel/cs__protect_kernel.h +0 -12
  144. data/lib/contrast/config/default_value.rb +0 -17
  145. data/lib/contrast/extension/protect/kernel.rb +0 -29
@@ -0,0 +1,94 @@
1
+ # Copyright (c) 2021 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
+ require 'contrast/agent/metric_telemetry_event'
6
+ require 'contrast/agent/version'
7
+ require 'contrast/utils/os'
8
+
9
+ module Contrast
10
+ module Agent
11
+ # This class will hold the Startup Metrics Telemetry Event
12
+ # The class will include initialization of the agent version, language version
13
+ # os type, arch and version
14
+ # application framework and version and server framework
15
+ # It will be initialized and send in Middleware#agent_startup_routine
16
+ class StartupMetricsTelemetryEvent < Contrast::Agent::MetricTelemetryEvent
17
+ include Contrast::Utils::OS
18
+
19
+ APP_AND_SERVER_DATA = ::Contrast::APP_CONTEXT.app_and_server_information.cs__freeze
20
+ # Multi-tenant Production Environments
21
+ SAAS_DEFAULT = { addr: 'app.contrastsecurity.com', type: 'SAAS_DEFAULT' }.cs__freeze
22
+ SAAS_CS = { addr: /cs[[:digit:]]+\.contrastsecurity\.com/, type: 'SAAS_DEFAULT' }.cs__freeze
23
+ SAAS_JP = { addr: 'app.contrastsecurity.jp', type: 'SAAS_DEFAULT' }.cs__freeze
24
+ SAAS_CE = { addr: 'ce.contrastsecurity.com', type: 'SAAS_CE' }.cs__freeze
25
+ # Multi-tenant Demo Environments
26
+ SAAS_DEMO = { addr: 'apptwo.contrastsecurity.com', type: 'SAAS_DEMO' }.cs__freeze
27
+ SAAS_POV = { addr: 'eval.contrastsecurity.com', type: 'SAAS_POV' }.cs__freeze
28
+ # Multi-tenant Testing Environment
29
+ SAAS_RESEARCH = { addr: 'security-research.contrastsecurity.com', type: 'SAAS_RESEARCH' }.cs__freeze
30
+ SAAS_ALPHA = { addr: 'alpha.contrastsecurity.com', type: 'SAAS_ALPHA' }.cs__freeze
31
+ SAAS_STAGING = { addr: 'teamserver-staging.contsec.com', type: 'SAAS_TESTING' }.cs__freeze
32
+ SAAS_STAGING_TOKYO = { addr: 'teamserver-staging.contsec.jp', type: 'SAAS_TESTING' }.cs__freeze
33
+ SAAS_TESTING = { addr: 'teamserver-darpa.contsec.com', type: 'SAAS_TESTING' }.cs__freeze
34
+ SAAS_OPS_TESTING = { addr: 'teamserver-ops.contsec.com', type: 'SAAS_TESTING' }.cs__freeze
35
+ # Fallback for Single-tenant Production Environments
36
+ SAAS_CUSTOM = { addr: 'contrastsecurity.com', type: 'SAAS_CUSTOM' }.cs__freeze
37
+ SAAS_CUSTOM_JP = { addr: 'contrastsecurity.jp', type: 'SAAS_CUSTOM' }.cs__freeze
38
+
39
+ SINGLE_MAP_TENANTS = [
40
+ SAAS_DEFAULT, SAAS_JP, SAAS_CE, SAAS_DEMO, SAAS_POV, SAAS_RESEARCH, SAAS_ALPHA,
41
+ SAAS_STAGING, SAAS_STAGING_TOKYO, SAAS_TESTING, SAAS_OPS_TESTING
42
+ ].cs__freeze
43
+ REGEXP_MAP_TENANTS = [SAAS_CS].cs__freeze
44
+ FALLBACK_TENANTS = [SAAS_CUSTOM, SAAS_CUSTOM_JP].cs__freeze
45
+ # Fallback for Custom, most likely self-hosted, Environments
46
+ EOP = 'EOP'
47
+
48
+ def initialize
49
+ super
50
+ add_tags
51
+ end
52
+
53
+ def path
54
+ '/startup'
55
+ end
56
+
57
+ def add_tags
58
+ @tags['teamserver'] = teamserver_type
59
+ @tags['agent_version'] = VERSION
60
+ @tags['ruby_version'] = RUBY_VERSION
61
+ @tags['os_type'] = sys_info['os_type'] == 'Darwin' ? 'MacOS' : 'Linux'
62
+ @tags['os_arch'] = sys_info['os_arch']
63
+ @tags['os_version'] = sys_info['os_version']
64
+ @tags['app_framework_and_version'] = APP_AND_SERVER_DATA[:application_info].to_s
65
+ @tags['server_framework_and_version'] = APP_AND_SERVER_DATA[:server_info].to_s
66
+ end
67
+
68
+ def sys_info
69
+ @sys_info ||= get_system_information if @sys_info.nil?
70
+ @sys_info
71
+ end
72
+
73
+ private
74
+
75
+ # Here we extract the TeamServer url type
76
+ #
77
+ # @return [String] the type of TeamServer environment to which we're connecting
78
+ def teamserver_type
79
+ @_teamserver_type ||= begin
80
+ url = Contrast::API.api_url
81
+ if (single = SINGLE_MAP_TENANTS.find { |tenant| url.include?(tenant[:addr]) })
82
+ single[:type]
83
+ elsif (regexp = REGEXP_MAP_TENANTS.find { |tenant| tenant[:addr].match?(url) })
84
+ regexp[:type]
85
+ elsif (fallback = FALLBACK_TENANTS.find { |tenant| url.include?(tenant[:addr]) })
86
+ fallback[:type]
87
+ else
88
+ EOP
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
@@ -10,12 +10,12 @@ module Contrast
10
10
  # this module handles one time static analysis tasks
11
11
  class StaticAnalysis
12
12
  include Singleton
13
- include Contrast::Components::Logger::InstanceMethods
14
13
  include Contrast::Components::Scope::InstanceMethods
14
+ extend Contrast::Components::Logger::InstanceMethods
15
15
 
16
16
  class << self
17
- # After the first request is complete, we do a one-time manual catchup to review and
18
- # report the already-loaded gems.
17
+ # After the first request is complete, we do a one-time manual catchup to review and report the already-loaded
18
+ # gems.
19
19
  def catchup
20
20
  @_catchup ||= begin
21
21
  threaded_analysis!
@@ -23,6 +23,8 @@ module Contrast
23
23
  end
24
24
  end
25
25
 
26
+ # TODO: RUBY-1354
27
+ # TODO: RUBY-1356
26
28
  def send_inventory_message
27
29
  return unless ::Contrast::INVENTORY.enabled?
28
30
 
@@ -0,0 +1,137 @@
1
+ # Copyright (c) 2021 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
+
10
+ module Contrast
11
+ module Agent
12
+ # This class will initialize and hold everything needed for the telemetry
13
+ class Telemetry < WorkerThread
14
+ include Contrast::Components::Logger::InstanceMethods
15
+ # this is where we will send the data from the agents
16
+ URL = 'https://telemetry.ruby.contrastsecurity.com/'
17
+ # Suggested timeout after each send is to be 3 hours (10800 seconds)
18
+ SUGGESTED_TIMEOUT = 10_800
19
+
20
+ class << self
21
+ include Contrast::Components::Logger::InstanceMethods
22
+ include Contrast::Config::EnvVariables
23
+
24
+ def application_id
25
+ @_application_id ||= begin
26
+ id = nil
27
+ mac = Contrast::Utils::Telemetry::Identifier.mac
28
+ app_name = Contrast::Utils::Telemetry::Identifier.app_name
29
+ id = mac + app_name if mac && app_name
30
+ Digest::SHA2.new(256).hexdigest(id || ('_' + SecureRandom.uuid))
31
+ end
32
+ end
33
+
34
+ def instance_id
35
+ @_instance_id ||= Digest::SHA2.new(256).hexdigest(Contrast::Utils::Telemetry::Identifier.mac ||
36
+ ('_' + SecureRandom.uuid))
37
+ end
38
+
39
+ def enabled?
40
+ @_enabled = telemetry_enabled? if @_enabled.nil?
41
+ @_enabled
42
+ end
43
+
44
+ private
45
+
46
+ def telemetry_enabled?
47
+ opt_out_telemetry = return_value(:telemetry_opt_outs)
48
+ return false if opt_out_telemetry.to_s.casecmp('true').zero?
49
+ return false if opt_out_telemetry.to_s.casecmp('1').zero?
50
+
51
+ # In case of connection error, do not create the background thread or queue,
52
+ # as if the opt-out env var was set
53
+ @_client = Contrast::Utils::TelemetryClient.new
54
+ ip_opt_out_telemetry = @_client.initialize_connection(URL)
55
+ if ip_opt_out_telemetry.nil?
56
+ logger.warn('Connection was not established properly!!!')
57
+ logger.warn('THE SERVICE IS GOING TO BE TERMINATED!!')
58
+ return false
59
+ end
60
+
61
+ true
62
+ end
63
+ end
64
+
65
+ def client
66
+ @_client ||= Contrast::Utils::TelemetryClient.new
67
+ end
68
+
69
+ def connection
70
+ @_connection ||= client.initialize_connection(URL)
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
+ event = queue.pop
91
+
92
+ begin
93
+ logger.debug('This is the current processed event', event)
94
+ res = client.send_request event, connection
95
+ sleep_time = client.handle_response res
96
+ sleep(sleep_time) unless sleep_time.nil?
97
+ rescue StandardError => e
98
+ logger.error('Could not send message to service from telemetry queue.', e)
99
+ end
100
+ sleep(SUGGESTED_TIMEOUT)
101
+ end
102
+ end
103
+
104
+ def send_event event
105
+ if ::Contrast::AGENT.disabled?
106
+ logger.warn('Attempted to queue event with Agent disabled', caller: caller, event: event)
107
+ return
108
+ end
109
+
110
+ return unless cs__class.enabled?
111
+
112
+ logger.debug('Enqueued event for sending', event_type: event.cs__class)
113
+
114
+ queue << event if event
115
+ end
116
+
117
+ def delete_queue!
118
+ @_queue&.clear
119
+ @_queue&.close
120
+ @_queue = nil
121
+ end
122
+
123
+ def stop!
124
+ return unless running?
125
+
126
+ super
127
+ delete_queue!
128
+ end
129
+
130
+ private
131
+
132
+ def queue
133
+ @_queue ||= Queue.new
134
+ end
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,33 @@
1
+ # Copyright (c) 2021 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
+ # This class will hold the basic information for a Telemetry Event
9
+ class TelemetryEvent
10
+ include Contrast::Utils
11
+
12
+ attr_reader :timestamp, :tags
13
+
14
+ def initialize
15
+ @tags = MetricsHash.new(String)
16
+ @timestamp = Time.now.iso8601
17
+ end
18
+
19
+ def path
20
+ ''
21
+ end
22
+
23
+ def to_hash **_args
24
+ {
25
+ tags: @tags,
26
+ timestamp: @timestamp,
27
+ instance: Contrast::Agent::Telemetry.instance_id,
28
+ application: Contrast::Agent::Telemetry.application_id
29
+ }
30
+ end
31
+ end
32
+ end
33
+ end
@@ -4,6 +4,8 @@
4
4
  require 'contrast/components/logger'
5
5
  require 'contrast/agent/service_heartbeat'
6
6
  require 'contrast/api/communication/messaging_queue'
7
+ require 'contrast/agent/reporting/report'
8
+ require 'contrast/agent/telemetry'
7
9
 
8
10
  module Contrast
9
11
  module Agent
@@ -22,41 +24,94 @@ module Contrast
22
24
  @heapdump_util = Contrast::Utils::HeapDumpUtil.new
23
25
  @heartbeat = Contrast::Agent::ServiceHeartbeat.new
24
26
  @messaging_queue = Contrast::Api::Communication::MessagingQueue.new
27
+ @telemetry = Contrast::Agent::Telemetry.new if Contrast::Agent::Telemetry.enabled?
28
+ @reporter = Contrast::Agent::Reporter.new if Contrast::Agent::Reporter.enabled?
25
29
  end
26
30
 
27
31
  def startup!
28
32
  return unless ::Contrast::AGENT.enabled?
29
33
 
34
+ telemetry_status = telemetry_thread_init
35
+ heartbeat_status = heartbeat_thread_init
36
+ messaging_status = messaging_thread_init
37
+ reporter_status = reporter_thread_init
38
+
39
+ logger.debug('ThreadWatcher started threads')
40
+
41
+ @pids[Process.pid] = messaging_status && heartbeat_status
42
+ return @pids unless Contrast::Agent::Telemetry.enabled?
43
+
44
+ @pids[Process.pid] = @pids[Process.pid] && telemetry_status
45
+ return @pids unless Contrast::Agent::Reporter.enabled?
46
+
47
+ @pids[Process.pid] = @pids[Process.pid] && reporter_status
48
+ end
49
+
50
+ def ensure_running?
51
+ return if @pids[Process.pid] == true
52
+
53
+ logger.debug('ThreadWatcher - threads not running')
54
+ startup!
55
+ end
56
+
57
+ def shutdown!
58
+ heartbeat.stop!
59
+ messaging_queue.stop!
60
+ heapdump_util.stop!
61
+ telemetry_queue&.stop!
62
+ reporter_queue&.stop!
63
+ end
64
+
65
+ def heartbeat_thread_init
30
66
  unless heartbeat.running?
31
67
  logger.debug('Attempting to start heartbeat thread')
32
68
  heartbeat.start_thread!
33
69
  end
34
70
  heartbeat_result = heartbeat.running?
35
71
  logger.debug('Heartbeat thread status', alive: heartbeat_result)
72
+ heartbeat_result
73
+ end
74
+
75
+ def telemetry_thread_init
76
+ @telemetry.start_thread! if @telemetry&.attempt_to_start?
77
+ telemetry_result = @telemetry&.running?
78
+ logger.debug('Telemetry thread status', alive: telemetry_result)
79
+ telemetry_result
80
+ end
36
81
 
82
+ def messaging_thread_init
37
83
  unless messaging_queue.running?
38
84
  logger.debug('Attempting to start messaging queue thread')
39
85
  messaging_queue.start_thread!
40
86
  end
41
87
  messaging_result = messaging_queue.running?
42
88
  logger.debug('Messaging thread status', alive: messaging_result)
89
+ messaging_result
90
+ end
43
91
 
44
- logger.debug('ThreadWatcher started threads')
45
-
46
- @pids[Process.pid] = messaging_result && heartbeat_result
92
+ def reporter_thread_init
93
+ @reporter.start_thread! if @reporter&.attempt_to_start?
94
+ unless @reporter&.running?
95
+ logger.debug('Attempting to start reporter thread')
96
+ @reporter&.start_thread!
97
+ end
98
+ reporter_result = @reporter&.running?
99
+ logger.debug('Reporter thread status', alive: reporter_result)
100
+ reporter_result
47
101
  end
48
102
 
49
- def ensure_running?
50
- return if @pids[Process.pid] == true
103
+ # @return [Contrast::Agent::Telemetry]
104
+ def telemetry_queue
105
+ return if @telemetry.nil?
51
106
 
52
- logger.debug('ThreadWatcher - threads not running')
53
- startup!
107
+ @telemetry
54
108
  end
55
109
 
56
- def shutdown!
57
- heartbeat.stop!
58
- messaging_queue.stop!
59
- heapdump_util.stop!
110
+ # @return [Contrast::Agent::Reporter]
111
+ def reporter_queue
112
+ return if @reporter.nil?
113
+
114
+ @reporter
60
115
  end
61
116
  end
62
117
  end
@@ -3,6 +3,6 @@
3
3
 
4
4
  module Contrast
5
5
  module Agent
6
- VERSION = '4.11.0'
6
+ VERSION = '4.14.0'
7
7
  end
8
8
  end
@@ -20,7 +20,6 @@ require 'contrast/extension/delegator'
20
20
  require 'contrast/extension/inventory'
21
21
  require 'contrast/extension/module'
22
22
  require 'contrast/extension/protect'
23
- require 'contrast/extension/protect/kernel'
24
23
 
25
24
  require 'contrast/utils/object_share'
26
25
  require 'contrast/utils/string_utils'
@@ -30,6 +29,9 @@ require 'contrast/utils/os'
30
29
  require 'contrast/utils/hash_digest'
31
30
  require 'contrast/utils/invalid_configuration_util'
32
31
 
32
+ # Collect findings
33
+ require 'contrast/utils/findings'
34
+
33
35
  # scoping
34
36
  require 'contrast/agent/scope'
35
37
 
@@ -49,19 +51,37 @@ module Contrast
49
51
  module Agent
50
52
  # build a map for tracking the context of the current request
51
53
  REQUEST_TRACKER = Contrast::Utils::ThreadTracker.new
54
+ FINDINGS = Contrast::Utils::Findings.new
52
55
 
56
+ # @return [Contrast::Framework::Manager]
53
57
  def self.framework_manager
54
58
  @_framework_manager ||= Contrast::Framework::Manager.new
55
59
  end
56
60
 
61
+ # @return [nil, Contrast::Utils::HeapDumpUtil]
57
62
  def self.heapdump_util
58
63
  thread_watcher.heapdump_util
59
64
  end
60
65
 
66
+ # @return [nil, Contrast::Api::Communication::MessagingQueue]
61
67
  def self.messaging_queue
62
68
  thread_watcher.messaging_queue
63
69
  end
64
70
 
71
+ # @return [nil, Contrast::Agent::Telemetry]
72
+ def self.telemetry_queue
73
+ return unless thread_watcher.telemetry_queue
74
+
75
+ thread_watcher.telemetry_queue
76
+ end
77
+
78
+ # @return [nil, Contrast::Agent::Reporter]
79
+ def self.reporter_queue
80
+ return unless thread_watcher.reporter_queue
81
+
82
+ thread_watcher.reporter_queue
83
+ end
84
+
65
85
  def self.thread_watcher
66
86
  @_thread_watcher ||= Contrast::Agent::ThreadWatcher.new
67
87
  end
@@ -4,7 +4,7 @@
4
4
  module Contrast
5
5
  module Api
6
6
  module Communication
7
- # Keeps track of the state of connections to the service.
7
+ # Keeps track of the state of connections to SpeedRacer.
8
8
  class ConnectionStatus
9
9
  def initialize
10
10
  @last_success = nil
@@ -12,25 +12,28 @@ module Contrast
12
12
  @startup_messages_sent = false
13
13
  end
14
14
 
15
- # Whether we have sent startup message to the service. True after successfully
16
- # sending startup messages to service and reset to false if we lose connection
17
- # to the service.
15
+ # Whether we have sent startup message to SpeedRacer. True after successfully sending startup messages to
16
+ # SpeedRacer and reset to false if we lose connection.
17
+ #
18
+ # @return [Boolean]
18
19
  def startup_messages_sent?
19
20
  @startup_messages_sent
20
21
  end
21
22
 
22
- # The last message sent was successful
23
+ # The last message sent was successful or not
24
+ #
25
+ # @return [Boolean]
23
26
  def connected?
24
27
  @last_success && (@last_failure.nil? || @last_success > @last_failure)
25
28
  end
26
29
 
27
- # The current state of the service is active with a successful message sent
30
+ # The current state of the SpeedRacer is active with a successful message sent
28
31
  def success!
29
32
  @startup_messages_sent = true
30
33
  @last_success = Time.now.to_f
31
34
  end
32
35
 
33
- # The service may be in some sort of error state
36
+ # The SpeedRacer may be in some sort of error state
34
37
  def failure!
35
38
  @startup_messages_sent = false
36
39
  @last_failure = Time.now.to_f
@@ -3,6 +3,7 @@
3
3
 
4
4
  require 'contrast/components/logger'
5
5
  require 'contrast/agent/worker_thread'
6
+ require 'contrast/agent/reporting/reporting_utilities/audit'
6
7
 
7
8
  module Contrast
8
9
  module Api
@@ -15,23 +16,46 @@ module Contrast
15
16
 
16
17
  def initialize
17
18
  @speedracer = Contrast::Api::Communication::Speedracer.new
19
+ @audit = Contrast::Agent::Reporting::Audit.new if ::Contrast::API.request_audit_enable
18
20
  super
19
21
  end
20
22
 
21
- # Use this to bypass the messaging queue and leave response processing to the caller
23
+ # Use this to bypass the messaging queue and leave response processing to the caller. We use this method for
24
+ # those operations which are blocking, meaning they must complete for the Agent to continue operations. These
25
+ # types of events include initial settings/ setup on startup and analysis required to complete before handing
26
+ # execution from our pre-filter operations to the application code (i.e. Protect's input analysis).
27
+ #
28
+ # @param event [Contrast::Api::Dtm] One of the DTMs valid for the event field of
29
+ # Contrast::Api::Dtm::Message|Contrast::Api::Dtm::Activity
30
+ # @return [Contrast::Api::Settings::AgentSettings,nil]
22
31
  def send_event_immediately event
23
32
  if ::Contrast::AGENT.disabled?
24
33
  logger.warn('Attempted to send event immediately with Agent disabled', caller: caller, event: event)
25
34
  return
26
35
  end
27
- speedracer.return_response(event)
36
+ response_data = speedracer.return_response(event)
37
+ return response_data unless ::Contrast::API.request_audit_enable
38
+
39
+ @audit&.audit_event(event, response_data)
40
+ response_data
28
41
  end
29
42
 
43
+ # A simple Queue used to hold messages that are ready to be sent to SpeedRacer but for which we do not need an
44
+ # immediate response
45
+ #
46
+ # @return [Queue]
30
47
  def queue
31
48
  @_queue ||= Queue.new
32
49
  end
33
50
 
34
- # Use this to add a message to the queue and process the response internally
51
+ # Use this to add a message to the queue and process the response internally. We use this method for those
52
+ # operations which are non-blocking, meaning they don't need to complete for the Agent to continue operations.
53
+ # These types of events include any post-request messaging that occurs after execution is returned from the
54
+ # application to our post-filter operations as well as those that don't alter the application's execution (i.e.
55
+ # Assess' vulnerability reporting).
56
+ #
57
+ # @param event [Contrast::Api::Dtm] One of the DTMs valid for the event field of
58
+ # Contrast::Api::Dtm::Message|Contrast::Api::Dtm::Activity
35
59
  def send_event_eventually event
36
60
  if ::Contrast::AGENT.disabled?
37
61
  logger.warn('Attempted to queue event with Agent disabled', caller: caller, event: event)
@@ -39,8 +63,14 @@ module Contrast
39
63
  end
40
64
  logger.debug('Enqueued event for sending', event_type: event.cs__class)
41
65
  queue << event if event
66
+ return unless ::Contrast::API.request_audit_enable
67
+
68
+ @audit&.audit_event(event)
42
69
  end
43
70
 
71
+ # Create the reporting thread, which will pull from the queue in order to send messages to SpeedRacer. If
72
+ # SpeedRacer is not running and should be, meaning the Agent is configured to control it, then we will also
73
+ # try to start that process.
44
74
  def start_thread!
45
75
  speedracer.ensure_startup!
46
76
  return if running?
@@ -60,12 +90,16 @@ module Contrast
60
90
  logger.debug('Started background sending thread.')
61
91
  end
62
92
 
93
+ # When the Agent shuts down, we terminate the message sending operations. This method clears, closes, and
94
+ # destroys the queue.
63
95
  def delete_queue!
64
96
  @_queue&.clear
65
97
  @_queue&.close
66
98
  @_queue = nil
67
99
  end
68
100
 
101
+ # When the Agent shuts down, we terminate the thread responsible for reporting. This method destroys that
102
+ # thread and any data we would have sent with it.
69
103
  def stop!
70
104
  return unless running?
71
105
 
@@ -7,10 +7,13 @@ require 'contrast/components/logger'
7
7
  module Contrast
8
8
  module Api
9
9
  module Communication
10
- # Handles processing deferred messages
10
+ # Handles processing deferred messages sent to SpeedRacer.
11
11
  class ResponseProcessor
12
12
  include Contrast::Components::Logger::InstanceMethods
13
13
 
14
+ # Use the given response to update the Agent's server features and application settings, allowing it to reflect
15
+ # the latest options configured by the user in TeamServer
16
+ #
14
17
  # @param response [Contrast::Api::Settings::AgentSettings]
15
18
  def process response
16
19
  logger.debug('Received a response', sent_ms: response&.sent_ms)
@@ -18,8 +21,8 @@ module Contrast
18
21
  server_features = process_server_response(response)
19
22
  app_settings = process_application_response(response)
20
23
 
21
- # ReactionProcessor is a design pattern from TeamServer.
22
- # Right now, there's one potential reaction, which is disabling the agent
24
+ # ReactionProcessor is a design pattern from TeamServer. Right now, there's one potential reaction, which is
25
+ # disabling the agent
23
26
  Contrast::Agent::ReactionProcessor.process(response&.application_settings)
24
27
 
25
28
  Contrast::Logger::Log.instance.update(server_features&.log_file, server_features&.log_level)
@@ -30,10 +33,10 @@ module Contrast
30
33
 
31
34
  private
32
35
 
33
- # Given some protobuf messages, update server features.
34
- # This is the bridge between Contrast Service <-> Settings.
36
+ # Given some protobuf messages, update server features. This is the bridge between SpeedRacer <-> Settings.
35
37
  #
36
38
  # @param response [Contrast::Api::Settings::AgentSettings]
39
+ # @return [Contrast::Api::Settings::ServerFeatures]
37
40
  def process_server_response response
38
41
  server_features = response&.server_features
39
42
  return unless server_features
@@ -45,10 +48,11 @@ module Contrast
45
48
  server_features
46
49
  end
47
50
 
48
- # Given some protobuf messages, update application settings.
49
- # This is the bridge between Contrast Service <-> Settings.
51
+ # Given some protobuf messages, update application settings. This is the bridge between SpeedRacer <->
52
+ # Settings.
50
53
  #
51
54
  # @param response [Contrast::Api::Settings::AgentSettings]
55
+ # @return [Contrast::Api::Settings::ApplicationSettings]
52
56
  def process_application_response response
53
57
  app_settings = response&.application_settings
54
58
  return unless app_settings
@@ -61,7 +65,10 @@ module Contrast
61
65
  end
62
66
 
63
67
  # This can't go in the Settings component because protect and assess depend on settings
64
- # I don't think it should go into contrast_service because that only handles connection specific data
68
+ # I don't think it should go into contrast_service because that only handles connection specific data.
69
+ #
70
+ # @param server_features [Contrast::Api::Settings::AgentSettings]
71
+ # @param app_settings [Contrast::Api::Settings::ApplicationSettings]
65
72
  def update_features server_features, app_settings
66
73
  return unless !!(server_features || app_settings)
67
74
  return unless ::Contrast::AGENT.enabled?