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
@@ -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,88 @@
|
|
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
|
+
# used in Contrast::Agent::Request
|
7
|
+
module RequestUtils
|
8
|
+
# Return a flattened hash of params with realized paths for keys, in
|
9
|
+
# addition to a separate, valueless, entry for each nest key.
|
10
|
+
# See RUBY-621 for more details.
|
11
|
+
# { key : { nested_key : ['x','y','z' ] } }
|
12
|
+
# becomes
|
13
|
+
# {
|
14
|
+
# key[nested_key][0] : 'x'
|
15
|
+
# key[nested_key][1] : 'y'
|
16
|
+
# key[nested_key][2] : 'z'
|
17
|
+
# key : ''
|
18
|
+
# nested_key : ''
|
19
|
+
# }
|
20
|
+
def normalize_params val, prefix: nil
|
21
|
+
# In non-recursive invocations, val should always be a Hash
|
22
|
+
# (rather than breaking this out into two methods)
|
23
|
+
case val
|
24
|
+
when Tempfile
|
25
|
+
# Skip if it's the auto-generated value from rails when it handles
|
26
|
+
# file uploads. The file name will still be sent to SR for analysis.
|
27
|
+
{}
|
28
|
+
when Hash
|
29
|
+
res = val.each_with_object({}) do |(k, v), hash|
|
30
|
+
k = Contrast::Utils::StringUtils.force_utf8(k)
|
31
|
+
nested_prefix = prefix.nil? ? k : "#{ prefix }[#{ k }]"
|
32
|
+
hash[k] = Contrast::Utils::ObjectShare::EMPTY_STRING
|
33
|
+
hash.merge! normalize_params(v, prefix: nested_prefix)
|
34
|
+
end
|
35
|
+
res[prefix] = Contrast::Utils::ObjectShare::EMPTY_STRING if prefix
|
36
|
+
res
|
37
|
+
when Enumerable
|
38
|
+
idx = 0
|
39
|
+
res = {}
|
40
|
+
while idx < val.length
|
41
|
+
res.merge! normalize_params(val[idx], prefix: "#{ prefix }[#{ idx }]")
|
42
|
+
idx += 1
|
43
|
+
end
|
44
|
+
res[prefix] = Contrast::Utils::ObjectShare::EMPTY_STRING if prefix
|
45
|
+
res
|
46
|
+
else
|
47
|
+
{ prefix => Contrast::Utils::StringUtils.force_utf8(val) }
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def read_body body
|
52
|
+
return body if body.is_a?(String)
|
53
|
+
|
54
|
+
begin
|
55
|
+
can_rewind = Contrast::Utils::DuckUtils.quacks_to?(body, :rewind)
|
56
|
+
# if we are after a middleware that failed to rewind
|
57
|
+
body.rewind if can_rewind
|
58
|
+
body.read
|
59
|
+
rescue StandardError => e
|
60
|
+
logger.error('Error in attempt to read body', message: e.message)
|
61
|
+
logger.trace('With Stack', e)
|
62
|
+
body.to_s
|
63
|
+
ensure
|
64
|
+
# be a good citizen and rewind
|
65
|
+
body.rewind if can_rewind
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def traverse_parsed_multipart multipart_data, current_names
|
70
|
+
return current_names unless multipart_data
|
71
|
+
|
72
|
+
multipart_data.each_value do |data_value|
|
73
|
+
next unless data_value.is_a?(Hash)
|
74
|
+
|
75
|
+
tempfile = data_value[:tempfile]
|
76
|
+
if tempfile.nil?
|
77
|
+
traverse_parsed_multipart(data_value, current_names)
|
78
|
+
else
|
79
|
+
name = data_value[:name].to_s
|
80
|
+
file_name = data_value[:filename].to_s
|
81
|
+
current_names[name] = file_name
|
82
|
+
end
|
83
|
+
end
|
84
|
+
current_names
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,97 @@
|
|
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
|
+
# used in Contrast::Agent::Response
|
7
|
+
module ResponseUtils
|
8
|
+
private
|
9
|
+
|
10
|
+
# From the dtm for normalized_response_headers:
|
11
|
+
# Key is UPPERCASE_UNDERSCORE
|
12
|
+
#
|
13
|
+
# Example: Content-Type: text/html; charset=utf-8
|
14
|
+
# "CONTENT_TYPE" => Content-Type,["text/html; charset=utf8"]
|
15
|
+
def append_pair map, key, value
|
16
|
+
return unless key && value
|
17
|
+
return if value.is_a?(Hash)
|
18
|
+
|
19
|
+
safe_key = Contrast::Utils::StringUtils.force_utf8(key)
|
20
|
+
hash_key = Contrast::Utils::StringUtils.normalized_key(safe_key)
|
21
|
+
map[hash_key] ||= Contrast::Api::Dtm::Pair.new
|
22
|
+
map[hash_key].key = safe_key
|
23
|
+
map[hash_key].values << Contrast::Utils::StringUtils.force_utf8(value)
|
24
|
+
end
|
25
|
+
|
26
|
+
HTTP_PREFIX = /^[Hh][Tt][Tt][Pp][_-]/i.cs__freeze
|
27
|
+
|
28
|
+
# Given some holder of the content of the response's body, extract that
|
29
|
+
# content and return it as a String
|
30
|
+
#
|
31
|
+
# @param body [String, Rack::File, Rack::BodyProxy,
|
32
|
+
# ActionDispatch::Response::RackBody, Rack::Response] Something that
|
33
|
+
# holds, wraps, or is the body of the Response
|
34
|
+
# @return [nil, String] the content of the body
|
35
|
+
def extract_body body
|
36
|
+
return unless body
|
37
|
+
return if defined?(Rack::File) && body.is_a?(Rack::File)
|
38
|
+
|
39
|
+
case body
|
40
|
+
when Rack::BodyProxy
|
41
|
+
handle_rack_body_proxy(body)
|
42
|
+
when ActionDispatch::Response::RackBody
|
43
|
+
extract_body(body.body) if defined?(ActionDispatch::Response::RackBody)
|
44
|
+
when Rack::Response
|
45
|
+
extract_body(body.body)
|
46
|
+
when Contrast::Utils::DuckUtils.quacks_to?(body, :each)
|
47
|
+
acc = []
|
48
|
+
body.each { |tmp| acc << read_or_string(tmp) }
|
49
|
+
acc.compact.join(Contrast::Utils::ObjectShare::NEW_LINE)
|
50
|
+
when ActionView::OutputBuffer
|
51
|
+
# https://stackoverflow.com/questions/15654676/how-to-convert-activesupportsafebuffer-to-string
|
52
|
+
body.to_str
|
53
|
+
else
|
54
|
+
read_or_string(body)
|
55
|
+
end
|
56
|
+
# if body.is_a?(Rack::BodyProxy)
|
57
|
+
# handle_rack_body_proxy(body)
|
58
|
+
# elsif (defined?(ActionDispatch::Response::RackBody) && body.is_a?(ActionDispatch::Response::RackBody)) ||
|
59
|
+
# body.is_a?(Rack::Response)
|
60
|
+
#
|
61
|
+
# extract_body(body.body)
|
62
|
+
# elsif Contrast::Utils::DuckUtils.quacks_to?(body, :each)
|
63
|
+
# acc = []
|
64
|
+
# body.each { |tmp| acc << read_or_string(tmp) }
|
65
|
+
# acc.compact.join(Contrast::Utils::ObjectShare::NEW_LINE)
|
66
|
+
# elsif ActionView::OutputBuffer
|
67
|
+
# # https://stackoverflow.com/questions/15654676/how-to-convert-activesupportsafebuffer-to-string
|
68
|
+
# body.to_str
|
69
|
+
# else
|
70
|
+
# read_or_string(body)
|
71
|
+
# end
|
72
|
+
end
|
73
|
+
|
74
|
+
def handle_rack_body_proxy body
|
75
|
+
next_body = body.instance_variable_get(:@body)
|
76
|
+
case next_body
|
77
|
+
when Array
|
78
|
+
extract_body(next_body[0])
|
79
|
+
else
|
80
|
+
extract_body(next_body)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def read_or_string obj
|
85
|
+
return unless obj
|
86
|
+
|
87
|
+
if Contrast::Utils::DuckUtils.quacks_to?(obj, :read)
|
88
|
+
tmp = obj.read
|
89
|
+
obj.rewind
|
90
|
+
tmp
|
91
|
+
else
|
92
|
+
obj.to_s
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,167 @@
|
|
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
|
+
# used in Contrast::Agent::Assesss::Policy::Propagator::Substitution
|
7
|
+
module SubstitutionUtils
|
8
|
+
CAPTURE_GROUP_REGEXP = /\\[[:digit:]]/.cs__freeze
|
9
|
+
CAPTURE_NAME_REGEXP = /\\k<[[:alpha:]]/.cs__freeze
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def substitution_tagger patcher, preshift, ret, block, global = true
|
14
|
+
return ret unless ret
|
15
|
+
|
16
|
+
begin
|
17
|
+
source = preshift.object
|
18
|
+
self_tracked = Contrast::Agent::Assess::Tracker.tracked?(source)
|
19
|
+
args = preshift.args[1]
|
20
|
+
incoming_tracked = args && determine_tracked(args)
|
21
|
+
return ret unless self_tracked || incoming_tracked
|
22
|
+
|
23
|
+
parent_events = []
|
24
|
+
if block
|
25
|
+
block_sub(self_tracked, source, ret)
|
26
|
+
elsif args.is_a?(String)
|
27
|
+
string_sub(parent_events, self_tracked, preshift, ret, args, incoming_tracked, global)
|
28
|
+
elsif args.is_a?(Hash)
|
29
|
+
hash_sub(self_tracked, source, ret)
|
30
|
+
else # Enumerator, only for gsub
|
31
|
+
pattern_gsub(parent_events, preshift, ret)
|
32
|
+
end
|
33
|
+
|
34
|
+
self_tracked_handle self_tracked, source, parent_events
|
35
|
+
string_build_event(parent_events, patcher, preshift, ret)
|
36
|
+
rescue StandardError => e
|
37
|
+
logger.error('Unable to apply gsub propagator', e)
|
38
|
+
end
|
39
|
+
ret
|
40
|
+
end
|
41
|
+
|
42
|
+
def self_tracked_handle self_tracked, source, parent_events
|
43
|
+
return unless self_tracked
|
44
|
+
|
45
|
+
source_properties = Contrast::Agent::Assess::Tracker.properties(source)
|
46
|
+
parent_event = source_properties&.event
|
47
|
+
parent_events.prepend(parent_event) if parent_event
|
48
|
+
end
|
49
|
+
|
50
|
+
def determine_tracked args
|
51
|
+
# if there's no arg, it can't be tracked
|
52
|
+
return false unless args
|
53
|
+
|
54
|
+
# if it's a string, just ask if it's tracked
|
55
|
+
case args
|
56
|
+
when String
|
57
|
+
Contrast::Agent::Assess::Tracker.tracked?(args)
|
58
|
+
# if it's a hash, ask if it has a tracked string
|
59
|
+
when Hash
|
60
|
+
args.values.any? { |value| value.is_a?(String) && Contrast::Agent::Assess::Tracker.tracked?(value) }
|
61
|
+
# this should never happen
|
62
|
+
else
|
63
|
+
false
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def string_sub parent_events, self_tracked, preshift, ret, incoming, incoming_tracked, global
|
68
|
+
return unless (properties = Contrast::Agent::Assess::Tracker.properties!(ret))
|
69
|
+
|
70
|
+
incoming_properties = Contrast::Agent::Assess::Tracker.properties(incoming)
|
71
|
+
parent_events << incoming_properties&.event if incoming_properties&.event
|
72
|
+
|
73
|
+
source = preshift.object
|
74
|
+
|
75
|
+
# We can't efficiently find the places that things were
|
76
|
+
# copied from regexp / captures. Trading accuracy for
|
77
|
+
# performance
|
78
|
+
if incoming.match?(CAPTURE_GROUP_REGEXP) || incoming.match?(CAPTURE_NAME_REGEXP)
|
79
|
+
properties.splat_from(source, ret) if self_tracked
|
80
|
+
return
|
81
|
+
end
|
82
|
+
|
83
|
+
# if it's just a straight insert, that we can do
|
84
|
+
# Copy the tags from us to the return
|
85
|
+
ranges = find_string_sub_insert(properties, preshift, incoming, ret, global)
|
86
|
+
|
87
|
+
properties.delete_tags_at_ranges(ranges)
|
88
|
+
properties.shift_tags(ranges)
|
89
|
+
return unless incoming_tracked
|
90
|
+
return unless incoming_properties
|
91
|
+
|
92
|
+
tags = incoming_properties.tag_keys
|
93
|
+
ranges.each do |range|
|
94
|
+
tags.each do |tag|
|
95
|
+
properties.add_tag(tag, range)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
# Find the points at which the new String was placed into the original
|
101
|
+
#
|
102
|
+
# @param properties [Contrast::Agent::Assess::Properties] the Properties of the ret
|
103
|
+
# @param preshift [Contrast::Agent::Assess::PreShift] the capture of the state of the code just prior to
|
104
|
+
# the invocation of the patched method
|
105
|
+
# @param incoming [String] the new String going into the substitution
|
106
|
+
# @param ret [String] the result of the substitution
|
107
|
+
# @param global [Boolean] if this was a global or single substitution
|
108
|
+
# @return [Array<Range>] the Ranges where substitution occurred
|
109
|
+
def find_string_sub_insert properties, preshift, incoming, ret, global
|
110
|
+
pattern = preshift.args[0]
|
111
|
+
source = preshift.object
|
112
|
+
|
113
|
+
properties.copy_from(source, ret)
|
114
|
+
# Figure out where inserts occurred
|
115
|
+
last_idx = 0
|
116
|
+
ranges = []
|
117
|
+
# For each insert, move the tag ranges
|
118
|
+
while last_idx
|
119
|
+
idx = source.index(pattern, last_idx)
|
120
|
+
break unless idx
|
121
|
+
|
122
|
+
last_idx = idx ? idx + 1 : nil
|
123
|
+
start_index = idx
|
124
|
+
end_index = idx + incoming.length
|
125
|
+
ranges << (start_index...end_index)
|
126
|
+
break unless global
|
127
|
+
end
|
128
|
+
ranges
|
129
|
+
end
|
130
|
+
|
131
|
+
def block_sub self_tracked, source, ret
|
132
|
+
return unless self_tracked
|
133
|
+
|
134
|
+
properties = Contrast::Agent::Assess::Tracker.properties!(ret)
|
135
|
+
properties&.splat_from(source, ret)
|
136
|
+
end
|
137
|
+
|
138
|
+
def hash_sub self_tracked, source, ret
|
139
|
+
return unless self_tracked
|
140
|
+
|
141
|
+
properties = Contrast::Agent::Assess::Tracker.properties!(ret)
|
142
|
+
properties&.splat_from(source, ret)
|
143
|
+
end
|
144
|
+
|
145
|
+
def pattern_gsub parent_events, preshift, ret
|
146
|
+
source = preshift.object
|
147
|
+
return unless (source_properties = Contrast::Agent::Assess::Tracker.properties(source))
|
148
|
+
return unless (properties = Contrast::Agent::Assess::Tracker.properties!(ret))
|
149
|
+
|
150
|
+
source_properties.tag_keys.each do |key|
|
151
|
+
properties.add_tag(key, 0...1)
|
152
|
+
end
|
153
|
+
parent_event = source_properties.event
|
154
|
+
parent_events << parent_event if parent_event
|
155
|
+
end
|
156
|
+
|
157
|
+
def string_build_event parent_events, patcher, preshift, ret
|
158
|
+
return unless Contrast::Agent::Assess::Tracker.tracked?(ret)
|
159
|
+
|
160
|
+
properties = Contrast::Agent::Assess::Tracker.properties(ret)
|
161
|
+
args = preshift.args
|
162
|
+
properties.build_event(patcher, ret, preshift.object, ret, args, 2)
|
163
|
+
properties.event.instance_variable_set(:@_parent_events, parent_events)
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
@@ -56,20 +56,20 @@ module Contrast
|
|
56
56
|
# Any overlapping ranges are merged before returning
|
57
57
|
#
|
58
58
|
# arr: the array of tags to which we are adding
|
59
|
-
#
|
60
|
-
def ordered_merge arr,
|
59
|
+
# add_arr: array of Tags or a single Tag to be added
|
60
|
+
def ordered_merge arr, add_arr
|
61
61
|
# [Contrast::Agent::Assess::Tag, ...]
|
62
|
-
if
|
63
|
-
return arr unless
|
64
|
-
return
|
62
|
+
if add_arr.is_a?(Array)
|
63
|
+
return arr unless add_arr.any?
|
64
|
+
return add_arr unless arr&.any?
|
65
65
|
|
66
|
-
|
66
|
+
add_arr.each { |new_element| single_ordered_merge(arr, new_element) }
|
67
67
|
# Contrast::Agent::Assess::Tag
|
68
68
|
else
|
69
|
-
return arr unless
|
70
|
-
return [
|
69
|
+
return arr unless add_arr
|
70
|
+
return [add_arr] unless arr
|
71
71
|
|
72
|
-
single_ordered_merge(arr,
|
72
|
+
single_ordered_merge(arr, add_arr)
|
73
73
|
end
|
74
74
|
smallerize(arr)
|
75
75
|
end
|