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.
- checksums.yaml +4 -4
- data/.simplecov +1 -0
- data/ext/cs__assess_module/cs__assess_module.c +48 -0
- data/ext/cs__assess_module/cs__assess_module.h +7 -0
- data/ext/cs__common/cs__common.c +5 -0
- data/ext/cs__common/cs__common.h +8 -0
- data/ext/cs__contrast_patch/cs__contrast_patch.c +16 -1
- data/ext/cs__os_information/cs__os_information.c +31 -0
- data/ext/cs__os_information/cs__os_information.h +7 -0
- data/ext/cs__os_information/extconf.rb +5 -0
- data/lib/contrast/agent/assess/policy/policy_node.rb +6 -6
- data/lib/contrast/agent/assess/policy/policy_scanner.rb +5 -0
- data/lib/contrast/agent/assess/policy/propagation_method.rb +2 -116
- data/lib/contrast/agent/assess/policy/propagation_node.rb +4 -4
- data/lib/contrast/agent/assess/policy/propagator/center.rb +1 -1
- data/lib/contrast/agent/assess/policy/propagator/substitution.rb +2 -154
- data/lib/contrast/agent/assess/policy/source_method.rb +2 -71
- data/lib/contrast/agent/assess/policy/trigger_method.rb +45 -110
- data/lib/contrast/agent/assess/policy/trigger_node.rb +14 -6
- data/lib/contrast/agent/assess/policy/trigger_validation/xss_validator.rb +1 -1
- data/lib/contrast/agent/assess/property/tagged.rb +53 -185
- data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +40 -6
- data/lib/contrast/agent/deadzone/policy/policy.rb +1 -1
- data/lib/contrast/agent/inventory/dependency_usage_analysis.rb +1 -0
- data/lib/contrast/agent/metric_telemetry_event.rb +26 -0
- data/lib/contrast/agent/middleware.rb +14 -62
- data/lib/contrast/agent/patching/policy/method_policy.rb +3 -89
- data/lib/contrast/agent/patching/policy/method_policy_extend.rb +111 -0
- data/lib/contrast/agent/patching/policy/patch.rb +28 -235
- data/lib/contrast/agent/patching/policy/patcher.rb +14 -49
- data/lib/contrast/agent/reporting/report.rb +21 -0
- data/lib/contrast/agent/reporting/reporter.rb +142 -0
- data/lib/contrast/agent/reporting/reporting_events/finding.rb +90 -0
- data/lib/contrast/agent/reporting/reporting_events/preflight.rb +25 -0
- data/lib/contrast/agent/reporting/reporting_events/preflight_message.rb +56 -0
- data/lib/contrast/agent/reporting/reporting_events/reporting_event.rb +37 -0
- data/lib/contrast/agent/reporting/reporting_utilities/audit.rb +127 -0
- data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +168 -0
- data/lib/contrast/agent/reporting/reporting_utilities/reporting_storage.rb +66 -0
- data/lib/contrast/agent/request.rb +2 -81
- data/lib/contrast/agent/request_context.rb +4 -128
- data/lib/contrast/agent/request_context_extend.rb +138 -0
- data/lib/contrast/agent/request_handler.rb +7 -3
- data/lib/contrast/agent/response.rb +2 -73
- data/lib/contrast/agent/startup_metrics_telemetry_event.rb +94 -0
- data/lib/contrast/agent/static_analysis.rb +5 -3
- data/lib/contrast/agent/telemetry.rb +137 -0
- data/lib/contrast/agent/telemetry_event.rb +33 -0
- data/lib/contrast/agent/thread_watcher.rb +66 -11
- data/lib/contrast/agent/version.rb +1 -1
- data/lib/contrast/agent.rb +21 -0
- data/lib/contrast/api/communication/connection_status.rb +10 -7
- data/lib/contrast/api/communication/messaging_queue.rb +37 -3
- data/lib/contrast/api/communication/response_processor.rb +15 -8
- data/lib/contrast/api/communication/service_lifecycle.rb +13 -3
- data/lib/contrast/api/communication/socket.rb +6 -8
- data/lib/contrast/api/communication/socket_client.rb +29 -12
- data/lib/contrast/api/communication/speedracer.rb +37 -1
- data/lib/contrast/api/communication/tcp_socket.rb +4 -3
- data/lib/contrast/api/communication/unix_socket.rb +1 -0
- data/lib/contrast/api/decorators/finding.rb +45 -0
- data/lib/contrast/components/api.rb +90 -0
- data/lib/contrast/components/app_context.rb +10 -41
- data/lib/contrast/components/app_context_extend.rb +78 -0
- data/lib/contrast/components/base.rb +23 -0
- data/lib/contrast/components/config.rb +92 -13
- data/lib/contrast/components/contrast_service.rb +11 -0
- data/lib/contrast/components/sampling.rb +2 -2
- data/lib/contrast/config/agent_configuration.rb +1 -1
- data/lib/contrast/config/api_configuration.rb +27 -0
- data/lib/contrast/config/api_proxy_configuration.rb +14 -0
- data/lib/contrast/config/application_configuration.rb +2 -3
- data/lib/contrast/config/assess_configuration.rb +3 -3
- data/lib/contrast/config/base_configuration.rb +17 -28
- data/lib/contrast/config/certification_configuration.rb +15 -0
- data/lib/contrast/config/env_variables.rb +18 -0
- data/lib/contrast/config/heap_dump_configuration.rb +6 -6
- data/lib/contrast/config/inventory_configuration.rb +1 -5
- data/lib/contrast/config/protect_rule_configuration.rb +1 -1
- data/lib/contrast/config/request_audit_configuration.rb +18 -0
- data/lib/contrast/config/root_configuration.rb +1 -0
- data/lib/contrast/config/ruby_configuration.rb +6 -6
- data/lib/contrast/config/service_configuration.rb +2 -2
- data/lib/contrast/config.rb +1 -1
- data/lib/contrast/configuration.rb +4 -2
- data/lib/contrast/extension/assess/array.rb +5 -7
- data/lib/contrast/extension/thread.rb +31 -12
- data/lib/contrast/framework/manager.rb +22 -44
- data/lib/contrast/framework/manager_extend.rb +50 -0
- data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +9 -6
- data/lib/contrast/framework/rails/patch/support.rb +31 -29
- data/lib/contrast/framework/rails/railtie.rb +1 -1
- data/lib/contrast/framework/sinatra/support.rb +2 -1
- data/lib/contrast/logger/application.rb +4 -0
- data/lib/contrast/logger/log.rb +8 -103
- data/lib/contrast/utils/assess/propagation_method_utils.rb +129 -0
- data/lib/contrast/utils/assess/property/tagged_utils.rb +165 -0
- data/lib/contrast/utils/assess/source_method_utils.rb +83 -0
- data/lib/contrast/utils/assess/tracking_util.rb +20 -15
- data/lib/contrast/utils/assess/trigger_method_utils.rb +138 -0
- data/lib/contrast/utils/class_util.rb +18 -14
- data/lib/contrast/utils/exclude_key.rb +20 -0
- data/lib/contrast/utils/findings.rb +62 -0
- data/lib/contrast/utils/hash_digest.rb +10 -73
- data/lib/contrast/utils/hash_digest_extend.rb +86 -0
- data/lib/contrast/utils/head_dump_utils_extend.rb +74 -0
- data/lib/contrast/utils/heap_dump_util.rb +2 -65
- data/lib/contrast/utils/invalid_configuration_util.rb +29 -0
- data/lib/contrast/utils/io_util.rb +1 -1
- data/lib/contrast/utils/log_utils.rb +108 -0
- data/lib/contrast/utils/metrics_hash.rb +59 -0
- data/lib/contrast/utils/middleware_utils.rb +87 -0
- data/lib/contrast/utils/net_http_base.rb +158 -0
- data/lib/contrast/utils/object_share.rb +1 -0
- data/lib/contrast/utils/os.rb +23 -0
- data/lib/contrast/utils/patching/policy/patch_utils.rb +232 -0
- data/lib/contrast/utils/patching/policy/patcher_utils.rb +54 -0
- data/lib/contrast/utils/request_utils.rb +88 -0
- data/lib/contrast/utils/response_utils.rb +97 -0
- data/lib/contrast/utils/substitution_utils.rb +167 -0
- data/lib/contrast/utils/tag_util.rb +9 -9
- data/lib/contrast/utils/telemetry.rb +79 -0
- data/lib/contrast/utils/telemetry_client.rb +90 -0
- data/lib/contrast/utils/telemetry_identifier.rb +130 -0
- data/lib/contrast.rb +18 -0
- data/ruby-agent.gemspec +7 -6
- data/service_executables/VERSION +1 -1
- data/service_executables/linux/contrast-service +0 -0
- data/service_executables/mac/contrast-service +0 -0
- metadata +69 -22
- data/lib/contrast/config/default_value.rb +0 -17
@@ -4,6 +4,7 @@
|
|
4
4
|
require 'monitor'
|
5
5
|
require 'contrast/components/logger'
|
6
6
|
require 'contrast/components/scope'
|
7
|
+
require 'contrast/utils/patching/policy/patch_utils'
|
7
8
|
|
8
9
|
require 'contrast/agent'
|
9
10
|
require 'contrast/logger/log'
|
@@ -32,6 +33,8 @@ module Contrast
|
|
32
33
|
# provides a map for which methods our renamed functions need to call
|
33
34
|
# and how.
|
34
35
|
module Patch
|
36
|
+
extend Contrast::Utils::Patching::PatchUtils
|
37
|
+
|
35
38
|
class << self
|
36
39
|
include Contrast::Agent::Assess::Policy::SourceMethod
|
37
40
|
include Contrast::Agent::Assess::Policy::PropagationMethod
|
@@ -57,226 +60,6 @@ module Contrast
|
|
57
60
|
end
|
58
61
|
end
|
59
62
|
|
60
|
-
# THIS IS CALLED FROM C. Do not change the signature lightly.
|
61
|
-
#
|
62
|
-
# This method functions to call the infilter methods from our
|
63
|
-
# patches, allowing for analysis and reporting at the point just
|
64
|
-
# before the patched code is invoked.
|
65
|
-
#
|
66
|
-
# @param method_policy [Contrast::Agent::Patching::Policy::MethodPolicy]
|
67
|
-
# Mapping of the triggers on the given method.
|
68
|
-
# @param method [Symbol] The method into which we're patching
|
69
|
-
# @param exception [StandardError] Any exception raised during the
|
70
|
-
# call of the patched method.
|
71
|
-
# @param object [Object] The object on which the method is invoked,
|
72
|
-
# typically what would be returned by self.
|
73
|
-
# @param args [Array<Object>] The arguments passed to the method
|
74
|
-
# being invoked.
|
75
|
-
def apply_pre_patch method_policy, method, exception, object, args
|
76
|
-
apply_protect(method_policy, method, exception, object, args)
|
77
|
-
apply_inventory(method_policy, method, exception, object, args)
|
78
|
-
rescue Contrast::SecurityException => e
|
79
|
-
# We were told to block something, so we gotta. Don't catch this
|
80
|
-
# one, let it get back to our Middleware or even all the way out to
|
81
|
-
# the framework
|
82
|
-
raise e
|
83
|
-
rescue StandardError => e
|
84
|
-
# Anything else was our bad and we gotta catch that to allow for
|
85
|
-
# normal application flow
|
86
|
-
logger.error('Unable to apply pre patch to method.', e)
|
87
|
-
rescue Exception => e # rubocop:disable Lint/RescueException
|
88
|
-
# This is something like NoMemoryError that we can't
|
89
|
-
# hope to handle. Nonetheless, shouldn't leak scope.
|
90
|
-
exit_contrast_scope!
|
91
|
-
raise e
|
92
|
-
end
|
93
|
-
|
94
|
-
# THIS IS CALLED FROM C. Do not change the signature lightly.
|
95
|
-
#
|
96
|
-
# This method functions to call the infilter methods from our
|
97
|
-
# patches, allowing for analysis and reporting at the point just
|
98
|
-
# after the patched code is invoked
|
99
|
-
#
|
100
|
-
# @param method_policy [Contrast::Agent::Patching::Policy::MethodPolicy]
|
101
|
-
# Mapping of the triggers on the given method.
|
102
|
-
# @param preshift [Contrast::Agent::Assess::PreShift] The capture
|
103
|
-
# of the state of the code just prior to the invocation of the
|
104
|
-
# patched method.
|
105
|
-
# @param object [Object] The object on which the method was
|
106
|
-
# invoked, typically what would be returned by self.
|
107
|
-
# @param ret [Object] The return of the method that was invoked.
|
108
|
-
# @param args [Array<Object>] The arguments passed to the method
|
109
|
-
# being invoked.
|
110
|
-
# @param block [Proc] The block passed to the method that was
|
111
|
-
# invoked.
|
112
|
-
def apply_post_patch method_policy, preshift, object, ret, args, block
|
113
|
-
apply_assess(method_policy, preshift, object, ret, args, block)
|
114
|
-
rescue StandardError => e
|
115
|
-
logger.error('Unable to apply post patch to method.', e)
|
116
|
-
end
|
117
|
-
|
118
|
-
# Apply the Protect patch which applies to the given method.
|
119
|
-
#
|
120
|
-
# @param method_policy [Contrast::Agent::Patching::Policy::MethodPolicy]
|
121
|
-
# Mapping of the triggers on the given method.
|
122
|
-
# @param method [Symbol] The method into which we're patching
|
123
|
-
# @param exception [StandardError] Any exception raised during the
|
124
|
-
# call of the patched method.
|
125
|
-
# @param object [Object] The object on which the method is invoked,
|
126
|
-
# typically what would be returned by self.
|
127
|
-
# @param args [Array<Object>] The arguments passed to the method
|
128
|
-
# being invoked.
|
129
|
-
def apply_protect method_policy, method, exception, object, args
|
130
|
-
return unless ::Contrast::AGENT.enabled?
|
131
|
-
return unless ::Contrast::PROTECT.enabled?
|
132
|
-
|
133
|
-
apply_trigger_only(method_policy&.protect_node, method, exception, object, args)
|
134
|
-
end
|
135
|
-
|
136
|
-
# Apply the Inventory patch which applies to the given method.
|
137
|
-
#
|
138
|
-
# @param method_policy [Contrast::Agent::Patching::Policy::MethodPolicy]
|
139
|
-
# Mapping of the triggers on the given method.
|
140
|
-
# @param method [Symbol] The method into which we're patching
|
141
|
-
# @param exception [StandardError] Any exception raised during the
|
142
|
-
# call of the patched method.
|
143
|
-
# @param object [Object] The object on which the method is invoked,
|
144
|
-
# typically what would be returned by self.
|
145
|
-
# @param args [Array<Object>] The arguments passed to the method
|
146
|
-
# being invoked.
|
147
|
-
def apply_inventory method_policy, method, exception, object, args
|
148
|
-
return unless ::Contrast::INVENTORY.enabled?
|
149
|
-
|
150
|
-
apply_trigger_only(method_policy&.inventory_node, method, exception, object, args)
|
151
|
-
end
|
152
|
-
|
153
|
-
# Apply the Assess patches which apply to the given method.
|
154
|
-
#
|
155
|
-
# @param method_policy [Contrast::Agent::Patching::Policy::MethodPolicy]
|
156
|
-
# Mapping of the triggers on the given method.
|
157
|
-
# @param preshift [Contrast::Agent::Assess::PreShift] The capture
|
158
|
-
# of the state of the code just prior to the invocation of the
|
159
|
-
# patched method.
|
160
|
-
# @param object [Object] The object on which the method was
|
161
|
-
# invoked, typically what would be returned by self.
|
162
|
-
# @param ret [Object] The return of the method that was invoked.
|
163
|
-
# @param args [Array<Object>] The arguments passed to the method
|
164
|
-
# being invoked.
|
165
|
-
# @param block [Proc] The block passed to the method that was
|
166
|
-
# invoked.
|
167
|
-
def apply_assess method_policy, preshift, object, ret, args, block
|
168
|
-
source_ret = nil
|
169
|
-
propagated_ret = nil
|
170
|
-
return ret unless method_policy && ::Contrast::ASSESS.enabled?
|
171
|
-
|
172
|
-
current_context = Contrast::Agent::REQUEST_TRACKER.current
|
173
|
-
return ret if current_context && !current_context.analyze_request?
|
174
|
-
|
175
|
-
trigger_node = method_policy.trigger_node
|
176
|
-
if trigger_node
|
177
|
-
Contrast::Agent::Assess::Policy::TriggerMethod.apply_trigger_rule(trigger_node, object, ret, args)
|
178
|
-
end
|
179
|
-
if method_policy.source_node
|
180
|
-
# If we were given a frozen return, and it was the target of a
|
181
|
-
# source, and we have frozen sources enabled, we'll need to
|
182
|
-
# replace the return. Note, this is not the default case.
|
183
|
-
source_ret = Contrast::Agent::Assess::Policy::SourceMethod.source_patchers(method_policy, object, ret,
|
184
|
-
args)
|
185
|
-
end
|
186
|
-
if method_policy.propagation_node
|
187
|
-
propagated_ret = Contrast::Agent::Assess::Policy::PropagationMethod.apply_propagation(
|
188
|
-
method_policy,
|
189
|
-
preshift,
|
190
|
-
object,
|
191
|
-
source_ret || ret,
|
192
|
-
args,
|
193
|
-
block)
|
194
|
-
end
|
195
|
-
handle_return(propagated_ret, source_ret, ret)
|
196
|
-
rescue StandardError => e
|
197
|
-
logger.error('Unable to assess method call.', e)
|
198
|
-
handle_return(propagated_ret, source_ret, ret)
|
199
|
-
rescue Exception => e # rubocop:disable Lint/RescueException
|
200
|
-
logger.error('Unable to assess method call.', e)
|
201
|
-
handle_return(propagated_ret, source_ret, ret)
|
202
|
-
raise e
|
203
|
-
end
|
204
|
-
|
205
|
-
# Generic invocation of the Inventory or Protect patch which apply
|
206
|
-
# to the given method.
|
207
|
-
#
|
208
|
-
# @param trigger_node [Contrast::Agent::Inventory::Policy::TriggerNode]
|
209
|
-
# Mapping of the specific trigger on the given method.
|
210
|
-
# @param method [Symbol] The method into which we're patching
|
211
|
-
# @param exception [StandardError] Any exception raised during the
|
212
|
-
# call of the patched method.
|
213
|
-
# @param object [Object] The object on which the method is invoked,
|
214
|
-
# typically what would be returned by self.
|
215
|
-
# @param args [Array<Object>] The arguments passed to the method
|
216
|
-
# being invoked.
|
217
|
-
def apply_trigger_only trigger_node, method, exception, object, args
|
218
|
-
return unless trigger_node
|
219
|
-
|
220
|
-
# If that rule only applies in the case of an exception being
|
221
|
-
# thrown and there's no exception here, move along, or vice versa
|
222
|
-
return if trigger_node.on_exception && !exception
|
223
|
-
return if !trigger_node.on_exception && exception
|
224
|
-
|
225
|
-
# Each patch has an applicator that handles logic for it. Think
|
226
|
-
# of this as being similar to propagator actions, most closely
|
227
|
-
# resembling CUSTOM - they all have a common interface but their
|
228
|
-
# own logic based on what's in the method(s) they've been patched
|
229
|
-
# into.
|
230
|
-
# Each patch also knows the method of its applicator. Some
|
231
|
-
# things, like AppliesXxeRule, have different methods depending
|
232
|
-
# on the library patched. This lets us handle the boilerplate of
|
233
|
-
# patching while still allowing for custom handling of the
|
234
|
-
# methods.
|
235
|
-
applicator_method = trigger_node.applicator_method
|
236
|
-
# By calling send like this, we can reuse all the patching.
|
237
|
-
# We `send` to the given method of the given class
|
238
|
-
# (applicator) since they all accept the same inputs
|
239
|
-
trigger_node.applicator.send(applicator_method, method, exception, trigger_node.properties, object, args)
|
240
|
-
end
|
241
|
-
|
242
|
-
# Method to choose which replaced return from the post_patch to
|
243
|
-
# actually return
|
244
|
-
#
|
245
|
-
# @param propagated_ret [Object, nil] The replaced return from the
|
246
|
-
# propagation patch.
|
247
|
-
# @param source_ret [Object, nil] The replaced return from the
|
248
|
-
# source patch.
|
249
|
-
# @param ret [Object, nil] The original return of the patched
|
250
|
-
# method.
|
251
|
-
# @return [Object, nil] The thing to return from the post patch.
|
252
|
-
def handle_return propagated_ret, source_ret, ret
|
253
|
-
safe_return = propagated_ret || source_ret || ret
|
254
|
-
safe_return.rewind if Contrast::Utils::IOUtil.should_rewind?(safe_return)
|
255
|
-
safe_return
|
256
|
-
end
|
257
|
-
|
258
|
-
# Given a module and method, construct an expected name for the
|
259
|
-
# alias by which Contrast will reference the original.
|
260
|
-
#
|
261
|
-
# @param patched_class [Module] the module being patched
|
262
|
-
# @param patched_method [Symbol] the method being patched
|
263
|
-
# @return [Symbol]
|
264
|
-
def build_method_name patched_class, patched_method
|
265
|
-
(Contrast::Utils::ObjectShare::CONTRAST_PATCHED_METHOD_START +
|
266
|
-
patched_class.cs__name.gsub('::', '_').downcase +
|
267
|
-
Contrast::Utils::ObjectShare::UNDERSCORE +
|
268
|
-
patched_method.to_s).to_sym
|
269
|
-
end
|
270
|
-
|
271
|
-
# Given a method, return a symbol in the format
|
272
|
-
# <method_start>_unbound_<method_name>
|
273
|
-
def build_unbound_method_name patcher_method
|
274
|
-
(Contrast::Utils::ObjectShare::CONTRAST_PATCHED_METHOD_START +
|
275
|
-
'unbound' +
|
276
|
-
Contrast::Utils::ObjectShare::UNDERSCORE +
|
277
|
-
patcher_method.to_s).to_sym
|
278
|
-
end
|
279
|
-
|
280
63
|
# @param mod [Module] the module in which the patch should be
|
281
64
|
# placed.
|
282
65
|
# @param methods [Array(Symbol)] all the instance or singleton
|
@@ -337,7 +120,7 @@ module Contrast
|
|
337
120
|
# :prepend -> prepend instance method of module
|
338
121
|
# [prepending singleton is easily supported too, just not implemented yet.]
|
339
122
|
# @return [Symbol] new alias for the underlying method (presumably, so the patched method can call it)
|
340
|
-
def register_c_patch target_module_name, unbound_method, impl = :alias_instance
|
123
|
+
def register_c_patch target_module_name, unbound_method, impl = :alias_instance
|
341
124
|
# These could be set as AfterLoadPatches.
|
342
125
|
method_name = unbound_method.name.to_sym # rubocop:disable Security/Module/Name -- ruby built in attribute.
|
343
126
|
underlying_method_name = build_unbound_method_name(method_name).to_sym
|
@@ -360,6 +143,30 @@ module Contrast
|
|
360
143
|
ERR
|
361
144
|
end
|
362
145
|
|
146
|
+
reflect_implementation impl, target_module, unbound_method, visibility
|
147
|
+
# Ougai::Logger.create_item_with_2args calls Hash#[]=, so we
|
148
|
+
# can't invoke this logging method or we'll seg fault as we'd
|
149
|
+
# change the method definition mid-call
|
150
|
+
# if method_name != :[]= &&
|
151
|
+
# Contrast::Agent::Logger.defined!
|
152
|
+
# logger.trace(
|
153
|
+
# 'Registered C-defined patch',
|
154
|
+
# implementation: impl,
|
155
|
+
# module: target_mod,
|
156
|
+
# method: method_name,
|
157
|
+
# visibility: visibility)
|
158
|
+
# end
|
159
|
+
underlying_method_name
|
160
|
+
end
|
161
|
+
|
162
|
+
# @param impl [Symbol] Strategy for applying the patch: { :alias_instance, :alias_singleton, or :prepend }:
|
163
|
+
# @param target_module [Module] The targeted module
|
164
|
+
# @param unbound_method [UnboundMethod] An unbound method, to be patched into target_module.
|
165
|
+
# @param visibility [Symbol] method visibility
|
166
|
+
def reflect_implementation impl, target_module, unbound_method, visibility
|
167
|
+
method_name = unbound_method.name.to_sym # rubocop:disable Security/Module/Name -- ruby built in attribute.
|
168
|
+
underlying_method_name = build_unbound_method_name(method_name).to_sym
|
169
|
+
|
363
170
|
case impl
|
364
171
|
when :alias_instance, :alias_singleton
|
365
172
|
# Core to patching. Ignore define method usage cop.
|
@@ -383,20 +190,6 @@ module Contrast
|
|
383
190
|
target_module.prepend prepending_module
|
384
191
|
# rubocop:enable Performance/Kernel/DefineMethod
|
385
192
|
end
|
386
|
-
|
387
|
-
# Ougai::Logger.create_item_with_2args calls Hash#[]=, so we
|
388
|
-
# can't invoke this logging method or we'll seg fault as we'd
|
389
|
-
# change the method definition mid-call
|
390
|
-
# if method_name != :[]= &&
|
391
|
-
# Contrast::Agent::Logger.defined!
|
392
|
-
# logger.trace(
|
393
|
-
# 'Registered C-defined patch',
|
394
|
-
# implementation: impl,
|
395
|
-
# module: target_module_name,
|
396
|
-
# method: method_name,
|
397
|
-
# visibility: visibility)
|
398
|
-
# end
|
399
|
-
underlying_method_name
|
400
193
|
end
|
401
194
|
|
402
195
|
# @return [Boolean]
|
@@ -10,6 +10,7 @@ require 'contrast/agent/patching/policy/module_policy'
|
|
10
10
|
require 'contrast/components/logger'
|
11
11
|
require 'contrast/components/scope'
|
12
12
|
require 'contrast/utils/class_util'
|
13
|
+
require 'contrast/utils/patching/policy/patcher_utils'
|
13
14
|
|
14
15
|
# assess
|
15
16
|
require 'contrast/agent/assess/policy/policy'
|
@@ -44,6 +45,7 @@ module Contrast
|
|
44
45
|
# and how.
|
45
46
|
module Patcher
|
46
47
|
extend Contrast::Agent::Patching::Policy::AfterLoadPatcher
|
48
|
+
extend Contrast::Utils::Patching::PatcherUtils
|
47
49
|
extend Contrast::Components::Logger::InstanceMethods
|
48
50
|
extend Contrast::Components::Scope::InstanceMethods
|
49
51
|
|
@@ -77,47 +79,6 @@ module Contrast
|
|
77
79
|
end
|
78
80
|
end
|
79
81
|
|
80
|
-
# This method is called by TracePointHook to instrument a specific class during a require
|
81
|
-
# or eval of dynamic class definition
|
82
|
-
def patch_specific_module mod
|
83
|
-
with_contrast_scope do
|
84
|
-
mod_name = mod.cs__name
|
85
|
-
return unless Contrast::Utils::ClassUtil.truly_defined?(mod_name)
|
86
|
-
return if ::Contrast::AGENT.skip_instrumentation?(mod_name)
|
87
|
-
|
88
|
-
load_patches_for_module(mod_name)
|
89
|
-
|
90
|
-
return if all_module_names.none?(mod_name)
|
91
|
-
|
92
|
-
module_data = Contrast::Agent::ModuleData.new(mod, mod_name)
|
93
|
-
patch_into_module(module_data)
|
94
|
-
all_module_names.delete(mod_name) if status_type.get_status(mod).patched?
|
95
|
-
rescue StandardError => e
|
96
|
-
logger.error('Unable to patch module', e, module: mod_name)
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
# We did it, team. We found a patcher(s) that applies to the given
|
101
|
-
# class (or module) and the given method. Time to do some tracking.
|
102
|
-
#
|
103
|
-
# @param mod [Module] the module in which the patch should be
|
104
|
-
# placed.
|
105
|
-
# @param methods [Array(Symbol)] all the instance or singleton
|
106
|
-
# methods in this mod.
|
107
|
-
# @param method_policy [Contrast::Agent::Patching::Policy::MethodPolicy]
|
108
|
-
# the policy that applies to the given method_name.
|
109
|
-
# @return [Boolean] if patched, either by this invocation or a
|
110
|
-
# previous, or not
|
111
|
-
def patch_method mod, methods, method_policy
|
112
|
-
return false unless methods&.any? # don't even build the name if there are no methods
|
113
|
-
|
114
|
-
if Contrast::Utils::ClassUtil.prepended_method?(mod, method_policy)
|
115
|
-
Contrast::Agent::Patching::Policy::Patch.instrument_with_prepend(mod, method_policy)
|
116
|
-
else
|
117
|
-
Contrast::Agent::Patching::Policy::Patch.instrument_with_alias(mod, methods, method_policy)
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
82
|
private
|
122
83
|
|
123
84
|
POLICIES = [
|
@@ -190,6 +151,18 @@ module Contrast
|
|
190
151
|
return
|
191
152
|
end
|
192
153
|
|
154
|
+
patch_methods status, module_data, module_policy
|
155
|
+
rescue StandardError => e
|
156
|
+
status&.failed_patch!
|
157
|
+
logger.warn('Patching failed', e, module: module_data.mod_name)
|
158
|
+
ensure
|
159
|
+
logger.trace('Patching complete',
|
160
|
+
module: module_data.mod_name,
|
161
|
+
result:
|
162
|
+
Contrast::Agent::Patching::Policy::PatchStatus.get_status(module_data.mod).patch_status)
|
163
|
+
end
|
164
|
+
|
165
|
+
def patch_methods status, module_data, module_policy
|
193
166
|
status.patching!
|
194
167
|
num_applied_patches = patch_into_instance_methods(module_data, module_policy)
|
195
168
|
num_applied_patches += patch_into_singleton_methods(module_data, module_policy)
|
@@ -199,14 +172,6 @@ module Contrast
|
|
199
172
|
else
|
200
173
|
status.partial_patch!
|
201
174
|
end
|
202
|
-
rescue StandardError => e
|
203
|
-
status&.failed_patch!
|
204
|
-
logger.warn('Patching failed', e, module: module_data.mod_name)
|
205
|
-
ensure
|
206
|
-
logger.trace('Patching complete',
|
207
|
-
module: module_data.mod_name,
|
208
|
-
result:
|
209
|
-
Contrast::Agent::Patching::Policy::PatchStatus.get_status(module_data.mod).patch_status)
|
210
175
|
end
|
211
176
|
|
212
177
|
# Get all of the instance methods on the given module, excluding
|
@@ -0,0 +1,21 @@
|
|
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 is the base module for our reporting functionality. It is intended to
|
7
|
+
# facilitate all the needed report generating functionality and eventual
|
8
|
+
# report to RS.
|
9
|
+
# Any class under this namespace should be required here, providing a
|
10
|
+
# single point of require for this functionality.
|
11
|
+
module Reporting
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
require 'contrast/agent/reporting/reporter'
|
17
|
+
require 'contrast/agent/reporting/reporting_utilities/audit'
|
18
|
+
require 'contrast/agent/reporting/reporting_utilities/reporter_client'
|
19
|
+
require 'contrast/agent/reporting/reporting_events/finding'
|
20
|
+
require 'contrast/agent/reporting/reporting_events/preflight_message'
|
21
|
+
require 'contrast/agent/reporting/reporting_events/preflight'
|
@@ -0,0 +1,142 @@
|
|
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/agent/worker_thread'
|
5
|
+
require 'contrast/agent/reporting/report'
|
6
|
+
require 'contrast/components/logger'
|
7
|
+
require 'contrast/utils/object_share'
|
8
|
+
require 'contrast/agent/version'
|
9
|
+
require 'base64'
|
10
|
+
|
11
|
+
module Contrast
|
12
|
+
module Agent
|
13
|
+
# This module will hold everything essential to reporting to TeamServer
|
14
|
+
class Reporter < WorkerThread
|
15
|
+
include Contrast::Components::Logger::InstanceMethods
|
16
|
+
include Contrast::Utils::ObjectShare
|
17
|
+
|
18
|
+
class << self
|
19
|
+
include Contrast::Components::Logger::InstanceMethods
|
20
|
+
|
21
|
+
# check if we can report to TS
|
22
|
+
#
|
23
|
+
# @return[Boolean] true if bypass is enabled, or false if bypass disabled
|
24
|
+
def enabled?
|
25
|
+
@_enabled = Contrast::CONTRAST_SERVICE.use_agent_communication? if @_enabled.nil?
|
26
|
+
@_enabled
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def headers
|
31
|
+
@_headers ||= {
|
32
|
+
app_name: Base64.encode64(Contrast::APP_CONTEXT.app_name).chomp!,
|
33
|
+
api_key: Contrast::API.api_key,
|
34
|
+
agent_version: [RUBY, Contrast::Agent::VERSION].join(SPACE),
|
35
|
+
app_language: RUBY,
|
36
|
+
app_path: Base64.encode64(Contrast::APP_CONTEXT.path).chomp!,
|
37
|
+
app_version: Contrast::APP_CONTEXT.app_version,
|
38
|
+
authorization: Base64.encode64("#{ Contrast::API.username }:#{ Contrast::API.service_key }").chomp!,
|
39
|
+
server_name: Base64.encode64(Contrast::APP_CONTEXT.server_name).chomp!,
|
40
|
+
server_path: Base64.encode64(Contrast::APP_CONTEXT.server_path).chomp!,
|
41
|
+
server_type: Base64.encode64(Contrast::APP_CONTEXT.server_type).chomp!,
|
42
|
+
content_type: 'application/json',
|
43
|
+
encoding: 'base64'
|
44
|
+
}.cs__freeze
|
45
|
+
end
|
46
|
+
|
47
|
+
def client
|
48
|
+
@_client ||= Contrast::Utils::ReporterClient.new headers
|
49
|
+
end
|
50
|
+
|
51
|
+
def connection
|
52
|
+
@_connection ||= client.initialize_connection
|
53
|
+
end
|
54
|
+
|
55
|
+
def audit
|
56
|
+
@_audit ||= Contrast::Agent::Reporting::Audit.new
|
57
|
+
end
|
58
|
+
|
59
|
+
def attempt_to_start?
|
60
|
+
unless cs__class.enabled?
|
61
|
+
logger.warn('Reporter service is disabled!')
|
62
|
+
return false
|
63
|
+
end
|
64
|
+
|
65
|
+
logger.debug('Attempting to start Reporter thread') unless running?
|
66
|
+
true
|
67
|
+
end
|
68
|
+
|
69
|
+
def start_thread!
|
70
|
+
return if running?
|
71
|
+
|
72
|
+
@_thread = Contrast::Agent::Thread.new do
|
73
|
+
logger.debug('Starting background Reporter thread.')
|
74
|
+
event = queue.pop
|
75
|
+
|
76
|
+
begin
|
77
|
+
logger.debug('This is the current processed event', event)
|
78
|
+
client.send_event event, connection
|
79
|
+
rescue StandardError => e
|
80
|
+
logger.error('Could not send message to service from Reporter queue.', e)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def send_event event
|
86
|
+
if ::Contrast::AGENT.disabled?
|
87
|
+
logger.warn('Attempted to queue event with Agent disabled', caller: caller, event: event)
|
88
|
+
return
|
89
|
+
end
|
90
|
+
|
91
|
+
return unless cs__class.enabled?
|
92
|
+
|
93
|
+
logger.debug('Enqueued event for sending', event_type: event.cs__class)
|
94
|
+
|
95
|
+
result = queue << event if event
|
96
|
+
return result unless ::Contrast::API.request_audit_enable
|
97
|
+
|
98
|
+
audit&.audit_event(event)
|
99
|
+
result
|
100
|
+
end
|
101
|
+
|
102
|
+
# Use this to bypass the messaging queue and leave response processing to the caller
|
103
|
+
def send_event_immediately event
|
104
|
+
if ::Contrast::AGENT.disabled?
|
105
|
+
logger.warn('Reporter attempted to send event immediately with Agent disabled', caller: caller, event: event)
|
106
|
+
return
|
107
|
+
end
|
108
|
+
response_data = client.send_event event, connection, true
|
109
|
+
if response_data.status == 200
|
110
|
+
# handle_response
|
111
|
+
client.send(:handle_response, event, response_data, connection)
|
112
|
+
end
|
113
|
+
|
114
|
+
return unless ::Contrast::API.request_audit_enable
|
115
|
+
|
116
|
+
audit&.audit_event(event, response_data)
|
117
|
+
response_data
|
118
|
+
rescue StandardError => e
|
119
|
+
logger.error('Could not send message to service from Reporter queue.', e)
|
120
|
+
end
|
121
|
+
|
122
|
+
def delete_queue!
|
123
|
+
@_queue&.clear
|
124
|
+
@_queue&.close
|
125
|
+
@_queue = nil
|
126
|
+
end
|
127
|
+
|
128
|
+
def stop!
|
129
|
+
return unless running?
|
130
|
+
|
131
|
+
super
|
132
|
+
delete_queue!
|
133
|
+
end
|
134
|
+
|
135
|
+
private
|
136
|
+
|
137
|
+
def queue
|
138
|
+
@_queue ||= Queue.new
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
@@ -0,0 +1,90 @@
|
|
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 'json'
|
5
|
+
require 'contrast/components/logger'
|
6
|
+
require 'contrast/agent/reporting/reporting_events/reporting_event'
|
7
|
+
|
8
|
+
module Contrast
|
9
|
+
module Agent
|
10
|
+
module Reporting
|
11
|
+
# This is the new Findings class which will include all the needed information
|
12
|
+
# for the new reporting system
|
13
|
+
class Finding < Contrast::Agent::Reporting::ReportingEvent
|
14
|
+
attr_accessor :events, :properties, :request, :hash_code
|
15
|
+
attr_reader :rule_id
|
16
|
+
|
17
|
+
def initialize rule_id
|
18
|
+
super
|
19
|
+
@event_type = :report_vulnerability
|
20
|
+
@events = []
|
21
|
+
@platform = Contrast::Utils::ObjectShare::RUBY
|
22
|
+
@rule_id = Contrast::Utils::StringUtils.truncate(rule_id)
|
23
|
+
@properties = {}
|
24
|
+
end
|
25
|
+
|
26
|
+
def attach_data trigger_node, source, object, ret, request, *args
|
27
|
+
@events << Contrast::Agent::Assess::Events::EventFactory.build(trigger_node, source, object, ret, args)
|
28
|
+
@request = request
|
29
|
+
from_properties source, @events
|
30
|
+
attach_routes request
|
31
|
+
end
|
32
|
+
|
33
|
+
def to_controlled_hash **_args
|
34
|
+
validate
|
35
|
+
{
|
36
|
+
platform: @platform,
|
37
|
+
ruleId: @rule_id,
|
38
|
+
request: request,
|
39
|
+
properties: properties,
|
40
|
+
events: events,
|
41
|
+
routes: routes
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
def validate
|
46
|
+
raise(ArgumentError, "#{ self } did not have a proper platform. Unable to continue.") unless @platform
|
47
|
+
raise(ArgumentError, "#{ self } did not have a proper rule. Unable to continue.") unless @rule_id
|
48
|
+
raise(ArgumentError, "#{ self } did not have proper events. Unable to continue.") if events.empty?
|
49
|
+
raise(ArgumentError, "#{ self } did not have proper properties. Unable to continue.") if properties.empty?
|
50
|
+
raise(ArgumentError, "#{ self } did not have a proper request. Unable to continue.") unless request
|
51
|
+
|
52
|
+
nil
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def from_properties source, events
|
58
|
+
return unless source
|
59
|
+
return unless Contrast::Agent::Assess::Tracker.trackable?(source)
|
60
|
+
|
61
|
+
properties = Contrast::Agent::Assess::Tracker.properties(source)
|
62
|
+
|
63
|
+
build_events events, properties.event if properties.event
|
64
|
+
@properties = properties if properties
|
65
|
+
end
|
66
|
+
|
67
|
+
def build_events events, event
|
68
|
+
return unless event
|
69
|
+
|
70
|
+
event.parent_events&.each do |parent_event|
|
71
|
+
build_events(events, parent_event)
|
72
|
+
end
|
73
|
+
|
74
|
+
events << event
|
75
|
+
end
|
76
|
+
|
77
|
+
def attach_routes request
|
78
|
+
context = Contrast::Agent::REQUEST_TRACKER.current
|
79
|
+
if context
|
80
|
+
@routes << context.route if context.route
|
81
|
+
elsif request&.route
|
82
|
+
@routes << request.route
|
83
|
+
elsif request&.path
|
84
|
+
@routes << request.path
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,25 @@
|
|
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/agent/reporting/reporting_events/reporting_event'
|
5
|
+
|
6
|
+
module Contrast
|
7
|
+
module Agent
|
8
|
+
module Reporting
|
9
|
+
# This class here will hold the needed preflights we will send for certain trace/traces
|
10
|
+
class Preflight < Contrast::Agent::Reporting::ReportingEvent
|
11
|
+
attr_accessor :messages
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
super
|
15
|
+
@event_type = :preflight
|
16
|
+
@messages = []
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_controlled_hash **_args
|
20
|
+
{ messages: @messages }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|