contrast-agent 4.6.0 → 4.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (190) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +6 -1
  3. data/.gitmodules +1 -1
  4. data/.simplecov +1 -0
  5. data/Rakefile +1 -2
  6. data/ext/build_funchook.rb +3 -3
  7. data/ext/extconf_common.rb +1 -5
  8. data/lib/contrast.rb +24 -14
  9. data/lib/contrast/agent/assess.rb +1 -1
  10. data/lib/contrast/agent/assess/contrast_event.rb +1 -4
  11. data/lib/contrast/agent/assess/contrast_object.rb +2 -2
  12. data/lib/contrast/agent/assess/events/event_factory.rb +2 -1
  13. data/lib/contrast/agent/assess/finalizers/hash.rb +2 -4
  14. data/lib/contrast/agent/assess/policy/dynamic_source_factory.rb +6 -3
  15. data/lib/contrast/agent/assess/policy/patcher.rb +16 -21
  16. data/lib/contrast/agent/assess/policy/policy.rb +1 -1
  17. data/lib/contrast/agent/assess/policy/policy_node.rb +25 -33
  18. data/lib/contrast/agent/assess/policy/policy_scanner.rb +3 -5
  19. data/lib/contrast/agent/assess/policy/preshift.rb +7 -5
  20. data/lib/contrast/agent/assess/policy/propagation_method.rb +10 -19
  21. data/lib/contrast/agent/assess/policy/propagation_node.rb +19 -8
  22. data/lib/contrast/agent/assess/policy/propagator.rb +1 -0
  23. data/lib/contrast/agent/assess/policy/propagator/center.rb +2 -1
  24. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +3 -6
  25. data/lib/contrast/agent/assess/policy/propagator/insert.rb +3 -1
  26. data/lib/contrast/agent/assess/policy/propagator/match_data.rb +2 -1
  27. data/lib/contrast/agent/assess/policy/propagator/rack_protection.rb +73 -0
  28. data/lib/contrast/agent/assess/policy/propagator/select.rb +2 -12
  29. data/lib/contrast/agent/assess/policy/propagator/split.rb +12 -13
  30. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +3 -10
  31. data/lib/contrast/agent/assess/policy/propagator/trim.rb +3 -15
  32. data/lib/contrast/agent/assess/policy/rewriter_patch.rb +13 -10
  33. data/lib/contrast/agent/assess/policy/source_method.rb +12 -12
  34. data/lib/contrast/agent/assess/policy/source_validation/source_validation.rb +1 -3
  35. data/lib/contrast/agent/assess/policy/trigger/reflected_xss.rb +5 -1
  36. data/lib/contrast/agent/assess/policy/trigger/xpath.rb +0 -3
  37. data/lib/contrast/agent/assess/policy/trigger_method.rb +8 -18
  38. data/lib/contrast/agent/assess/policy/trigger_node.rb +3 -2
  39. data/lib/contrast/agent/assess/policy/trigger_validation/redos_validator.rb +4 -3
  40. data/lib/contrast/agent/assess/policy/trigger_validation/ssrf_validator.rb +1 -2
  41. data/lib/contrast/agent/assess/policy/trigger_validation/xss_validator.rb +1 -8
  42. data/lib/contrast/agent/assess/property/evented.rb +8 -5
  43. data/lib/contrast/agent/assess/rule/provider/hardcoded_key.rb +11 -5
  44. data/lib/contrast/agent/assess/rule/provider/hardcoded_password.rb +4 -1
  45. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +7 -9
  46. data/lib/contrast/agent/at_exit_hook.rb +3 -3
  47. data/lib/contrast/agent/class_reopener.rb +9 -6
  48. data/lib/contrast/agent/disable_reaction.rb +4 -7
  49. data/lib/contrast/agent/exclusion_matcher.rb +7 -14
  50. data/lib/contrast/agent/inventory/dependencies.rb +2 -0
  51. data/lib/contrast/agent/inventory/dependency_analysis.rb +2 -6
  52. data/lib/contrast/agent/inventory/dependency_usage_analysis.rb +3 -5
  53. data/lib/contrast/agent/inventory/policy/datastores.rb +3 -4
  54. data/lib/contrast/agent/inventory/policy/policy.rb +1 -1
  55. data/lib/contrast/agent/middleware.rb +17 -18
  56. data/lib/contrast/agent/module_data.rb +3 -3
  57. data/lib/contrast/agent/patching/policy/after_load_patch.rb +3 -3
  58. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +9 -9
  59. data/lib/contrast/agent/patching/policy/method_policy.rb +6 -2
  60. data/lib/contrast/agent/patching/policy/module_policy.rb +14 -7
  61. data/lib/contrast/agent/patching/policy/patch.rb +20 -25
  62. data/lib/contrast/agent/patching/policy/patch_status.rb +6 -7
  63. data/lib/contrast/agent/patching/policy/patcher.rb +21 -18
  64. data/lib/contrast/agent/patching/policy/policy.rb +2 -4
  65. data/lib/contrast/agent/patching/policy/policy_node.rb +16 -7
  66. data/lib/contrast/agent/patching/policy/trigger_node.rb +21 -8
  67. data/lib/contrast/agent/protect/policy/applies_command_injection_rule.rb +1 -1
  68. data/lib/contrast/agent/protect/policy/applies_deserialization_rule.rb +1 -1
  69. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +1 -1
  70. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +2 -3
  71. data/lib/contrast/agent/protect/policy/applies_sqli_rule.rb +1 -1
  72. data/lib/contrast/agent/protect/policy/applies_xxe_rule.rb +5 -9
  73. data/lib/contrast/agent/protect/policy/policy.rb +1 -1
  74. data/lib/contrast/agent/protect/policy/rule_applicator.rb +7 -9
  75. data/lib/contrast/agent/protect/rule/base.rb +20 -23
  76. data/lib/contrast/agent/protect/rule/base_service.rb +9 -5
  77. data/lib/contrast/agent/protect/rule/cmd_injection.rb +18 -23
  78. data/lib/contrast/agent/protect/rule/deserialization.rb +6 -13
  79. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +3 -14
  80. data/lib/contrast/agent/protect/rule/no_sqli.rb +6 -2
  81. data/lib/contrast/agent/protect/rule/no_sqli/mongo_no_sql_scanner.rb +1 -3
  82. data/lib/contrast/agent/protect/rule/path_traversal.rb +6 -10
  83. data/lib/contrast/agent/protect/rule/sqli.rb +1 -1
  84. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +1 -1
  85. data/lib/contrast/agent/protect/rule/xss.rb +1 -1
  86. data/lib/contrast/agent/protect/rule/xxe.rb +5 -12
  87. data/lib/contrast/agent/protect/rule/xxe/entity_wrapper.rb +1 -2
  88. data/lib/contrast/agent/reaction_processor.rb +13 -13
  89. data/lib/contrast/agent/request.rb +27 -26
  90. data/lib/contrast/agent/request_context.rb +17 -22
  91. data/lib/contrast/agent/request_handler.rb +5 -3
  92. data/lib/contrast/agent/response.rb +2 -3
  93. data/lib/contrast/agent/rewriter.rb +9 -6
  94. data/lib/contrast/agent/rule_set.rb +5 -4
  95. data/lib/contrast/agent/service_heartbeat.rb +4 -6
  96. data/lib/contrast/agent/static_analysis.rb +6 -5
  97. data/lib/contrast/agent/thread.rb +2 -4
  98. data/lib/contrast/agent/thread_watcher.rb +3 -4
  99. data/lib/contrast/agent/tracepoint_hook.rb +5 -5
  100. data/lib/contrast/agent/version.rb +1 -1
  101. data/lib/contrast/api/communication/messaging_queue.rb +4 -5
  102. data/lib/contrast/api/communication/response_processor.rb +11 -13
  103. data/lib/contrast/api/communication/service_lifecycle.rb +9 -6
  104. data/lib/contrast/api/communication/socket_client.rb +22 -31
  105. data/lib/contrast/api/communication/speedracer.rb +8 -13
  106. data/lib/contrast/api/decorators/address.rb +2 -3
  107. data/lib/contrast/api/decorators/agent_startup.rb +7 -9
  108. data/lib/contrast/api/decorators/application_startup.rb +12 -10
  109. data/lib/contrast/api/decorators/application_update.rb +0 -4
  110. data/lib/contrast/api/decorators/http_request.rb +3 -7
  111. data/lib/contrast/api/decorators/instrumentation_mode.rb +3 -5
  112. data/lib/contrast/api/decorators/library.rb +8 -6
  113. data/lib/contrast/api/decorators/message.rb +9 -9
  114. data/lib/contrast/api/decorators/trace_event.rb +3 -1
  115. data/lib/contrast/api/decorators/trace_event_object.rb +3 -6
  116. data/lib/contrast/api/decorators/trace_taint_range_tags.rb +1 -6
  117. data/lib/contrast/components/agent.rb +17 -17
  118. data/lib/contrast/components/app_context.rb +11 -15
  119. data/lib/contrast/components/assess.rb +16 -16
  120. data/lib/contrast/components/base.rb +40 -0
  121. data/lib/contrast/components/config.rb +2 -3
  122. data/lib/contrast/components/contrast_service.rb +12 -18
  123. data/lib/contrast/components/heap_dump.rb +5 -4
  124. data/lib/contrast/components/inventory.rb +2 -7
  125. data/lib/contrast/components/logger.rb +1 -2
  126. data/lib/contrast/components/protect.rb +10 -13
  127. data/lib/contrast/components/sampling.rb +13 -7
  128. data/lib/contrast/components/scope.rb +0 -4
  129. data/lib/contrast/components/settings.rb +5 -7
  130. data/lib/contrast/config/assess_rules_configuration.rb +1 -3
  131. data/lib/contrast/config/base_configuration.rb +4 -5
  132. data/lib/contrast/config/exception_configuration.rb +1 -5
  133. data/lib/contrast/config/heap_dump_configuration.rb +12 -6
  134. data/lib/contrast/config/logger_configuration.rb +1 -5
  135. data/lib/contrast/configuration.rb +6 -18
  136. data/lib/contrast/extension/assess/array.rb +3 -10
  137. data/lib/contrast/extension/assess/erb.rb +1 -7
  138. data/lib/contrast/extension/assess/eval_trigger.rb +4 -9
  139. data/lib/contrast/extension/assess/exec_trigger.rb +3 -9
  140. data/lib/contrast/extension/assess/fiber.rb +8 -17
  141. data/lib/contrast/extension/assess/hash.rb +3 -3
  142. data/lib/contrast/extension/assess/kernel.rb +4 -13
  143. data/lib/contrast/extension/assess/marshal.rb +6 -10
  144. data/lib/contrast/extension/assess/regexp.rb +6 -10
  145. data/lib/contrast/extension/assess/string.rb +8 -6
  146. data/lib/contrast/extension/kernel.rb +2 -2
  147. data/lib/contrast/extension/protect/kernel.rb +0 -5
  148. data/lib/contrast/framework/manager.rb +3 -5
  149. data/lib/contrast/framework/rack/patch/session_cookie.rb +11 -24
  150. data/lib/contrast/framework/rack/patch/support.rb +6 -4
  151. data/lib/contrast/framework/rails/patch/assess_configuration.rb +12 -9
  152. data/lib/contrast/framework/rails/patch/support.rb +41 -35
  153. data/lib/contrast/framework/rails/railtie.rb +34 -0
  154. data/lib/contrast/framework/rails/rewrite/action_controller_railties_helper_inherited.rb +4 -1
  155. data/lib/contrast/framework/rails/rewrite/active_record_attribute_methods_read.rb +2 -0
  156. data/lib/contrast/framework/rails/rewrite/active_record_named.rb +5 -4
  157. data/lib/contrast/framework/rails/rewrite/active_record_time_zone_inherited.rb +2 -0
  158. data/lib/contrast/framework/rails/support.rb +2 -2
  159. data/lib/contrast/framework/sinatra/support.rb +3 -1
  160. data/lib/contrast/funchook/funchook.rb +5 -8
  161. data/lib/contrast/logger/application.rb +13 -15
  162. data/lib/contrast/logger/format.rb +2 -5
  163. data/lib/contrast/logger/log.rb +26 -9
  164. data/lib/contrast/logger/request.rb +1 -6
  165. data/lib/contrast/security_exception.rb +1 -1
  166. data/lib/contrast/tasks/config.rb +0 -1
  167. data/lib/contrast/tasks/service.rb +6 -7
  168. data/lib/contrast/utils/assess/sampling_util.rb +2 -3
  169. data/lib/contrast/utils/assess/tracking_util.rb +3 -6
  170. data/lib/contrast/utils/class_util.rb +0 -8
  171. data/lib/contrast/utils/hash_digest.rb +2 -5
  172. data/lib/contrast/utils/heap_dump_util.rb +5 -3
  173. data/lib/contrast/utils/invalid_configuration_util.rb +4 -3
  174. data/lib/contrast/utils/inventory_util.rb +2 -3
  175. data/lib/contrast/utils/io_util.rb +3 -5
  176. data/lib/contrast/utils/job_servers_running.rb +13 -7
  177. data/lib/contrast/utils/os.rb +4 -4
  178. data/lib/contrast/utils/ruby_ast_rewriter.rb +2 -1
  179. data/lib/contrast/utils/string_utils.rb +2 -3
  180. data/lib/contrast/utils/tag_util.rb +25 -19
  181. data/resources/assess/policy.json +55 -0
  182. data/ruby-agent.gemspec +17 -16
  183. data/service_executables/VERSION +1 -1
  184. data/service_executables/linux/contrast-service +0 -0
  185. data/service_executables/mac/contrast-service +0 -0
  186. data/sonar-project.properties +9 -0
  187. metadata +61 -46
  188. data/lib/contrast/agent/railtie.rb +0 -31
  189. data/lib/contrast/components/interface.rb +0 -195
  190. data/lib/contrast/delegators/input_analysis.rb +0 -12
@@ -20,12 +20,18 @@ module Contrast
20
20
  # @return [Contrast::Agent::Patching::Policy::ModulePolicy]
21
21
  def create_module_policy module_name
22
22
  module_policy = Contrast::Agent::Patching::Policy::ModulePolicy.new
23
- module_policy.source_nodes = nodes_for_module(Contrast::Agent::Assess::Policy::Policy.instance.sources, module_name)
24
- module_policy.propagator_nodes = nodes_for_module(Contrast::Agent::Assess::Policy::Policy.instance.propagators, module_name)
25
- module_policy.trigger_nodes = nodes_for_module(Contrast::Agent::Assess::Policy::Policy.instance.triggers, module_name)
26
- module_policy.protect_nodes = nodes_for_module(Contrast::Agent::Protect::Policy::Policy.instance.triggers, module_name)
27
- module_policy.inventory_nodes = nodes_for_module(Contrast::Agent::Inventory::Policy::Policy.instance.triggers, module_name)
28
- module_policy.deadzone_nodes = nodes_for_module(Contrast::Agent::Deadzone::Policy::Policy.instance.deadzones, module_name)
23
+ module_policy.source_nodes = nodes_for_module(
24
+ Contrast::Agent::Assess::Policy::Policy.instance.sources, module_name)
25
+ module_policy.propagator_nodes = nodes_for_module(
26
+ Contrast::Agent::Assess::Policy::Policy.instance.propagators, module_name)
27
+ module_policy.trigger_nodes = nodes_for_module(
28
+ Contrast::Agent::Assess::Policy::Policy.instance.triggers, module_name)
29
+ module_policy.protect_nodes = nodes_for_module(
30
+ Contrast::Agent::Protect::Policy::Policy.instance.triggers, module_name)
31
+ module_policy.inventory_nodes = nodes_for_module(
32
+ Contrast::Agent::Inventory::Policy::Policy.instance.triggers, module_name)
33
+ module_policy.deadzone_nodes = nodes_for_module(
34
+ Contrast::Agent::Deadzone::Policy::Policy.instance.deadzones, module_name)
29
35
  module_policy
30
36
  end
31
37
 
@@ -42,7 +48,8 @@ module Contrast
42
48
  end
43
49
  end
44
50
 
45
- attr_accessor :source_nodes, :propagator_nodes, :trigger_nodes, :inventory_nodes, :protect_nodes, :deadzone_nodes
51
+ attr_accessor :source_nodes, :propagator_nodes, :trigger_nodes, :inventory_nodes, :protect_nodes,
52
+ :deadzone_nodes
46
53
 
47
54
  def empty?
48
55
  return false if source_nodes.any?
@@ -2,7 +2,8 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'monitor'
5
- require 'contrast/components/interface'
5
+ require 'contrast/components/logger'
6
+ require 'contrast/components/scope'
6
7
 
7
8
  require 'contrast/agent'
8
9
  require 'contrast/logger/log'
@@ -35,9 +36,8 @@ module Contrast
35
36
  include Contrast::Agent::Assess::Policy::SourceMethod
36
37
  include Contrast::Agent::Assess::Policy::PropagationMethod
37
38
  include Contrast::Agent::Assess::Policy::TriggerMethod
38
-
39
- include Contrast::Components::Interface
40
- access_component :agent, :analysis, :logging, :scope
39
+ include Contrast::Components::Logger::InstanceMethods
40
+ include Contrast::Components::Scope::InstanceMethods
41
41
 
42
42
  POLICIES = [
43
43
  Contrast::Agent::Assess::Policy::Policy,
@@ -127,14 +127,10 @@ module Contrast
127
127
  # @param args [Array<Object>] The arguments passed to the method
128
128
  # being invoked.
129
129
  def apply_protect method_policy, method, exception, object, args
130
- return unless AGENT.enabled?
131
- return unless PROTECT.enabled?
132
-
133
- apply_trigger_only(method_policy&.protect_node,
134
- method,
135
- exception,
136
- object,
137
- 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)
138
134
  end
139
135
 
140
136
  # Apply the Inventory patch which applies to the given method.
@@ -149,13 +145,9 @@ module Contrast
149
145
  # @param args [Array<Object>] The arguments passed to the method
150
146
  # being invoked.
151
147
  def apply_inventory method_policy, method, exception, object, args
152
- return unless INVENTORY.enabled?
148
+ return unless ::Contrast::INVENTORY.enabled?
153
149
 
154
- apply_trigger_only(method_policy&.inventory_node,
155
- method,
156
- exception,
157
- object,
158
- args)
150
+ apply_trigger_only(method_policy&.inventory_node, method, exception, object, args)
159
151
  end
160
152
 
161
153
  # Apply the Assess patches which apply to the given method.
@@ -175,18 +167,21 @@ module Contrast
175
167
  def apply_assess method_policy, preshift, object, ret, args, block
176
168
  source_ret = nil
177
169
  propagated_ret = nil
178
- return ret unless method_policy && ASSESS.enabled?
170
+ return ret unless method_policy && ::Contrast::ASSESS.enabled?
179
171
 
180
172
  current_context = Contrast::Agent::REQUEST_TRACKER.current
181
173
  return ret if current_context && !current_context.analyze_request?
182
174
 
183
175
  trigger_node = method_policy.trigger_node
184
- Contrast::Agent::Assess::Policy::TriggerMethod.apply_trigger_rule(trigger_node, object, ret, args) if trigger_node
176
+ if trigger_node
177
+ Contrast::Agent::Assess::Policy::TriggerMethod.apply_trigger_rule(trigger_node, object, ret, args)
178
+ end
185
179
  if method_policy.source_node
186
180
  # If we were given a frozen return, and it was the target of a
187
181
  # source, and we have frozen sources enabled, we'll need to
188
182
  # replace the return. Note, this is not the default case.
189
- source_ret = Contrast::Agent::Assess::Policy::SourceMethod.source_patchers(method_policy, object, ret, args)
183
+ source_ret = Contrast::Agent::Assess::Policy::SourceMethod.source_patchers(method_policy, object, ret,
184
+ args)
190
185
  end
191
186
  if method_policy.propagation_node
192
187
  propagated_ret = Contrast::Agent::Assess::Policy::PropagationMethod.apply_propagation(
@@ -277,9 +272,9 @@ module Contrast
277
272
  # <method_start>_unbound_<method_name>
278
273
  def build_unbound_method_name patcher_method
279
274
  (Contrast::Utils::ObjectShare::CONTRAST_PATCHED_METHOD_START +
280
- 'unbound' +
281
- Contrast::Utils::ObjectShare::UNDERSCORE +
282
- patcher_method.to_s).to_sym
275
+ 'unbound' +
276
+ Contrast::Utils::ObjectShare::UNDERSCORE +
277
+ patcher_method.to_s).to_sym
283
278
  end
284
279
 
285
280
  # @param mod [Module] the module in which the patch should be
@@ -339,7 +334,7 @@ module Contrast
339
334
  # @return [Symbol] new alias for the underlying method (presumably, so the patched method can call it)
340
335
  def register_c_patch target_module_name, unbound_method, impl = :alias_instance
341
336
  # These could be set as AfterLoadPatches.
342
- method_name = unbound_method.name.to_sym
337
+ method_name = unbound_method.name.to_sym # rubocop:disable Security/Module/Name -- ruby built in attribute.
343
338
  underlying_method_name = build_unbound_method_name(method_name).to_sym
344
339
 
345
340
  target_module = Module.cs__const_get(target_module_name)
@@ -68,7 +68,10 @@ module Contrast
68
68
  def set_info_for mod, method, method_policy, is_instance_method, cs_method
69
69
  mod.instance_variable_set(method_info_key, {}) unless mod.instance_variable_defined?(method_info_key)
70
70
  holder = mod.instance_variable_get(method_info_key)
71
- holder[method_name_key(method, is_instance_method)] = [method_policy, cs_method] unless holder.key?(method)
71
+ # if we already have this information, then we don't need to set it as we'll update the info on access
72
+ return if holder.key?(method)
73
+
74
+ holder[method_name_key(method, is_instance_method)] = [method_policy, cs_method]
72
75
  end
73
76
 
74
77
  private
@@ -153,9 +156,7 @@ module Contrast
153
156
  end
154
157
 
155
158
  def patched?
156
- @patch_status == :PATCHED ||
157
- @patch_status == :NONE ||
158
- @patch_status == :FAILED
159
+ @patch_status == :PATCHED || @patch_status == :NONE || @patch_status == :FAILED
159
160
  end
160
161
 
161
162
  def rewriting!
@@ -179,9 +180,7 @@ module Contrast
179
180
  end
180
181
 
181
182
  def rewritten?
182
- @rewrite_status == :REWRITTEN ||
183
- @rewrite_status == :NO_REWRITE ||
184
- @rewrite_status == :FAILED_REWRITE
183
+ @rewrite_status == :REWRITTEN || @rewrite_status == :NO_REWRITE || @rewrite_status == :FAILED_REWRITE
185
184
  end
186
185
  end
187
186
  end
@@ -7,13 +7,15 @@ require 'monitor'
7
7
  require 'contrast/agent/patching/policy/patch_status'
8
8
  require 'contrast/agent/patching/policy/method_policy'
9
9
  require 'contrast/agent/patching/policy/module_policy'
10
- require 'contrast/components/interface'
10
+ require 'contrast/components/logger'
11
+ require 'contrast/components/scope'
11
12
  require 'contrast/utils/class_util'
12
13
 
13
14
  # assess
14
15
  require 'contrast/agent/assess/policy/policy'
15
16
  require 'contrast/agent/assess/policy/policy_scanner'
16
- require 'contrast/agent/assess/policy/rewriter_patch'
17
+ # TODO: RUBY-714 remove guard w/ EOL of 2.5
18
+ require 'contrast/agent/assess/policy/rewriter_patch' if RUBY_VERSION < '2.6.0'
17
19
  require 'contrast/agent/assess/policy/source_method'
18
20
  require 'contrast/agent/assess/policy/trigger_method'
19
21
 
@@ -42,9 +44,8 @@ module Contrast
42
44
  # and how.
43
45
  module Patcher
44
46
  extend Contrast::Agent::Patching::Policy::AfterLoadPatcher
45
-
46
- include Contrast::Components::Interface
47
- access_component :agent, :analysis, :logging, :scope
47
+ extend Contrast::Components::Logger::InstanceMethods
48
+ extend Contrast::Components::Scope::InstanceMethods
48
49
 
49
50
  class << self
50
51
  # Hook to install the Contrast changes needed to allow for the
@@ -53,7 +54,7 @@ module Contrast
53
54
  def patch
54
55
  catchup_after_load_patches
55
56
  catchup_loaded_methods
56
- Contrast::Agent::Assess::Policy::RewriterPatch.rewrite_interpolations
57
+ Contrast::Agent::Assess::Policy::RewriterPatch.rewrite_interpolations if RUBY_VERSION < '2.6.0' # TODO: RUBY-714 remove guard w/ EOL of 2.5
57
58
  end
58
59
 
59
60
  # Hook to only monkeypatch Contrast. This will not trigger any
@@ -81,11 +82,11 @@ module Contrast
81
82
  with_contrast_scope do
82
83
  mod_name = mod.cs__name
83
84
  return unless Contrast::Utils::ClassUtil.truly_defined?(mod_name)
84
- return if AGENT.skip_instrumentation?(mod_name)
85
+ return if ::Contrast::AGENT.skip_instrumentation?(mod_name)
85
86
 
86
87
  load_patches_for_module(mod_name)
87
88
 
88
- return unless all_module_names.any? { |name| name == mod_name }
89
+ return unless all_module_names.any?(mod_name)
89
90
 
90
91
  module_data = Contrast::Agent::ModuleData.new(mod, mod_name)
91
92
  patch_into_module(module_data)
@@ -143,7 +144,8 @@ module Contrast
143
144
  # @return [Array<String>] the names of all the Modules for which
144
145
  # there patches in our policies
145
146
  def all_module_names
146
- @_all_module_names ||= POLICIES.each_with_object(Set.new) { |policy, set| set.merge(policy.instance.module_names) }
147
+ @_all_module_names ||=
148
+ POLICIES.each_with_object(Set.new) { |policy, set| set.merge(policy.instance.module_names) }
147
149
  end
148
150
 
149
151
  # Hook to only monkeypatch Contrast. This will not trigger any
@@ -154,7 +156,7 @@ module Contrast
154
156
  logger.trace_with_time('Running patching') do
155
157
  patched = []
156
158
  all_module_names.each do |patchable_name|
157
- next if AGENT.skip_instrumentation?(patchable_name)
159
+ next if ::Contrast::AGENT.skip_instrumentation?(patchable_name)
158
160
 
159
161
  patchable_mod = patchable(patchable_name)
160
162
  next unless patchable_mod
@@ -179,7 +181,7 @@ module Contrast
179
181
 
180
182
  # Begin patching our sources into the given module. Any patcher that has the name of the module will be
181
183
  # evaluated for patching. Find all the patchers that apply to this class, sorted by type.
182
- module_policy = Contrast::Agent::Patching::Policy::ModulePolicy.create_module_policy(module_data.name)
184
+ module_policy = Contrast::Agent::Patching::Policy::ModulePolicy.create_module_policy(module_data.mod_name)
183
185
  # If there's nothing to match, then set that status and exit
184
186
  if module_policy.empty?
185
187
  status.no_patch!
@@ -189,8 +191,7 @@ module Contrast
189
191
  status.patching!
190
192
  num_applied_patches = patch_into_instance_methods(module_data, module_policy)
191
193
  num_applied_patches += patch_into_singleton_methods(module_data, module_policy)
192
- if adjust_for_prepend(module_data) ||
193
- module_policy.num_expected_patches == num_applied_patches
194
+ if adjust_for_prepend(module_data) || module_policy.num_expected_patches == num_applied_patches
194
195
 
195
196
  status.patched!
196
197
  else
@@ -198,12 +199,12 @@ module Contrast
198
199
  end
199
200
  rescue StandardError => e
200
201
  status&.failed_patch!
201
- logger.warn('Patching failed', e, module: module_data.name)
202
+ logger.warn('Patching failed', e, module: module_data.mod_name)
202
203
  ensure
203
204
  logger.trace('Patching complete',
204
- module: module_data.name,
205
- result: Contrast::Agent::Patching::Policy::PatchStatus.get_status(
206
- module_data.mod).patch_status)
205
+ module: module_data.mod_name,
206
+ result:
207
+ Contrast::Agent::Patching::Policy::PatchStatus.get_status(module_data.mod).patch_status)
207
208
  end
208
209
 
209
210
  # Get all of the instance methods on the given module, excluding
@@ -270,7 +271,9 @@ module Contrast
270
271
  def patch_into_methods mod, methods, module_policy, is_instance_method
271
272
  count = 0
272
273
  methods.each do |method|
273
- method_policy = Contrast::Agent::Patching::Policy::MethodPolicy.build_method_policy(method, module_policy, is_instance_method)
274
+ method_policy = Contrast::Agent::Patching::Policy::MethodPolicy.build_method_policy(method,
275
+ module_policy,
276
+ is_instance_method)
274
277
  next if method_policy.empty?
275
278
 
276
279
  patched = patch_method(mod, methods, method_policy)
@@ -5,7 +5,7 @@ require 'json'
5
5
  require 'singleton'
6
6
 
7
7
  require 'contrast'
8
- require 'contrast/components/interface'
8
+ require 'contrast/components/logger'
9
9
  require 'contrast/agent/patching/policy/module_policy'
10
10
  require 'contrast/agent/patching/policy/method_policy'
11
11
 
@@ -19,7 +19,7 @@ module Contrast
19
19
  # @abstract
20
20
  class Policy
21
21
  include Singleton
22
- include Contrast::Components::Interface
22
+ include Contrast::Components::Logger::InstanceMethods
23
23
 
24
24
  # Indicates the folder in `resources` where this policy lives.
25
25
  def self.policy_folder
@@ -36,8 +36,6 @@ module Contrast
36
36
  raise(NoMethodError, 'specify the concrete node type for this poilcy')
37
37
  end
38
38
 
39
- access_component :analysis, :logging
40
-
41
39
  attr_reader :sources, :propagators, :triggers, :providers
42
40
 
43
41
  SOURCES_KEY = 'sources'
@@ -1,7 +1,7 @@
1
1
  # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'contrast/components/interface'
4
+ require 'contrast/components/scope'
5
5
 
6
6
  module Contrast
7
7
  module Agent
@@ -12,8 +12,7 @@ module Contrast
12
12
  #
13
13
  # @abstract
14
14
  class PolicyNode
15
- include Contrast::Components::Interface
16
- access_component :analysis, :scope
15
+ include Contrast::Components::Scope::InstanceMethods
17
16
 
18
17
  attr_accessor :class_name, :instance_method, :method_name, :method_visibility
19
18
  attr_reader :properties, :method_scope
@@ -44,10 +43,20 @@ module Contrast
44
43
  # later on. Really, if they don't have these things, they couldn't have
45
44
  # done their jobs anyway.
46
45
  def validate
47
- raise(ArgumentError, "#{ node_class } #{ id } did not have a proper class name. Unable to create.") unless class_name
48
- raise(ArgumentError, "#{ node_class } #{ id } did not have a proper method name. Unable to create.") unless method_name
49
- raise(ArgumentError, "#{ node_class } #{ id } has a non symbol @method_name value. Unable to create.") unless method_name.is_a?(Symbol)
50
- raise(ArgumentError, "#{ node_class } #{ id } has a non symbol @method_visibility value. Unable to create.") unless method_visibility.is_a?(Symbol)
46
+ unless class_name
47
+ raise(ArgumentError, "#{ node_class } #{ id } did not have a proper class name. Unable to create.")
48
+ end
49
+ unless method_name
50
+ raise(ArgumentError, "#{ node_class } #{ id } did not have a proper method name. Unable to create.")
51
+ end
52
+ unless method_name.is_a?(Symbol)
53
+ raise(ArgumentError, "#{ node_class } #{ id } has a non symbol @method_name value. Unable to create.")
54
+ end
55
+
56
+ unless method_visibility.is_a?(Symbol)
57
+ raise(ArgumentError,
58
+ "#{ node_class } #{ id } has a non symbol @method_visibility value. Unable to create.")
59
+ end
51
60
  unless method_scope.nil? || Contrast::Agent::Scope.valid_scope?(method_scope)
52
61
  raise(ArgumentError, "#{ node_class } #{ id } requires an undefined scope. Unable to create.")
53
62
  end
@@ -21,7 +21,8 @@ module Contrast
21
21
  JSON_OPTIONAL_PROPS = 'optional_properties'
22
22
  JSON_ON_EXCEPTION = 'on_exception'
23
23
 
24
- attr_reader :applicator, :applicator_method, :on_exception, :optional_properties, :required_properties, :rule_id
24
+ attr_reader :applicator, :applicator_method, :on_exception, :optional_properties, :required_properties,
25
+ :rule_id
25
26
 
26
27
  def initialize trigger_hash = {}, rule_hash = {}
27
28
  super(trigger_hash)
@@ -42,9 +43,10 @@ module Contrast
42
43
 
43
44
  def validate
44
45
  super
45
- unless applicator.public_methods(false).any? { |method| method == applicator_method }
46
+ unless applicator.public_methods(false).any?(applicator_method)
46
47
  raise(ArgumentError,
47
- "#{ id } did not have a proper applicator method: #{ applicator } does not respond to #{ applicator_method }. Unable to create.")
48
+ "#{ id } did not have a proper applicator method: "\
49
+ "#{ applicator } does not respond to #{ applicator_method }. Unable to create.")
48
50
  end
49
51
  validate_properties
50
52
  validate_rule
@@ -52,20 +54,31 @@ module Contrast
52
54
 
53
55
  def validate_properties
54
56
  if (required_properties & optional_properties).any?
55
- raise(ArgumentError, "#{ rule_id } had overlapping elements between required and optional properties. Unable to create.")
57
+ raise(ArgumentError,
58
+ "#{ rule_id } had overlapping elements between required and optional properties. Unable to create.")
56
59
  end
57
60
  if (properties.keys - (required_properties | optional_properties)).any?
58
61
  raise(ArgumentError, "#{ id } had an unexpected property. Unable to create.")
59
62
  end
60
- raise(ArgumentError, "#{ id } did not have a required property. Unable to create.") if (required_properties - properties.keys).any?
63
+
64
+ return unless (required_properties - properties.keys).any?
65
+
66
+ raise(ArgumentError, "#{ id } did not have a required property. Unable to create.")
61
67
  end
62
68
 
63
69
  def validate_rule
64
70
  raise(ArgumentError, 'Unknown rule did not have a proper name. Unable to create.') unless rule_id
65
71
  raise(ArgumentError, "#{ id } did not have a proper applicator. Unable to create.") unless applicator
66
- raise(ArgumentError, "#{ id } did not have a proper applicator method. Unable to create.") unless applicator_method
67
- raise(ArgumentError, "#{ id } did not have a proper set of required properties. Unable to create.") unless required_properties
68
- raise(ArgumentError, "#{ id } did not have a proper set of optional properties. Unable to create.") unless optional_properties
72
+
73
+ unless applicator_method
74
+ raise(ArgumentError, "#{ id } did not have a proper applicator method. Unable to create.")
75
+ end
76
+ unless required_properties
77
+ raise(ArgumentError, "#{ id } did not have a proper set of required properties. Unable to create.")
78
+ end
79
+ return if optional_properties
80
+
81
+ raise(ArgumentError, "#{ id } did not have a proper set of optional properties. Unable to create.")
69
82
  end
70
83
 
71
84
  private
@@ -37,7 +37,7 @@ module Contrast
37
37
 
38
38
  protected
39
39
 
40
- def name
40
+ def rule_name
41
41
  Contrast::Agent::Protect::Rule::CmdInjection::NAME
42
42
  end
43
43
 
@@ -71,7 +71,7 @@ module Contrast
71
71
 
72
72
  protected
73
73
 
74
- def name
74
+ def rule_name
75
75
  Contrast::Agent::Protect::Rule::Deserialization::NAME
76
76
  end
77
77
 
@@ -34,7 +34,7 @@ module Contrast
34
34
 
35
35
  protected
36
36
 
37
- def name
37
+ def rule_name
38
38
  Contrast::Agent::Protect::Rule::NoSqli::NAME
39
39
  end
40
40
 
@@ -42,15 +42,14 @@ module Contrast
42
42
 
43
43
  protected
44
44
 
45
- def name
45
+ def rule_name
46
46
  Contrast::Agent::Protect::Rule::PathTraversal::NAME
47
47
  end
48
48
 
49
49
  private
50
50
 
51
51
  def possible_write? input
52
- input.cs__respond_to?(:to_s) &&
53
- input.to_s.include?(Contrast::Utils::ObjectShare::WRITE_FLAG)
52
+ input.cs__respond_to?(:to_s) && input.to_s.include?(Contrast::Utils::ObjectShare::WRITE_FLAG)
54
53
  end
55
54
 
56
55
  READ = 'read'