contrast-agent 4.11.0 → 4.14.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +4 -4
  2. data/.simplecov +1 -0
  3. data/ext/cs__assess_module/cs__assess_module.c +48 -0
  4. data/ext/cs__assess_module/cs__assess_module.h +7 -0
  5. data/ext/cs__common/cs__common.c +24 -7
  6. data/ext/cs__common/cs__common.h +12 -2
  7. data/ext/cs__contrast_patch/cs__contrast_patch.c +48 -11
  8. data/ext/cs__contrast_patch/cs__contrast_patch.h +5 -2
  9. data/ext/cs__os_information/cs__os_information.c +31 -0
  10. data/ext/cs__os_information/cs__os_information.h +7 -0
  11. data/ext/{cs__protect_kernel → cs__os_information}/extconf.rb +0 -0
  12. data/lib/contrast/agent/assess/contrast_event.rb +1 -1
  13. data/lib/contrast/agent/assess/contrast_object.rb +1 -1
  14. data/lib/contrast/agent/assess/policy/dynamic_source_factory.rb +2 -0
  15. data/lib/contrast/agent/assess/policy/policy_node.rb +6 -6
  16. data/lib/contrast/agent/assess/policy/policy_scanner.rb +5 -0
  17. data/lib/contrast/agent/assess/policy/preshift.rb +19 -6
  18. data/lib/contrast/agent/assess/policy/propagation_method.rb +2 -116
  19. data/lib/contrast/agent/assess/policy/propagation_node.rb +4 -4
  20. data/lib/contrast/agent/assess/policy/propagator/center.rb +1 -1
  21. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +2 -0
  22. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +2 -154
  23. data/lib/contrast/agent/assess/policy/source_method.rb +2 -71
  24. data/lib/contrast/agent/assess/policy/trigger_method.rb +45 -110
  25. data/lib/contrast/agent/assess/policy/trigger_node.rb +62 -21
  26. data/lib/contrast/agent/assess/policy/trigger_validation/xss_validator.rb +1 -1
  27. data/lib/contrast/agent/assess/property/tagged.rb +66 -189
  28. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +40 -6
  29. data/lib/contrast/agent/deadzone/policy/policy.rb +6 -0
  30. data/lib/contrast/agent/inventory/dependency_usage_analysis.rb +1 -0
  31. data/lib/contrast/agent/metric_telemetry_event.rb +26 -0
  32. data/lib/contrast/agent/middleware.rb +14 -62
  33. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +0 -1
  34. data/lib/contrast/agent/patching/policy/method_policy.rb +3 -44
  35. data/lib/contrast/agent/patching/policy/method_policy_extend.rb +111 -0
  36. data/lib/contrast/agent/patching/policy/patch.rb +37 -238
  37. data/lib/contrast/agent/patching/policy/patcher.rb +15 -50
  38. data/lib/contrast/agent/reporting/report.rb +21 -0
  39. data/lib/contrast/agent/reporting/reporter.rb +142 -0
  40. data/lib/contrast/agent/reporting/reporting_events/finding.rb +90 -0
  41. data/lib/contrast/agent/reporting/reporting_events/preflight.rb +25 -0
  42. data/lib/contrast/agent/reporting/reporting_events/preflight_message.rb +56 -0
  43. data/lib/contrast/agent/reporting/reporting_events/reporting_event.rb +37 -0
  44. data/lib/contrast/agent/reporting/reporting_utilities/audit.rb +127 -0
  45. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +168 -0
  46. data/lib/contrast/agent/reporting/reporting_utilities/reporting_storage.rb +66 -0
  47. data/lib/contrast/agent/request.rb +2 -81
  48. data/lib/contrast/agent/request_context.rb +18 -126
  49. data/lib/contrast/agent/request_context_extend.rb +138 -0
  50. data/lib/contrast/agent/request_handler.rb +7 -3
  51. data/lib/contrast/agent/response.rb +2 -73
  52. data/lib/contrast/agent/rule_set.rb +2 -4
  53. data/lib/contrast/agent/startup_metrics_telemetry_event.rb +94 -0
  54. data/lib/contrast/agent/static_analysis.rb +5 -3
  55. data/lib/contrast/agent/telemetry.rb +137 -0
  56. data/lib/contrast/agent/telemetry_event.rb +33 -0
  57. data/lib/contrast/agent/thread_watcher.rb +66 -11
  58. data/lib/contrast/agent/version.rb +1 -1
  59. data/lib/contrast/agent.rb +21 -1
  60. data/lib/contrast/api/communication/connection_status.rb +10 -7
  61. data/lib/contrast/api/communication/messaging_queue.rb +37 -3
  62. data/lib/contrast/api/communication/response_processor.rb +15 -8
  63. data/lib/contrast/api/communication/service_lifecycle.rb +13 -3
  64. data/lib/contrast/api/communication/socket.rb +6 -8
  65. data/lib/contrast/api/communication/socket_client.rb +29 -12
  66. data/lib/contrast/api/communication/speedracer.rb +37 -1
  67. data/lib/contrast/api/communication/tcp_socket.rb +4 -3
  68. data/lib/contrast/api/communication/unix_socket.rb +1 -0
  69. data/lib/contrast/api/decorators/finding.rb +45 -0
  70. data/lib/contrast/components/api.rb +90 -0
  71. data/lib/contrast/components/app_context.rb +10 -41
  72. data/lib/contrast/components/app_context_extend.rb +78 -0
  73. data/lib/contrast/components/assess.rb +7 -0
  74. data/lib/contrast/components/base.rb +23 -0
  75. data/lib/contrast/components/config.rb +92 -13
  76. data/lib/contrast/components/contrast_service.rb +11 -0
  77. data/lib/contrast/components/sampling.rb +2 -2
  78. data/lib/contrast/config/agent_configuration.rb +1 -1
  79. data/lib/contrast/config/api_configuration.rb +27 -0
  80. data/lib/contrast/config/api_proxy_configuration.rb +14 -0
  81. data/lib/contrast/config/application_configuration.rb +2 -3
  82. data/lib/contrast/config/assess_configuration.rb +3 -2
  83. data/lib/contrast/config/base_configuration.rb +17 -28
  84. data/lib/contrast/config/certification_configuration.rb +15 -0
  85. data/lib/contrast/config/env_variables.rb +18 -0
  86. data/lib/contrast/config/heap_dump_configuration.rb +6 -6
  87. data/lib/contrast/config/inventory_configuration.rb +1 -5
  88. data/lib/contrast/config/protect_rule_configuration.rb +1 -1
  89. data/lib/contrast/config/request_audit_configuration.rb +18 -0
  90. data/lib/contrast/config/root_configuration.rb +1 -0
  91. data/lib/contrast/config/ruby_configuration.rb +6 -6
  92. data/lib/contrast/config/service_configuration.rb +2 -2
  93. data/lib/contrast/config.rb +1 -1
  94. data/lib/contrast/configuration.rb +4 -2
  95. data/lib/contrast/extension/assess/array.rb +5 -7
  96. data/lib/contrast/framework/manager.rb +22 -44
  97. data/lib/contrast/framework/manager_extend.rb +50 -0
  98. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +9 -6
  99. data/lib/contrast/framework/rails/patch/support.rb +31 -29
  100. data/lib/contrast/framework/rails/railtie.rb +1 -1
  101. data/lib/contrast/framework/sinatra/support.rb +2 -1
  102. data/lib/contrast/logger/application.rb +4 -0
  103. data/lib/contrast/logger/log.rb +8 -103
  104. data/lib/contrast/utils/assess/propagation_method_utils.rb +129 -0
  105. data/lib/contrast/utils/assess/property/tagged_utils.rb +165 -0
  106. data/lib/contrast/utils/assess/source_method_utils.rb +83 -0
  107. data/lib/contrast/utils/assess/tracking_util.rb +20 -15
  108. data/lib/contrast/utils/assess/trigger_method_utils.rb +138 -0
  109. data/lib/contrast/utils/class_util.rb +65 -54
  110. data/lib/contrast/utils/exclude_key.rb +20 -0
  111. data/lib/contrast/utils/findings.rb +62 -0
  112. data/lib/contrast/utils/hash_digest.rb +10 -73
  113. data/lib/contrast/utils/hash_digest_extend.rb +86 -0
  114. data/lib/contrast/utils/head_dump_utils_extend.rb +74 -0
  115. data/lib/contrast/utils/heap_dump_util.rb +2 -65
  116. data/lib/contrast/utils/invalid_configuration_util.rb +29 -0
  117. data/lib/contrast/utils/io_util.rb +1 -1
  118. data/lib/contrast/utils/log_utils.rb +108 -0
  119. data/lib/contrast/utils/lru_cache.rb +4 -2
  120. data/lib/contrast/utils/metrics_hash.rb +59 -0
  121. data/lib/contrast/utils/middleware_utils.rb +87 -0
  122. data/lib/contrast/utils/net_http_base.rb +158 -0
  123. data/lib/contrast/utils/object_share.rb +1 -0
  124. data/lib/contrast/utils/os.rb +23 -0
  125. data/lib/contrast/utils/patching/policy/patch_utils.rb +232 -0
  126. data/lib/contrast/utils/patching/policy/patcher_utils.rb +54 -0
  127. data/lib/contrast/utils/request_utils.rb +88 -0
  128. data/lib/contrast/utils/response_utils.rb +97 -0
  129. data/lib/contrast/utils/substitution_utils.rb +167 -0
  130. data/lib/contrast/utils/tag_util.rb +9 -9
  131. data/lib/contrast/utils/telemetry.rb +79 -0
  132. data/lib/contrast/utils/telemetry_client.rb +90 -0
  133. data/lib/contrast/utils/telemetry_identifier.rb +130 -0
  134. data/lib/contrast.rb +19 -1
  135. data/resources/assess/policy.json +12 -6
  136. data/resources/deadzone/policy.json +86 -5
  137. data/ruby-agent.gemspec +7 -6
  138. data/service_executables/VERSION +1 -1
  139. data/service_executables/linux/contrast-service +0 -0
  140. data/service_executables/mac/contrast-service +0 -0
  141. metadata +68 -26
  142. data/ext/cs__protect_kernel/cs__protect_kernel.c +0 -47
  143. data/ext/cs__protect_kernel/cs__protect_kernel.h +0 -12
  144. data/lib/contrast/config/default_value.rb +0 -17
  145. data/lib/contrast/extension/protect/kernel.rb +0 -29
@@ -0,0 +1,111 @@
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
+ module Patching
7
+ module Policy
8
+ # This class is used to map each method to the trigger node that applies to it
9
+ module MethodPolicyExtend
10
+ # Given a Contrast::Agent::Patching::Policy::ModulePolicy, parse
11
+ # out its information for the given method in order to construct a
12
+ # Contrast::Agent::Patching::Policy::MethodPolicy
13
+ #
14
+ # @param method_name [Symbol] the name of the method for this policy
15
+ # @param module_policy [Contrast::Agent::Patching::Policy::ModulePolicy]
16
+ # the entire policy for this module
17
+ # @param instance_method [Boolean] true if this method is an
18
+ # instance method
19
+ # @return [Contrast::Agent::Patching::Policy::MethodPolicy]
20
+ def build_method_policy method_name, module_policy, instance_method
21
+ source_node = find_method_node(module_policy.source_nodes, method_name, instance_method)
22
+ propagation_node = find_method_node(module_policy.propagator_nodes, method_name, instance_method)
23
+ trigger_node = find_method_node(module_policy.trigger_nodes, method_name, instance_method)
24
+ protect_node = find_method_node(module_policy.protect_nodes, method_name, instance_method)
25
+ inventory_node = find_method_node(module_policy.inventory_nodes, method_name, instance_method)
26
+ deadzone_node = find_method_node(module_policy.deadzone_nodes, method_name, instance_method)
27
+ method_visibility = find_visibility(source_node, propagation_node, trigger_node, protect_node,
28
+ inventory_node, deadzone_node)
29
+ method_policy = MethodPolicy.new(method_name: method_name,
30
+ method_visibility: method_visibility,
31
+ instance_method: instance_method,
32
+ source_node: source_node,
33
+ propagation_node: propagation_node,
34
+ trigger_node: trigger_node,
35
+ protect_node: protect_node,
36
+ inventory_node: inventory_node,
37
+ deadzone_node: deadzone_node)
38
+
39
+ return method_policy unless check_method_policy_nodes_empty? source_node, propagation_node, trigger_node,
40
+ protect_node, inventory_node, deadzone_node
41
+
42
+ create_new_node(module_policy, method_policy) if module_policy.deadzone_nodes&.any?
43
+ method_policy
44
+ end
45
+
46
+ def find_method_node nodes, method_name, is_instance_method
47
+ return unless nodes
48
+
49
+ nodes.find do |node|
50
+ node.instance_method? == is_instance_method && node.method_name == method_name
51
+ end
52
+ end
53
+
54
+ def find_visibility *nodes
55
+ nodes.find { |node| node }&.method_visibility
56
+ end
57
+
58
+ def check_method_policy_nodes_empty?(source_node, propagation_node, trigger_node, protect_node,
59
+ inventory_node, deadzone_node)
60
+ return false unless source_node.nil? && propagation_node.nil? && trigger_node.nil? && protect_node.nil? &&
61
+ inventory_node.nil? && deadzone_node.nil?
62
+
63
+ true
64
+ end
65
+
66
+ private
67
+
68
+ def create_new_node module_policy, method_policy
69
+ return if module_policy.deadzone_nodes.empty?
70
+
71
+ module_policy.deadzone_nodes.map do |node|
72
+ next unless node.method_name.nil?
73
+
74
+ klass = Module.cs__const_get(node.class_name)
75
+ next unless it_defined? klass, method_policy.method_name
76
+
77
+ new_node = set_new_node method_policy, klass, node
78
+ method_policy.instance_variable_set(:@method_visibility, new_node.method_visibility)
79
+ method_policy.instance_variable_set(:@deadzone_node, node)
80
+ module_policy.deadzone_nodes << new_node
81
+ break unless method_policy.deadzone_node.nil?
82
+ end
83
+ end
84
+
85
+ # Helper method for creating new node
86
+ #
87
+ # @param method_policy [Contrast::Agent::Patching::Policy::MethodPolicy]
88
+ # used to map each method to the trigger node that applies to it
89
+ # @param klass [String] classname
90
+ # @param node [Contrast::Agent::Patching::Policy::PolicyNode]
91
+ # @return @_set_new_node [Contrast::Agent::Deadzone::Policy::DeadzoneNode]
92
+ def set_new_node method_policy, klass, node
93
+ new_node = {}
94
+ new_node['instance_method'] = method_policy.instance_method
95
+ new_node['method_visibility'] =
96
+ klass.private_method_defined?(method_policy.method_name) ? 'private' : 'public'
97
+ new_node['method_name'] = method_policy.method_name
98
+ new_node['class_name'] = node.class_name
99
+ @_set_new_node = Contrast::Agent::Deadzone::Policy::DeadzoneNode.new(new_node)
100
+ end
101
+
102
+ def it_defined? klass, method_name
103
+ klass.instance_methods(false).include?(method_name) ||
104
+ klass.private_instance_methods(false).include?(method_name) ||
105
+ klass.singleton_methods(false).include?(method_name)
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
@@ -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
@@ -343,7 +126,7 @@ module Contrast
343
126
  underlying_method_name = build_unbound_method_name(method_name).to_sym
344
127
 
345
128
  target_module = Module.cs__const_get(target_module_name)
346
-
129
+ target_module = target_module.cs__singleton_class if %i[prepend_singleton prepend].include? impl
347
130
  target_module = target_module.cs__singleton_class if %i[alias_singleton prepend].include? impl
348
131
 
349
132
  visibility = if target_module.private_instance_methods(false).include?(method_name)
@@ -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.
@@ -370,27 +177,19 @@ module Contrast
370
177
  target_module.send(:define_method, method_name, unbound_method.bind(target_module))
371
178
  end
372
179
  target_module.send(visibility, method_name) # e.g., module.private(:my_method)
373
- when :prepend
374
- prepending_module = Module.new
375
- prepending_module.send(:define_method, method_name, unbound_method.bind(target_module))
376
- prepending_module.send(visibility, method_name)
180
+ when :prepend_instance, :prepend_singleton
181
+
182
+ unless target_module.instance_methods(false).include? underlying_method_name
183
+
184
+ prepending_module = Module.new
185
+ prepending_module.send(:define_method, method_name, unbound_method.bind(target_module))
186
+ prepending_module.send(visibility, method_name)
187
+
188
+ end
377
189
  # This prepends to the singleton class (it patches a class method)
378
190
  target_module.prepend prepending_module
379
191
  # rubocop:enable Performance/Kernel/DefineMethod
380
192
  end
381
- # Ougai::Logger.create_item_with_2args calls Hash#[]=, so we
382
- # can't invoke this logging method or we'll seg fault as we'd
383
- # change the method definition mid-call
384
- # if method_name != :[]= &&
385
- # Contrast::Agent::Logger.defined!
386
- # logger.trace(
387
- # 'Registered C-defined patch',
388
- # implementation: impl,
389
- # module: target_module_name,
390
- # method: method_name,
391
- # visibility: visibility)
392
- # end
393
- underlying_method_name
394
193
  end
395
194
 
396
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
@@ -246,6 +211,7 @@ module Contrast
246
211
  def patch_into_instance_methods module_data, module_policy
247
212
  mod = module_data.mod
248
213
  methods = all_instance_methods(mod, true)
214
+ methods.delete(:initialize) if mod.to_s.starts_with?('RSpec') && mod.to_s.include?('Matchers')
249
215
  patch_into_methods(mod, methods, module_policy, true)
250
216
  end
251
217
 
@@ -309,6 +275,5 @@ require 'contrast/extension/module'
309
275
  require 'contrast/extension/assess'
310
276
  require 'contrast/extension/inventory'
311
277
  require 'contrast/extension/protect'
312
- require 'contrast/extension/protect/kernel'
313
278
 
314
279
  require 'cs__contrast_patch/cs__contrast_patch'
@@ -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'