contrast-agent 4.8.0 → 4.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (147) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +6 -1
  3. data/.rspec +0 -1
  4. data/.rspec_parallel +6 -0
  5. data/.simplecov +1 -0
  6. data/ext/cs__contrast_patch/cs__contrast_patch.c +0 -1
  7. data/ext/cs__contrast_patch/cs__contrast_patch.h +0 -2
  8. data/lib/contrast/agent/assess/contrast_event.rb +1 -5
  9. data/lib/contrast/agent/assess/contrast_object.rb +0 -3
  10. data/lib/contrast/agent/assess/finalizers/hash.rb +2 -5
  11. data/lib/contrast/agent/assess/policy/patcher.rb +5 -4
  12. data/lib/contrast/agent/assess/policy/policy.rb +1 -1
  13. data/lib/contrast/agent/assess/policy/policy_scanner.rb +2 -6
  14. data/lib/contrast/agent/assess/policy/preshift.rb +16 -12
  15. data/lib/contrast/agent/assess/policy/propagation_method.rb +102 -59
  16. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +2 -7
  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 +10 -6
  20. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +3 -3
  21. data/lib/contrast/agent/assess/policy/rewriter_patch.rb +6 -7
  22. data/lib/contrast/agent/assess/policy/source_method.rb +18 -22
  23. data/lib/contrast/agent/assess/policy/trigger/xpath.rb +0 -4
  24. data/lib/contrast/agent/assess/policy/trigger_method.rb +62 -88
  25. data/lib/contrast/agent/assess/policy/trigger_node.rb +1 -1
  26. data/lib/contrast/agent/assess/property/evented.rb +2 -1
  27. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +3 -4
  28. data/lib/contrast/agent/at_exit_hook.rb +3 -3
  29. data/lib/contrast/agent/class_reopener.rb +6 -5
  30. data/lib/contrast/agent/disable_reaction.rb +4 -5
  31. data/lib/contrast/agent/exclusion_matcher.rb +2 -7
  32. data/lib/contrast/agent/inventory/database_config.rb +117 -0
  33. data/lib/contrast/agent/inventory/dependency_analysis.rb +2 -6
  34. data/lib/contrast/agent/inventory/dependency_usage_analysis.rb +9 -10
  35. data/lib/contrast/agent/inventory/policy/datastores.rb +5 -6
  36. data/lib/contrast/agent/inventory/policy/policy.rb +1 -1
  37. data/lib/contrast/agent/middleware.rb +15 -13
  38. data/lib/contrast/agent/patching/policy/after_load_patch.rb +6 -3
  39. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +21 -16
  40. data/lib/contrast/agent/patching/policy/module_policy.rb +2 -4
  41. data/lib/contrast/agent/patching/policy/patch.rb +13 -8
  42. data/lib/contrast/agent/patching/policy/patch_status.rb +3 -7
  43. data/lib/contrast/agent/patching/policy/patcher.rb +14 -14
  44. data/lib/contrast/agent/patching/policy/policy.rb +2 -4
  45. data/lib/contrast/agent/patching/policy/policy_node.rb +2 -3
  46. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +1 -1
  47. data/lib/contrast/agent/protect/policy/policy.rb +1 -1
  48. data/lib/contrast/agent/protect/policy/rule_applicator.rb +3 -5
  49. data/lib/contrast/agent/protect/rule/base.rb +10 -10
  50. data/lib/contrast/agent/protect/rule/cmd_injection.rb +4 -5
  51. data/lib/contrast/agent/protect/rule/no_sqli.rb +7 -53
  52. data/lib/contrast/agent/protect/rule/path_traversal.rb +1 -5
  53. data/lib/contrast/agent/protect/rule/sql_sample_builder.rb +137 -0
  54. data/lib/contrast/agent/protect/rule/sqli.rb +7 -70
  55. data/lib/contrast/agent/reaction_processor.rb +3 -4
  56. data/lib/contrast/agent/request.rb +13 -7
  57. data/lib/contrast/agent/request_context.rb +36 -34
  58. data/lib/contrast/agent/request_handler.rb +5 -3
  59. data/lib/contrast/agent/response.rb +2 -3
  60. data/lib/contrast/agent/rewriter.rb +4 -3
  61. data/lib/contrast/agent/rule_set.rb +5 -4
  62. data/lib/contrast/agent/scope.rb +32 -20
  63. data/lib/contrast/agent/service_heartbeat.rb +2 -3
  64. data/lib/contrast/agent/static_analysis.rb +7 -6
  65. data/lib/contrast/agent/thread.rb +2 -4
  66. data/lib/contrast/agent/thread_watcher.rb +3 -4
  67. data/lib/contrast/agent/tracepoint_hook.rb +20 -7
  68. data/lib/contrast/agent/version.rb +1 -1
  69. data/lib/contrast/api/communication/messaging_queue.rb +16 -11
  70. data/lib/contrast/api/communication/response_processor.rb +11 -11
  71. data/lib/contrast/api/communication/service_lifecycle.rb +9 -5
  72. data/lib/contrast/api/communication/socket_client.rb +18 -14
  73. data/lib/contrast/api/communication/speedracer.rb +5 -6
  74. data/lib/contrast/api/decorators/address.rb +2 -3
  75. data/lib/contrast/api/decorators/agent_startup.rb +7 -9
  76. data/lib/contrast/api/decorators/application_startup.rb +9 -10
  77. data/lib/contrast/api/decorators/application_update.rb +0 -4
  78. data/lib/contrast/api/decorators/http_request.rb +3 -7
  79. data/lib/contrast/api/decorators/instrumentation_mode.rb +3 -5
  80. data/lib/contrast/api/decorators/message.rb +7 -7
  81. data/lib/contrast/api/decorators/route_coverage.rb +24 -1
  82. data/lib/contrast/api/decorators/trace_event_object.rb +2 -3
  83. data/lib/contrast/components/agent.rb +13 -15
  84. data/lib/contrast/components/app_context.rb +7 -11
  85. data/lib/contrast/components/assess.rb +19 -16
  86. data/lib/contrast/components/base.rb +40 -0
  87. data/lib/contrast/components/config.rb +1 -2
  88. data/lib/contrast/components/contrast_service.rb +8 -11
  89. data/lib/contrast/components/heap_dump.rb +5 -4
  90. data/lib/contrast/components/inventory.rb +2 -7
  91. data/lib/contrast/components/logger.rb +14 -10
  92. data/lib/contrast/components/protect.rb +10 -13
  93. data/lib/contrast/components/sampling.rb +5 -5
  94. data/lib/contrast/components/scope.rb +9 -32
  95. data/lib/contrast/components/settings.rb +1 -5
  96. data/lib/contrast/config/base_configuration.rb +14 -6
  97. data/lib/contrast/configuration.rb +22 -19
  98. data/lib/contrast/extension/assess/array.rb +3 -15
  99. data/lib/contrast/extension/assess/eval_trigger.rb +2 -23
  100. data/lib/contrast/extension/assess/fiber.rb +6 -16
  101. data/lib/contrast/extension/assess/hash.rb +3 -13
  102. data/lib/contrast/extension/assess/kernel.rb +3 -14
  103. data/lib/contrast/extension/assess/marshal.rb +6 -14
  104. data/lib/contrast/extension/assess/regexp.rb +5 -15
  105. data/lib/contrast/extension/assess/string.rb +6 -31
  106. data/lib/contrast/extension/extension.rb +61 -0
  107. data/lib/contrast/extension/kernel.rb +2 -4
  108. data/lib/contrast/extension/protect/kernel.rb +0 -15
  109. data/lib/contrast/framework/grape/support.rb +174 -0
  110. data/lib/contrast/framework/manager.rb +44 -9
  111. data/lib/contrast/framework/rack/patch/session_cookie.rb +6 -6
  112. data/lib/contrast/framework/rack/support.rb +1 -1
  113. data/lib/contrast/framework/rails/patch/assess_configuration.rb +5 -8
  114. data/lib/contrast/framework/rails/patch/support.rb +43 -36
  115. data/lib/contrast/framework/rails/railtie.rb +8 -6
  116. data/lib/contrast/framework/rails/rewrite/active_record_named.rb +4 -4
  117. data/lib/contrast/framework/rails/support.rb +60 -13
  118. data/lib/contrast/framework/sinatra/support.rb +1 -1
  119. data/lib/contrast/funchook/funchook.rb +4 -3
  120. data/lib/contrast/logger/application.rb +1 -6
  121. data/lib/contrast/logger/log.rb +103 -13
  122. data/lib/contrast/logger/request.rb +0 -4
  123. data/lib/contrast/tasks/config.rb +0 -1
  124. data/lib/contrast/tasks/service.rb +1 -6
  125. data/lib/contrast/utils/assess/sampling_util.rb +2 -3
  126. data/lib/contrast/utils/assess/tracking_util.rb +2 -4
  127. data/lib/contrast/utils/class_util.rb +26 -19
  128. data/lib/contrast/utils/heap_dump_util.rb +5 -3
  129. data/lib/contrast/utils/invalid_configuration_util.rb +4 -3
  130. data/lib/contrast/utils/io_util.rb +46 -40
  131. data/lib/contrast/utils/job_servers_running.rb +4 -3
  132. data/lib/contrast/utils/lru_cache.rb +43 -0
  133. data/lib/contrast/utils/os.rb +2 -3
  134. data/lib/contrast/utils/ruby_ast_rewriter.rb +16 -13
  135. data/lib/contrast/utils/string_utils.rb +2 -3
  136. data/lib/contrast/utils/tag_util.rb +26 -19
  137. data/lib/contrast.rb +24 -14
  138. data/resources/assess/policy.json +197 -2
  139. data/resources/deadzone/policy.json +10 -0
  140. data/ruby-agent.gemspec +13 -3
  141. data/service_executables/VERSION +1 -1
  142. data/service_executables/linux/contrast-service +0 -0
  143. data/service_executables/mac/contrast-service +0 -0
  144. metadata +91 -25
  145. data/lib/contrast/components/interface.rb +0 -196
  146. data/lib/contrast/delegators/input_analysis.rb +0 -12
  147. data/lib/contrast/utils/inventory_util.rb +0 -114
@@ -2,7 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/agent/assess/policy/trigger_method'
5
- require 'contrast/components/interface'
5
+ require 'contrast/components/logger'
6
6
  require 'contrast/extension/module'
7
7
 
8
8
  module Contrast
@@ -18,11 +18,10 @@ module Contrast
18
18
  # given value set
19
19
  # 3) redacted_marker : the value to plug in for the obfuscated value
20
20
  module HardcodedValueRule
21
- include Contrast::Components::Interface
22
- access_component :analysis, :app_context, :logging
21
+ include Contrast::Components::Logger::InstanceMethods
23
22
 
24
23
  def disabled?
25
- !ASSESS.enabled? || ASSESS.rule_disabled?(rule_id)
24
+ !::Contrast::ASSESS.enabled? || ::Contrast::ASSESS.rule_disabled?(rule_id)
26
25
  end
27
26
 
28
27
  # TODO: RUBY-1014 - remove `#analyze`
@@ -1,14 +1,14 @@
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/logger'
5
5
 
6
6
  module Contrast
7
7
  module Agent
8
8
  # This module adds an at_exit hook for us to send messages that may be lost at process exit
9
9
  module AtExitHook
10
- include Contrast::Components::Interface
11
- access_component :logging
10
+ extend Contrast::Components::Logger::InstanceMethods
11
+
12
12
  def self.exit_hook
13
13
  @_exit_hook ||= begin
14
14
  at_exit do
@@ -5,7 +5,8 @@ return unless RUBY_VERSION < '2.6.0' # TODO: RUBY-714 remove guard w/ EOL of 2.5
5
5
 
6
6
  require 'ripper'
7
7
  require 'contrast/extension/module'
8
- require 'contrast/components/interface'
8
+ require 'contrast/components/logger'
9
+ require 'contrast/components/scope'
9
10
  require 'contrast/logger/log'
10
11
 
11
12
  # This method is left purposefully at the top level namespace. Moving it
@@ -20,7 +21,7 @@ require 'contrast/logger/log'
20
21
  def unbound_eval _class_name, content
21
22
  # Yuck, this is a top-level method that has to break encapsulation
22
23
  # in order to access scoping!
23
- Contrast::Components::Scope::COMPONENT_INTERFACE.scope_for_current_ec.enter_contrast_scope!
24
+ ::Contrast::SCOPE.scope_for_current_ec.enter_contrast_scope!
24
25
  eval(content) # rubocop:disable Security/Eval
25
26
  rescue Exception # rubocop:disable Lint/RescueException
26
27
  # We can't use components here, so we have to access the log directly. I hate
@@ -29,7 +30,7 @@ rescue Exception # rubocop:disable Lint/RescueException
29
30
  # And we need to return nil here, not the value from the logger.
30
31
  nil
31
32
  ensure
32
- Contrast::Components::Scope::COMPONENT_INTERFACE.scope_for_current_ec.exit_contrast_scope!
33
+ ::Contrast::SCOPE.scope_for_current_ec.exit_contrast_scope!
33
34
  end
34
35
 
35
36
  module Contrast
@@ -38,8 +39,8 @@ module Contrast
38
39
  # @deprecated Changes to this class are discouraged as this approach is
39
40
  # being phased out with support for those language versions.
40
41
  class ClassReopener
41
- include Contrast::Components::Interface
42
- access_component :logging, :scope
42
+ include Contrast::Components::Logger::InstanceMethods
43
+ include Contrast::Components::Scope::InstanceMethods
43
44
 
44
45
  END_NEW_LINE = "end\n"
45
46
  PROTECTED_WITH_NEW_LINE = "protected\n"
@@ -1,20 +1,19 @@
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/logger'
5
5
 
6
6
  module Contrast
7
7
  module Agent
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
12
- include Contrast::Components::Interface
13
- access_component :agent, :logging
11
+ module DisableReaction
12
+ extend Contrast::Components::Logger::InstanceMethods
14
13
 
15
14
  def self.run _reaction, level
16
15
  logger.with_level(level, 'Contrast received instructions to disable itself - Disabling now')
17
- AGENT.disable!
16
+ ::Contrast::AGENT.disable!
18
17
  end
19
18
  end
20
19
  end
@@ -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/logger'
5
5
 
6
6
  module Contrast
7
7
  module Agent
@@ -9,8 +9,7 @@ module Contrast
9
9
  # the Application. If a request or an event matches one of these, the
10
10
  # functions of the Agent are suppressed for that request or event.
11
11
  class ExclusionMatcher
12
- include Contrast::Components::Interface
13
- access_component :logging
12
+ include Contrast::Components::Logger::InstanceMethods
14
13
 
15
14
  # Create a matcher around an exclusion sent from TeamServer.
16
15
  #
@@ -95,10 +94,6 @@ module Contrast
95
94
  @exclusion.type == Contrast::Api::Settings::Exclusion::ExclusionType::CODE
96
95
  end
97
96
 
98
- def exc_name
99
- @exclusion.name # rubocop:disable Security/Module/Name -- part of the API.
100
- end
101
-
102
97
  def match_all?
103
98
  @exclusion.urls.nil? || @exclusion.urls.empty?
104
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
@@ -2,7 +2,6 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/agent/inventory/dependencies'
5
- require 'contrast/components/interface'
6
5
  require 'contrast/utils/object_share'
7
6
 
8
7
  module Contrast
@@ -12,17 +11,14 @@ module Contrast
12
11
  class DependencyAnalysis
13
12
  include Singleton
14
13
  include Contrast::Agent::Inventory::Dependencies
15
- include Contrast::Components::Interface
16
-
17
- access_component :analysis
18
14
 
19
15
  # Report the dependencies of this application
20
16
  #
21
17
  # @return [Array<Contrast::Api::Dtm::Library>] protobuf form of the
22
18
  # Gem::Specification that have been loaded for this application.
23
19
  def library_pb_list
24
- return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless INVENTORY.enabled?
25
- return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless INVENTORY.analyze_libraries?
20
+ return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless ::Contrast::INVENTORY.enabled?
21
+ return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless ::Contrast::INVENTORY.analyze_libraries?
26
22
 
27
23
  loaded_specs.each_with_object([]) do |(_name, spec), reported_lib_list|
28
24
  next unless spec
@@ -2,7 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/agent/inventory/dependencies'
5
- require 'contrast/components/interface'
5
+ require 'contrast/components/logger'
6
6
  require 'contrast/utils/object_share'
7
7
  require 'set'
8
8
 
@@ -12,11 +12,9 @@ module Contrast
12
12
  # Used to analyze class usage for reporting
13
13
  class DependencyUsageAnalysis
14
14
  include Singleton
15
- include Contrast::Components::Interface
15
+ include Contrast::Components::Logger::InstanceMethods
16
16
  include Contrast::Agent::Inventory::Dependencies
17
17
 
18
- access_component :analysis, :config, :logging
19
-
20
18
  def initialize
21
19
  return unless enabled?
22
20
 
@@ -62,7 +60,7 @@ module Contrast
62
60
 
63
61
  digest = Contrast::Utils::Sha256Builder.instance.build_from_spec(spec)
64
62
  unless digest
65
- 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?
66
64
  return
67
65
  end
68
66
  report_path = adjust_path_for_reporting(path, spec)
@@ -78,11 +76,12 @@ module Contrast
78
76
  return unless enabled?
79
77
  return unless activity
80
78
 
81
- # 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
82
81
  gem_spec_digest_to_files = @lock.synchronize do
83
- copy = @gemdigest_cache.dup
84
- @gemdigest_cache.clear
85
- copy
82
+ hold = @gemdigest_cache
83
+ @gemdigest_cache = Hash.new { |hash, key| hash[key] = Set.new }
84
+ hold
86
85
  end
87
86
 
88
87
  gem_spec_digest_to_files.each_pair do |digest, files|
@@ -107,7 +106,7 @@ module Contrast
107
106
 
108
107
  # We only use this if inventory and library analysis are enabled
109
108
  def enabled?
110
- @_enabled = INVENTORY.enabled? && INVENTORY.analyze_libraries? if @_enabled.nil?
109
+ @_enabled = ::Contrast::INVENTORY.enabled? && ::Contrast::INVENTORY.analyze_libraries? if @_enabled.nil?
111
110
  @_enabled
112
111
  end
113
112
  end
@@ -1,8 +1,8 @@
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'
5
- require 'contrast/utils/inventory_util'
4
+ require 'contrast/components/logger'
5
+ require 'contrast/agent/inventory/database_config'
6
6
 
7
7
  module Contrast
8
8
  module Agent
@@ -14,15 +14,14 @@ module Contrast
14
14
  # the given invocation is worth reporting or not.
15
15
  module DataStores
16
16
  class << self
17
- include Contrast::Components::Interface
17
+ extend Contrast::Components::Logger::InstanceMethods
18
18
 
19
- access_component :analysis, :logging
20
19
  # The key used in policy.json to indicate the database type to
21
20
  # report.
22
21
  DATA_STORE_MARKER = 'data_store'
23
22
 
24
23
  def report_data_store _method, _exception, properties, object, _args
25
- return unless INVENTORY.enabled?
24
+ return unless ::Contrast::INVENTORY.enabled?
26
25
 
27
26
  marker = properties[DATA_STORE_MARKER]
28
27
  return unless marker
@@ -43,7 +42,7 @@ module Contrast
43
42
  context.activity.query_count += 1
44
43
  return unless context.activity.query_count == 1
45
44
 
46
- Contrast::Utils::InventoryUtil.append_db_config(context.activity)
45
+ Contrast::Agent::Inventory::DatabaseConfig.append_db_config(context.activity)
47
46
  end
48
47
  end
49
48
  end
@@ -19,7 +19,7 @@ module Contrast
19
19
  end
20
20
 
21
21
  def disabled_globally?
22
- !INVENTORY.enabled?
22
+ !::Contrast::INVENTORY.enabled?
23
23
  end
24
24
 
25
25
  def node_type
@@ -7,7 +7,8 @@ require 'rack'
7
7
 
8
8
  require 'contrast/security_exception'
9
9
  require 'contrast/utils/object_share'
10
- require 'contrast/components/interface'
10
+ require 'contrast/components/logger'
11
+ require 'contrast/components/scope'
11
12
  require 'contrast/utils/heap_dump_util'
12
13
  require 'contrast/agent/request_handler'
13
14
  require 'contrast/agent/static_analysis'
@@ -20,8 +21,8 @@ module Contrast
20
21
  # initialize ourselves as a rack middleware inside of #initialize. Afterwards, we process each http request and
21
22
  # response as it goes through the middleware stack inside of #call.
22
23
  class Middleware
23
- include Contrast::Components::Interface
24
- access_component :agent, :config, :logging, :scope, :settings
24
+ include Contrast::Components::Logger::InstanceMethods
25
+ include Contrast::Components::Scope::InstanceMethods
25
26
 
26
27
  attr_reader :app
27
28
 
@@ -36,10 +37,10 @@ module Contrast
36
37
  def initialize app, _legacy_param = nil
37
38
  @app = app # THIS MUST BE FIRST AND ALWAYS SET!
38
39
  setup_agent # THIS MUST BE SECOND AND ALWAYS CALLED!
39
- unless AGENT.enabled?
40
+ unless ::Contrast::AGENT.enabled?
40
41
  logger.error('The Agent was unable to initialize before the application middleware was initialized. '\
41
42
  'Disabling permanently.')
42
- AGENT.disable! # ensure the agent is disabled (probably redundant)
43
+ ::Contrast::AGENT.disable! # ensure the agent is disabled (probably redundant)
43
44
  return
44
45
  end
45
46
  agent_startup_routine
@@ -53,27 +54,28 @@ module Contrast
53
54
  # @return [Array,Rack::Response] the Response of this and subsequent Middlewares to be passed back to the user up
54
55
  # the Rack framework.
55
56
  def call env
56
- return app.call(env) unless AGENT.enabled?
57
+ return app.call(env) unless ::Contrast::AGENT.enabled?
57
58
 
58
59
  Contrast::Agent.heapdump_util.start_thread!
59
60
  handle_first_request
60
61
  call_with_agent(env)
61
62
  end
63
+ ::Contrast::Components::Logger.add_trace_log_timing_for(::Contrast::Agent::Middleware, :call)
62
64
 
63
65
  private
64
66
 
65
67
  def setup_agent
66
- SETTINGS.reset_state
68
+ ::Contrast::SETTINGS.reset_state
67
69
 
68
70
  inform_deprecations
69
71
 
70
- if CONFIG.invalid?
71
- AGENT.disable!
72
+ if ::Contrast::CONFIG.invalid?
73
+ ::Contrast::AGENT.disable!
72
74
  logger.error('!!! CONFIG FILE IS INVALID - DISABLING CONTRAST AGENT !!!')
73
- elsif AGENT.disabled?
75
+ elsif ::Contrast::AGENT.disabled?
74
76
  logger.warn('Contrast disabled by configuration. Continuing without instrumentation.')
75
77
  else
76
- AGENT.enable!
78
+ ::Contrast::AGENT.enable!
77
79
  end
78
80
  end
79
81
 
@@ -92,7 +94,7 @@ module Contrast
92
94
  end
93
95
 
94
96
  logger.debug_with_time('middleware: enabling tracepoint') do
95
- AGENT.enable_tracepoint
97
+ ::Contrast::AGENT.enable_tracepoint
96
98
  end
97
99
  Contrast::Agent::AtExitHook.exit_hook
98
100
  end
@@ -191,7 +193,7 @@ module Contrast
191
193
  # config.agent.ruby.exceptions.capture? is set
192
194
  def handle_exception exception
193
195
  if security_exception?(exception)
194
- exception_control = AGENT.exception_control
196
+ exception_control = ::Contrast::AGENT.exception_control
195
197
  raise exception unless exception_control[:enable]
196
198
 
197
199
  [exception_control[:status], {}, [exception_control[:message]]]
@@ -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
  require 'contrast/extension/module'
6
6
  require 'contrast/utils/class_util'
7
7
 
@@ -11,8 +11,8 @@ module Contrast
11
11
  module Policy
12
12
  # Used to handle tracking patches that need to apply special instrumentation when a module is loaded
13
13
  class AfterLoadPatch
14
- include Contrast::Components::Interface
15
- access_component :scope
14
+ include Contrast::Components::Scope::InstanceMethods
15
+
16
16
  attr_reader :applied, :module_name, :instrumentation_file_path, :method_to_instrument, :instrumenting_module
17
17
 
18
18
  def initialize module_name, instrumentation_file_path, method_to_instrument: nil, instrumenting_module: nil
@@ -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
@@ -2,8 +2,9 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/agent/patching/policy/after_load_patch'
5
- require 'contrast/components/interface'
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
@@ -12,8 +13,7 @@ module Contrast
12
13
  # Some modules diverge from our generic instrumentation and require custom instrumentation
13
14
  # after they've been loaded
14
15
  module AfterLoadPatcher
15
- include Contrast::Components::Interface
16
- access_component :agent, :logging
16
+ include Contrast::Components::Logger::InstanceMethods
17
17
 
18
18
  # After initialization run a catchup check to instrument any already loaded modules we care about
19
19
  def catchup_after_load_patches
@@ -30,18 +30,23 @@ module Contrast
30
30
  # extensions.
31
31
  def apply_direct_patches!
32
32
  @_apply_direct_patches ||= begin
33
- Contrast::Extension::Assess::ArrayPropagator.instrument_array_track
34
- Contrast::Extension::Assess::EvalTrigger.instrument_basic_object_track
35
- Contrast::Extension::Assess::EvalTrigger.instrument_module_track
36
- Contrast::Extension::Assess::FiberPropagator.instrument_fiber_track
37
- Contrast::Extension::Assess::HashPropagator.instrument_hash_track
38
- Contrast::Extension::Assess::KernelPropagator.instrument_kernel_track
39
- Contrast::Extension::Assess::MarshalPropagator.instrument_marshal_load
40
- Contrast::Extension::Assess::RegexpPropagator.instrument_regexp_track
41
- Contrast::Extension::Assess::StringPropagator.instrument_string
42
- Contrast::Extension::Assess::StringPropagator.instrument_string_interpolation
43
-
44
- 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
49
+ Contrast::Extension::Assess::InstrumentHelper.instrument 'cs__protect_kernel/cs__protect_kernel'
45
50
  true
46
51
  end
47
52
  end
@@ -49,7 +54,7 @@ module Contrast
49
54
  def apply_load_patches!
50
55
  after_load_patches.each do |after_load_patch|
51
56
  next unless after_load_patch.target_defined?
52
- next if AGENT.skip_instrumentation?(after_load_patch.module_name)
57
+ next if ::Contrast::AGENT.skip_instrumentation?(after_load_patch.module_name)
53
58
 
54
59
  logger.trace('Catching up on already loaded afterload patch - applying instrumentation',
55
60
  module: after_load_patch.module_name)
@@ -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
@@ -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,8 +127,8 @@ 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?
130
+ return unless ::Contrast::AGENT.enabled?
131
+ return unless ::Contrast::PROTECT.enabled?
132
132
 
133
133
  apply_trigger_only(method_policy&.protect_node, method, exception, object, args)
134
134
  end
@@ -145,7 +145,7 @@ module Contrast
145
145
  # @param args [Array<Object>] The arguments passed to the method
146
146
  # being invoked.
147
147
  def apply_inventory method_policy, method, exception, object, args
148
- return unless INVENTORY.enabled?
148
+ return unless ::Contrast::INVENTORY.enabled?
149
149
 
150
150
  apply_trigger_only(method_policy&.inventory_node, method, exception, object, args)
151
151
  end
@@ -167,7 +167,7 @@ module Contrast
167
167
  def apply_assess method_policy, preshift, object, ret, args, block
168
168
  source_ret = nil
169
169
  propagated_ret = nil
170
- return ret unless method_policy && ASSESS.enabled?
170
+ return ret unless method_policy && ::Contrast::ASSESS.enabled?
171
171
 
172
172
  current_context = Contrast::Agent::REQUEST_TRACKER.current
173
173
  return ret if current_context && !current_context.analyze_request?
@@ -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
@@ -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