contrast-agent 4.6.0 → 4.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (190) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +6 -1
  3. data/.gitmodules +1 -1
  4. data/.simplecov +1 -0
  5. data/Rakefile +1 -2
  6. data/ext/build_funchook.rb +3 -3
  7. data/ext/extconf_common.rb +1 -5
  8. data/lib/contrast.rb +24 -14
  9. data/lib/contrast/agent/assess.rb +1 -1
  10. data/lib/contrast/agent/assess/contrast_event.rb +1 -4
  11. data/lib/contrast/agent/assess/contrast_object.rb +2 -2
  12. data/lib/contrast/agent/assess/events/event_factory.rb +2 -1
  13. data/lib/contrast/agent/assess/finalizers/hash.rb +2 -4
  14. data/lib/contrast/agent/assess/policy/dynamic_source_factory.rb +6 -3
  15. data/lib/contrast/agent/assess/policy/patcher.rb +16 -21
  16. data/lib/contrast/agent/assess/policy/policy.rb +1 -1
  17. data/lib/contrast/agent/assess/policy/policy_node.rb +25 -33
  18. data/lib/contrast/agent/assess/policy/policy_scanner.rb +3 -5
  19. data/lib/contrast/agent/assess/policy/preshift.rb +7 -5
  20. data/lib/contrast/agent/assess/policy/propagation_method.rb +10 -19
  21. data/lib/contrast/agent/assess/policy/propagation_node.rb +19 -8
  22. data/lib/contrast/agent/assess/policy/propagator.rb +1 -0
  23. data/lib/contrast/agent/assess/policy/propagator/center.rb +2 -1
  24. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +3 -6
  25. data/lib/contrast/agent/assess/policy/propagator/insert.rb +3 -1
  26. data/lib/contrast/agent/assess/policy/propagator/match_data.rb +2 -1
  27. data/lib/contrast/agent/assess/policy/propagator/rack_protection.rb +73 -0
  28. data/lib/contrast/agent/assess/policy/propagator/select.rb +2 -12
  29. data/lib/contrast/agent/assess/policy/propagator/split.rb +12 -13
  30. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +3 -10
  31. data/lib/contrast/agent/assess/policy/propagator/trim.rb +3 -15
  32. data/lib/contrast/agent/assess/policy/rewriter_patch.rb +13 -10
  33. data/lib/contrast/agent/assess/policy/source_method.rb +12 -12
  34. data/lib/contrast/agent/assess/policy/source_validation/source_validation.rb +1 -3
  35. data/lib/contrast/agent/assess/policy/trigger/reflected_xss.rb +5 -1
  36. data/lib/contrast/agent/assess/policy/trigger/xpath.rb +0 -3
  37. data/lib/contrast/agent/assess/policy/trigger_method.rb +8 -18
  38. data/lib/contrast/agent/assess/policy/trigger_node.rb +3 -2
  39. data/lib/contrast/agent/assess/policy/trigger_validation/redos_validator.rb +4 -3
  40. data/lib/contrast/agent/assess/policy/trigger_validation/ssrf_validator.rb +1 -2
  41. data/lib/contrast/agent/assess/policy/trigger_validation/xss_validator.rb +1 -8
  42. data/lib/contrast/agent/assess/property/evented.rb +8 -5
  43. data/lib/contrast/agent/assess/rule/provider/hardcoded_key.rb +11 -5
  44. data/lib/contrast/agent/assess/rule/provider/hardcoded_password.rb +4 -1
  45. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +7 -9
  46. data/lib/contrast/agent/at_exit_hook.rb +3 -3
  47. data/lib/contrast/agent/class_reopener.rb +9 -6
  48. data/lib/contrast/agent/disable_reaction.rb +4 -7
  49. data/lib/contrast/agent/exclusion_matcher.rb +7 -14
  50. data/lib/contrast/agent/inventory/dependencies.rb +2 -0
  51. data/lib/contrast/agent/inventory/dependency_analysis.rb +2 -6
  52. data/lib/contrast/agent/inventory/dependency_usage_analysis.rb +3 -5
  53. data/lib/contrast/agent/inventory/policy/datastores.rb +3 -4
  54. data/lib/contrast/agent/inventory/policy/policy.rb +1 -1
  55. data/lib/contrast/agent/middleware.rb +17 -18
  56. data/lib/contrast/agent/module_data.rb +3 -3
  57. data/lib/contrast/agent/patching/policy/after_load_patch.rb +3 -3
  58. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +9 -9
  59. data/lib/contrast/agent/patching/policy/method_policy.rb +6 -2
  60. data/lib/contrast/agent/patching/policy/module_policy.rb +14 -7
  61. data/lib/contrast/agent/patching/policy/patch.rb +20 -25
  62. data/lib/contrast/agent/patching/policy/patch_status.rb +6 -7
  63. data/lib/contrast/agent/patching/policy/patcher.rb +21 -18
  64. data/lib/contrast/agent/patching/policy/policy.rb +2 -4
  65. data/lib/contrast/agent/patching/policy/policy_node.rb +16 -7
  66. data/lib/contrast/agent/patching/policy/trigger_node.rb +21 -8
  67. data/lib/contrast/agent/protect/policy/applies_command_injection_rule.rb +1 -1
  68. data/lib/contrast/agent/protect/policy/applies_deserialization_rule.rb +1 -1
  69. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +1 -1
  70. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +2 -3
  71. data/lib/contrast/agent/protect/policy/applies_sqli_rule.rb +1 -1
  72. data/lib/contrast/agent/protect/policy/applies_xxe_rule.rb +5 -9
  73. data/lib/contrast/agent/protect/policy/policy.rb +1 -1
  74. data/lib/contrast/agent/protect/policy/rule_applicator.rb +7 -9
  75. data/lib/contrast/agent/protect/rule/base.rb +20 -23
  76. data/lib/contrast/agent/protect/rule/base_service.rb +9 -5
  77. data/lib/contrast/agent/protect/rule/cmd_injection.rb +18 -23
  78. data/lib/contrast/agent/protect/rule/deserialization.rb +6 -13
  79. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +3 -14
  80. data/lib/contrast/agent/protect/rule/no_sqli.rb +6 -2
  81. data/lib/contrast/agent/protect/rule/no_sqli/mongo_no_sql_scanner.rb +1 -3
  82. data/lib/contrast/agent/protect/rule/path_traversal.rb +6 -10
  83. data/lib/contrast/agent/protect/rule/sqli.rb +1 -1
  84. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +1 -1
  85. data/lib/contrast/agent/protect/rule/xss.rb +1 -1
  86. data/lib/contrast/agent/protect/rule/xxe.rb +5 -12
  87. data/lib/contrast/agent/protect/rule/xxe/entity_wrapper.rb +1 -2
  88. data/lib/contrast/agent/reaction_processor.rb +13 -13
  89. data/lib/contrast/agent/request.rb +27 -26
  90. data/lib/contrast/agent/request_context.rb +17 -22
  91. data/lib/contrast/agent/request_handler.rb +5 -3
  92. data/lib/contrast/agent/response.rb +2 -3
  93. data/lib/contrast/agent/rewriter.rb +9 -6
  94. data/lib/contrast/agent/rule_set.rb +5 -4
  95. data/lib/contrast/agent/service_heartbeat.rb +4 -6
  96. data/lib/contrast/agent/static_analysis.rb +6 -5
  97. data/lib/contrast/agent/thread.rb +2 -4
  98. data/lib/contrast/agent/thread_watcher.rb +3 -4
  99. data/lib/contrast/agent/tracepoint_hook.rb +5 -5
  100. data/lib/contrast/agent/version.rb +1 -1
  101. data/lib/contrast/api/communication/messaging_queue.rb +4 -5
  102. data/lib/contrast/api/communication/response_processor.rb +11 -13
  103. data/lib/contrast/api/communication/service_lifecycle.rb +9 -6
  104. data/lib/contrast/api/communication/socket_client.rb +22 -31
  105. data/lib/contrast/api/communication/speedracer.rb +8 -13
  106. data/lib/contrast/api/decorators/address.rb +2 -3
  107. data/lib/contrast/api/decorators/agent_startup.rb +7 -9
  108. data/lib/contrast/api/decorators/application_startup.rb +12 -10
  109. data/lib/contrast/api/decorators/application_update.rb +0 -4
  110. data/lib/contrast/api/decorators/http_request.rb +3 -7
  111. data/lib/contrast/api/decorators/instrumentation_mode.rb +3 -5
  112. data/lib/contrast/api/decorators/library.rb +8 -6
  113. data/lib/contrast/api/decorators/message.rb +9 -9
  114. data/lib/contrast/api/decorators/trace_event.rb +3 -1
  115. data/lib/contrast/api/decorators/trace_event_object.rb +3 -6
  116. data/lib/contrast/api/decorators/trace_taint_range_tags.rb +1 -6
  117. data/lib/contrast/components/agent.rb +17 -17
  118. data/lib/contrast/components/app_context.rb +11 -15
  119. data/lib/contrast/components/assess.rb +16 -16
  120. data/lib/contrast/components/base.rb +40 -0
  121. data/lib/contrast/components/config.rb +2 -3
  122. data/lib/contrast/components/contrast_service.rb +12 -18
  123. data/lib/contrast/components/heap_dump.rb +5 -4
  124. data/lib/contrast/components/inventory.rb +2 -7
  125. data/lib/contrast/components/logger.rb +1 -2
  126. data/lib/contrast/components/protect.rb +10 -13
  127. data/lib/contrast/components/sampling.rb +13 -7
  128. data/lib/contrast/components/scope.rb +0 -4
  129. data/lib/contrast/components/settings.rb +5 -7
  130. data/lib/contrast/config/assess_rules_configuration.rb +1 -3
  131. data/lib/contrast/config/base_configuration.rb +4 -5
  132. data/lib/contrast/config/exception_configuration.rb +1 -5
  133. data/lib/contrast/config/heap_dump_configuration.rb +12 -6
  134. data/lib/contrast/config/logger_configuration.rb +1 -5
  135. data/lib/contrast/configuration.rb +6 -18
  136. data/lib/contrast/extension/assess/array.rb +3 -10
  137. data/lib/contrast/extension/assess/erb.rb +1 -7
  138. data/lib/contrast/extension/assess/eval_trigger.rb +4 -9
  139. data/lib/contrast/extension/assess/exec_trigger.rb +3 -9
  140. data/lib/contrast/extension/assess/fiber.rb +8 -17
  141. data/lib/contrast/extension/assess/hash.rb +3 -3
  142. data/lib/contrast/extension/assess/kernel.rb +4 -13
  143. data/lib/contrast/extension/assess/marshal.rb +6 -10
  144. data/lib/contrast/extension/assess/regexp.rb +6 -10
  145. data/lib/contrast/extension/assess/string.rb +8 -6
  146. data/lib/contrast/extension/kernel.rb +2 -2
  147. data/lib/contrast/extension/protect/kernel.rb +0 -5
  148. data/lib/contrast/framework/manager.rb +3 -5
  149. data/lib/contrast/framework/rack/patch/session_cookie.rb +11 -24
  150. data/lib/contrast/framework/rack/patch/support.rb +6 -4
  151. data/lib/contrast/framework/rails/patch/assess_configuration.rb +12 -9
  152. data/lib/contrast/framework/rails/patch/support.rb +41 -35
  153. data/lib/contrast/framework/rails/railtie.rb +34 -0
  154. data/lib/contrast/framework/rails/rewrite/action_controller_railties_helper_inherited.rb +4 -1
  155. data/lib/contrast/framework/rails/rewrite/active_record_attribute_methods_read.rb +2 -0
  156. data/lib/contrast/framework/rails/rewrite/active_record_named.rb +5 -4
  157. data/lib/contrast/framework/rails/rewrite/active_record_time_zone_inherited.rb +2 -0
  158. data/lib/contrast/framework/rails/support.rb +2 -2
  159. data/lib/contrast/framework/sinatra/support.rb +3 -1
  160. data/lib/contrast/funchook/funchook.rb +5 -8
  161. data/lib/contrast/logger/application.rb +13 -15
  162. data/lib/contrast/logger/format.rb +2 -5
  163. data/lib/contrast/logger/log.rb +26 -9
  164. data/lib/contrast/logger/request.rb +1 -6
  165. data/lib/contrast/security_exception.rb +1 -1
  166. data/lib/contrast/tasks/config.rb +0 -1
  167. data/lib/contrast/tasks/service.rb +6 -7
  168. data/lib/contrast/utils/assess/sampling_util.rb +2 -3
  169. data/lib/contrast/utils/assess/tracking_util.rb +3 -6
  170. data/lib/contrast/utils/class_util.rb +0 -8
  171. data/lib/contrast/utils/hash_digest.rb +2 -5
  172. data/lib/contrast/utils/heap_dump_util.rb +5 -3
  173. data/lib/contrast/utils/invalid_configuration_util.rb +4 -3
  174. data/lib/contrast/utils/inventory_util.rb +2 -3
  175. data/lib/contrast/utils/io_util.rb +3 -5
  176. data/lib/contrast/utils/job_servers_running.rb +13 -7
  177. data/lib/contrast/utils/os.rb +4 -4
  178. data/lib/contrast/utils/ruby_ast_rewriter.rb +2 -1
  179. data/lib/contrast/utils/string_utils.rb +2 -3
  180. data/lib/contrast/utils/tag_util.rb +25 -19
  181. data/resources/assess/policy.json +55 -0
  182. data/ruby-agent.gemspec +17 -16
  183. data/service_executables/VERSION +1 -1
  184. data/service_executables/linux/contrast-service +0 -0
  185. data/service_executables/mac/contrast-service +0 -0
  186. data/sonar-project.properties +9 -0
  187. metadata +61 -46
  188. data/lib/contrast/agent/railtie.rb +0 -31
  189. data/lib/contrast/components/interface.rb +0 -195
  190. data/lib/contrast/delegators/input_analysis.rb +0 -12
@@ -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
@@ -1,9 +1,12 @@
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
+ return unless RUBY_VERSION < '2.6.0' # TODO: RUBY-714 remove guard w/ EOL of 2.5
5
+
4
6
  require 'ripper'
5
7
  require 'contrast/extension/module'
6
- require 'contrast/components/interface'
8
+ require 'contrast/components/logger'
9
+ require 'contrast/components/scope'
7
10
  require 'contrast/logger/log'
8
11
 
9
12
  # This method is left purposefully at the top level namespace. Moving it
@@ -18,7 +21,7 @@ require 'contrast/logger/log'
18
21
  def unbound_eval _class_name, content
19
22
  # Yuck, this is a top-level method that has to break encapsulation
20
23
  # in order to access scoping!
21
- Contrast::Components::Scope::COMPONENT_INTERFACE.scope_for_current_ec.enter_contrast_scope!
24
+ ::Contrast::SCOPE.scope_for_current_ec.enter_contrast_scope!
22
25
  eval(content) # rubocop:disable Security/Eval
23
26
  rescue Exception # rubocop:disable Lint/RescueException
24
27
  # We can't use components here, so we have to access the log directly. I hate
@@ -27,7 +30,7 @@ rescue Exception # rubocop:disable Lint/RescueException
27
30
  # And we need to return nil here, not the value from the logger.
28
31
  nil
29
32
  ensure
30
- Contrast::Components::Scope::COMPONENT_INTERFACE.scope_for_current_ec.exit_contrast_scope!
33
+ ::Contrast::SCOPE.scope_for_current_ec.exit_contrast_scope!
31
34
  end
32
35
 
33
36
  module Contrast
@@ -36,8 +39,8 @@ module Contrast
36
39
  # @deprecated Changes to this class are discouraged as this approach is
37
40
  # being phased out with support for those language versions.
38
41
  class ClassReopener
39
- include Contrast::Components::Interface
40
- access_component :logging, :scope
42
+ include Contrast::Components::Logger::InstanceMethods
43
+ include Contrast::Components::Scope::InstanceMethods
41
44
 
42
45
  END_NEW_LINE = "end\n"
43
46
  PROTECTED_WITH_NEW_LINE = "protected\n"
@@ -48,7 +51,7 @@ module Contrast
48
51
  :public_instance_methods, :protected_instance_methods, :private_instance_methods, :locations
49
52
 
50
53
  def initialize module_data
51
- @class_module_path = module_data.name
54
+ @class_module_path = module_data.mod_name
52
55
  clazz = module_data.mod
53
56
  @is_class = clazz.is_a?(Class)
54
57
  @public_instance_methods = []
@@ -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,14 +9,11 @@ module Contrast
9
9
  # typically because some configuration setting did not satisfy requirements
10
10
  # set by the Organization's Administrator
11
11
  class DisableReaction
12
- include Contrast::Components::Interface
13
- access_component :agent, :logging
12
+ extend Contrast::Components::Logger::InstanceMethods
14
13
 
15
14
  def self.run _reaction, level
16
- logger.with_level(
17
- level,
18
- 'Contrast received instructions to disable itself - Disabling now')
19
- AGENT.disable!
15
+ logger.with_level(level, 'Contrast received instructions to disable itself - Disabling now')
16
+ ::Contrast::AGENT.disable!
20
17
  end
21
18
  end
22
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
  #
@@ -47,7 +46,7 @@ module Contrast
47
46
  return if @wildcard_url
48
47
  return unless @exclusion.urls&.any?
49
48
 
50
- @wildcard_url ||= @exclusion.urls.any? { |test| test == '/.*' }
49
+ @wildcard_url ||= @exclusion.urls.any?('/.*')
51
50
  return if @wildcard_url
52
51
 
53
52
  @urls = []
@@ -95,8 +94,8 @@ module Contrast
95
94
  @exclusion.type == Contrast::Api::Settings::Exclusion::ExclusionType::CODE
96
95
  end
97
96
 
98
- def name
99
- @exclusion.name
97
+ def exc_name
98
+ @exclusion.name # rubocop:disable Security/Module/Name -- part of the API.
100
99
  end
101
100
 
102
101
  def match_all?
@@ -109,10 +108,7 @@ module Contrast
109
108
  #
110
109
  # @param rule - the id of the rule which we're checking for exclusion
111
110
  def protection_rule? rule
112
- protect? &&
113
- (@exclusion.protection_rules.empty? ||
114
- @exclusion.protection_rules.include?(rule)
115
- )
111
+ protect? && (@exclusion.protection_rules.empty? || @exclusion.protection_rules.include?(rule))
116
112
  end
117
113
 
118
114
  # Determine if the given rule is excluded by this exclusion.
@@ -121,10 +117,7 @@ module Contrast
121
117
  #
122
118
  # @param rule - the id of the rule which we're checking for exclusion
123
119
  def assess_rule? rule
124
- assess? &&
125
- (@exclusion.assessment_rules.empty? ||
126
- @exclusion.assessment_rules.include?(rule)
127
- )
120
+ assess? && (@exclusion.assessment_rules.empty? || @exclusion.assessment_rules.include?(rule))
128
121
  end
129
122
 
130
123
  def match_code? stack_trace
@@ -33,6 +33,7 @@ module Contrast
33
33
  # then contrast depends on it. If its array of dependents is 1, then contrast is the
34
34
  # only dependency in that list. Since only contrast depends on it, we should ignore it.
35
35
  def find_contrast_gems
36
+ # rubocop:disable Security/Module/Name -- here name is part of Ruby Gems.
36
37
  ignore = Set.new([CONTRAST_AGENT])
37
38
  contrast_specs = Gem::DependencyList.from_specs.specs.find do |dependency|
38
39
  dependency.name == CONTRAST_AGENT
@@ -43,6 +44,7 @@ module Contrast
43
44
  ignore.add(dependency.name) if contrast_dep_set.include?(dependency.name) && dependents.length == 1
44
45
  end
45
46
  ignore
47
+ # rubocop:enable Security/Module/Name
46
48
  end
47
49
  end
48
50
  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
 
@@ -107,7 +105,7 @@ module Contrast
107
105
 
108
106
  # We only use this if inventory and library analysis are enabled
109
107
  def enabled?
110
- @_enabled = INVENTORY.enabled? && INVENTORY.analyze_libraries? if @_enabled.nil?
108
+ @_enabled = ::Contrast::INVENTORY.enabled? && ::Contrast::INVENTORY.analyze_libraries? if @_enabled.nil?
111
109
  @_enabled
112
110
  end
113
111
  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
  require 'contrast/utils/inventory_util'
6
6
 
7
7
  module Contrast
@@ -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
@@ -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,11 +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
- logger.error(
41
- 'The Agent was unable to initialize before the application middleware was initialized. ' \
42
- 'Disabling permanently.')
43
- AGENT.disable! # ensure the agent is disabled (probably redundant)
40
+ unless ::Contrast::AGENT.enabled?
41
+ logger.error('The Agent was unable to initialize before the application middleware was initialized. '\
42
+ 'Disabling permanently.')
43
+ ::Contrast::AGENT.disable! # ensure the agent is disabled (probably redundant)
44
44
  return
45
45
  end
46
46
  agent_startup_routine
@@ -54,7 +54,7 @@ module Contrast
54
54
  # @return [Array,Rack::Response] the Response of this and subsequent Middlewares to be passed back to the user up
55
55
  # the Rack framework.
56
56
  def call env
57
- return app.call(env) unless AGENT.enabled?
57
+ return app.call(env) unless ::Contrast::AGENT.enabled?
58
58
 
59
59
  Contrast::Agent.heapdump_util.start_thread!
60
60
  handle_first_request
@@ -64,17 +64,17 @@ module Contrast
64
64
  private
65
65
 
66
66
  def setup_agent
67
- SETTINGS.reset_state
67
+ ::Contrast::SETTINGS.reset_state
68
68
 
69
69
  inform_deprecations
70
70
 
71
- if CONFIG.invalid?
72
- AGENT.disable!
71
+ if ::Contrast::CONFIG.invalid?
72
+ ::Contrast::AGENT.disable!
73
73
  logger.error('!!! CONFIG FILE IS INVALID - DISABLING CONTRAST AGENT !!!')
74
- elsif AGENT.disabled?
74
+ elsif ::Contrast::AGENT.disabled?
75
75
  logger.warn('Contrast disabled by configuration. Continuing without instrumentation.')
76
76
  else
77
- AGENT.enable!
77
+ ::Contrast::AGENT.enable!
78
78
  end
79
79
  end
80
80
 
@@ -93,7 +93,7 @@ module Contrast
93
93
  end
94
94
 
95
95
  logger.debug_with_time('middleware: enabling tracepoint') do
96
- AGENT.enable_tracepoint
96
+ ::Contrast::AGENT.enable_tracepoint
97
97
  end
98
98
  Contrast::Agent::AtExitHook.exit_hook
99
99
  end
@@ -192,7 +192,7 @@ module Contrast
192
192
  # config.agent.ruby.exceptions.capture? is set
193
193
  def handle_exception exception
194
194
  if security_exception?(exception)
195
- exception_control = AGENT.exception_control
195
+ exception_control = ::Contrast::AGENT.exception_control
196
196
  raise exception unless exception_control[:enable]
197
197
 
198
198
  [exception_control[:status], {}, [exception_control[:message]]]
@@ -207,8 +207,7 @@ module Contrast
207
207
  # @param exception [Exception]
208
208
  # @return [Boolean]
209
209
  def security_exception? exception
210
- exception.is_a?(Contrast::SecurityException) ||
211
- exception.message&.include?(SECURITY_EXCEPTION_MARKER)
210
+ exception.is_a?(Contrast::SecurityException) || exception.message&.include?(SECURITY_EXCEPTION_MARKER)
212
211
  end
213
212
 
214
213
  # As we deprecate support to prepare to remove dead code, we need to inform our users still relying on the now
@@ -6,11 +6,11 @@ module Contrast
6
6
  # A simple wrapper around a Module and a call to its name, used to avoid
7
7
  # calling the Module#name method and generating extra Strings
8
8
  class ModuleData
9
- attr_reader :mod, :name
9
+ attr_reader :mod, :mod_name
10
10
 
11
- def initialize mod, name = nil
11
+ def initialize mod, mod_name = nil
12
12
  @mod = mod
13
- @name = name || mod.cs__name
13
+ @mod_name = mod_name || mod.cs__name
14
14
  end
15
15
  end
16
16
  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/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
@@ -2,7 +2,7 @@
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
7
 
8
8
  module Contrast
@@ -12,8 +12,7 @@ module Contrast
12
12
  # Some modules diverge from our generic instrumentation and require custom instrumentation
13
13
  # after they've been loaded
14
14
  module AfterLoadPatcher
15
- include Contrast::Components::Interface
16
- access_component :agent, :logging
15
+ include Contrast::Components::Logger::InstanceMethods
17
16
 
18
17
  # After initialization run a catchup check to instrument any already loaded modules we care about
19
18
  def catchup_after_load_patches
@@ -49,16 +48,17 @@ module Contrast
49
48
  def apply_load_patches!
50
49
  after_load_patches.each do |after_load_patch|
51
50
  next unless after_load_patch.target_defined?
52
- next if AGENT.skip_instrumentation?(after_load_patch.module_name)
51
+ next if ::Contrast::AGENT.skip_instrumentation?(after_load_patch.module_name)
53
52
 
54
- logger.trace(
55
- 'Catching up on already loaded afterload patch - applying instrumentation',
56
- module: after_load_patch.module_name)
53
+ logger.trace('Catching up on already loaded afterload patch - applying instrumentation',
54
+ module: after_load_patch.module_name)
57
55
  after_load_patch.instrument!
58
56
  rescue NameError => e
59
- logger.error('Method undefined in afterload patch', e, module: after_load_patch.module_name, method: after_load_patch.method_to_instrument)
57
+ logger.error('Method undefined in afterload patch', e, module: after_load_patch.module_name,
58
+ method: after_load_patch.method_to_instrument)
60
59
  rescue StandardError => e
61
- logger.error('Afterload patch failed to apply', e, module: after_load_patch.module_name, method: after_load_patch.method_to_instrument)
60
+ logger.error('Afterload patch failed to apply', e, module: after_load_patch.module_name,
61
+ method: after_load_patch.method_to_instrument)
62
62
  end
63
63
  after_load_patches.delete_if(&:applied?)
64
64
  end
@@ -49,7 +49,10 @@ module Contrast
49
49
  private
50
50
 
51
51
  def nodes
52
- @_nodes ||= [source_node, propagation_node, trigger_node, inventory_node, protect_node, deadzone_node].compact
52
+ @_nodes ||= [
53
+ source_node, propagation_node, trigger_node, inventory_node, protect_node,
54
+ deadzone_node
55
+ ].compact
53
56
  end
54
57
 
55
58
  def method_scopes
@@ -76,7 +79,8 @@ module Contrast
76
79
  protect_node = find_method_node(module_policy.protect_nodes, method_name, instance_method)
77
80
  inventory_node = find_method_node(module_policy.inventory_nodes, method_name, instance_method)
78
81
  deadzone_node = find_method_node(module_policy.deadzone_nodes, method_name, instance_method)
79
- method_visibility = find_visibility(source_node, propagation_node, trigger_node, protect_node, inventory_node, deadzone_node)
82
+ method_visibility = find_visibility(source_node, propagation_node, trigger_node, protect_node,
83
+ inventory_node, deadzone_node)
80
84
  MethodPolicy.new(method_name: method_name,
81
85
  method_visibility: method_visibility,
82
86
  instance_method: instance_method,