contrast-agent 3.13.0 → 3.16.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (241) hide show
  1. checksums.yaml +4 -4
  2. data/exe/contrast_service +1 -7
  3. data/ext/cs__assess_active_record_named/cs__active_record_named.c +8 -7
  4. data/ext/cs__assess_array/cs__assess_array.c +6 -5
  5. data/ext/cs__assess_basic_object/cs__assess_basic_object.c +5 -5
  6. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.c +2 -1
  7. data/ext/cs__assess_hash/cs__assess_hash.c +18 -17
  8. data/ext/cs__assess_hash/cs__assess_hash.h +2 -1
  9. data/ext/cs__assess_kernel/cs__assess_kernel.c +7 -8
  10. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +18 -16
  11. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.h +1 -0
  12. data/ext/cs__assess_module/cs__assess_module.c +6 -6
  13. data/ext/cs__assess_regexp/cs__assess_regexp.c +4 -4
  14. data/ext/cs__assess_string/cs__assess_string.c +31 -16
  15. data/ext/cs__assess_string/cs__assess_string.h +6 -1
  16. data/ext/cs__assess_string_interpolation26/cs__assess_string_interpolation26.c +4 -2
  17. data/ext/cs__assess_yield_track/cs__assess_yield_track.c +2 -2
  18. data/ext/cs__common/cs__common.c +48 -39
  19. data/ext/cs__common/cs__common.h +16 -21
  20. data/ext/cs__contrast_patch/cs__contrast_patch.c +27 -25
  21. data/ext/cs__contrast_patch/cs__contrast_patch.h +5 -7
  22. data/ext/cs__protect_kernel/cs__protect_kernel.c +11 -12
  23. data/ext/cs__protect_kernel/cs__protect_kernel.h +2 -2
  24. data/lib/contrast-agent.rb +1 -1
  25. data/lib/contrast.rb +13 -23
  26. data/lib/contrast/agent.rb +39 -46
  27. data/lib/contrast/agent/assess.rb +12 -12
  28. data/lib/contrast/agent/assess/contrast_event.rb +151 -85
  29. data/lib/contrast/agent/assess/events/event_factory.rb +2 -2
  30. data/lib/contrast/agent/assess/events/source_event.rb +3 -3
  31. data/lib/contrast/agent/assess/finalizers/freeze.rb +15 -0
  32. data/lib/contrast/agent/assess/finalizers/hash.rb +97 -0
  33. data/lib/contrast/agent/assess/policy/dynamic_source_factory.rb +11 -4
  34. data/lib/contrast/agent/assess/policy/patcher.rb +6 -6
  35. data/lib/contrast/agent/assess/policy/policy.rb +9 -11
  36. data/lib/contrast/agent/assess/policy/policy_node.rb +17 -12
  37. data/lib/contrast/agent/assess/policy/policy_scanner.rb +9 -4
  38. data/lib/contrast/agent/assess/policy/preshift.rb +13 -7
  39. data/lib/contrast/agent/assess/policy/propagation_method.rb +64 -44
  40. data/lib/contrast/agent/assess/policy/propagation_node.rb +2 -2
  41. data/lib/contrast/agent/assess/policy/propagator.rb +18 -18
  42. data/lib/contrast/agent/assess/policy/propagator/append.rb +8 -5
  43. data/lib/contrast/agent/assess/policy/propagator/base.rb +1 -1
  44. data/lib/contrast/agent/assess/policy/propagator/center.rb +9 -5
  45. data/lib/contrast/agent/assess/policy/propagator/custom.rb +1 -1
  46. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +6 -4
  47. data/lib/contrast/agent/assess/policy/propagator/insert.rb +7 -4
  48. data/lib/contrast/agent/assess/policy/propagator/keep.rb +4 -1
  49. data/lib/contrast/agent/assess/policy/propagator/match_data.rb +7 -9
  50. data/lib/contrast/agent/assess/policy/propagator/next.rb +7 -5
  51. data/lib/contrast/agent/assess/policy/propagator/prepend.rb +13 -5
  52. data/lib/contrast/agent/assess/policy/propagator/remove.rb +8 -4
  53. data/lib/contrast/agent/assess/policy/propagator/replace.rb +5 -2
  54. data/lib/contrast/agent/assess/policy/propagator/reverse.rb +7 -5
  55. data/lib/contrast/agent/assess/policy/propagator/select.rb +13 -7
  56. data/lib/contrast/agent/assess/policy/propagator/splat.rb +10 -9
  57. data/lib/contrast/agent/assess/policy/propagator/split.rb +27 -22
  58. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +52 -35
  59. data/lib/contrast/agent/assess/policy/propagator/trim.rb +11 -5
  60. data/lib/contrast/agent/assess/policy/rewriter_patch.rb +5 -5
  61. data/lib/contrast/agent/assess/policy/source_method.rb +90 -72
  62. data/lib/contrast/agent/assess/policy/source_validation/cross_site_validator.rb +1 -1
  63. data/lib/contrast/agent/assess/policy/source_validation/source_validation.rb +1 -1
  64. data/lib/contrast/agent/assess/policy/trigger/reflected_xss.rb +16 -12
  65. data/lib/contrast/agent/assess/policy/trigger/xpath.rb +2 -2
  66. data/lib/contrast/agent/assess/policy/trigger_method.rb +81 -33
  67. data/lib/contrast/agent/assess/policy/trigger_node.rb +41 -46
  68. data/lib/contrast/agent/assess/policy/trigger_validation/ssrf_validator.rb +2 -1
  69. data/lib/contrast/agent/assess/policy/trigger_validation/trigger_validation.rb +2 -2
  70. data/lib/contrast/agent/assess/properties.rb +15 -5
  71. data/lib/contrast/agent/assess/property/evented.rb +7 -20
  72. data/lib/contrast/agent/assess/property/tagged.rb +13 -7
  73. data/lib/contrast/agent/assess/property/updated.rb +131 -0
  74. data/lib/contrast/agent/assess/rule.rb +2 -2
  75. data/lib/contrast/agent/assess/rule/base.rb +3 -4
  76. data/lib/contrast/agent/assess/rule/provider.rb +3 -3
  77. data/lib/contrast/agent/assess/rule/provider/hardcoded_key.rb +58 -5
  78. data/lib/contrast/agent/assess/rule/provider/hardcoded_password.rb +24 -9
  79. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +84 -16
  80. data/lib/contrast/agent/assess/tag.rb +1 -1
  81. data/lib/contrast/agent/assess/tracker.rb +66 -0
  82. data/lib/contrast/agent/at_exit_hook.rb +6 -6
  83. data/lib/contrast/agent/class_reopener.rb +14 -11
  84. data/lib/contrast/agent/deadzone/policy/deadzone_node.rb +1 -1
  85. data/lib/contrast/agent/deadzone/policy/policy.rb +2 -2
  86. data/lib/contrast/agent/disable_reaction.rb +1 -1
  87. data/lib/contrast/agent/exclusion_matcher.rb +1 -1
  88. data/lib/contrast/agent/inventory/policy/datastores.rb +2 -2
  89. data/lib/contrast/agent/inventory/policy/policy.rb +3 -3
  90. data/lib/contrast/agent/inventory/policy/trigger_node.rb +1 -1
  91. data/lib/contrast/agent/middleware.rb +32 -32
  92. data/lib/contrast/agent/patching/policy/after_load_patch.rb +9 -9
  93. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +23 -22
  94. data/lib/contrast/agent/patching/policy/module_policy.rb +11 -11
  95. data/lib/contrast/agent/patching/policy/patch.rb +15 -15
  96. data/lib/contrast/agent/patching/policy/patcher.rb +43 -44
  97. data/lib/contrast/agent/patching/policy/policy.rb +23 -12
  98. data/lib/contrast/agent/patching/policy/policy_node.rb +1 -1
  99. data/lib/contrast/agent/patching/policy/trigger_node.rb +2 -2
  100. data/lib/contrast/agent/protect/policy/applies_command_injection_rule.rb +6 -8
  101. data/lib/contrast/agent/protect/policy/applies_deserialization_rule.rb +2 -2
  102. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +2 -2
  103. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +3 -3
  104. data/lib/contrast/agent/protect/policy/applies_sqli_rule.rb +2 -2
  105. data/lib/contrast/agent/protect/policy/applies_xxe_rule.rb +4 -4
  106. data/lib/contrast/agent/protect/policy/policy.rb +8 -8
  107. data/lib/contrast/agent/protect/policy/rule_applicator.rb +1 -1
  108. data/lib/contrast/agent/protect/policy/trigger_node.rb +1 -1
  109. data/lib/contrast/agent/protect/rule.rb +18 -18
  110. data/lib/contrast/agent/protect/rule/base.rb +4 -3
  111. data/lib/contrast/agent/protect/rule/base_service.rb +1 -1
  112. data/lib/contrast/agent/protect/rule/cmd_injection.rb +5 -5
  113. data/lib/contrast/agent/protect/rule/deserialization.rb +1 -1
  114. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +1 -1
  115. data/lib/contrast/agent/protect/rule/no_sqli.rb +1 -1
  116. data/lib/contrast/agent/protect/rule/no_sqli/mongo_no_sql_scanner.rb +1 -0
  117. data/lib/contrast/agent/protect/rule/path_traversal.rb +4 -5
  118. data/lib/contrast/agent/protect/rule/sqli.rb +2 -2
  119. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +1 -1
  120. data/lib/contrast/agent/protect/rule/xss.rb +1 -1
  121. data/lib/contrast/agent/protect/rule/xxe.rb +3 -5
  122. data/lib/contrast/agent/protect/rule/xxe/entity_wrapper.rb +2 -2
  123. data/lib/contrast/agent/railtie.rb +1 -1
  124. data/lib/contrast/agent/reaction_processor.rb +2 -2
  125. data/lib/contrast/agent/request.rb +45 -43
  126. data/lib/contrast/agent/request_context.rb +10 -6
  127. data/lib/contrast/agent/response.rb +23 -12
  128. data/lib/contrast/agent/rewriter.rb +6 -9
  129. data/lib/contrast/agent/service_heartbeat.rb +2 -2
  130. data/lib/contrast/agent/static_analysis.rb +9 -9
  131. data/lib/contrast/agent/thread.rb +1 -1
  132. data/lib/contrast/agent/thread_watcher.rb +2 -2
  133. data/lib/contrast/agent/tracepoint_hook.rb +1 -1
  134. data/lib/contrast/agent/version.rb +1 -1
  135. data/lib/contrast/api.rb +4 -4
  136. data/lib/contrast/api/communication.rb +9 -9
  137. data/lib/contrast/api/communication/messaging_queue.rb +3 -6
  138. data/lib/contrast/api/communication/response_processor.rb +1 -1
  139. data/lib/contrast/api/communication/socket_client.rb +41 -6
  140. data/lib/contrast/api/communication/speedracer.rb +1 -1
  141. data/lib/contrast/api/communication/tcp_socket.rb +1 -1
  142. data/lib/contrast/api/communication/unix_socket.rb +1 -1
  143. data/lib/contrast/api/decorators.rb +14 -14
  144. data/lib/contrast/api/decorators/address.rb +20 -19
  145. data/lib/contrast/api/decorators/application_settings.rb +3 -2
  146. data/lib/contrast/api/decorators/application_update.rb +7 -8
  147. data/lib/contrast/api/decorators/http_request.rb +13 -12
  148. data/lib/contrast/api/decorators/input_analysis.rb +3 -2
  149. data/lib/contrast/api/decorators/message.rb +4 -2
  150. data/lib/contrast/api/decorators/rasp_rule_sample.rb +2 -1
  151. data/lib/contrast/api/decorators/route_coverage.rb +3 -2
  152. data/lib/contrast/api/decorators/server_features.rb +3 -2
  153. data/lib/contrast/api/decorators/trace_event.rb +28 -25
  154. data/lib/contrast/api/decorators/trace_event_object.rb +6 -5
  155. data/lib/contrast/api/decorators/trace_event_signature.rb +5 -4
  156. data/lib/contrast/api/decorators/trace_taint_range.rb +4 -3
  157. data/lib/contrast/api/decorators/user_input.rb +4 -4
  158. data/lib/contrast/common_agent_configuration.rb +2 -2
  159. data/lib/contrast/components/agent.rb +2 -2
  160. data/lib/contrast/components/app_context.rb +50 -39
  161. data/lib/contrast/components/config.rb +7 -17
  162. data/lib/contrast/components/contrast_service.rb +10 -10
  163. data/lib/contrast/components/interface.rb +15 -15
  164. data/lib/contrast/components/logger.rb +1 -1
  165. data/lib/contrast/components/scope.rb +3 -3
  166. data/lib/contrast/components/settings.rb +20 -23
  167. data/lib/contrast/config.rb +18 -18
  168. data/lib/contrast/config/application_configuration.rb +5 -2
  169. data/lib/contrast/config/base_configuration.rb +2 -2
  170. data/lib/contrast/config/protect_rule_configuration.rb +1 -1
  171. data/lib/contrast/config/service_configuration.rb +8 -0
  172. data/lib/contrast/configuration.rb +93 -52
  173. data/lib/contrast/extension/assess.rb +21 -22
  174. data/lib/contrast/extension/assess/array.rb +18 -11
  175. data/lib/contrast/extension/assess/erb.rb +11 -3
  176. data/lib/contrast/extension/assess/eval_trigger.rb +7 -7
  177. data/lib/contrast/extension/assess/exec_trigger.rb +2 -2
  178. data/lib/contrast/extension/assess/fiber.rb +14 -14
  179. data/lib/contrast/extension/assess/hash.rb +7 -6
  180. data/lib/contrast/extension/assess/kernel.rb +34 -28
  181. data/lib/contrast/extension/assess/marshal.rb +67 -0
  182. data/lib/contrast/extension/assess/regexp.rb +10 -9
  183. data/lib/contrast/extension/assess/string.rb +23 -23
  184. data/lib/contrast/extension/inventory.rb +4 -4
  185. data/lib/contrast/extension/kernel.rb +1 -1
  186. data/lib/contrast/extension/module.rb +1 -1
  187. data/lib/contrast/extension/protect.rb +3 -3
  188. data/lib/contrast/extension/protect/kernel.rb +4 -4
  189. data/lib/contrast/extension/protect/psych.rb +2 -2
  190. data/lib/contrast/framework/base_support.rb +1 -1
  191. data/lib/contrast/framework/manager.rb +10 -11
  192. data/lib/contrast/framework/rack/patch/session_cookie.rb +22 -28
  193. data/lib/contrast/framework/rack/patch/support.rb +1 -1
  194. data/lib/contrast/framework/rack/support.rb +2 -2
  195. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +13 -13
  196. data/lib/contrast/framework/rails/patch/assess_configuration.rb +6 -12
  197. data/lib/contrast/framework/rails/patch/rails_application_configuration.rb +11 -11
  198. data/lib/contrast/framework/rails/patch/support.rb +4 -4
  199. data/lib/contrast/framework/rails/rewrite/action_controller_railties_helper_inherited.rb +11 -11
  200. data/lib/contrast/framework/rails/rewrite/active_record_attribute_methods_read.rb +12 -12
  201. data/lib/contrast/framework/rails/rewrite/active_record_named.rb +4 -4
  202. data/lib/contrast/framework/rails/rewrite/active_record_time_zone_inherited.rb +12 -12
  203. data/lib/contrast/framework/rails/support.rb +64 -14
  204. data/lib/contrast/framework/sinatra/patch/base.rb +12 -12
  205. data/lib/contrast/framework/sinatra/patch/support.rb +1 -1
  206. data/lib/contrast/framework/sinatra/support.rb +6 -6
  207. data/lib/contrast/funchook/funchook.rb +1 -1
  208. data/lib/contrast/logger/application.rb +13 -5
  209. data/lib/contrast/logger/format.rb +22 -9
  210. data/lib/contrast/logger/log.rb +17 -10
  211. data/lib/contrast/logger/request.rb +30 -0
  212. data/lib/contrast/tasks/config.rb +1 -1
  213. data/lib/contrast/tasks/service.rb +2 -2
  214. data/lib/contrast/utils/assess/sampling_util.rb +2 -2
  215. data/lib/contrast/utils/assess/tracking_util.rb +49 -4
  216. data/lib/contrast/utils/boolean_util.rb +1 -1
  217. data/lib/contrast/utils/class_util.rb +2 -2
  218. data/lib/contrast/utils/duck_utils.rb +0 -10
  219. data/lib/contrast/utils/env_configuration_item.rb +2 -1
  220. data/lib/contrast/utils/gemfile_reader.rb +5 -5
  221. data/lib/contrast/utils/hash_digest.rb +2 -1
  222. data/lib/contrast/utils/heap_dump_util.rb +2 -2
  223. data/lib/contrast/utils/invalid_configuration_util.rb +21 -22
  224. data/lib/contrast/utils/inventory_util.rb +4 -4
  225. data/lib/contrast/utils/io_util.rb +1 -1
  226. data/lib/contrast/utils/os.rb +1 -1
  227. data/lib/contrast/utils/ruby_ast_rewriter.rb +1 -1
  228. data/lib/contrast/utils/sha256_builder.rb +2 -2
  229. data/lib/contrast/utils/stack_trace_utils.rb +2 -2
  230. data/lib/contrast/utils/string_utils.rb +11 -6
  231. data/resources/assess/policy.json +31 -22
  232. data/resources/deadzone/policy.json +5 -0
  233. data/ruby-agent.gemspec +21 -19
  234. data/service_executables/VERSION +1 -1
  235. data/service_executables/linux/contrast-service +0 -0
  236. data/service_executables/mac/contrast-service +0 -0
  237. metadata +67 -29
  238. data/lib/contrast/agent/assess/insulator.rb +0 -49
  239. data/lib/contrast/agent/require_state.rb +0 -61
  240. data/lib/contrast/extension/assess/assess_extension.rb +0 -147
  241. data/lib/contrast/utils/freeze_util.rb +0 -32
@@ -1,7 +1,7 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- cs__scoped_require 'contrast/framework/rails/patch/assess_configuration'
4
+ require 'contrast/framework/rails/patch/assess_configuration'
5
5
  module Contrast
6
6
  module Framework
7
7
  module Rails
@@ -13,16 +13,16 @@ module Contrast
13
13
  class RailsApplicationConfiguration
14
14
  def self.instrument
15
15
  @_instrument ||= begin
16
- ::Rails::Application::Configuration.class_eval do
17
- alias_method :cs__patched_session_store, :session_store
18
- def session_store *args
19
- ret = cs__patched_session_store(*args)
20
- Contrast::Framework::Rails::Patch::AssessConfiguration.analyze_session_store(*args)
21
- ret
22
- end
23
- end
24
- true
25
- end
16
+ ::Rails::Application::Configuration.class_eval do
17
+ alias_method :cs__patched_session_store, :session_store
18
+ def session_store *args
19
+ ret = cs__patched_session_store(*args)
20
+ Contrast::Framework::Rails::Patch::AssessConfiguration.analyze_session_store(*args)
21
+ ret
22
+ end
23
+ end
24
+ true
25
+ end
26
26
  end
27
27
  end
28
28
  end
@@ -1,8 +1,8 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- cs__scoped_require 'contrast/framework/rails/patch/rails_application_configuration'
5
- cs__scoped_require 'contrast/agent/patching/policy/after_load_patch'
4
+ require 'contrast/framework/rails/patch/rails_application_configuration'
5
+ require 'contrast/agent/patching/policy/after_load_patch'
6
6
 
7
7
  module Contrast
8
8
  module Framework
@@ -11,7 +11,7 @@ module Contrast
11
11
  # Extension point allowing for the registration of Patches required to
12
12
  # support the Rails framework.
13
13
  module Support
14
- # (See BaseSupport#before_load_patches)
14
+ # (See BaseSupport#before_load_patches!)
15
15
  def before_load_patches!
16
16
  return unless defined?(::Rails)
17
17
 
@@ -20,7 +20,7 @@ module Contrast
20
20
  # (i.e., where we normally patch) we will miss the configuration
21
21
  # and will never be able to report session misconfiguration rules.
22
22
  Contrast::Framework::Rails::Patch::RailsApplicationConfiguration.instrument
23
- cs__scoped_require 'contrast/agent/railtie' if ::Rails::VERSION::MAJOR.to_i >= 3
23
+ require 'contrast/agent/railtie' if ::Rails::VERSION::MAJOR.to_i >= 3
24
24
  end
25
25
 
26
26
  # (See BaseSupport#after_load_patches)
@@ -15,17 +15,17 @@ module Contrast
15
15
  class ActionControllerRailtiesHelperInherited
16
16
  def self.instrument
17
17
  @_instrument ||= begin
18
- ::ActionController::Railties::Helpers.class_eval do
19
- alias_method :cs__patched_helper_inherited, :inherited
20
- def inherited klass
21
- klass&.instance_variable_set(:@cs__defining_class, true)
22
- cs__patched_helper_inherited(klass)
23
- ensure
24
- klass&.instance_variable_set(:@cs__defining_class, false)
25
- end
26
- end
27
- true
28
- end
18
+ ::ActionController::Railties::Helpers.class_eval do
19
+ alias_method :cs__patched_helper_inherited, :inherited
20
+ def inherited klass # rubocop:disable Lint/MissingSuper
21
+ klass&.instance_variable_set(:@cs__defining_class, true)
22
+ cs__patched_helper_inherited(klass) # This calls the original inherited, which should handle super as needed.
23
+ ensure
24
+ klass&.instance_variable_set(:@cs__defining_class, false)
25
+ end
26
+ end
27
+ true
28
+ end
29
29
  end
30
30
  end
31
31
  end
@@ -17,20 +17,20 @@ module Contrast
17
17
  class ActiveRecordAttributeMethodsRead
18
18
  def self.instrument
19
19
  @_instrument ||= begin
20
- ::ActiveRecord::AttributeMethods::Read::ClassMethods.class_eval do
21
- alias_method :cs__patched_define_method_attribute, :define_method_attribute
20
+ ::ActiveRecord::AttributeMethods::Read::ClassMethods.class_eval do
21
+ alias_method :cs__patched_define_method_attribute, :define_method_attribute
22
22
 
23
- def define_method_attribute *args, &block
24
- ret = cs__patched_define_method_attribute(*args, &block)
25
- method_name = args[0]
26
- Contrast::Agent::Assess::Policy::Patcher.patch_assess_method(self, method_name)
27
- ret
28
- end
23
+ def define_method_attribute *args, &block
24
+ ret = cs__patched_define_method_attribute(*args, &block)
25
+ method_name = args[0]
26
+ Contrast::Agent::Assess::Policy::Patcher.patch_assess_method(self, method_name)
27
+ ret
28
+ end
29
29
 
30
- protected :cs__patched_define_method_attribute, :define_method_attribute
31
- end
32
- true
33
- end
30
+ protected :cs__patched_define_method_attribute, :define_method_attribute
31
+ end
32
+ true
33
+ end
34
34
  end
35
35
  end
36
36
  end
@@ -1,7 +1,7 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- cs__scoped_require 'contrast/components/interface'
4
+ require 'contrast/components/interface'
5
5
 
6
6
  module Contrast
7
7
  module Framework
@@ -58,9 +58,9 @@ module Contrast
58
58
 
59
59
  def instrument
60
60
  @_instrument_named_track ||= begin
61
- cs__scoped_require 'cs__assess_active_record_named/cs__assess_active_record_named'
62
- true
63
- end
61
+ require 'cs__assess_active_record_named/cs__assess_active_record_named'
62
+ true
63
+ end
64
64
  rescue StandardError, LoadError => e
65
65
  logger.error('Error loading active record named track patch', e)
66
66
  false
@@ -12,19 +12,19 @@ module Contrast
12
12
  class ActiveRecordTimeZoneInherited
13
13
  def self.instrument
14
14
  @_instrument ||= begin
15
- ::ActiveRecord::AttributeMethods::TimeZoneConversion::ClassMethods.class_eval do
16
- private
15
+ ::ActiveRecord::AttributeMethods::TimeZoneConversion::ClassMethods.class_eval do
16
+ private
17
17
 
18
- alias_method :cs__patched_inherited, :inherited
19
- def inherited klass
20
- klass&.instance_variable_set(:@cs__defining_class, true)
21
- cs__patched_inherited(klass)
22
- ensure
23
- klass&.instance_variable_set(:@cs__defining_class, false)
24
- end
25
- end
26
- true
27
- end
18
+ alias_method :cs__patched_inherited, :inherited
19
+ def inherited klass # rubocop:disable Lint/MissingSuper
20
+ klass&.instance_variable_set(:@cs__defining_class, true)
21
+ cs__patched_inherited(klass) # This calls the original inherited, which should handle super as needed.
22
+ ensure
23
+ klass&.instance_variable_set(:@cs__defining_class, false)
24
+ end
25
+ end
26
+ true
27
+ end
28
28
  end
29
29
  end
30
30
  end
@@ -1,10 +1,10 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- cs__scoped_require 'contrast/api/dtm.pb'
5
- cs__scoped_require 'contrast/framework/base_support'
6
- cs__scoped_require 'contrast/framework/rails/patch/support'
7
- cs__scoped_require 'contrast/utils/string_utils'
4
+ require 'contrast/api/dtm.pb'
5
+ require 'contrast/framework/base_support'
6
+ require 'contrast/framework/rails/patch/support'
7
+ require 'contrast/utils/string_utils'
8
8
 
9
9
  module Contrast
10
10
  module Framework
@@ -42,10 +42,7 @@ module Contrast
42
42
  end
43
43
 
44
44
  def collect_routes
45
- routes = ::Rails.application.routes.routes.map do |route|
46
- Contrast::Api::Dtm::RouteCoverage.from_action_dispatch_journey(route)
47
- end
48
- routes
45
+ find_all_routes(::Rails.application, [])
49
46
  end
50
47
 
51
48
  def current_route request
@@ -59,13 +56,17 @@ module Contrast
59
56
  full_routes = ::Rails.application.routes.router.send(:find_routes, request.rack_request)
60
57
  return if full_routes.empty?
61
58
 
62
- full_route = full_routes[0] # [match_data, path_parameters, route]
63
- return unless full_route
64
-
65
- route = full_route[2] # route w/ highest precedence
66
- return unless route
59
+ full_route = full_routes[0]
67
60
 
68
- Contrast::Api::Dtm::RouteCoverage.from_action_dispatch_journey(route)
61
+ # the route is directly implemented within the application
62
+ if direct_route?(full_route)
63
+ route = full_route[2] # route w/ highest precedence
64
+ Contrast::Api::Dtm::RouteCoverage.from_action_dispatch_journey(route)
65
+ else
66
+ engine_route(full_route, request)
67
+ end
68
+ rescue StandardError => _e
69
+ nil
69
70
  end
70
71
 
71
72
  def retrieve_request env
@@ -79,6 +80,55 @@ module Contrast
79
80
 
80
81
  env[AC_INSTANCE].cs__class.included_modules.include?(::ActionController::Live)
81
82
  end
83
+
84
+ private
85
+
86
+ # route is not mounted within an engine
87
+ def direct_route? full_route
88
+ full_route[2]&.app&.cs__class == ActionDispatch::Routing::RouteSet::Dispatcher ||
89
+ (full_route[2].cs__class == ActionDispatch::Journey::Route && full_route[2]&.app&.cs__class == ActionDispatch::Routing::Mapper::Constraints)
90
+ end
91
+
92
+ def engine_route full_route, request
93
+ engine_route = full_route[2] # supposed route - but actually an Engine mount point
94
+ return unless engine_route
95
+
96
+ engine_mount_name = engine_route.name
97
+ return unless engine_mount_name
98
+
99
+ engine_path_segments = request.rack_request.path_info.split(engine_mount_name)
100
+ return if engine_path_segments.empty?
101
+
102
+ path_within_engine = engine_path_segments[-1]
103
+ return unless path_within_engine
104
+
105
+ engine_router = engine_route.app&.app&.routes&.router
106
+ return unless engine_router
107
+
108
+ # Get all routes regardless of http method
109
+ matching_routes = engine_router.send(:filter_routes, path_within_engine)
110
+ return unless matching_routes
111
+
112
+ # filter for current http method
113
+ reportable_routes = engine_router.send(:match_routes, matching_routes, request.rack_request)
114
+ return if reportable_routes.empty?
115
+
116
+ Contrast::Api::Dtm::RouteCoverage.from_action_dispatch_journey(reportable_routes[0])
117
+ end
118
+
119
+ # Rails engine routes need to be detected by inspecting Engine class route set
120
+ def find_all_routes app, route_list
121
+ return route_list unless app.cs__respond_to?(:routes) && app.routes.cs__respond_to?(:routes)
122
+
123
+ app.routes.routes.each do |route|
124
+ if route.cs__respond_to?(:app) && route.app.cs__class == ActionDispatch::Routing::RouteSet::Dispatcher
125
+ route_list << Contrast::Api::Dtm::RouteCoverage.from_action_dispatch_journey(route)
126
+ elsif route.app.app.cs__respond_to?(:routes)
127
+ route_list += find_all_routes(route.app.app, [])
128
+ end
129
+ end
130
+ route_list
131
+ end
82
132
  end
83
133
  end
84
134
  end
@@ -1,7 +1,7 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- cs__scoped_require 'contrast/framework/sinatra/support'
4
+ require 'contrast/framework/sinatra/support'
5
5
 
6
6
  module Contrast
7
7
  module Framework
@@ -34,17 +34,17 @@ module Contrast
34
34
 
35
35
  def instrument
36
36
  @_instrument ||= begin
37
- ::Sinatra::Base.class_eval do
38
- alias_method :cs__patched_sinatra_base_call!, :call!
39
- # publicly available method for Sinatra::Base things -- unfortunately,
40
- # getting the routes appear to require a lookup every time
41
- def call! *args
42
- Contrast::Framework::Sinatra::Patch::Base.map_route(cs__class, settings, *args)
43
- cs__patched_sinatra_base_call!(*args)
44
- end
45
- end
46
- true
47
- end
37
+ ::Sinatra::Base.class_eval do
38
+ alias_method :cs__patched_sinatra_base_call!, :call!
39
+ # publicly available method for Sinatra::Base things -- unfortunately,
40
+ # getting the routes appear to require a lookup every time
41
+ def call! *args
42
+ Contrast::Framework::Sinatra::Patch::Base.map_route(cs__class, settings, *args)
43
+ cs__patched_sinatra_base_call!(*args)
44
+ end
45
+ end
46
+ true
47
+ end
48
48
  end
49
49
 
50
50
  private
@@ -1,7 +1,7 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- cs__scoped_require 'contrast/agent/patching/policy/after_load_patch'
4
+ require 'contrast/agent/patching/policy/after_load_patch'
5
5
 
6
6
  module Contrast
7
7
  module Framework
@@ -1,8 +1,8 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- cs__scoped_require 'contrast/framework/base_support'
5
- cs__scoped_require 'contrast/framework/sinatra/patch/support'
4
+ require 'contrast/framework/base_support'
5
+ require 'contrast/framework/sinatra/patch/support'
6
6
 
7
7
  module Contrast
8
8
  module Framework
@@ -70,10 +70,10 @@ module Contrast
70
70
  return nil unless defined?(::Sinatra) && defined?(::Sinatra::Base)
71
71
 
72
72
  @_app_class ||= begin
73
- sinatra_layers = ObjectSpace.each_object(::Sinatra::Base).to_a
74
- result_layer = sinatra_layers.find { |layer| layer.app.nil? }
75
- result_layer
76
- end
73
+ sinatra_layers = ObjectSpace.each_object(::Sinatra::Base).to_a
74
+ result_layer = sinatra_layers.find { |layer| layer.app.nil? }
75
+ result_layer
76
+ end
77
77
  end
78
78
 
79
79
  # Iterate over every class that extends Sinatra::Base, pull out its routes
@@ -1,7 +1,7 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- cs__scoped_require 'contrast/components/interface'
4
+ require 'contrast/components/interface'
5
5
  # This module is used to find funchook library and determine availability
6
6
  module Funchook
7
7
  include Contrast::Components::Interface
@@ -1,11 +1,11 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- cs__scoped_require 'contrast/components/interface'
4
+ require 'contrast/components/interface'
5
5
 
6
6
  module Contrast
7
7
  module Logger
8
- # # Our decorator for the Ougai logger allowing for the logging of the
8
+ # Our decorator for the Ougai logger allowing for the logging of the
9
9
  # application environment, used to provide context during troubleshooting.
10
10
  module Application
11
11
  include Contrast::Components::Interface
@@ -33,9 +33,17 @@ module Contrast
33
33
  def application_configuration
34
34
  return unless info?
35
35
 
36
- loggable = CONFIG.raw.send(:load_config)
37
- loggable.delete('api')
38
- info('Current configuration', configuration: JSON.pretty_generate(loggable))
36
+ loggable = CONFIG.raw.loggable
37
+ info('Current configuration', configuration: loggable)
38
+ env_keys = ENV.keys.select { |env_key| env_key&.to_s&.start_with?(Contrast::Components::Config::CONTRAST_ENV_MARKER) }
39
+ env_items = env_keys.map { |env_key| Contrast::Utils::EnvConfigurationItem.new(env_key, nil) }
40
+ env_translations = env_items.each_with_object({}) do |conversion, hash|
41
+ hash[conversion.key] = conversion.dot_path_array.join('.')
42
+ end
43
+ info('Set by environment', overrides: env_translations)
44
+ rescue StandardError => e
45
+ puts e
46
+ sleep(5)
39
47
  end
40
48
 
41
49
  def application_libraries
@@ -1,8 +1,9 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- cs__scoped_require 'ougai'
5
- cs__scoped_require 'contrast/utils/thread_tracker'
4
+ require 'ougai'
5
+ require 'contrast/utils/object_share'
6
+ require 'contrast/utils/thread_tracker'
6
7
 
7
8
  module Contrast
8
9
  module Logger
@@ -31,21 +32,33 @@ module Contrast
31
32
 
32
33
  private
33
34
 
34
- # The extra values to add to the Bunyan hash. Because this is unique per
35
- # thread, we'll create it once and then store it, rather than create a
36
- # new hash on every log call.
35
+ # The extra values to add to the Bunyan hash.
37
36
  # @return [Hash{Symbol => Object}] the extra data to log
38
- # tid: current thread id
39
37
  def format_hash
40
- hash = LOG_TRACKER.get(:thread_id_hash)
38
+ thread_hash.merge(request_hash)
39
+ end
40
+
41
+ # Because this is unique per thread, we'll create it once and then store
42
+ # it, rather than create a new hash on every log call during the request.
43
+ def thread_hash
44
+ hash = LOG_TRACKER.get(:logging_hash)
41
45
  unless hash
42
46
  hash = {
43
- tid: Thread.current.object_id
47
+ thread_id: Thread.current.object_id
44
48
  }
45
- LOG_TRACKER.set(:thread_id_hash, hash)
49
+ LOG_TRACKER.set(:logging_hash, hash)
46
50
  end
47
51
  hash
48
52
  end
53
+
54
+ NO_REQUEST_HASH = { request_id: -1 }.cs__freeze
55
+ def request_hash
56
+ @request_tracker_defined ||= defined?(Contrast::Agent) &&
57
+ defined?(Contrast::Agent::REQUEST_TRACKER)
58
+ return NO_REQUEST_HASH unless @request_tracker_defined
59
+
60
+ Contrast::Agent::REQUEST_TRACKER&.current&.logging_hash || NO_REQUEST_HASH
61
+ end
49
62
  end
50
63
  end
51
64
  end