contrast-agent 4.9.0 → 4.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (109) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec +0 -1
  3. data/.rspec_parallel +6 -0
  4. data/ext/cs__common/cs__common.c +19 -7
  5. data/ext/cs__common/cs__common.h +4 -2
  6. data/ext/cs__contrast_patch/cs__contrast_patch.c +32 -11
  7. data/ext/cs__contrast_patch/cs__contrast_patch.h +5 -4
  8. data/lib/contrast/agent/assess/contrast_event.rb +1 -2
  9. data/lib/contrast/agent/assess/contrast_object.rb +1 -4
  10. data/lib/contrast/agent/assess/finalizers/hash.rb +0 -1
  11. data/lib/contrast/agent/assess/policy/dynamic_source_factory.rb +2 -0
  12. data/lib/contrast/agent/assess/policy/patcher.rb +0 -1
  13. data/lib/contrast/agent/assess/policy/policy_scanner.rb +0 -2
  14. data/lib/contrast/agent/assess/policy/preshift.rb +29 -12
  15. data/lib/contrast/agent/assess/policy/propagation_method.rb +100 -57
  16. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +2 -2
  17. data/lib/contrast/agent/assess/policy/propagator/match_data.rb +31 -11
  18. data/lib/contrast/agent/assess/policy/propagator/remove.rb +4 -9
  19. data/lib/contrast/agent/assess/policy/propagator/split.rb +3 -2
  20. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +1 -0
  21. data/lib/contrast/agent/assess/policy/rewriter_patch.rb +0 -1
  22. data/lib/contrast/agent/assess/policy/source_method.rb +13 -17
  23. data/lib/contrast/agent/assess/policy/trigger/xpath.rb +0 -1
  24. data/lib/contrast/agent/assess/policy/trigger_method.rb +60 -85
  25. data/lib/contrast/agent/assess/policy/trigger_node.rb +52 -19
  26. data/lib/contrast/agent/assess/property/evented.rb +2 -1
  27. data/lib/contrast/agent/assess/property/tagged.rb +34 -25
  28. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +0 -1
  29. data/lib/contrast/agent/deadzone/policy/policy.rb +6 -0
  30. data/lib/contrast/agent/disable_reaction.rb +1 -1
  31. data/lib/contrast/agent/exclusion_matcher.rb +0 -4
  32. data/lib/contrast/agent/inventory/database_config.rb +117 -0
  33. data/lib/contrast/agent/inventory/dependency_usage_analysis.rb +6 -5
  34. data/lib/contrast/agent/inventory/policy/datastores.rb +2 -2
  35. data/lib/contrast/agent/middleware.rb +1 -0
  36. data/lib/contrast/agent/patching/policy/after_load_patch.rb +3 -0
  37. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +17 -12
  38. data/lib/contrast/agent/patching/policy/method_policy.rb +54 -9
  39. data/lib/contrast/agent/patching/policy/module_policy.rb +2 -4
  40. data/lib/contrast/agent/patching/policy/patch.rb +17 -6
  41. data/lib/contrast/agent/patching/policy/patch_status.rb +3 -7
  42. data/lib/contrast/agent/patching/policy/patcher.rb +9 -9
  43. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +1 -1
  44. data/lib/contrast/agent/protect/rule/no_sqli.rb +7 -53
  45. data/lib/contrast/agent/protect/rule/sql_sample_builder.rb +137 -0
  46. data/lib/contrast/agent/protect/rule/sqli.rb +7 -70
  47. data/lib/contrast/agent/reaction_processor.rb +1 -1
  48. data/lib/contrast/agent/request.rb +9 -4
  49. data/lib/contrast/agent/request_context.rb +51 -33
  50. data/lib/contrast/agent/rule_set.rb +2 -4
  51. data/lib/contrast/agent/scope.rb +32 -20
  52. data/lib/contrast/agent/static_analysis.rb +1 -1
  53. data/lib/contrast/agent/tracepoint_hook.rb +16 -3
  54. data/lib/contrast/agent/version.rb +1 -1
  55. data/lib/contrast/agent.rb +0 -1
  56. data/lib/contrast/api/communication/messaging_queue.rb +12 -6
  57. data/lib/contrast/api/communication/service_lifecycle.rb +4 -1
  58. data/lib/contrast/api/communication/socket_client.rb +4 -4
  59. data/lib/contrast/api/decorators/agent_startup.rb +4 -4
  60. data/lib/contrast/api/decorators/application_startup.rb +6 -5
  61. data/lib/contrast/api/decorators/route_coverage.rb +24 -1
  62. data/lib/contrast/components/agent.rb +5 -2
  63. data/lib/contrast/components/assess.rb +13 -3
  64. data/lib/contrast/components/base.rb +2 -2
  65. data/lib/contrast/components/config.rb +1 -0
  66. data/lib/contrast/components/contrast_service.rb +4 -2
  67. data/lib/contrast/components/logger.rb +13 -8
  68. data/lib/contrast/components/scope.rb +9 -28
  69. data/lib/contrast/config/assess_configuration.rb +1 -0
  70. data/lib/contrast/config/base_configuration.rb +14 -6
  71. data/lib/contrast/configuration.rb +19 -15
  72. data/lib/contrast/extension/assess/array.rb +1 -11
  73. data/lib/contrast/extension/assess/eval_trigger.rb +0 -20
  74. data/lib/contrast/extension/assess/fiber.rb +0 -11
  75. data/lib/contrast/extension/assess/hash.rb +0 -10
  76. data/lib/contrast/extension/assess/kernel.rb +1 -10
  77. data/lib/contrast/extension/assess/marshal.rb +3 -11
  78. data/lib/contrast/extension/assess/regexp.rb +0 -11
  79. data/lib/contrast/extension/assess/string.rb +1 -26
  80. data/lib/contrast/extension/extension.rb +61 -0
  81. data/lib/contrast/framework/grape/support.rb +174 -0
  82. data/lib/contrast/framework/manager.rb +42 -6
  83. data/lib/contrast/framework/rack/support.rb +1 -1
  84. data/lib/contrast/framework/rails/patch/assess_configuration.rb +0 -1
  85. data/lib/contrast/framework/rails/patch/support.rb +6 -3
  86. data/lib/contrast/framework/rails/railtie.rb +1 -1
  87. data/lib/contrast/framework/rails/rewrite/active_record_named.rb +1 -0
  88. data/lib/contrast/framework/rails/support.rb +60 -13
  89. data/lib/contrast/framework/sinatra/support.rb +1 -1
  90. data/lib/contrast/logger/log.rb +89 -15
  91. data/lib/contrast/tasks/config.rb +0 -1
  92. data/lib/contrast/utils/class_util.rb +58 -44
  93. data/lib/contrast/utils/io_util.rb +43 -35
  94. data/lib/contrast/utils/lru_cache.rb +45 -0
  95. data/lib/contrast/utils/ruby_ast_rewriter.rb +16 -13
  96. data/lib/contrast/utils/tag_util.rb +2 -1
  97. data/lib/contrast.rb +1 -1
  98. data/resources/assess/policy.json +208 -7
  99. data/resources/deadzone/policy.json +91 -0
  100. data/ruby-agent.gemspec +10 -2
  101. data/service_executables/VERSION +1 -1
  102. data/service_executables/linux/contrast-service +0 -0
  103. data/service_executables/mac/contrast-service +0 -0
  104. metadata +74 -26
  105. data/ext/cs__protect_kernel/cs__protect_kernel.c +0 -47
  106. data/ext/cs__protect_kernel/cs__protect_kernel.h +0 -12
  107. data/ext/cs__protect_kernel/extconf.rb +0 -5
  108. data/lib/contrast/extension/protect/kernel.rb +0 -39
  109. data/lib/contrast/utils/inventory_util.rb +0 -113
@@ -125,6 +125,27 @@ module Contrast
125
125
  tags[label] = tag_ranges
126
126
  end
127
127
 
128
+ # Returns a list of all current tags.
129
+ #
130
+ # @return [Hash<Contrast::Agent::Assess::Tag>]
131
+ def get_tags # rubocop:disable Naming/AccessorMethodName
132
+ return Contrast::Utils::ObjectShare::EMPTY_HASH unless tracked?
133
+
134
+ tags
135
+ end
136
+
137
+ # We'll use this as a helper method to retrieve tags from the hash.
138
+ # Because the hash auto-populates an empty array when we try to
139
+ # access a tag in it, we cannot use the [] method without side
140
+ # effect. To get around this, we'll use a fetch work around.
141
+ #
142
+ # @param label [Symbol] the label to look up
143
+ # @return [Array<Contrast::Agent::Assess::Tag>] all the tags with
144
+ # that label
145
+ def fetch_tag label
146
+ get_tags.fetch(label, nil) if tracked?
147
+ end
148
+
128
149
  # Remove all tags with a given label
129
150
  def delete_tags label
130
151
  tags.delete(label) if tracked?
@@ -152,18 +173,6 @@ module Contrast
152
173
  tags.delete_if { |_, value| value.empty? }
153
174
  end
154
175
 
155
- # We'll use this as a helper method to retrieve tags from the hash.
156
- # Because the hash auto-populates an empty array when we try to
157
- # access a tag in it, we cannot use the [] method without side
158
- # effect. To get around this, we'll use a fetch work around.
159
- #
160
- # @param label [Symbol] the label to look up
161
- # @return [Array<Contrast::Agent::Assess::Tag>] all the tags with
162
- # that label
163
- def fetch_tag label
164
- tags.fetch(label, nil) if tracked?
165
- end
166
-
167
176
  # Remove all tags within the given ranges.
168
177
  # This does not delete an entire tag if part of that tag is
169
178
  # outside this range, meaning we may reduce sizes of tags
@@ -218,19 +227,6 @@ module Contrast
218
227
  end
219
228
  end
220
229
 
221
- # Because of the auto-fill thing, we should not allow direct access to
222
- # the tags hash. Instead, the methods above should be used to do
223
- # operations like add, delete, and fetch.
224
- #
225
- # CONTRAST-22914
226
- # please do NOT expose this w/ an attr_reader / accessor. there are
227
- # helper methods in this class that safely access the hash. the tags
228
- # method is private to avoid the side effect of a direct lookup with
229
- # `[]` adding an empty array to the hash.
230
- def tags
231
- @_tags ||= Hash.new { |h, k| h[k] = [] }
232
- end
233
-
234
230
  # Remove the tag ranges covering the given range
235
231
  def remove_tags range
236
232
  return unless tracked?
@@ -334,6 +330,19 @@ module Contrast
334
330
 
335
331
  private
336
332
 
333
+ # Because of the auto-fill thing, we should not allow direct access to
334
+ # the tags hash. Instead, the methods above should be used to do
335
+ # operations like add, delete, and fetch.
336
+ #
337
+ # CONTRAST-22914
338
+ # please do NOT expose this w/ an attr_reader / accessor. there are
339
+ # helper methods in this class that safely access the hash. the tags
340
+ # method is private to avoid the side effect of a direct lookup with
341
+ # `[]` adding an empty array to the hash.
342
+ def tags
343
+ @_tags ||= Hash.new { |h, k| h[k] = [] }
344
+ end
345
+
337
346
  # Given a tag, compare it to a given range and, if any part of that tag is within the range, return a new tag
338
347
  # covering the union of the original tag and the range. This new tag will start at the
339
348
  # max(tag.start, range.start) and end at min(tag.end, range.end)
@@ -20,7 +20,6 @@ module Contrast
20
20
  module HardcodedValueRule
21
21
  include Contrast::Components::Logger::InstanceMethods
22
22
 
23
-
24
23
  def disabled?
25
24
  !::Contrast::ASSESS.enabled? || ::Contrast::ASSESS.rule_disabled?(rule_id)
26
25
  end
@@ -35,6 +35,12 @@ module Contrast
35
35
  end
36
36
  end
37
37
 
38
+ def validate
39
+ return if class_name
40
+
41
+ raise(ArgumentError, "#{ @node_class } #{ id } did not have a proper class name. Unable to create.")
42
+ end
43
+
38
44
  def module_names
39
45
  @_module_names ||= Set.new(deadzones.map(&:class_name))
40
46
  end
@@ -8,7 +8,7 @@ module Contrast
8
8
  # A Reaction from TeamServer which indicates the Agent should be disabled,
9
9
  # typically because some configuration setting did not satisfy requirements
10
10
  # set by the Organization's Administrator
11
- class DisableReaction
11
+ module DisableReaction
12
12
  extend Contrast::Components::Logger::InstanceMethods
13
13
 
14
14
  def self.run _reaction, level
@@ -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)
@@ -76,11 +76,12 @@ module Contrast
76
76
  return unless enabled?
77
77
  return unless activity
78
78
 
79
- # Copy gemdigest_cache and clear it in sync.
79
+ # Disconnect gemdigest_cache and replace it with an empty one; synch so new libs cannot be added between the
80
+ # assignment and the replace
80
81
  gem_spec_digest_to_files = @lock.synchronize do
81
- copy = @gemdigest_cache.dup
82
- @gemdigest_cache.clear
83
- copy
82
+ hold = @gemdigest_cache
83
+ @gemdigest_cache = Hash.new { |hash, key| hash[key] = Set.new }
84
+ hold
84
85
  end
85
86
 
86
87
  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
@@ -60,6 +60,7 @@ module Contrast
60
60
  handle_first_request
61
61
  call_with_agent(env)
62
62
  end
63
+ ::Contrast::Components::Logger.add_trace_log_timing_for(::Contrast::Agent::Middleware, :call)
63
64
 
64
65
  private
65
66
 
@@ -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
@@ -290,6 +290,11 @@ module Contrast
290
290
  # we've already patched this class, don't do it again
291
291
  return true if methods.include?(cs_method_name)
292
292
 
293
+ # that method is within Contrast definition so it should be skipped
294
+ method = mod.instance_method(method_policy.method_name) if method_policy.instance_method
295
+ method = mod.singleton_method(method_policy.method_name) unless method_policy.instance_method
296
+ return true if method.owner <= Contrast
297
+
293
298
  begin
294
299
  contrast_define_method(mod, method_policy, cs_method_name)
295
300
  rescue NameError => e
@@ -332,13 +337,13 @@ module Contrast
332
337
  # :prepend -> prepend instance method of module
333
338
  # [prepending singleton is easily supported too, just not implemented yet.]
334
339
  # @return [Symbol] new alias for the underlying method (presumably, so the patched method can call it)
335
- def register_c_patch target_module_name, unbound_method, impl = :alias_instance
340
+ def register_c_patch target_module_name, unbound_method, impl = :alias_instance # rubocop:disable Metrics/AbcSize
336
341
  # These could be set as AfterLoadPatches.
337
342
  method_name = unbound_method.name.to_sym # rubocop:disable Security/Module/Name -- ruby built in attribute.
338
343
  underlying_method_name = build_unbound_method_name(method_name).to_sym
339
344
 
340
345
  target_module = Module.cs__const_get(target_module_name)
341
-
346
+ target_module = target_module.cs__singleton_class if %i[prepend_singleton prepend].include? impl
342
347
  target_module = target_module.cs__singleton_class if %i[alias_singleton prepend].include? impl
343
348
 
344
349
  visibility = if target_module.private_instance_methods(false).include?(method_name)
@@ -365,14 +370,20 @@ module Contrast
365
370
  target_module.send(:define_method, method_name, unbound_method.bind(target_module))
366
371
  end
367
372
  target_module.send(visibility, method_name) # e.g., module.private(:my_method)
368
- when :prepend
369
- prepending_module = Module.new
370
- prepending_module.send(:define_method, method_name, unbound_method.bind(target_module))
371
- prepending_module.send(visibility, method_name)
373
+ when :prepend_instance, :prepend_singleton
374
+
375
+ unless target_module.instance_methods(false).include? underlying_method_name
376
+
377
+ prepending_module = Module.new
378
+ prepending_module.send(:define_method, method_name, unbound_method.bind(target_module))
379
+ prepending_module.send(visibility, method_name)
380
+
381
+ end
372
382
  # This prepends to the singleton class (it patches a class method)
373
383
  target_module.prepend prepending_module
374
384
  # rubocop:enable Performance/Kernel/DefineMethod
375
385
  end
386
+
376
387
  # Ougai::Logger.create_item_with_2args calls Hash#[]=, so we
377
388
  # can't invoke this logging method or we'll seg fault as we'd
378
389
  # change the method definition mid-call
@@ -15,13 +15,9 @@ module Contrast
15
15
  # @param mod [Module] the Module for which the status is asked
16
16
  # @return [Contrast::Agent::Patching::Policy::PatchStatus]
17
17
  def get_status mod
18
- if mod.cs__const_defined?(status_key, false)
19
- mod.cs__const_get(status_key, false)
20
- else
21
- s = new
22
- mod.cs__const_set(status_key, s)
23
- s
24
- end
18
+ return mod.cs__const_get(status_key) if mod.cs__const_defined?(status_key, false)
19
+
20
+ mod.cs__const_set(status_key, new)
25
21
  end
26
22
 
27
23
  # Allows our C patches to look up the :MethodPolicy for a given
@@ -54,7 +54,8 @@ module Contrast
54
54
  def patch
55
55
  catchup_after_load_patches
56
56
  catchup_loaded_methods
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
+ # TODO: RUBY-714 remove guard w/ EOL of 2.5
58
+ Contrast::Agent::Assess::Policy::RewriterPatch.rewrite_interpolations if RUBY_VERSION < '2.6.0'
58
59
  end
59
60
 
60
61
  # Hook to only monkeypatch Contrast. This will not trigger any
@@ -86,7 +87,7 @@ module Contrast
86
87
 
87
88
  load_patches_for_module(mod_name)
88
89
 
89
- return unless all_module_names.any?(mod_name)
90
+ return if all_module_names.none?(mod_name)
90
91
 
91
92
  module_data = Contrast::Agent::ModuleData.new(mod, mod_name)
92
93
  patch_into_module(module_data)
@@ -176,12 +177,13 @@ module Contrast
176
177
  # @param redo_patch [Boolean] a trigger to force patching regardless of the state of the
177
178
  # Contrast::Agent::Patching::Policy::PatchStatus status on the Module
178
179
  def patch_into_module module_data, redo_patch = false
179
- status = status_type.get_status(module_data.mod)
180
+ status = Contrast::Agent::Patching::Policy::PatchStatus.get_status(module_data.mod)
180
181
  return if (status&.patched? || status&.patching?) && !redo_patch
181
182
 
182
183
  # Begin patching our sources into the given module. Any patcher that has the name of the module will be
183
184
  # evaluated for patching. Find all the patchers that apply to this class, sorted by type.
184
185
  module_policy = Contrast::Agent::Patching::Policy::ModulePolicy.create_module_policy(module_data.mod_name)
186
+
185
187
  # If there's nothing to match, then set that status and exit
186
188
  if module_policy.empty?
187
189
  status.no_patch!
@@ -191,8 +193,8 @@ module Contrast
191
193
  status.patching!
192
194
  num_applied_patches = patch_into_instance_methods(module_data, module_policy)
193
195
  num_applied_patches += patch_into_singleton_methods(module_data, module_policy)
194
- if adjust_for_prepend(module_data) || module_policy.num_expected_patches == num_applied_patches
195
196
 
197
+ if adjust_for_prepend(module_data) || module_policy.num_expected_patches == num_applied_patches
196
198
  status.patched!
197
199
  else
198
200
  status.partial_patch!
@@ -244,6 +246,7 @@ module Contrast
244
246
  def patch_into_instance_methods module_data, module_policy
245
247
  mod = module_data.mod
246
248
  methods = all_instance_methods(mod, true)
249
+ methods.delete(:initialize) if mod.to_s.starts_with?('RSpec') && mod.to_s.include?('Matchers')
247
250
  patch_into_methods(mod, methods, module_policy, true)
248
251
  end
249
252
 
@@ -258,8 +261,7 @@ module Contrast
258
261
  patch_into_methods(mod, methods, module_policy, false)
259
262
  end
260
263
 
261
- # We've found the patchers that apply to this class (or module). Now we'll
262
- # filter on the given method.
264
+ # We've found the patchers that apply to this class (or module). Now we'll filter on the given method.
263
265
  #
264
266
  # @param mod [Module] The module from which to retrieve instance methods.
265
267
  # @param methods [Array<Symbol>] The names of all the methods in in this module
@@ -276,8 +278,7 @@ module Contrast
276
278
  is_instance_method)
277
279
  next if method_policy.empty?
278
280
 
279
- patched = patch_method(mod, methods, method_policy)
280
- count += 1 if patched
281
+ count += 1 if patch_method(mod, methods, method_policy)
281
282
  end
282
283
  count
283
284
  end
@@ -309,6 +310,5 @@ require 'contrast/extension/module'
309
310
  require 'contrast/extension/assess'
310
311
  require 'contrast/extension/inventory'
311
312
  require 'contrast/extension/protect'
312
- require 'contrast/extension/protect/kernel'
313
313
 
314
314
  require 'cs__contrast_patch/cs__contrast_patch'
@@ -14,7 +14,7 @@ module Contrast
14
14
  # infilter methods of the rule should be invoked.
15
15
  module AppliesNoSqliRule
16
16
  extend Contrast::Agent::Protect::Policy::RuleApplicator
17
-
17
+ DATABASE_NOSQL = 'MongoDB'
18
18
  class << self
19
19
  def invoke method, _exception, properties, _object, args
20
20
  return unless valid_input?(args)