contrast-agent 4.9.1 → 4.13.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +0 -1
- data/.rspec_parallel +6 -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 +24 -7
- data/ext/cs__common/cs__common.h +12 -2
- data/ext/cs__contrast_patch/cs__contrast_patch.c +48 -12
- data/ext/cs__contrast_patch/cs__contrast_patch.h +5 -4
- 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__protect_kernel → cs__os_information}/extconf.rb +0 -0
- data/lib/contrast/agent/assess/contrast_event.rb +1 -2
- data/lib/contrast/agent/assess/contrast_object.rb +1 -4
- data/lib/contrast/agent/assess/finalizers/hash.rb +0 -1
- data/lib/contrast/agent/assess/policy/dynamic_source_factory.rb +2 -0
- data/lib/contrast/agent/assess/policy/patcher.rb +0 -1
- data/lib/contrast/agent/assess/policy/policy_scanner.rb +0 -2
- data/lib/contrast/agent/assess/policy/preshift.rb +29 -12
- data/lib/contrast/agent/assess/policy/propagation_method.rb +71 -142
- data/lib/contrast/agent/assess/policy/propagation_node.rb +4 -4
- data/lib/contrast/agent/assess/policy/propagator/database_write.rb +2 -2
- data/lib/contrast/agent/assess/policy/propagator/match_data.rb +31 -11
- data/lib/contrast/agent/assess/policy/propagator/remove.rb +4 -9
- data/lib/contrast/agent/assess/policy/propagator/split.rb +3 -2
- data/lib/contrast/agent/assess/policy/propagator/substitution.rb +1 -0
- data/lib/contrast/agent/assess/policy/rewriter_patch.rb +0 -1
- data/lib/contrast/agent/assess/policy/source_method.rb +15 -88
- data/lib/contrast/agent/assess/policy/trigger/xpath.rb +0 -1
- data/lib/contrast/agent/assess/policy/trigger_method.rb +45 -172
- data/lib/contrast/agent/assess/policy/trigger_node.rb +52 -19
- data/lib/contrast/agent/assess/property/evented.rb +2 -1
- data/lib/contrast/agent/assess/property/tagged.rb +15 -132
- data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +0 -1
- data/lib/contrast/agent/deadzone/policy/policy.rb +6 -0
- data/lib/contrast/agent/disable_reaction.rb +1 -1
- data/lib/contrast/agent/exclusion_matcher.rb +0 -4
- data/lib/contrast/agent/inventory/database_config.rb +117 -0
- data/lib/contrast/agent/inventory/dependency_usage_analysis.rb +7 -5
- data/lib/contrast/agent/inventory/policy/datastores.rb +2 -2
- data/lib/contrast/agent/metric_telemetry_event.rb +26 -0
- data/lib/contrast/agent/middleware.rb +23 -0
- data/lib/contrast/agent/patching/policy/after_load_patch.rb +3 -0
- data/lib/contrast/agent/patching/policy/after_load_patcher.rb +17 -12
- data/lib/contrast/agent/patching/policy/method_policy.rb +54 -9
- data/lib/contrast/agent/patching/policy/module_policy.rb +2 -4
- data/lib/contrast/agent/patching/policy/patch.rb +42 -238
- data/lib/contrast/agent/patching/policy/patch_status.rb +3 -7
- data/lib/contrast/agent/patching/policy/patcher.rb +10 -49
- data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +1 -1
- data/lib/contrast/agent/protect/rule/no_sqli.rb +7 -53
- data/lib/contrast/agent/protect/rule/sql_sample_builder.rb +137 -0
- data/lib/contrast/agent/protect/rule/sqli.rb +7 -70
- data/lib/contrast/agent/reaction_processor.rb +1 -1
- data/lib/contrast/agent/request.rb +9 -4
- data/lib/contrast/agent/request_context.rb +51 -33
- data/lib/contrast/agent/request_handler.rb +7 -3
- data/lib/contrast/agent/rule_set.rb +2 -4
- data/lib/contrast/agent/scope.rb +32 -20
- data/lib/contrast/agent/startup_metrics_telemetry_event.rb +71 -0
- data/lib/contrast/agent/static_analysis.rb +5 -3
- data/lib/contrast/agent/telemetry.rb +129 -0
- data/lib/contrast/agent/telemetry_event.rb +34 -0
- data/lib/contrast/agent/thread_watcher.rb +43 -14
- data/lib/contrast/agent/tracepoint_hook.rb +16 -3
- data/lib/contrast/agent/version.rb +1 -1
- data/lib/contrast/agent.rb +6 -1
- data/lib/contrast/api/communication/messaging_queue.rb +12 -6
- data/lib/contrast/api/communication/service_lifecycle.rb +4 -1
- data/lib/contrast/api/communication/socket_client.rb +4 -4
- data/lib/contrast/api/decorators/agent_startup.rb +4 -4
- data/lib/contrast/api/decorators/application_startup.rb +6 -5
- data/lib/contrast/api/decorators/route_coverage.rb +24 -1
- data/lib/contrast/components/agent.rb +5 -2
- data/lib/contrast/components/api.rb +34 -0
- data/lib/contrast/components/app_context.rb +24 -0
- data/lib/contrast/components/assess.rb +13 -3
- data/lib/contrast/components/base.rb +2 -2
- data/lib/contrast/components/config.rb +91 -11
- data/lib/contrast/components/contrast_service.rb +10 -2
- data/lib/contrast/components/logger.rb +13 -8
- data/lib/contrast/components/scope.rb +9 -28
- data/lib/contrast/config/api_configuration.rb +22 -0
- data/lib/contrast/config/assess_configuration.rb +1 -0
- data/lib/contrast/config/base_configuration.rb +14 -6
- data/lib/contrast/config/env_variables.rb +25 -0
- data/lib/contrast/config/root_configuration.rb +1 -0
- data/lib/contrast/config/service_configuration.rb +2 -1
- data/lib/contrast/config.rb +1 -0
- data/lib/contrast/configuration.rb +22 -15
- data/lib/contrast/extension/assess/array.rb +1 -11
- data/lib/contrast/extension/assess/eval_trigger.rb +0 -20
- data/lib/contrast/extension/assess/fiber.rb +0 -11
- data/lib/contrast/extension/assess/hash.rb +0 -10
- data/lib/contrast/extension/assess/kernel.rb +1 -10
- data/lib/contrast/extension/assess/marshal.rb +3 -11
- data/lib/contrast/extension/assess/regexp.rb +0 -11
- data/lib/contrast/extension/assess/string.rb +1 -26
- data/lib/contrast/extension/extension.rb +61 -0
- data/lib/contrast/framework/grape/support.rb +174 -0
- data/lib/contrast/framework/manager.rb +56 -18
- data/lib/contrast/framework/rack/support.rb +1 -1
- data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +9 -6
- data/lib/contrast/framework/rails/patch/assess_configuration.rb +0 -1
- data/lib/contrast/framework/rails/patch/support.rb +35 -30
- data/lib/contrast/framework/rails/railtie.rb +1 -1
- data/lib/contrast/framework/rails/rewrite/active_record_named.rb +1 -0
- data/lib/contrast/framework/rails/support.rb +60 -13
- data/lib/contrast/framework/sinatra/support.rb +1 -1
- data/lib/contrast/logger/application.rb +4 -0
- data/lib/contrast/logger/log.rb +89 -15
- data/lib/contrast/utils/assess/propagation_method_utils.rb +129 -0
- data/lib/contrast/utils/assess/property/tagged_utils.rb +142 -0
- data/lib/contrast/utils/assess/source_method_utils.rb +83 -0
- data/lib/contrast/utils/assess/trigger_method_utils.rb +138 -0
- data/lib/contrast/utils/class_util.rb +58 -44
- data/lib/contrast/utils/exclude_key.rb +20 -0
- data/lib/contrast/utils/io_util.rb +43 -35
- data/lib/contrast/utils/lru_cache.rb +45 -0
- data/lib/contrast/utils/metrics_hash.rb +59 -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/requests_client.rb +150 -0
- data/lib/contrast/utils/ruby_ast_rewriter.rb +16 -13
- data/lib/contrast/utils/tag_util.rb +2 -1
- data/lib/contrast/utils/telemetry.rb +78 -0
- data/lib/contrast/utils/telemetry_identifier.rb +137 -0
- data/lib/contrast.rb +19 -1
- data/resources/assess/policy.json +208 -7
- data/resources/deadzone/policy.json +91 -0
- data/ruby-agent.gemspec +12 -2
- data/service_executables/VERSION +1 -1
- data/service_executables/linux/contrast-service +0 -0
- data/service_executables/mac/contrast-service +0 -0
- metadata +102 -18
- data/ext/cs__protect_kernel/cs__protect_kernel.c +0 -47
- data/ext/cs__protect_kernel/cs__protect_kernel.h +0 -12
- data/lib/contrast/extension/protect/kernel.rb +0 -39
- data/lib/contrast/utils/inventory_util.rb +0 -113
@@ -0,0 +1,232 @@
|
|
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 Utils
|
6
|
+
module Patching
|
7
|
+
# This module will include all methods for different patch applies from Patch module
|
8
|
+
# and some other module methods from the same place, so we can ease the main module
|
9
|
+
module PatchUtils
|
10
|
+
# Method to choose which replaced return from the post_patch to
|
11
|
+
# actually return
|
12
|
+
#
|
13
|
+
# @param propagated_ret [Object, nil] The replaced return from the
|
14
|
+
# propagation patch.
|
15
|
+
# @param source_ret [Object, nil] The replaced return from the
|
16
|
+
# source patch.
|
17
|
+
# @param ret [Object, nil] The original return of the patched
|
18
|
+
# method.
|
19
|
+
# @return [Object, nil] The thing to return from the post patch.
|
20
|
+
def handle_return propagated_ret, source_ret, ret
|
21
|
+
safe_return = propagated_ret || source_ret || ret
|
22
|
+
safe_return.rewind if Contrast::Utils::IOUtil.should_rewind?(safe_return)
|
23
|
+
safe_return
|
24
|
+
end
|
25
|
+
|
26
|
+
# Given a module and method, construct an expected name for the
|
27
|
+
# alias by which Contrast will reference the original.
|
28
|
+
#
|
29
|
+
# @param patched_class [Module] the module being patched
|
30
|
+
# @param patched_method [Symbol] the method being patched
|
31
|
+
# @return [Symbol]
|
32
|
+
def build_method_name patched_class, patched_method
|
33
|
+
(Contrast::Utils::ObjectShare::CONTRAST_PATCHED_METHOD_START +
|
34
|
+
patched_class.cs__name.gsub('::', '_').downcase +
|
35
|
+
Contrast::Utils::ObjectShare::UNDERSCORE +
|
36
|
+
patched_method.to_s).to_sym
|
37
|
+
end
|
38
|
+
|
39
|
+
# Given a method, return a symbol in the format
|
40
|
+
# <method_start>_unbound_<method_name>
|
41
|
+
def build_unbound_method_name patcher_method
|
42
|
+
(Contrast::Utils::ObjectShare::CONTRAST_PATCHED_METHOD_START +
|
43
|
+
'unbound' +
|
44
|
+
Contrast::Utils::ObjectShare::UNDERSCORE +
|
45
|
+
patcher_method.to_s).to_sym
|
46
|
+
end
|
47
|
+
|
48
|
+
# ===== PATCH APPLIERS =====
|
49
|
+
# THIS IS CALLED FROM C. Do not change the signature lightly.
|
50
|
+
#
|
51
|
+
# This method functions to call the infilter methods from our
|
52
|
+
# patches, allowing for analysis and reporting at the point just
|
53
|
+
# before the patched code is invoked.
|
54
|
+
#
|
55
|
+
# @param method_policy [Contrast::Agent::Patching::Policy::MethodPolicy]
|
56
|
+
# Mapping of the triggers on the given method.
|
57
|
+
# @param method [Symbol] The method into which we're patching
|
58
|
+
# @param exception [StandardError] Any exception raised during the
|
59
|
+
# call of the patched method.
|
60
|
+
# @param object [Object] The object on which the method is invoked,
|
61
|
+
# typically what would be returned by self.
|
62
|
+
# @param args [Array<Object>] The arguments passed to the method
|
63
|
+
# being invoked.
|
64
|
+
def apply_pre_patch method_policy, method, exception, object, args
|
65
|
+
apply_protect(method_policy, method, exception, object, args)
|
66
|
+
apply_inventory(method_policy, method, exception, object, args)
|
67
|
+
rescue Contrast::SecurityException => e
|
68
|
+
# We were told to block something, so we gotta. Don't catch this
|
69
|
+
# one, let it get back to our Middleware or even all the way out to
|
70
|
+
# the framework
|
71
|
+
raise e
|
72
|
+
rescue StandardError => e
|
73
|
+
# Anything else was our bad and we gotta catch that to allow for
|
74
|
+
# normal application flow
|
75
|
+
logger.error('Unable to apply pre patch to method.', e)
|
76
|
+
rescue Exception => e # rubocop:disable Lint/RescueException
|
77
|
+
# This is something like NoMemoryError that we can't
|
78
|
+
# hope to handle. Nonetheless, shouldn't leak scope.
|
79
|
+
exit_contrast_scope!
|
80
|
+
raise e
|
81
|
+
end
|
82
|
+
|
83
|
+
# THIS IS CALLED FROM C. Do not change the signature lightly.
|
84
|
+
#
|
85
|
+
# This method functions to call the infilter methods from our
|
86
|
+
# patches, allowing for analysis and reporting at the point just
|
87
|
+
# after the patched code is invoked
|
88
|
+
#
|
89
|
+
# @param method_policy [Contrast::Agent::Patching::Policy::MethodPolicy]
|
90
|
+
# Mapping of the triggers on the given method.
|
91
|
+
# @param preshift [Contrast::Agent::Assess::PreShift] The capture
|
92
|
+
# of the state of the code just prior to the invocation of the
|
93
|
+
# patched method.
|
94
|
+
# @param object [Object] The object on which the method was
|
95
|
+
# invoked, typically what would be returned by self.
|
96
|
+
# @param ret [Object] The return of the method that was invoked.
|
97
|
+
# @param args [Array<Object>] The arguments passed to the method
|
98
|
+
# being invoked.
|
99
|
+
# @param block [Proc] The block passed to the method that was
|
100
|
+
# invoked.
|
101
|
+
def apply_post_patch method_policy, preshift, object, ret, args, block
|
102
|
+
apply_assess(method_policy, preshift, object, ret, args, block)
|
103
|
+
rescue StandardError => e
|
104
|
+
logger.error('Unable to apply post patch to method.', e)
|
105
|
+
end
|
106
|
+
|
107
|
+
# Apply the Protect patch which applies to the given method.
|
108
|
+
#
|
109
|
+
# @param method_policy [Contrast::Agent::Patching::Policy::MethodPolicy]
|
110
|
+
# Mapping of the triggers on the given method.
|
111
|
+
# @param method [Symbol] The method into which we're patching
|
112
|
+
# @param exception [StandardError] Any exception raised during the
|
113
|
+
# call of the patched method.
|
114
|
+
# @param object [Object] The object on which the method is invoked,
|
115
|
+
# typically what would be returned by self.
|
116
|
+
# @param args [Array<Object>] The arguments passed to the method
|
117
|
+
# being invoked.
|
118
|
+
def apply_protect method_policy, method, exception, object, args
|
119
|
+
return unless ::Contrast::AGENT.enabled?
|
120
|
+
return unless ::Contrast::PROTECT.enabled?
|
121
|
+
|
122
|
+
apply_trigger_only(method_policy&.protect_node, method, exception, object, args)
|
123
|
+
end
|
124
|
+
|
125
|
+
# Apply the Inventory patch which applies to the given method.
|
126
|
+
#
|
127
|
+
# @param method_policy [Contrast::Agent::Patching::Policy::MethodPolicy]
|
128
|
+
# Mapping of the triggers on the given method.
|
129
|
+
# @param method [Symbol] The method into which we're patching
|
130
|
+
# @param exception [StandardError] Any exception raised during the
|
131
|
+
# call of the patched method.
|
132
|
+
# @param object [Object] The object on which the method is invoked,
|
133
|
+
# typically what would be returned by self.
|
134
|
+
# @param args [Array<Object>] The arguments passed to the method
|
135
|
+
# being invoked.
|
136
|
+
def apply_inventory method_policy, method, exception, object, args
|
137
|
+
return unless ::Contrast::INVENTORY.enabled?
|
138
|
+
|
139
|
+
apply_trigger_only(method_policy&.inventory_node, method, exception, object, args)
|
140
|
+
end
|
141
|
+
|
142
|
+
# Apply the Assess patches which apply to the given method.
|
143
|
+
#
|
144
|
+
# @param method_policy [Contrast::Agent::Patching::Policy::MethodPolicy]
|
145
|
+
# Mapping of the triggers on the given method.
|
146
|
+
# @param preshift [Contrast::Agent::Assess::PreShift] The capture
|
147
|
+
# of the state of the code just prior to the invocation of the
|
148
|
+
# patched method.
|
149
|
+
# @param object [Object] The object on which the method was
|
150
|
+
# invoked, typically what would be returned by self.
|
151
|
+
# @param ret [Object] The return of the method that was invoked.
|
152
|
+
# @param args [Array<Object>] The arguments passed to the method
|
153
|
+
# being invoked.
|
154
|
+
# @param block [Proc] The block passed to the method that was
|
155
|
+
# invoked.
|
156
|
+
def apply_assess method_policy, preshift, object, ret, args, block
|
157
|
+
source_ret = nil
|
158
|
+
propagated_ret = nil
|
159
|
+
return ret unless method_policy && ::Contrast::ASSESS.enabled?
|
160
|
+
|
161
|
+
current_context = Contrast::Agent::REQUEST_TRACKER.current
|
162
|
+
return ret if current_context && !current_context.analyze_request?
|
163
|
+
|
164
|
+
trigger_node = method_policy.trigger_node
|
165
|
+
if trigger_node
|
166
|
+
Contrast::Agent::Assess::Policy::TriggerMethod.apply_trigger_rule(trigger_node, object, ret, args)
|
167
|
+
end
|
168
|
+
if method_policy.source_node
|
169
|
+
# If we were given a frozen return, and it was the target of a
|
170
|
+
# source, and we have frozen sources enabled, we'll need to
|
171
|
+
# replace the return. Note, this is not the default case.
|
172
|
+
source_ret = Contrast::Agent::Assess::Policy::SourceMethod.source_patchers(method_policy, object, ret, args)
|
173
|
+
end
|
174
|
+
if method_policy.propagation_node
|
175
|
+
propagated_ret = Contrast::Agent::Assess::Policy::PropagationMethod.apply_propagation(
|
176
|
+
method_policy,
|
177
|
+
preshift,
|
178
|
+
object,
|
179
|
+
source_ret || ret,
|
180
|
+
args,
|
181
|
+
block)
|
182
|
+
end
|
183
|
+
handle_return(propagated_ret, source_ret, ret)
|
184
|
+
rescue StandardError => e
|
185
|
+
logger.error('Unable to assess method call.', e)
|
186
|
+
handle_return(propagated_ret, source_ret, ret)
|
187
|
+
rescue Exception => e # rubocop:disable Lint/RescueException
|
188
|
+
logger.error('Unable to assess method call.', e)
|
189
|
+
handle_return(propagated_ret, source_ret, ret)
|
190
|
+
raise e
|
191
|
+
end
|
192
|
+
|
193
|
+
# Generic invocation of the Inventory or Protect patch which apply
|
194
|
+
# to the given method.
|
195
|
+
#
|
196
|
+
# @param trigger_node [Contrast::Agent::Inventory::Policy::TriggerNode]
|
197
|
+
# Mapping of the specific trigger on the given method.
|
198
|
+
# @param method [Symbol] The method into which we're patching
|
199
|
+
# @param exception [StandardError] Any exception raised during the
|
200
|
+
# call of the patched method.
|
201
|
+
# @param object [Object] The object on which the method is invoked,
|
202
|
+
# typically what would be returned by self.
|
203
|
+
# @param args [Array<Object>] The arguments passed to the method
|
204
|
+
# being invoked.
|
205
|
+
def apply_trigger_only trigger_node, method, exception, object, args
|
206
|
+
return unless trigger_node
|
207
|
+
|
208
|
+
# If that rule only applies in the case of an exception being
|
209
|
+
# thrown and there's no exception here, move along, or vice versa
|
210
|
+
return if trigger_node.on_exception && !exception
|
211
|
+
return if !trigger_node.on_exception && exception
|
212
|
+
|
213
|
+
# Each patch has an applicator that handles logic for it. Think
|
214
|
+
# of this as being similar to propagator actions, most closely
|
215
|
+
# resembling CUSTOM - they all have a common interface but their
|
216
|
+
# own logic based on what's in the method(s) they've been patched
|
217
|
+
# into.
|
218
|
+
# Each patch also knows the method of its applicator. Some
|
219
|
+
# things, like AppliesXxeRule, have different methods depending
|
220
|
+
# on the library patched. This lets us handle the boilerplate of
|
221
|
+
# patching while still allowing for custom handling of the
|
222
|
+
# methods.
|
223
|
+
applicator_method = trigger_node.applicator_method
|
224
|
+
# By calling send like this, we can reuse all the patching.
|
225
|
+
# We `send` to the given method of the given class
|
226
|
+
# (applicator) since they all accept the same inputs
|
227
|
+
trigger_node.applicator.send(applicator_method, method, exception, trigger_node.properties, object, args)
|
228
|
+
end
|
229
|
+
end
|
230
|
+
end
|
231
|
+
end
|
232
|
+
end
|
@@ -0,0 +1,54 @@
|
|
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 Utils
|
6
|
+
module Patching
|
7
|
+
# This module will include patch methods from Patcher module
|
8
|
+
# in case of need to add new logic to the Patcher
|
9
|
+
# please do it here
|
10
|
+
module PatcherUtils
|
11
|
+
# This method is called by TracePointHook to instrument a specific class during a require
|
12
|
+
# or eval of dynamic class definition
|
13
|
+
def patch_specific_module mod
|
14
|
+
with_contrast_scope do
|
15
|
+
mod_name = mod.cs__name
|
16
|
+
return unless Contrast::Utils::ClassUtil.truly_defined?(mod_name)
|
17
|
+
return if ::Contrast::AGENT.skip_instrumentation?(mod_name)
|
18
|
+
|
19
|
+
load_patches_for_module(mod_name)
|
20
|
+
|
21
|
+
return if all_module_names.none?(mod_name)
|
22
|
+
|
23
|
+
module_data = Contrast::Agent::ModuleData.new(mod, mod_name)
|
24
|
+
patch_into_module(module_data)
|
25
|
+
all_module_names.delete(mod_name) if status_type.get_status(mod).patched?
|
26
|
+
rescue StandardError => e
|
27
|
+
logger.error('Unable to patch module', e, module: mod_name)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# We did it, team. We found a patcher(s) that applies to the given
|
32
|
+
# class (or module) and the given method. Time to do some tracking.
|
33
|
+
#
|
34
|
+
# @param mod [Module] the module in which the patch should be
|
35
|
+
# placed.
|
36
|
+
# @param methods [Array(Symbol)] all the instance or singleton
|
37
|
+
# methods in this mod.
|
38
|
+
# @param method_policy [Contrast::Agent::Patching::Policy::MethodPolicy]
|
39
|
+
# the policy that applies to the given method_name.
|
40
|
+
# @return [Boolean] if patched, either by this invocation or a
|
41
|
+
# previous, or not
|
42
|
+
def patch_method mod, methods, method_policy
|
43
|
+
return false unless methods&.any? # don't even build the name if there are no methods
|
44
|
+
|
45
|
+
if Contrast::Utils::ClassUtil.prepended_method?(mod, method_policy)
|
46
|
+
Contrast::Agent::Patching::Policy::Patch.instrument_with_prepend(mod, method_policy)
|
47
|
+
else
|
48
|
+
Contrast::Agent::Patching::Policy::Patch.instrument_with_alias(mod, methods, method_policy)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,150 @@
|
|
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 'net/http'
|
5
|
+
require 'contrast/components/logger'
|
6
|
+
require 'contrast/utils/object_share'
|
7
|
+
require 'contrast/agent/version'
|
8
|
+
require 'socket'
|
9
|
+
|
10
|
+
module Contrast
|
11
|
+
module Utils
|
12
|
+
# This module creates a Net::HTTP client and initiates a connection to the provided result
|
13
|
+
module RequestsClient
|
14
|
+
ENDPOINT = 'api/v1/telemetry/metrics' # /TelemetryEvent.path
|
15
|
+
|
16
|
+
class << self
|
17
|
+
include Contrast::Components::Logger::InstanceMethods
|
18
|
+
# This method initializes the Net::HTTP client we'll need
|
19
|
+
# @param url [String]
|
20
|
+
# @return [Net::HTTP, nil]
|
21
|
+
def initialize_connection url
|
22
|
+
addr = URI(url)
|
23
|
+
return if addr.host.nil? || addr.port.nil?
|
24
|
+
return if addr.scheme != 'https'
|
25
|
+
|
26
|
+
@_net_http_client = Net::HTTP.new(addr.host, addr.port)
|
27
|
+
@_net_http_client.open_timeout = 5
|
28
|
+
@_net_http_client.read_timeout = 5
|
29
|
+
@_net_http_client.use_ssl = true
|
30
|
+
@_net_http_client.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
31
|
+
@_net_http_client.verify_depth = 5
|
32
|
+
@_net_http_client.start
|
33
|
+
return unless @_net_http_client.started?
|
34
|
+
|
35
|
+
logger.warn('Starting Telemetry connection test')
|
36
|
+
return unless connection_verified? @_net_http_client
|
37
|
+
|
38
|
+
@_net_http_client
|
39
|
+
rescue Net::OpenTimeout, Net::ReadTimeout => e
|
40
|
+
logger.warn('Telemetry connection failed', e.message)
|
41
|
+
nil
|
42
|
+
end
|
43
|
+
|
44
|
+
# This method will be responsible for building the request
|
45
|
+
# @param event[Contrast::Agent::TelemetryEvent,Contrast::Agent::StartupMetricsTelemetryEvent]
|
46
|
+
# @return [Net::HTTP::Post]
|
47
|
+
def build_request event
|
48
|
+
return unless valid_event? event
|
49
|
+
|
50
|
+
string_body = event.to_json.to_s
|
51
|
+
header = { 'User-Agent' => "<#{ Contrast::Utils::ObjectShare::RUBY }>-<#{ Contrast::Agent::VERSION }>" }
|
52
|
+
path = ENDPOINT + event.path
|
53
|
+
@_request = Net::HTTP::Post.new(path, header)
|
54
|
+
@_request.body = string_body
|
55
|
+
@_request
|
56
|
+
end
|
57
|
+
|
58
|
+
# This method will create the actual request and send it
|
59
|
+
# @param event[Contrast::Agent::TelemetryEvent]
|
60
|
+
# @param connection[Net::HTTP]
|
61
|
+
def send_request event, connection
|
62
|
+
return if connection.nil? || event.nil?
|
63
|
+
return unless valid_event? event
|
64
|
+
|
65
|
+
req = build_request event
|
66
|
+
connection.request req
|
67
|
+
end
|
68
|
+
|
69
|
+
# This method will handle the response from the tenant
|
70
|
+
# @param res [Net::HTTPResponse]
|
71
|
+
# @return sleep_time [Integer, nil]
|
72
|
+
def handle_response res
|
73
|
+
status_code = res.code.to_i
|
74
|
+
ready_after = if res.to_hash.keys.map(&:downcase).include?('ready-after')
|
75
|
+
res['Ready-After']
|
76
|
+
else
|
77
|
+
60
|
78
|
+
end
|
79
|
+
ready_after if status_code == 429
|
80
|
+
end
|
81
|
+
|
82
|
+
# This method will be responsible for validating the event
|
83
|
+
# @param event[Contrast::Agent::TelemetryEvent,Contrast::Agent::StartupMetricsTelemetryEvent]
|
84
|
+
def valid_event? event
|
85
|
+
return false unless event.cs__is_a?(Contrast::Agent::TelemetryEvent)
|
86
|
+
return false unless event.cs__is_a?(Contrast::Agent::StartupMetricsTelemetryEvent)
|
87
|
+
|
88
|
+
true
|
89
|
+
end
|
90
|
+
|
91
|
+
# Validates connection with Telemetry assigned domain.
|
92
|
+
# If connection is running, SSL certificate of the endpoint is valid, Ip address is resolvable
|
93
|
+
# and response is received without peer's reset or refuse of connection,
|
94
|
+
# then validation returns true. Error handling is in place so that the work of the agent will continue as
|
95
|
+
# normal without Telemetry.
|
96
|
+
#
|
97
|
+
# @param client [Net::HTTP]
|
98
|
+
# @return [Boolean] true | false
|
99
|
+
def connection_verified? client
|
100
|
+
return @_connection_verified unless @_connection_verified.nil?
|
101
|
+
|
102
|
+
# Before RUBY 2.7 there is no #ipaddr
|
103
|
+
ipaddr = if RUBY_VERSION < '2.7.0'
|
104
|
+
socket = TCPSocket.open(client.address, client.port)
|
105
|
+
ipaddr = socket.peeraddr[3]
|
106
|
+
socket.close
|
107
|
+
ipaddr
|
108
|
+
else
|
109
|
+
client.ipaddr
|
110
|
+
end
|
111
|
+
response = client.request(Net::HTTP::Get.new(client.address))
|
112
|
+
verify_cert = OpenSSL::SSL.verify_certificate_identity(client.peer_cert, client.address)
|
113
|
+
resolved = resolved? client.address, ipaddr
|
114
|
+
@_connection_verified = if resolved && response && verify_cert
|
115
|
+
true
|
116
|
+
else
|
117
|
+
false
|
118
|
+
end
|
119
|
+
rescue OpenSSL::SSL::SSLError, Resolv::ResolvError, Errno::ECONNRESET, Errno::ECONNREFUSED,
|
120
|
+
Errno::ETIMEDOUT, Errno::ESHUTDOWN, Errno::EHOSTDOWN, Errno::EHOSTUNREACH, Errno::EISCONN,
|
121
|
+
Errno::ECONNABORTED, Errno::ENETRESET, Errno::ENETUNREACH => e
|
122
|
+
|
123
|
+
logger.warn('Telemetry connection failed', e.message)
|
124
|
+
false
|
125
|
+
end
|
126
|
+
|
127
|
+
private
|
128
|
+
|
129
|
+
# Resolves the address of the assigned telemetry domain to array of corresponding IPs (if more than one)
|
130
|
+
# and runs a matcher to see if current connection IP is in the list.
|
131
|
+
# This is called within #verify_connection, if called on it's own there will be no
|
132
|
+
# error handling.
|
133
|
+
#
|
134
|
+
# @param address [String] Human friendly address of assigned telemetry domain
|
135
|
+
# @param ipaddr [String] Machine friendly IP address of the assigned telemetry domain
|
136
|
+
# @return[Boolean] true if both addresses are resolved | false if one of the addresses
|
137
|
+
# is non-resolvable
|
138
|
+
def resolved? address, ipaddr
|
139
|
+
return @_resolved unless @_resolved.nil?
|
140
|
+
|
141
|
+
@_resolved = if (addresses = Resolv.getaddresses address)
|
142
|
+
addresses.any? { |addr| addr.include?(ipaddr) }
|
143
|
+
else
|
144
|
+
false
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
@@ -36,36 +36,39 @@ module Contrast
|
|
36
36
|
# the replace within the given node.
|
37
37
|
def on_dstr node
|
38
38
|
return if node.children.all? { |child_node| child_node.type == :str }
|
39
|
-
|
40
39
|
new_content = +'('
|
41
|
-
|
40
|
+
idx = 0
|
41
|
+
while idx < node.children.length
|
42
|
+
#node.children.each_with_index do |child_node, index|
|
43
|
+
child_node = node.children[idx]
|
42
44
|
# A begin node looks like #{some_code} in ruby, we do a substring
|
43
45
|
# from [2...-1] to get rid of the #{ & trailing }.
|
44
46
|
if child_node.type == :begin
|
45
47
|
code = child_node.
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
48
|
+
location.
|
49
|
+
expression.
|
50
|
+
source_buffer.
|
51
|
+
source[child_node.location.begin.begin_pos...child_node.location.end.end_pos]
|
50
52
|
code = code[2...-1]
|
51
53
|
new_content += "((#{ code }).to_s)"
|
52
54
|
|
53
|
-
|
54
|
-
|
55
|
-
|
55
|
+
# For interpolations that use class, instance, or global variables,
|
56
|
+
# those are NOT within a begin block, but instead are a ivar, cvar,
|
57
|
+
# or gvar node, not stripping of interpolation markers required.
|
56
58
|
elsif VARIABLES.include?(child_node.type)
|
57
59
|
variable = child_node.children.first
|
58
60
|
new_content << "((#{ variable }).to_s)"
|
59
61
|
|
60
|
-
|
61
|
-
|
62
|
-
|
62
|
+
# When interpolation includes strings before or after an
|
63
|
+
# interpolation they are simple :str nodes, in order to preserve
|
64
|
+
# escaping we need to do a dump of the string value.
|
63
65
|
elsif child_node.type == :str
|
64
66
|
literal_value = child_node.children.first
|
65
67
|
literal_value = literal_value.dump[1...-1]
|
66
68
|
new_content << "\"#{ literal_value }\""
|
67
69
|
end
|
68
|
-
new_content << ' + ' unless
|
70
|
+
new_content << ' + ' unless idx == node.children.length - 1
|
71
|
+
idx += 1
|
69
72
|
end
|
70
73
|
new_content << ')'
|
71
74
|
if node.location.cs__respond_to?(:heredoc_body)
|
@@ -8,7 +8,8 @@ module Contrast
|
|
8
8
|
class << self
|
9
9
|
# Determine if the given array of tags is covered by the other
|
10
10
|
#
|
11
|
-
# @param remaining_ranges [Array<Contrast::Agent::Assess::Tag>] the tags left that haven't been covered by
|
11
|
+
# @param remaining_ranges [Array<Contrast::Agent::Assess::Tag>] the tags left that haven't been covered by
|
12
|
+
# those given
|
12
13
|
# @param ranges Array<Contrast::Agent::Assess::Tag> the tags that are covering the first
|
13
14
|
def covered? remaining_ranges, ranges
|
14
15
|
return true unless remaining_ranges&.any?
|
@@ -0,0 +1,78 @@
|
|
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/telemetry'
|
5
|
+
require 'contrast/utils/telemetry_identifier'
|
6
|
+
|
7
|
+
module Contrast
|
8
|
+
module Utils
|
9
|
+
# Tools for supporting the Telemetry feature
|
10
|
+
module Telemetry
|
11
|
+
DIR = '/ect/contrast/ruby-agent/'.cs__freeze
|
12
|
+
FILE = '.telemetry'.cs__freeze
|
13
|
+
CURRENT_DIR = Dir.pwd.cs__freeze
|
14
|
+
CONFIG_DIR = CURRENT_DIR + '/config/contrast/'.cs__freeze
|
15
|
+
MESSAGE = {
|
16
|
+
disclaimer:
|
17
|
+
"\n===================================================================================================" \
|
18
|
+
"\n\nThe [Contrast Security] [Ruby Agent] " \
|
19
|
+
"collects usage data in order to help us improve compatibility\n" \
|
20
|
+
"and security coverage. The data is anonymous and does not contain application data. It is collected\n" \
|
21
|
+
"by Contrast and is never shared. You can opt-out of telemetry by setting the\n" \
|
22
|
+
"'CONTRAST_AGENT_TELEMETRY_OPTOUT' environment variable to '1' or 'true'.\n\n" \
|
23
|
+
"===================================================================================================\n\n"
|
24
|
+
}.cs__freeze
|
25
|
+
|
26
|
+
# Here we create the .telemetry file. If the file exist we do nothing.
|
27
|
+
#
|
28
|
+
# @return[Boolean, nil] true if file is created, false if file already exist
|
29
|
+
# and nil if Telemetry is disabled or on unsupported OS.
|
30
|
+
def self.create_telemetry_file
|
31
|
+
write_mark_file DIR, FILE, CONFIG_DIR
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.disclaimer
|
35
|
+
@_disclaimer = MESSAGE[:disclaimer]
|
36
|
+
end
|
37
|
+
|
38
|
+
class << self
|
39
|
+
private
|
40
|
+
|
41
|
+
# Create the mark file
|
42
|
+
#
|
43
|
+
# @param dir [String] Directory in which the file is to be created
|
44
|
+
# @param file [String] filename of the mark file
|
45
|
+
# @param config_dir [String] path for the config folder in working directory
|
46
|
+
# @return[Boolean, nil] true if file is created, false if file already exist
|
47
|
+
# and nil if Telemetry is disabled or on unsupported OS.
|
48
|
+
def write_mark_file dir, file, config_dir
|
49
|
+
return unless Contrast::Agent::Telemetry.enabled?
|
50
|
+
return if Contrast::Utils::OS.windows?
|
51
|
+
|
52
|
+
@dir = dir
|
53
|
+
# After macOS Catalina, you can no longer store files or data in the read-only system volume,
|
54
|
+
# nor can we write to the "root" directory ( / ). This results in Errno::EROFS exception.
|
55
|
+
# So for the MacOS we would use the config directory of the instrumented application.
|
56
|
+
@dir = config_dir if Contrast::Utils::OS.mac?
|
57
|
+
return false if File.file? @dir + file
|
58
|
+
|
59
|
+
begin
|
60
|
+
return true if touch @dir, file # rubocop:disable Rails/SkipsModelValidations
|
61
|
+
rescue Errno::EROFS
|
62
|
+
# If we don't have permission to write to root directory use config instead
|
63
|
+
return true if touch config_dir, file # rubocop:disable Rails/SkipsModelValidations
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# Touches .telemetry file
|
68
|
+
#
|
69
|
+
# @return[Boolean] true if success, false if fails
|
70
|
+
def touch dir, file
|
71
|
+
FileUtils.mkdir_p dir unless Dir.exist? dir
|
72
|
+
FileUtils.touch dir + file
|
73
|
+
File.file? dir + file
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|