contrast-agent 4.9.1 → 4.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (140) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec +0 -1
  3. data/.rspec_parallel +6 -0
  4. data/ext/cs__assess_module/cs__assess_module.c +48 -0
  5. data/ext/cs__assess_module/cs__assess_module.h +7 -0
  6. data/ext/cs__common/cs__common.c +24 -7
  7. data/ext/cs__common/cs__common.h +12 -2
  8. data/ext/cs__contrast_patch/cs__contrast_patch.c +48 -12
  9. data/ext/cs__contrast_patch/cs__contrast_patch.h +5 -4
  10. data/ext/cs__os_information/cs__os_information.c +31 -0
  11. data/ext/cs__os_information/cs__os_information.h +7 -0
  12. data/ext/{cs__protect_kernel → cs__os_information}/extconf.rb +0 -0
  13. data/lib/contrast/agent/assess/contrast_event.rb +1 -2
  14. data/lib/contrast/agent/assess/contrast_object.rb +1 -4
  15. data/lib/contrast/agent/assess/finalizers/hash.rb +0 -1
  16. data/lib/contrast/agent/assess/policy/dynamic_source_factory.rb +2 -0
  17. data/lib/contrast/agent/assess/policy/patcher.rb +0 -1
  18. data/lib/contrast/agent/assess/policy/policy_scanner.rb +0 -2
  19. data/lib/contrast/agent/assess/policy/preshift.rb +29 -12
  20. data/lib/contrast/agent/assess/policy/propagation_method.rb +71 -142
  21. data/lib/contrast/agent/assess/policy/propagation_node.rb +4 -4
  22. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +2 -2
  23. data/lib/contrast/agent/assess/policy/propagator/match_data.rb +31 -11
  24. data/lib/contrast/agent/assess/policy/propagator/remove.rb +4 -9
  25. data/lib/contrast/agent/assess/policy/propagator/split.rb +3 -2
  26. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +1 -0
  27. data/lib/contrast/agent/assess/policy/rewriter_patch.rb +0 -1
  28. data/lib/contrast/agent/assess/policy/source_method.rb +15 -88
  29. data/lib/contrast/agent/assess/policy/trigger/xpath.rb +0 -1
  30. data/lib/contrast/agent/assess/policy/trigger_method.rb +45 -172
  31. data/lib/contrast/agent/assess/policy/trigger_node.rb +52 -19
  32. data/lib/contrast/agent/assess/property/evented.rb +2 -1
  33. data/lib/contrast/agent/assess/property/tagged.rb +15 -132
  34. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +0 -1
  35. data/lib/contrast/agent/deadzone/policy/policy.rb +6 -0
  36. data/lib/contrast/agent/disable_reaction.rb +1 -1
  37. data/lib/contrast/agent/exclusion_matcher.rb +0 -4
  38. data/lib/contrast/agent/inventory/database_config.rb +117 -0
  39. data/lib/contrast/agent/inventory/dependency_usage_analysis.rb +7 -5
  40. data/lib/contrast/agent/inventory/policy/datastores.rb +2 -2
  41. data/lib/contrast/agent/metric_telemetry_event.rb +26 -0
  42. data/lib/contrast/agent/middleware.rb +23 -0
  43. data/lib/contrast/agent/patching/policy/after_load_patch.rb +3 -0
  44. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +17 -12
  45. data/lib/contrast/agent/patching/policy/method_policy.rb +54 -9
  46. data/lib/contrast/agent/patching/policy/module_policy.rb +2 -4
  47. data/lib/contrast/agent/patching/policy/patch.rb +42 -238
  48. data/lib/contrast/agent/patching/policy/patch_status.rb +3 -7
  49. data/lib/contrast/agent/patching/policy/patcher.rb +10 -49
  50. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +1 -1
  51. data/lib/contrast/agent/protect/rule/no_sqli.rb +7 -53
  52. data/lib/contrast/agent/protect/rule/sql_sample_builder.rb +137 -0
  53. data/lib/contrast/agent/protect/rule/sqli.rb +7 -70
  54. data/lib/contrast/agent/reaction_processor.rb +1 -1
  55. data/lib/contrast/agent/request.rb +9 -4
  56. data/lib/contrast/agent/request_context.rb +51 -33
  57. data/lib/contrast/agent/request_handler.rb +7 -3
  58. data/lib/contrast/agent/rule_set.rb +2 -4
  59. data/lib/contrast/agent/scope.rb +32 -20
  60. data/lib/contrast/agent/startup_metrics_telemetry_event.rb +71 -0
  61. data/lib/contrast/agent/static_analysis.rb +5 -3
  62. data/lib/contrast/agent/telemetry.rb +129 -0
  63. data/lib/contrast/agent/telemetry_event.rb +34 -0
  64. data/lib/contrast/agent/thread_watcher.rb +43 -14
  65. data/lib/contrast/agent/tracepoint_hook.rb +16 -3
  66. data/lib/contrast/agent/version.rb +1 -1
  67. data/lib/contrast/agent.rb +6 -1
  68. data/lib/contrast/api/communication/messaging_queue.rb +12 -6
  69. data/lib/contrast/api/communication/service_lifecycle.rb +4 -1
  70. data/lib/contrast/api/communication/socket_client.rb +4 -4
  71. data/lib/contrast/api/decorators/agent_startup.rb +4 -4
  72. data/lib/contrast/api/decorators/application_startup.rb +6 -5
  73. data/lib/contrast/api/decorators/route_coverage.rb +24 -1
  74. data/lib/contrast/components/agent.rb +5 -2
  75. data/lib/contrast/components/api.rb +34 -0
  76. data/lib/contrast/components/app_context.rb +24 -0
  77. data/lib/contrast/components/assess.rb +13 -3
  78. data/lib/contrast/components/base.rb +2 -2
  79. data/lib/contrast/components/config.rb +91 -11
  80. data/lib/contrast/components/contrast_service.rb +10 -2
  81. data/lib/contrast/components/logger.rb +13 -8
  82. data/lib/contrast/components/scope.rb +9 -28
  83. data/lib/contrast/config/api_configuration.rb +22 -0
  84. data/lib/contrast/config/assess_configuration.rb +1 -0
  85. data/lib/contrast/config/base_configuration.rb +14 -6
  86. data/lib/contrast/config/env_variables.rb +25 -0
  87. data/lib/contrast/config/root_configuration.rb +1 -0
  88. data/lib/contrast/config/service_configuration.rb +2 -1
  89. data/lib/contrast/config.rb +1 -0
  90. data/lib/contrast/configuration.rb +22 -15
  91. data/lib/contrast/extension/assess/array.rb +1 -11
  92. data/lib/contrast/extension/assess/eval_trigger.rb +0 -20
  93. data/lib/contrast/extension/assess/fiber.rb +0 -11
  94. data/lib/contrast/extension/assess/hash.rb +0 -10
  95. data/lib/contrast/extension/assess/kernel.rb +1 -10
  96. data/lib/contrast/extension/assess/marshal.rb +3 -11
  97. data/lib/contrast/extension/assess/regexp.rb +0 -11
  98. data/lib/contrast/extension/assess/string.rb +1 -26
  99. data/lib/contrast/extension/extension.rb +61 -0
  100. data/lib/contrast/framework/grape/support.rb +174 -0
  101. data/lib/contrast/framework/manager.rb +56 -18
  102. data/lib/contrast/framework/rack/support.rb +1 -1
  103. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +9 -6
  104. data/lib/contrast/framework/rails/patch/assess_configuration.rb +0 -1
  105. data/lib/contrast/framework/rails/patch/support.rb +35 -30
  106. data/lib/contrast/framework/rails/railtie.rb +1 -1
  107. data/lib/contrast/framework/rails/rewrite/active_record_named.rb +1 -0
  108. data/lib/contrast/framework/rails/support.rb +60 -13
  109. data/lib/contrast/framework/sinatra/support.rb +1 -1
  110. data/lib/contrast/logger/application.rb +4 -0
  111. data/lib/contrast/logger/log.rb +89 -15
  112. data/lib/contrast/utils/assess/propagation_method_utils.rb +129 -0
  113. data/lib/contrast/utils/assess/property/tagged_utils.rb +142 -0
  114. data/lib/contrast/utils/assess/source_method_utils.rb +83 -0
  115. data/lib/contrast/utils/assess/trigger_method_utils.rb +138 -0
  116. data/lib/contrast/utils/class_util.rb +58 -44
  117. data/lib/contrast/utils/exclude_key.rb +20 -0
  118. data/lib/contrast/utils/io_util.rb +43 -35
  119. data/lib/contrast/utils/lru_cache.rb +45 -0
  120. data/lib/contrast/utils/metrics_hash.rb +59 -0
  121. data/lib/contrast/utils/os.rb +23 -0
  122. data/lib/contrast/utils/patching/policy/patch_utils.rb +232 -0
  123. data/lib/contrast/utils/patching/policy/patcher_utils.rb +54 -0
  124. data/lib/contrast/utils/requests_client.rb +150 -0
  125. data/lib/contrast/utils/ruby_ast_rewriter.rb +16 -13
  126. data/lib/contrast/utils/tag_util.rb +2 -1
  127. data/lib/contrast/utils/telemetry.rb +78 -0
  128. data/lib/contrast/utils/telemetry_identifier.rb +137 -0
  129. data/lib/contrast.rb +19 -1
  130. data/resources/assess/policy.json +208 -7
  131. data/resources/deadzone/policy.json +91 -0
  132. data/ruby-agent.gemspec +12 -2
  133. data/service_executables/VERSION +1 -1
  134. data/service_executables/linux/contrast-service +0 -0
  135. data/service_executables/mac/contrast-service +0 -0
  136. metadata +102 -18
  137. data/ext/cs__protect_kernel/cs__protect_kernel.c +0 -47
  138. data/ext/cs__protect_kernel/cs__protect_kernel.h +0 -12
  139. data/lib/contrast/extension/protect/kernel.rb +0 -39
  140. data/lib/contrast/utils/inventory_util.rb +0 -113
@@ -94,10 +94,6 @@ module Contrast
94
94
  @exclusion.type == Contrast::Api::Settings::Exclusion::ExclusionType::CODE
95
95
  end
96
96
 
97
- def exc_name
98
- @exclusion.name # rubocop:disable Security/Module/Name -- part of the API.
99
- end
100
-
101
97
  def match_all?
102
98
  @exclusion.urls.nil? || @exclusion.urls.empty?
103
99
  end
@@ -0,0 +1,117 @@
1
+ # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ require 'contrast/utils/timer'
5
+ require 'contrast/utils/object_share'
6
+ require 'contrast/components/logger'
7
+
8
+ module Contrast
9
+ module Agent
10
+ module Inventory
11
+ # Methods used for parsing database connection configurations
12
+ # for getting inventory information from the application
13
+ module DatabaseConfig
14
+ extend Contrast::Components::Logger::InstanceMethods
15
+
16
+ # TeamServer only accepts certain values for ArchitectureComponents.
17
+ # DO NOT CHANGE THIS!
18
+ AC_TYPE_DB = 'db'
19
+ # TeamServer only accepts certain values for FlowMap Services.
20
+ # DO NOT CHANGE THIS
21
+ ADAPTER = 'adapter'
22
+ HOST = 'host'
23
+ PORT = 'port'
24
+ DATABASE = 'database'
25
+ DEFAULT = 'default'
26
+ LOCALHOST = 'localhost'
27
+
28
+ def self.active_record_config
29
+ return @_active_record_config if instance_variable_defined?(:@_active_record_config)
30
+
31
+ @_active_record_config = ActiveRecord::Base.connection_config rescue nil # rubocop:disable Style/RescueModifier
32
+ end
33
+
34
+ def self.append_db_config(activity_or_update,
35
+ hash_or_str = Contrast::Agent::Inventory::DatabaseConfig.active_record_config)
36
+ arr = build_from_db_config(hash_or_str)
37
+ return unless arr&.any?
38
+
39
+ arr.each do |a|
40
+ next unless a
41
+
42
+ if activity_or_update.is_a?(Contrast::Api::Dtm::Activity)
43
+ activity_or_update.architectures << a
44
+ else
45
+ activity_or_update.components << a
46
+ end
47
+ end
48
+ rescue StandardError => e
49
+ logger.error('Unable to append db config', e)
50
+ nil
51
+ end
52
+
53
+ def self.build_from_db_config hash_or_str
54
+ return unless hash_or_str
55
+
56
+ if hash_or_str.is_a?(Hash)
57
+ build_from_db_hash(hash_or_str)
58
+ else
59
+ build_from_db_string(hash_or_str.to_s)
60
+ end
61
+ end
62
+
63
+ def self.build_from_db_hash hash
64
+ ac = Contrast::Api::Dtm::ArchitectureComponent.new
65
+ ac.vendor = hash[:adapter] || hash[ADAPTER] || Contrast::Utils::ObjectShare::EMPTY_STRING
66
+ ac.remote_host = host_from_hash(hash)
67
+ ac.remote_port = port_from_hash(hash)
68
+ ac.type = AC_TYPE_DB
69
+ ac.url = hash[:database] || hash[DATABASE] || DEFAULT
70
+ [ac]
71
+ end
72
+
73
+ def self.host_from_hash hash
74
+ hash[:host] || hash[HOST] || Contrast::Utils::ObjectShare::EMPTY_STRING
75
+ end
76
+
77
+ def self.port_from_hash hash
78
+ p = hash[:port] || hash[PORT] || Contrast::Utils::ObjectShare::EMPTY_STRING
79
+ p.to_i
80
+ end
81
+
82
+ # Examples:
83
+ # mongodb://[user:pass@]host1[:port1][,host2[:port2],[,hostN[:portN]]][/[database][?options]]
84
+ # postgresql://scott:tiger@localhost/mydatabase
85
+ # mysql+mysqlconnector://scott:tiger@localhost/foo
86
+ def self.build_from_db_string str
87
+ adapter, hosts, database = split_connection_str(str)
88
+ acs = []
89
+ hosts.split(Contrast::Utils::ObjectShare::COMMA).map do |s|
90
+ host, port = s.split(Contrast::Utils::ObjectShare::COLON)
91
+
92
+ ac = Contrast::Api::Dtm::ArchitectureComponent.new
93
+ ac.vendor = Contrast::Utils::StringUtils.force_utf8(adapter)
94
+ ac.remote_host = Contrast::Utils::StringUtils.force_utf8(host)
95
+ ac.remote_port = port.to_i
96
+ ac.type = AC_TYPE_DB
97
+ ac.url = Contrast::Utils::StringUtils.force_utf8(database)
98
+ acs << ac
99
+ end
100
+ acs
101
+ end
102
+
103
+ def self.split_connection_str str
104
+ adapter, str = str.split(Contrast::Utils::ObjectShare::COLON_SLASH_SLASH)
105
+ _auth, str = str.split(Contrast::Utils::ObjectShare::AT)
106
+ # Not currently used
107
+ # user, pass = auth.split(Contrast::Utils::ObjectShare::COLON)
108
+ hosts, db_and_options = str.split(Contrast::Utils::ObjectShare::SLASH)
109
+ hosts << LOCALHOST if hosts.empty?
110
+ database, _options = db_and_options.split(Contrast::Utils::ObjectShare::QUESTION_MARK)
111
+
112
+ [adapter, hosts, database]
113
+ end
114
+ end
115
+ end
116
+ end
117
+ end
@@ -60,7 +60,7 @@ module Contrast
60
60
 
61
61
  digest = Contrast::Utils::Sha256Builder.instance.build_from_spec(spec)
62
62
  unless digest
63
- logger.debug('Unable to resolve digest for gem spec', spec: spec.to_s)
63
+ logger.debug('Unable to resolve digest for gem spec', spec: spec.to_s) if logger.debug?
64
64
  return
65
65
  end
66
66
  report_path = adjust_path_for_reporting(path, spec)
@@ -71,16 +71,18 @@ module Contrast
71
71
 
72
72
  # Populate the library_usages field of the Activity message using the data stored in the @gemdigest_cache.
73
73
  #
74
+ # TODO: RUBY-1355
74
75
  # @param activity [Contrast::Api::Dtm::Activity] the message to which to append the usage data
75
76
  def generate_library_usage activity
76
77
  return unless enabled?
77
78
  return unless activity
78
79
 
79
- # Copy gemdigest_cache and clear it in sync.
80
+ # Disconnect gemdigest_cache and replace it with an empty one; synch so new libs cannot be added between the
81
+ # assignment and the replace
80
82
  gem_spec_digest_to_files = @lock.synchronize do
81
- copy = @gemdigest_cache.dup
82
- @gemdigest_cache.clear
83
- copy
83
+ hold = @gemdigest_cache
84
+ @gemdigest_cache = Hash.new { |hash, key| hash[key] = Set.new }
85
+ hold
84
86
  end
85
87
 
86
88
  gem_spec_digest_to_files.each_pair do |digest, files|
@@ -2,7 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/components/logger'
5
- require 'contrast/utils/inventory_util'
5
+ require 'contrast/agent/inventory/database_config'
6
6
 
7
7
  module Contrast
8
8
  module Agent
@@ -42,7 +42,7 @@ module Contrast
42
42
  context.activity.query_count += 1
43
43
  return unless context.activity.query_count == 1
44
44
 
45
- Contrast::Utils::InventoryUtil.append_db_config(context.activity)
45
+ Contrast::Agent::Inventory::DatabaseConfig.append_db_config(context.activity)
46
46
  end
47
47
  end
48
48
  end
@@ -0,0 +1,26 @@
1
+ # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ require 'contrast/utils/metrics_hash'
5
+ require 'contrast/agent/telemetry_event'
6
+
7
+ module Contrast
8
+ module Agent
9
+ # This class will hold the basic information for a Telemetry Event
10
+ class MetricTelemetryEvent < Contrast::Agent::TelemetryEvent
11
+ include Contrast::Utils
12
+
13
+ attr_reader :fields
14
+
15
+ def initialize
16
+ super
17
+ @fields = MetricsHash.new(Numeric)
18
+ @fields['_filter'] = 0
19
+ end
20
+
21
+ def to_json **_args
22
+ super.merge!({ fields: @fields })
23
+ end
24
+ end
25
+ end
26
+ end
@@ -10,8 +10,10 @@ require 'contrast/utils/object_share'
10
10
  require 'contrast/components/logger'
11
11
  require 'contrast/components/scope'
12
12
  require 'contrast/utils/heap_dump_util'
13
+ require 'contrast/utils/telemetry'
13
14
  require 'contrast/agent/request_handler'
14
15
  require 'contrast/agent/static_analysis'
16
+ require 'contrast/agent/startup_metrics_telemetry_event'
15
17
 
16
18
  require 'contrast/utils/timer'
17
19
 
@@ -60,6 +62,7 @@ module Contrast
60
62
  handle_first_request
61
63
  call_with_agent(env)
62
64
  end
65
+ ::Contrast::Components::Logger.add_trace_log_timing_for(::Contrast::Agent::Middleware, :call)
63
66
 
64
67
  private
65
68
 
@@ -67,6 +70,7 @@ module Contrast
67
70
  ::Contrast::SETTINGS.reset_state
68
71
 
69
72
  inform_deprecations
73
+ telemetry_disclaimer
70
74
 
71
75
  if ::Contrast::CONFIG.invalid?
72
76
  ::Contrast::AGENT.disable!
@@ -88,6 +92,13 @@ module Contrast
88
92
  Contrast::Agent.thread_watcher.ensure_running?
89
93
  end
90
94
 
95
+ if Contrast::Agent::Telemetry.enabled?
96
+ logger.debug_with_time('middleware: sending startup metrics telemetry event') do
97
+ event = Contrast::Agent::StartupMetricsTelemetryEvent.new
98
+ Contrast::Agent.thread_watcher.telemetry_queue.send_event(event)
99
+ end
100
+ end
101
+
91
102
  logger.debug_with_time('middleware: instrument shared libraries and patch') do
92
103
  Contrast::Agent::Patching::Policy::Patcher.patch
93
104
  end
@@ -224,6 +235,18 @@ module Contrast
224
235
  Kernel.warn('[Contrast Security] [DEPRECATION] Support for Ruby 2.5 will be removed in April 2021. '\
225
236
  'Please contact Customer Support prior if you require continued support.')
226
237
  end
238
+
239
+ # Displays Telemetry disclaimer if Telemetry is enabled.
240
+ # if .telemetry file doesn't exist we create one and then show the disclaimer.
241
+ # if the file already exists we do nothing.
242
+ def telemetry_disclaimer
243
+ return unless Contrast::Agent::Telemetry.enabled?
244
+ return unless Contrast::Utils::Telemetry.create_telemetry_file
245
+
246
+ logger.info Contrast::Utils::Telemetry.disclaimer
247
+ $stdout.print Contrast::Utils::Telemetry.disclaimer
248
+ true
249
+ end
227
250
  end
228
251
  end
229
252
  end
@@ -59,7 +59,10 @@ module Contrast
59
59
  end
60
60
 
61
61
  def instrument!
62
+ return if instrumenting_module == :'Contrast::Framework::Rails::Rewrite' && RAILS_VER >= '2.6.0'
63
+
62
64
  require instrumentation_file_path
65
+
63
66
  if instrumenting_module
64
67
  mod = Module.cs__const_get(instrumenting_module)
65
68
  with_contrast_scope { mod.instrument } if mod
@@ -4,6 +4,7 @@
4
4
  require 'contrast/agent/patching/policy/after_load_patch'
5
5
  require 'contrast/components/logger'
6
6
  require 'contrast/framework/manager'
7
+ require 'contrast/extension/extension'
7
8
 
8
9
  module Contrast
9
10
  module Agent
@@ -29,18 +30,22 @@ module Contrast
29
30
  # extensions.
30
31
  def apply_direct_patches!
31
32
  @_apply_direct_patches ||= begin
32
- Contrast::Extension::Assess::ArrayPropagator.instrument_array_track
33
- Contrast::Extension::Assess::EvalTrigger.instrument_basic_object_track
34
- Contrast::Extension::Assess::EvalTrigger.instrument_module_track
35
- Contrast::Extension::Assess::FiberPropagator.instrument_fiber_track
36
- Contrast::Extension::Assess::HashPropagator.instrument_hash_track
37
- Contrast::Extension::Assess::KernelPropagator.instrument_kernel_track
38
- Contrast::Extension::Assess::MarshalPropagator.instrument_marshal_load
39
- Contrast::Extension::Assess::RegexpPropagator.instrument_regexp_track
40
- Contrast::Extension::Assess::StringPropagator.instrument_string
41
- Contrast::Extension::Assess::StringPropagator.instrument_string_interpolation
42
-
43
- Contrast::Extension::Protect::Kernel.instrument
33
+ paths = %w[
34
+ array
35
+ basic_object
36
+ module
37
+ fiber_track
38
+ hash
39
+ kernel
40
+ marshal_module
41
+ regexp
42
+ string
43
+ string_interpolation26
44
+ ].cs__freeze
45
+ paths.each do |p|
46
+ path_part = "cs__assess_#{ p }"
47
+ Contrast::Extension::Assess::InstrumentHelper.instrument "#{ path_part }/#{ path_part }"
48
+ end
44
49
  true
45
50
  end
46
51
  end
@@ -81,15 +81,21 @@ module Contrast
81
81
  deadzone_node = find_method_node(module_policy.deadzone_nodes, method_name, instance_method)
82
82
  method_visibility = find_visibility(source_node, propagation_node, trigger_node, protect_node,
83
83
  inventory_node, deadzone_node)
84
- MethodPolicy.new(method_name: method_name,
85
- method_visibility: method_visibility,
86
- instance_method: instance_method,
87
- source_node: source_node,
88
- propagation_node: propagation_node,
89
- trigger_node: trigger_node,
90
- protect_node: protect_node,
91
- inventory_node: inventory_node,
92
- deadzone_node: deadzone_node)
84
+ method_policy = MethodPolicy.new(method_name: method_name,
85
+ method_visibility: method_visibility,
86
+ instance_method: instance_method,
87
+ source_node: source_node,
88
+ propagation_node: propagation_node,
89
+ trigger_node: trigger_node,
90
+ protect_node: protect_node,
91
+ inventory_node: inventory_node,
92
+ deadzone_node: deadzone_node)
93
+
94
+ return method_policy unless check_method_policy_nodes_empty? source_node, propagation_node, trigger_node,
95
+ protect_node, inventory_node, deadzone_node
96
+
97
+ create_new_node(module_policy, method_policy) if module_policy.deadzone_nodes&.any?
98
+ method_policy
93
99
  end
94
100
 
95
101
  def find_method_node nodes, method_name, is_instance_method
@@ -103,6 +109,45 @@ module Contrast
103
109
  def find_visibility *nodes
104
110
  nodes.find { |node| node }&.method_visibility
105
111
  end
112
+
113
+ def check_method_policy_nodes_empty?(source_node, propagation_node, trigger_node, protect_node,
114
+ inventory_node, deadzone_node)
115
+ return false unless source_node.nil? && propagation_node.nil? && trigger_node.nil? && protect_node.nil? &&
116
+ inventory_node.nil? && deadzone_node.nil?
117
+
118
+ true
119
+ end
120
+
121
+ private
122
+
123
+ def create_new_node module_policy, method_policy
124
+ return if module_policy.deadzone_nodes.empty?
125
+
126
+ module_policy.deadzone_nodes.map do |node|
127
+ next unless node.method_name.nil?
128
+
129
+ klass = Module.cs__const_get(node.class_name)
130
+ next unless it_defined? klass, method_policy.method_name
131
+
132
+ new_node = {}
133
+ new_node['instance_method'] = method_policy.instance_method
134
+ new_node['method_visibility'] =
135
+ klass.private_method_defined?(method_policy.method_name) ? 'private' : 'public'
136
+ new_node['method_name'] = method_policy.method_name
137
+ new_node['class_name'] = node.class_name
138
+ new_node = Contrast::Agent::Deadzone::Policy::DeadzoneNode.new(new_node)
139
+ method_policy.instance_variable_set(:@method_visibility, new_node.method_visibility)
140
+ method_policy.instance_variable_set(:@deadzone_node, new_node)
141
+ module_policy.deadzone_nodes << new_node
142
+ break unless method_policy.deadzone_node.nil?
143
+ end
144
+ end
145
+
146
+ def it_defined? klass, method_name
147
+ klass.instance_methods(false).include?(method_name) ||
148
+ klass.private_instance_methods(false).include?(method_name) ||
149
+ klass.singleton_methods(false).include?(method_name)
150
+ end
106
151
  end
107
152
  end
108
153
  end
@@ -12,11 +12,9 @@ module Contrast
12
12
  # rather than new.
13
13
  class ModulePolicy
14
14
  class << self
15
- # Given the name of a module, create a :ModulePolicy for it using
16
- # the Policy of each supported feature
15
+ # Given the name of a module, create a :ModulePolicy for it using the Policy of each supported feature.
17
16
  #
18
- # @param module_name [String] the name of the module to which the
19
- # policy applies
17
+ # @param module_name [String] the name of the module to which the policy applies.
20
18
  # @return [Contrast::Agent::Patching::Policy::ModulePolicy]
21
19
  def create_module_policy module_name
22
20
  module_policy = Contrast::Agent::Patching::Policy::ModulePolicy.new