contrast-agent 4.7.0 → 4.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +4 -4
  2. data/.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/finalizers/hash.rb +2 -5
  10. data/lib/contrast/agent/assess/policy/patcher.rb +5 -4
  11. data/lib/contrast/agent/assess/policy/policy.rb +1 -1
  12. data/lib/contrast/agent/assess/policy/policy_scanner.rb +2 -6
  13. data/lib/contrast/agent/assess/policy/preshift.rb +11 -8
  14. data/lib/contrast/agent/assess/policy/propagation_method.rb +102 -59
  15. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +2 -7
  16. data/lib/contrast/agent/assess/policy/propagator/match_data.rb +31 -11
  17. data/lib/contrast/agent/assess/policy/propagator/rack_protection.rb +73 -0
  18. data/lib/contrast/agent/assess/policy/propagator/split.rb +10 -6
  19. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +3 -3
  20. data/lib/contrast/agent/assess/policy/propagator.rb +1 -0
  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 +61 -86
  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 +8 -9
  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 +9 -5
  57. data/lib/contrast/agent/request_context.rb +28 -31
  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/service_heartbeat.rb +2 -3
  63. data/lib/contrast/agent/static_analysis.rb +7 -6
  64. data/lib/contrast/agent/thread.rb +2 -4
  65. data/lib/contrast/agent/thread_watcher.rb +3 -4
  66. data/lib/contrast/agent/tracepoint_hook.rb +10 -5
  67. data/lib/contrast/agent/version.rb +1 -1
  68. data/lib/contrast/api/communication/messaging_queue.rb +16 -11
  69. data/lib/contrast/api/communication/response_processor.rb +11 -11
  70. data/lib/contrast/api/communication/service_lifecycle.rb +9 -5
  71. data/lib/contrast/api/communication/socket_client.rb +18 -14
  72. data/lib/contrast/api/communication/speedracer.rb +5 -6
  73. data/lib/contrast/api/decorators/address.rb +2 -3
  74. data/lib/contrast/api/decorators/agent_startup.rb +7 -9
  75. data/lib/contrast/api/decorators/application_startup.rb +9 -10
  76. data/lib/contrast/api/decorators/application_update.rb +0 -4
  77. data/lib/contrast/api/decorators/http_request.rb +3 -7
  78. data/lib/contrast/api/decorators/instrumentation_mode.rb +3 -5
  79. data/lib/contrast/api/decorators/message.rb +7 -7
  80. data/lib/contrast/api/decorators/route_coverage.rb +24 -1
  81. data/lib/contrast/api/decorators/trace_event_object.rb +2 -3
  82. data/lib/contrast/components/agent.rb +13 -15
  83. data/lib/contrast/components/app_context.rb +7 -11
  84. data/lib/contrast/components/assess.rb +19 -16
  85. data/lib/contrast/components/base.rb +40 -0
  86. data/lib/contrast/components/config.rb +1 -2
  87. data/lib/contrast/components/contrast_service.rb +8 -11
  88. data/lib/contrast/components/heap_dump.rb +5 -4
  89. data/lib/contrast/components/inventory.rb +2 -7
  90. data/lib/contrast/components/logger.rb +14 -10
  91. data/lib/contrast/components/protect.rb +10 -13
  92. data/lib/contrast/components/sampling.rb +5 -5
  93. data/lib/contrast/components/scope.rb +9 -32
  94. data/lib/contrast/components/settings.rb +1 -5
  95. data/lib/contrast/config/base_configuration.rb +14 -6
  96. data/lib/contrast/configuration.rb +22 -19
  97. data/lib/contrast/extension/assess/array.rb +3 -15
  98. data/lib/contrast/extension/assess/eval_trigger.rb +2 -23
  99. data/lib/contrast/extension/assess/fiber.rb +6 -16
  100. data/lib/contrast/extension/assess/hash.rb +3 -13
  101. data/lib/contrast/extension/assess/kernel.rb +3 -14
  102. data/lib/contrast/extension/assess/marshal.rb +6 -14
  103. data/lib/contrast/extension/assess/regexp.rb +5 -15
  104. data/lib/contrast/extension/assess/string.rb +6 -31
  105. data/lib/contrast/extension/extension.rb +61 -0
  106. data/lib/contrast/extension/kernel.rb +2 -4
  107. data/lib/contrast/extension/protect/kernel.rb +0 -15
  108. data/lib/contrast/framework/grape/support.rb +174 -0
  109. data/lib/contrast/framework/manager.rb +44 -9
  110. data/lib/contrast/framework/rack/patch/session_cookie.rb +6 -6
  111. data/lib/contrast/framework/rack/support.rb +1 -1
  112. data/lib/contrast/framework/rails/patch/assess_configuration.rb +5 -8
  113. data/lib/contrast/framework/rails/patch/support.rb +44 -37
  114. data/lib/contrast/framework/rails/railtie.rb +34 -0
  115. data/lib/contrast/framework/rails/rewrite/active_record_named.rb +4 -4
  116. data/lib/contrast/framework/rails/support.rb +60 -13
  117. data/lib/contrast/framework/sinatra/support.rb +1 -1
  118. data/lib/contrast/funchook/funchook.rb +4 -3
  119. data/lib/contrast/logger/application.rb +1 -6
  120. data/lib/contrast/logger/log.rb +103 -13
  121. data/lib/contrast/logger/request.rb +0 -4
  122. data/lib/contrast/tasks/config.rb +0 -1
  123. data/lib/contrast/tasks/service.rb +1 -6
  124. data/lib/contrast/utils/assess/sampling_util.rb +2 -3
  125. data/lib/contrast/utils/assess/tracking_util.rb +2 -4
  126. data/lib/contrast/utils/heap_dump_util.rb +5 -3
  127. data/lib/contrast/utils/invalid_configuration_util.rb +4 -3
  128. data/lib/contrast/utils/io_util.rb +3 -5
  129. data/lib/contrast/utils/job_servers_running.rb +4 -3
  130. data/lib/contrast/utils/os.rb +2 -3
  131. data/lib/contrast/utils/ruby_ast_rewriter.rb +16 -13
  132. data/lib/contrast/utils/string_utils.rb +2 -3
  133. data/lib/contrast/utils/tag_util.rb +26 -19
  134. data/lib/contrast.rb +24 -14
  135. data/resources/assess/policy.json +252 -2
  136. data/resources/deadzone/policy.json +10 -0
  137. data/ruby-agent.gemspec +14 -3
  138. data/service_executables/VERSION +1 -1
  139. data/service_executables/linux/contrast-service +0 -0
  140. data/service_executables/mac/contrast-service +0 -0
  141. metadata +104 -24
  142. data/lib/contrast/agent/railtie.rb +0 -31
  143. data/lib/contrast/components/interface.rb +0 -196
  144. data/lib/contrast/delegators/input_analysis.rb +0 -12
  145. data/lib/contrast/utils/inventory_util.rb +0 -114
@@ -68,7 +68,7 @@ module Contrast
68
68
  end
69
69
 
70
70
  def rule_disabled?
71
- ASSESS.rule_disabled?(rule_id)
71
+ ::Contrast::ASSESS.rule_disabled?(rule_id)
72
72
  end
73
73
 
74
74
  # Indicate if this is a dataflow based trigger, meaning it has a proper
@@ -50,7 +50,8 @@ module Contrast
50
50
  return unless (current_request = Contrast::Agent::REQUEST_TRACKER.current)
51
51
 
52
52
  if current_request.observed_route.sources.any? do |source|
53
- source.type == event.forced_source_type && source.name == event.forced_source_name
53
+ source.type == event.forced_source_type &&
54
+ source.name == event.forced_source_name # rubocop:disable Security/Module/Name
54
55
  end
55
56
 
56
57
  return
@@ -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
 
@@ -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]]]