contrast-agent 4.12.0 → 4.14.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (131) 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 +5 -0
  6. data/ext/cs__common/cs__common.h +8 -0
  7. data/ext/cs__contrast_patch/cs__contrast_patch.c +16 -1
  8. data/ext/cs__os_information/cs__os_information.c +31 -0
  9. data/ext/cs__os_information/cs__os_information.h +7 -0
  10. data/ext/cs__os_information/extconf.rb +5 -0
  11. data/lib/contrast/agent/assess/policy/policy_node.rb +6 -6
  12. data/lib/contrast/agent/assess/policy/policy_scanner.rb +5 -0
  13. data/lib/contrast/agent/assess/policy/propagation_method.rb +2 -116
  14. data/lib/contrast/agent/assess/policy/propagation_node.rb +4 -4
  15. data/lib/contrast/agent/assess/policy/propagator/center.rb +1 -1
  16. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +2 -154
  17. data/lib/contrast/agent/assess/policy/source_method.rb +2 -71
  18. data/lib/contrast/agent/assess/policy/trigger_method.rb +45 -110
  19. data/lib/contrast/agent/assess/policy/trigger_node.rb +14 -6
  20. data/lib/contrast/agent/assess/policy/trigger_validation/xss_validator.rb +1 -1
  21. data/lib/contrast/agent/assess/property/tagged.rb +53 -185
  22. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +40 -6
  23. data/lib/contrast/agent/deadzone/policy/policy.rb +1 -1
  24. data/lib/contrast/agent/inventory/dependency_usage_analysis.rb +1 -0
  25. data/lib/contrast/agent/metric_telemetry_event.rb +26 -0
  26. data/lib/contrast/agent/middleware.rb +14 -62
  27. data/lib/contrast/agent/patching/policy/method_policy.rb +3 -89
  28. data/lib/contrast/agent/patching/policy/method_policy_extend.rb +111 -0
  29. data/lib/contrast/agent/patching/policy/patch.rb +28 -235
  30. data/lib/contrast/agent/patching/policy/patcher.rb +14 -49
  31. data/lib/contrast/agent/reporting/report.rb +21 -0
  32. data/lib/contrast/agent/reporting/reporter.rb +142 -0
  33. data/lib/contrast/agent/reporting/reporting_events/finding.rb +90 -0
  34. data/lib/contrast/agent/reporting/reporting_events/preflight.rb +25 -0
  35. data/lib/contrast/agent/reporting/reporting_events/preflight_message.rb +56 -0
  36. data/lib/contrast/agent/reporting/reporting_events/reporting_event.rb +37 -0
  37. data/lib/contrast/agent/reporting/reporting_utilities/audit.rb +127 -0
  38. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +168 -0
  39. data/lib/contrast/agent/reporting/reporting_utilities/reporting_storage.rb +66 -0
  40. data/lib/contrast/agent/request.rb +2 -81
  41. data/lib/contrast/agent/request_context.rb +4 -128
  42. data/lib/contrast/agent/request_context_extend.rb +138 -0
  43. data/lib/contrast/agent/request_handler.rb +7 -3
  44. data/lib/contrast/agent/response.rb +2 -73
  45. data/lib/contrast/agent/startup_metrics_telemetry_event.rb +94 -0
  46. data/lib/contrast/agent/static_analysis.rb +5 -3
  47. data/lib/contrast/agent/telemetry.rb +137 -0
  48. data/lib/contrast/agent/telemetry_event.rb +33 -0
  49. data/lib/contrast/agent/thread_watcher.rb +66 -11
  50. data/lib/contrast/agent/version.rb +1 -1
  51. data/lib/contrast/agent.rb +21 -0
  52. data/lib/contrast/api/communication/connection_status.rb +10 -7
  53. data/lib/contrast/api/communication/messaging_queue.rb +37 -3
  54. data/lib/contrast/api/communication/response_processor.rb +15 -8
  55. data/lib/contrast/api/communication/service_lifecycle.rb +13 -3
  56. data/lib/contrast/api/communication/socket.rb +6 -8
  57. data/lib/contrast/api/communication/socket_client.rb +29 -12
  58. data/lib/contrast/api/communication/speedracer.rb +37 -1
  59. data/lib/contrast/api/communication/tcp_socket.rb +4 -3
  60. data/lib/contrast/api/communication/unix_socket.rb +1 -0
  61. data/lib/contrast/api/decorators/finding.rb +45 -0
  62. data/lib/contrast/components/api.rb +90 -0
  63. data/lib/contrast/components/app_context.rb +10 -41
  64. data/lib/contrast/components/app_context_extend.rb +78 -0
  65. data/lib/contrast/components/base.rb +23 -0
  66. data/lib/contrast/components/config.rb +92 -13
  67. data/lib/contrast/components/contrast_service.rb +11 -0
  68. data/lib/contrast/components/sampling.rb +2 -2
  69. data/lib/contrast/config/agent_configuration.rb +1 -1
  70. data/lib/contrast/config/api_configuration.rb +27 -0
  71. data/lib/contrast/config/api_proxy_configuration.rb +14 -0
  72. data/lib/contrast/config/application_configuration.rb +2 -3
  73. data/lib/contrast/config/assess_configuration.rb +3 -3
  74. data/lib/contrast/config/base_configuration.rb +17 -28
  75. data/lib/contrast/config/certification_configuration.rb +15 -0
  76. data/lib/contrast/config/env_variables.rb +18 -0
  77. data/lib/contrast/config/heap_dump_configuration.rb +6 -6
  78. data/lib/contrast/config/inventory_configuration.rb +1 -5
  79. data/lib/contrast/config/protect_rule_configuration.rb +1 -1
  80. data/lib/contrast/config/request_audit_configuration.rb +18 -0
  81. data/lib/contrast/config/root_configuration.rb +1 -0
  82. data/lib/contrast/config/ruby_configuration.rb +6 -6
  83. data/lib/contrast/config/service_configuration.rb +2 -2
  84. data/lib/contrast/config.rb +1 -1
  85. data/lib/contrast/configuration.rb +4 -2
  86. data/lib/contrast/extension/assess/array.rb +5 -7
  87. data/lib/contrast/extension/thread.rb +31 -12
  88. data/lib/contrast/framework/manager.rb +22 -44
  89. data/lib/contrast/framework/manager_extend.rb +50 -0
  90. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +9 -6
  91. data/lib/contrast/framework/rails/patch/support.rb +31 -29
  92. data/lib/contrast/framework/rails/railtie.rb +1 -1
  93. data/lib/contrast/framework/sinatra/support.rb +2 -1
  94. data/lib/contrast/logger/application.rb +4 -0
  95. data/lib/contrast/logger/log.rb +8 -103
  96. data/lib/contrast/utils/assess/propagation_method_utils.rb +129 -0
  97. data/lib/contrast/utils/assess/property/tagged_utils.rb +165 -0
  98. data/lib/contrast/utils/assess/source_method_utils.rb +83 -0
  99. data/lib/contrast/utils/assess/tracking_util.rb +20 -15
  100. data/lib/contrast/utils/assess/trigger_method_utils.rb +138 -0
  101. data/lib/contrast/utils/class_util.rb +18 -14
  102. data/lib/contrast/utils/exclude_key.rb +20 -0
  103. data/lib/contrast/utils/findings.rb +62 -0
  104. data/lib/contrast/utils/hash_digest.rb +10 -73
  105. data/lib/contrast/utils/hash_digest_extend.rb +86 -0
  106. data/lib/contrast/utils/head_dump_utils_extend.rb +74 -0
  107. data/lib/contrast/utils/heap_dump_util.rb +2 -65
  108. data/lib/contrast/utils/invalid_configuration_util.rb +29 -0
  109. data/lib/contrast/utils/io_util.rb +1 -1
  110. data/lib/contrast/utils/log_utils.rb +108 -0
  111. data/lib/contrast/utils/metrics_hash.rb +59 -0
  112. data/lib/contrast/utils/middleware_utils.rb +87 -0
  113. data/lib/contrast/utils/net_http_base.rb +158 -0
  114. data/lib/contrast/utils/object_share.rb +1 -0
  115. data/lib/contrast/utils/os.rb +23 -0
  116. data/lib/contrast/utils/patching/policy/patch_utils.rb +232 -0
  117. data/lib/contrast/utils/patching/policy/patcher_utils.rb +54 -0
  118. data/lib/contrast/utils/request_utils.rb +88 -0
  119. data/lib/contrast/utils/response_utils.rb +97 -0
  120. data/lib/contrast/utils/substitution_utils.rb +167 -0
  121. data/lib/contrast/utils/tag_util.rb +9 -9
  122. data/lib/contrast/utils/telemetry.rb +79 -0
  123. data/lib/contrast/utils/telemetry_client.rb +90 -0
  124. data/lib/contrast/utils/telemetry_identifier.rb +130 -0
  125. data/lib/contrast.rb +18 -0
  126. data/ruby-agent.gemspec +7 -6
  127. data/service_executables/VERSION +1 -1
  128. data/service_executables/linux/contrast-service +0 -0
  129. data/service_executables/mac/contrast-service +0 -0
  130. metadata +69 -22
  131. data/lib/contrast/config/default_value.rb +0 -17
@@ -7,6 +7,8 @@ require 'contrast/agent/response'
7
7
  require 'contrast/agent/inventory/database_config'
8
8
  require 'contrast/components/logger'
9
9
  require 'contrast/components/scope'
10
+ require 'contrast/utils/request_utils'
11
+ require 'contrast/agent/request_context_extend'
10
12
 
11
13
  module Contrast
12
14
  module Agent
@@ -27,6 +29,8 @@ module Contrast
27
29
  class RequestContext
28
30
  include Contrast::Components::Logger::InstanceMethods
29
31
  include Contrast::Components::Scope::InstanceMethods
32
+ include Contrast::Utils::RequestUtils
33
+ include Contrast::Agent::RequestContextExtend
30
34
 
31
35
  EMPTY_INPUT_ANALYSIS_PB = Contrast::Api::Settings::InputAnalysis.new
32
36
 
@@ -100,98 +104,6 @@ module Contrast
100
104
  ::Contrast::ASSESS.enabled?
101
105
  end
102
106
 
103
- # Convert the discovered route for this request to appropriate forms and disseminate it to those locations
104
- # where it is necessary for our route coverage and finding vulnerability discovery features to function.
105
- #
106
- # @param route [Contrast::Api::Dtm::RouteCoverage, nil] the route of the current request, as determined from the
107
- # framework
108
- def append_route_coverage route
109
- return unless route
110
-
111
- # For our findings
112
- @route = route
113
-
114
- # For SR findings
115
- @activity.routes << route
116
-
117
- # For TS routes
118
- @observed_route.signature = route.route
119
- @observed_route.verb = route.verb
120
- @observed_route.url = route.url if route.url
121
- @request.route = route
122
- @request.observed_route = @observed_route
123
- end
124
-
125
- # Collect the results for the given rule with the given action
126
- #
127
- # @param rule [String] the id of the rule to which the results apply
128
- # @param response_type [Symbol] the result of the response, matching a value of
129
- # Contrast::Api::Dtm::AttackResult::ResponseType
130
- # @return [Array<Contrast::Api::Dtm::AttackResult>]
131
- def results_for rule, response_type = nil
132
- if response_type.nil?
133
- activity.results.select { |r| r.rule_id == rule }
134
- else
135
- activity.results.select { |r| r.rule_id == rule && r.response == response_type }
136
- end
137
- end
138
-
139
- def service_extract_request
140
- return false unless ::Contrast::AGENT.enabled?
141
- return false unless ::Contrast::PROTECT.enabled?
142
- return false if @do_not_track
143
-
144
- service_response = Contrast::Agent.messaging_queue.send_event_immediately(@activity.http_request)
145
- return false unless service_response
146
-
147
- handle_protect_state(service_response)
148
- ia = service_response.input_analysis
149
- if ia
150
- if logger.trace?
151
- logger.trace('Analysis from Contrast Service', evaluations: ia.results.length)
152
- logger.trace('Results', input_analysis: ia.inspect)
153
- end
154
- @speedracer_input_analysis = ia
155
- speedracer_input_analysis.request = request
156
- else
157
- logger.trace('Analysis from Contrast Service was empty.')
158
- false
159
- end
160
- rescue Contrast::SecurityException => e
161
- raise e
162
- rescue StandardError => e
163
- logger.warn('Unable to extract Contrast Service information from request', e)
164
- false
165
- end
166
-
167
- # NOTE: this method is only used as a backstop if Speedracer sends Input Evaluations when the protect state
168
- # indicates a security exception should be thrown. This method ensures that the attack reports are generated.
169
- # Normally these should be generated on Speedracer for any attacks detected during prefilter.
170
- #
171
- # @param agent_settings [Contrast::Api::Settings::AgentSettings]
172
- def handle_protect_state agent_settings
173
- return unless agent_settings&.protect_state
174
-
175
- state = agent_settings.protect_state
176
- @uuid = state.uuid
177
- @do_not_track = true unless state.track_request
178
- return unless state.security_exception
179
-
180
- # If Contrast Service has NOT handled the input analysis, handle them here
181
- build_attack_results(agent_settings)
182
- logger.debug('Contrast Service said to block this request')
183
- raise Contrast::SecurityException.new(nil, (state.security_message || 'Blocking suspicious behavior'))
184
- end
185
-
186
- # append anything we've learned to the request seen message this is the sum-total of all inventory information
187
- # that has been accumulated since the last request
188
- def extract_after rack_response
189
- @response = Contrast::Agent::Response.new(rack_response)
190
- activity.http_response = @response.dtm if @sample_res
191
- rescue StandardError => e
192
- logger.error('Unable to extract information after request', e)
193
- end
194
-
195
107
  def add_property key, value
196
108
  @_properties[key] = value
197
109
  end
@@ -205,42 +117,6 @@ module Contrast
205
117
  @server_activity = Contrast::Api::Dtm::ServerActivity.new
206
118
  @observed_route = Contrast::Api::Dtm::ObservedRoute.new
207
119
  end
208
-
209
- private
210
-
211
- # Generate attack results directly from any evaluations on the agent settings object.
212
- #
213
- # @param agent_settings [Contrast::Api::Settings::AgentSettings]
214
- def build_attack_results agent_settings
215
- return unless agent_settings&.input_analysis&.results&.any?
216
-
217
- attack_results_by_rule = {}
218
- agent_settings.input_analysis.results.each do |ia_result|
219
- rule_id = ia_result.rule_id
220
- rule = ::Contrast::PROTECT.rule(rule_id)
221
- next unless rule
222
-
223
- if logger.debug?
224
- logger.debug('Building attack result from Contrast Service input analysis result',
225
- result: ia_result.inspect)
226
- end
227
-
228
- attack_result = if rule.mode == :BLOCK
229
- # special case for rules (like reflected xss) that used to have an infilter / block mode
230
- # but now are just block at perimeter
231
- rule.build_attack_with_match(self, ia_result, attack_results_by_rule[rule_id],
232
- ia_result.value)
233
- else
234
- rule.build_attack_without_match(self, ia_result, attack_results_by_rule[rule_id])
235
- end
236
- attack_results_by_rule[rule_id] = attack_result
237
- end
238
-
239
- attack_results_by_rule.each_pair do |_, attack_result|
240
- logger.info('Blocking attack result', rule: attack_result.rule_id)
241
- activity.results << attack_result
242
- end
243
- end
244
120
  end
245
121
  end
246
122
  end
@@ -0,0 +1,138 @@
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
+ module Contrast
5
+ module Agent
6
+ # This class extends RequestContexts: this class acts to encapsulate information about the currently
7
+ # executed request, making it available to the Agent for the duration of the request in a standardized
8
+ # and normalized format which the Agent understands.
9
+ module RequestContextExtend
10
+ BUILD_ATTACK_LOGGER_MESSAGE = 'Building attack result from Contrast Service input analysis result'
11
+ # Convert the discovered route for this request to appropriate forms and disseminate it to those locations
12
+ # where it is necessary for our route coverage and finding vulnerability discovery features to function.
13
+ #
14
+ # @param route [Contrast::Api::Dtm::RouteCoverage, nil] the route of the current request, as determined from the
15
+ # framework
16
+ def append_route_coverage route
17
+ return unless route
18
+
19
+ # For our findings
20
+ @route = route
21
+
22
+ # For SR findings
23
+ @activity.routes << route
24
+
25
+ # For TS routes
26
+ @observed_route.signature = route.route
27
+ @observed_route.verb = route.verb
28
+ @observed_route.url = route.url if route.url
29
+ @request.route = route
30
+ @request.observed_route = @observed_route
31
+ end
32
+
33
+ # Collect the results for the given rule with the given action
34
+ #
35
+ # @param rule [String] the id of the rule to which the results apply
36
+ # @param response_type [Symbol] the result of the response, matching a value of
37
+ # Contrast::Api::Dtm::AttackResult::ResponseType
38
+ # @return [Array<Contrast::Api::Dtm::AttackResult>]
39
+ def results_for rule, response_type = nil
40
+ if response_type.nil?
41
+ activity.results.select { |r| r.rule_id == rule }
42
+ else
43
+ activity.results.select { |r| r.rule_id == rule && r.response == response_type }
44
+ end
45
+ end
46
+
47
+ def service_extract_request
48
+ return false unless ::Contrast::AGENT.enabled?
49
+ return false unless ::Contrast::PROTECT.enabled?
50
+ return false if @do_not_track
51
+
52
+ service_response = Contrast::Agent.messaging_queue.send_event_immediately(@activity.http_request)
53
+ return false unless service_response
54
+
55
+ handle_protect_state(service_response)
56
+ ia = service_response.input_analysis
57
+ if ia
58
+ if logger.trace?
59
+ logger.trace('Analysis from Contrast Service', evaluations: ia.results.length)
60
+ logger.trace('Results', input_analysis: ia.inspect)
61
+ end
62
+ @speedracer_input_analysis = ia
63
+ speedracer_input_analysis.request = request
64
+ else
65
+ logger.trace('Analysis from Contrast Service was empty.')
66
+ false
67
+ end
68
+ rescue Contrast::SecurityException => e
69
+ raise e
70
+ rescue StandardError => e
71
+ logger.warn('Unable to extract Contrast Service information from request', e)
72
+ false
73
+ end
74
+
75
+ # NOTE: this method is only used as a backstop if Speedracer sends Input Evaluations when the protect state
76
+ # indicates a security exception should be thrown. This method ensures that the attack reports are generated.
77
+ # Normally these should be generated on Speedracer for any attacks detected during prefilter.
78
+ #
79
+ # @param agent_settings [Contrast::Api::Settings::AgentSettings]
80
+ def handle_protect_state agent_settings
81
+ return unless agent_settings&.protect_state
82
+
83
+ state = agent_settings.protect_state
84
+ @uuid = state.uuid
85
+ @do_not_track = true unless state.track_request
86
+ return unless state.security_exception
87
+
88
+ # If Contrast Service has NOT handled the input analysis, handle them here
89
+ build_attack_results(agent_settings)
90
+ logger.debug('Contrast Service said to block this request')
91
+ raise Contrast::SecurityException.new(nil, (state.security_message || 'Blocking suspicious behavior'))
92
+ end
93
+
94
+ # append anything we've learned to the request seen message this is the sum-total of all inventory information
95
+ # that has been accumulated since the last request
96
+ def extract_after rack_response
97
+ @response = Contrast::Agent::Response.new(rack_response)
98
+ activity.http_response = @response.dtm if @sample_res
99
+ rescue StandardError => e
100
+ logger.error('Unable to extract information after request', e)
101
+ end
102
+
103
+ private
104
+
105
+ # Generate attack results directly from any evaluations on the agent settings object.
106
+ #
107
+ # @param agent_settings [Contrast::Api::Settings::AgentSettings]
108
+ def build_attack_results agent_settings
109
+ return unless agent_settings&.input_analysis&.results&.any?
110
+
111
+ results_by_rule = {}
112
+ agent_settings.input_analysis.results.each do |ia_result|
113
+ rule_id = ia_result.rule_id
114
+ rule = ::Contrast::PROTECT.rule(rule_id)
115
+ next unless rule
116
+
117
+ logger.debug(BUILD_ATTACK_LOGGER_MESSAGE, result: ia_result.inspect) if logger.debug?
118
+ results_by_rule[rule_id] = attack_result rule, rule_id, ia_result, results_by_rule
119
+ end
120
+
121
+ results_by_rule.each_pair do |_, attack_result|
122
+ logger.info('Blocking attack result', rule: attack_result.rule_id)
123
+ activity.results << attack_result
124
+ end
125
+ end
126
+
127
+ def attack_result rule, rule_id, ia_result, results_by_rule
128
+ @_attack_result = if rule.mode == :BLOCK
129
+ # special case for rules (like reflected xss) that used to have an infilter / block mode
130
+ # but now are just block at perimeter
131
+ rule.build_attack_with_match(self, ia_result, results_by_rule[rule_id], ia_result.value)
132
+ else
133
+ rule.build_attack_without_match(self, ia_result, results_by_rule[rule_id])
134
+ end
135
+ end
136
+ end
137
+ end
138
+ end
@@ -6,19 +6,23 @@ require 'contrast/components/scope'
6
6
 
7
7
  module Contrast
8
8
  module Agent
9
- # This class is instantiated when we receive a request and the agent is enabled to process
10
- # that request. It holds the ruleset that we perform filtering operations on (currently
11
- # prefilter and postfilter).
9
+ # This class is instantiated when we receive a request and the agent is enabled to process that request. It holds
10
+ # the ruleset that we perform filtering operations on (currently prefilter and postfilter).
12
11
  class RequestHandler
13
12
  include Contrast::Components::Logger::InstanceMethods
14
13
 
15
14
  attr_reader :ruleset, :context
16
15
 
16
+ # @param context [Contrast::Agent::RequestContext] the context of the request for which this handler applies
17
17
  def initialize context
18
18
  @context = context
19
19
  @ruleset = ::Contrast::AGENT.ruleset
20
20
  end
21
21
 
22
+ # TODO: RUBY-1353
23
+ # TODO: RUBY-1355
24
+ # TODO: RUBY-1357
25
+ # TODO: RUBY-1358
22
26
  def send_activity_messages
23
27
  Contrast::Agent::Inventory::DependencyUsageAnalysis.instance.generate_library_usage(context.activity)
24
28
  [context.server_activity, context.activity, context.observed_route].each do |message|
@@ -8,6 +8,7 @@ require 'contrast/utils/object_share'
8
8
  require 'contrast/utils/string_utils'
9
9
  require 'contrast/utils/hash_digest'
10
10
  require 'contrast/components/logger'
11
+ require 'contrast/utils/response_utils'
11
12
 
12
13
  module Contrast
13
14
  module Agent
@@ -17,6 +18,7 @@ module Contrast
17
18
  # order to avoid repeatedly creating Strings & thrashing GC.
18
19
  class Response
19
20
  include Contrast::Components::Logger::InstanceMethods
21
+ include Contrast::Utils::ResponseUtils
20
22
 
21
23
  extend Forwardable
22
24
 
@@ -88,79 +90,6 @@ module Contrast
88
90
  body_content = @is_array ? rack_response[2] : rack_response.body
89
91
  extract_body(body_content)
90
92
  end
91
-
92
- private
93
-
94
- # From the dtm for normalized_response_headers:
95
- # Key is UPPERCASE_UNDERSCORE
96
- #
97
- # Example: Content-Type: text/html; charset=utf-8
98
- # "CONTENT_TYPE" => Content-Type,["text/html; charset=utf8"]
99
- def append_pair map, key, value
100
- return unless key && value
101
- return if value.is_a?(Hash)
102
-
103
- safe_key = Contrast::Utils::StringUtils.force_utf8(key)
104
- hash_key = Contrast::Utils::StringUtils.normalized_key(safe_key)
105
- map[hash_key] ||= Contrast::Api::Dtm::Pair.new
106
- map[hash_key].key = safe_key
107
- map[hash_key].values << Contrast::Utils::StringUtils.force_utf8(value)
108
- end
109
-
110
- HTTP_PREFIX = /^[Hh][Tt][Tt][Pp][_-]/i.cs__freeze
111
-
112
- # Given some holder of the content of the response's body, extract that
113
- # content and return it as a String
114
- #
115
- # @param body [String, Rack::File, Rack::BodyProxy,
116
- # ActionDispatch::Response::RackBody, Rack::Response] Something that
117
- # holds, wraps, or is the body of the Response
118
- # @return [nil, String] the content of the body
119
- def extract_body body
120
- return unless body
121
-
122
- if defined?(Rack::File) && body.is_a?(Rack::File)
123
- # not sure what to do in this situation, so don't do anything.
124
- nil
125
- elsif body.is_a?(Rack::BodyProxy)
126
- handle_rack_body_proxy(body)
127
- elsif (defined?(ActionDispatch::Response::RackBody) && body.is_a?(ActionDispatch::Response::RackBody)) ||
128
- body.is_a?(Rack::Response)
129
-
130
- extract_body(body.body)
131
- elsif Contrast::Utils::DuckUtils.quacks_to?(body, :each)
132
- acc = []
133
- body.each { |tmp| acc << read_or_string(tmp) }
134
- acc.compact.join(Contrast::Utils::ObjectShare::NEW_LINE)
135
- elsif ActionView::OutputBuffer
136
- # https://stackoverflow.com/questions/15654676/how-to-convert-activesupportsafebuffer-to-string
137
- body.to_str
138
- else
139
- read_or_string(body)
140
- end
141
- end
142
-
143
- def handle_rack_body_proxy body
144
- next_body = body.instance_variable_get(:@body)
145
- case next_body
146
- when Array
147
- extract_body(next_body[0])
148
- else
149
- extract_body(next_body)
150
- end
151
- end
152
-
153
- def read_or_string obj
154
- return unless obj
155
-
156
- if Contrast::Utils::DuckUtils.quacks_to?(obj, :read)
157
- tmp = obj.read
158
- obj.rewind
159
- tmp
160
- else
161
- obj.to_s
162
- end
163
- end
164
93
  end
165
94
  end
166
95
  end
@@ -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