contrast-agent 3.10.2 → 3.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (266) hide show
  1. checksums.yaml +4 -4
  2. data/.simplecov +5 -2
  3. data/ext/build_funchook.rb +13 -17
  4. data/ext/cs__assess_active_record_named/cs__active_record_named.c +5 -12
  5. data/ext/cs__assess_active_record_named/extconf.rb +3 -0
  6. data/ext/cs__assess_array/cs__assess_array.c +3 -5
  7. data/ext/cs__assess_array/extconf.rb +3 -0
  8. data/ext/cs__assess_basic_object/cs__assess_basic_object.c +10 -4
  9. data/ext/cs__assess_basic_object/extconf.rb +3 -0
  10. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.c +4 -3
  11. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.h +3 -3
  12. data/ext/cs__assess_fiber_track/extconf.rb +3 -0
  13. data/ext/cs__assess_hash/cs__assess_hash.c +40 -17
  14. data/ext/cs__assess_hash/cs__assess_hash.h +4 -6
  15. data/ext/cs__assess_hash/extconf.rb +3 -0
  16. data/ext/cs__assess_kernel/cs__assess_kernel.c +10 -8
  17. data/ext/cs__assess_kernel/cs__assess_kernel.h +1 -0
  18. data/ext/cs__assess_kernel/extconf.rb +3 -0
  19. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +3 -6
  20. data/ext/cs__assess_marshal_module/extconf.rb +3 -0
  21. data/ext/cs__assess_module/cs__assess_module.c +13 -9
  22. data/ext/cs__assess_module/extconf.rb +3 -0
  23. data/ext/cs__assess_regexp/cs__assess_regexp.c +13 -9
  24. data/ext/cs__assess_regexp/cs__assess_regexp.h +1 -0
  25. data/ext/cs__assess_regexp/extconf.rb +3 -0
  26. data/ext/cs__assess_string/cs__assess_string.c +5 -8
  27. data/ext/cs__assess_string/cs__assess_string.h +2 -1
  28. data/ext/cs__assess_string/extconf.rb +3 -0
  29. data/ext/cs__assess_string_interpolation26/cs__assess_string_interpolation26.c +2 -2
  30. data/ext/cs__assess_string_interpolation26/cs__assess_string_interpolation26.h +3 -3
  31. data/ext/cs__assess_string_interpolation26/extconf.rb +3 -0
  32. data/ext/cs__assess_yield_track/cs__assess_yield_track.h +1 -1
  33. data/ext/cs__assess_yield_track/extconf.rb +3 -0
  34. data/ext/cs__common/cs__common.c +79 -0
  35. data/ext/cs__common/cs__common.h +34 -0
  36. data/ext/cs__common/extconf.rb +9 -8
  37. data/ext/cs__contrast_patch/cs__contrast_patch.h +1 -6
  38. data/ext/cs__contrast_patch/extconf.rb +3 -0
  39. data/ext/cs__protect_kernel/cs__protect_kernel.c +20 -11
  40. data/ext/cs__protect_kernel/extconf.rb +3 -0
  41. data/ext/extconf_common.rb +10 -8
  42. data/funchook/autom4te.cache/output.0 +1 -13
  43. data/funchook/autom4te.cache/requests +44 -45
  44. data/funchook/autom4te.cache/traces.0 +0 -3
  45. data/funchook/config.log +378 -217
  46. data/funchook/config.status +23 -24
  47. data/funchook/configure +1 -13
  48. data/funchook/src/Makefile +7 -7
  49. data/funchook/src/config.h +2 -2
  50. data/funchook/src/decoder.o +0 -0
  51. data/funchook/src/distorm.o +0 -0
  52. data/funchook/src/funchook.o +0 -0
  53. data/funchook/src/funchook_io.o +0 -0
  54. data/funchook/src/funchook_syscall.o +0 -0
  55. data/funchook/src/funchook_unix.o +0 -0
  56. data/funchook/src/funchook_x86.o +0 -0
  57. data/funchook/src/instructions.o +0 -0
  58. data/funchook/src/insts.o +0 -0
  59. data/funchook/src/libfunchook.dylib +0 -0
  60. data/funchook/src/mnemonics.o +0 -0
  61. data/funchook/src/operands.o +0 -0
  62. data/funchook/src/os_func.o +0 -0
  63. data/funchook/src/os_func_unix.o +0 -0
  64. data/funchook/src/prefix.o +0 -0
  65. data/funchook/src/printf_base.o +0 -0
  66. data/funchook/src/textdefs.o +0 -0
  67. data/funchook/src/wstring.o +0 -0
  68. data/funchook/test/Makefile +2 -2
  69. data/funchook/test/funchook_test +0 -0
  70. data/funchook/test/libfunchook_test.so +0 -0
  71. data/funchook/test/libfunchook_test.so.dSYM/Contents/Info.plist +20 -0
  72. data/funchook/test/libfunchook_test.so.dSYM/Contents/Resources/DWARF/libfunchook_test.so +0 -0
  73. data/funchook/test/test_main.o +0 -0
  74. data/funchook/test/x86_64_test.o +0 -0
  75. data/lib/contrast.rb +1 -0
  76. data/lib/contrast/agent.rb +21 -15
  77. data/lib/contrast/agent/assess.rb +1 -2
  78. data/lib/contrast/agent/assess/adjusted_span.rb +3 -1
  79. data/lib/contrast/agent/assess/contrast_event.rb +16 -62
  80. data/lib/contrast/agent/assess/events/event_factory.rb +25 -0
  81. data/lib/contrast/agent/assess/events/source_event.rb +83 -0
  82. data/lib/contrast/agent/assess/insulator.rb +0 -4
  83. data/lib/contrast/agent/assess/policy/patcher.rb +5 -2
  84. data/lib/contrast/agent/assess/policy/policy_node.rb +0 -7
  85. data/lib/contrast/agent/assess/policy/policy_scanner.rb +1 -1
  86. data/lib/contrast/agent/assess/policy/preshift.rb +1 -1
  87. data/lib/contrast/agent/assess/policy/propagation_method.rb +65 -33
  88. data/lib/contrast/agent/assess/policy/propagation_node.rb +2 -1
  89. data/lib/contrast/agent/assess/policy/propagator.rb +1 -0
  90. data/lib/contrast/agent/assess/policy/propagator/match_data.rb +80 -0
  91. data/lib/contrast/agent/assess/policy/propagator/select.rb +35 -22
  92. data/lib/contrast/agent/assess/policy/propagator/split.rb +26 -6
  93. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +2 -0
  94. data/lib/contrast/agent/assess/policy/rewriter_patch.rb +37 -26
  95. data/lib/contrast/agent/assess/policy/source_method.rb +20 -20
  96. data/lib/contrast/agent/assess/policy/source_node.rb +0 -15
  97. data/lib/contrast/agent/assess/policy/trigger_method.rb +29 -40
  98. data/lib/contrast/agent/assess/policy/trigger_node.rb +3 -6
  99. data/lib/contrast/agent/assess/policy/trigger_validation/ssrf_validator.rb +2 -31
  100. data/lib/contrast/agent/assess/properties.rb +5 -3
  101. data/lib/contrast/agent/assess/rule/base.rb +1 -5
  102. data/lib/contrast/agent/assess/rule/csrf/csrf_applicator.rb +2 -22
  103. data/lib/contrast/agent/assess/rule/csrf/csrf_watcher.rb +5 -1
  104. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +2 -2
  105. data/lib/contrast/agent/assess/rule/redos.rb +4 -4
  106. data/lib/contrast/agent/assess/tag.rb +24 -14
  107. data/lib/contrast/agent/at_exit_hook.rb +16 -13
  108. data/lib/contrast/agent/class_reopener.rb +14 -4
  109. data/lib/contrast/agent/deadzone/policy/policy.rb +2 -2
  110. data/lib/contrast/agent/disable_reaction.rb +3 -4
  111. data/lib/contrast/agent/exclusion_matcher.rb +8 -48
  112. data/lib/contrast/agent/feature_state.rb +45 -75
  113. data/lib/contrast/agent/logger.rb +173 -0
  114. data/lib/contrast/agent/middleware.rb +87 -250
  115. data/lib/contrast/agent/module_data.rb +2 -1
  116. data/lib/contrast/agent/patching/policy/after_load_patch.rb +2 -1
  117. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +21 -4
  118. data/lib/contrast/agent/patching/policy/method_policy.rb +3 -3
  119. data/lib/contrast/agent/patching/policy/module_policy.rb +0 -25
  120. data/lib/contrast/agent/patching/policy/patch.rb +96 -23
  121. data/lib/contrast/agent/patching/policy/patcher.rb +19 -19
  122. data/lib/contrast/agent/patching/policy/policy.rb +7 -7
  123. data/lib/contrast/agent/patching/policy/policy_node.rb +2 -10
  124. data/lib/contrast/agent/patching/policy/trigger_node.rb +1 -4
  125. data/lib/contrast/agent/protect/rule/base.rb +14 -33
  126. data/lib/contrast/agent/protect/rule/base_service.rb +3 -1
  127. data/lib/contrast/agent/protect/rule/cmd_injection.rb +10 -13
  128. data/lib/contrast/agent/protect/rule/csrf.rb +2 -1
  129. data/lib/contrast/agent/protect/rule/csrf/csrf_evaluator.rb +11 -14
  130. data/lib/contrast/agent/protect/rule/default_scanner.rb +0 -13
  131. data/lib/contrast/agent/protect/rule/deserialization.rb +2 -0
  132. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +2 -2
  133. data/lib/contrast/agent/protect/rule/no_sqli.rb +4 -4
  134. data/lib/contrast/agent/protect/rule/path_traversal.rb +5 -4
  135. data/lib/contrast/agent/protect/rule/sqli.rb +1 -0
  136. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +2 -0
  137. data/lib/contrast/agent/protect/rule/xss.rb +2 -0
  138. data/lib/contrast/agent/protect/rule/xxe.rb +10 -4
  139. data/lib/contrast/agent/railtie.rb +2 -8
  140. data/lib/contrast/agent/reaction_processor.rb +5 -5
  141. data/lib/contrast/agent/request.rb +9 -12
  142. data/lib/contrast/agent/request_context.rb +12 -14
  143. data/lib/contrast/agent/request_handler.rb +35 -0
  144. data/lib/contrast/agent/response.rb +33 -30
  145. data/lib/contrast/agent/rewriter.rb +22 -10
  146. data/lib/contrast/agent/rule_set.rb +49 -0
  147. data/lib/contrast/agent/scope.rb +0 -6
  148. data/lib/contrast/agent/service_heartbeat.rb +1 -2
  149. data/lib/contrast/agent/settings_state.rb +10 -74
  150. data/lib/contrast/agent/socket_client.rb +17 -11
  151. data/lib/contrast/agent/static_analysis.rb +42 -0
  152. data/lib/contrast/agent/thread.rb +1 -1
  153. data/lib/contrast/agent/tracepoint_hook.rb +1 -5
  154. data/lib/contrast/agent/version.rb +1 -1
  155. data/lib/contrast/api.rb +1 -1
  156. data/lib/contrast/api/decorators.rb +14 -0
  157. data/lib/contrast/api/decorators/application_settings.rb +37 -0
  158. data/lib/contrast/api/decorators/application_update.rb +66 -0
  159. data/lib/contrast/api/decorators/exclusion.rb +20 -0
  160. data/lib/contrast/api/decorators/input_analysis.rb +17 -0
  161. data/lib/contrast/api/decorators/server_features.rb +24 -0
  162. data/lib/contrast/api/speedracer.rb +27 -23
  163. data/lib/contrast/api/tcp_socket.rb +0 -2
  164. data/lib/contrast/components/agent.rb +27 -22
  165. data/lib/contrast/components/app_context.rb +18 -43
  166. data/lib/contrast/components/config.rb +7 -5
  167. data/lib/contrast/components/contrast_service.rb +0 -4
  168. data/lib/contrast/components/heap_dump.rb +12 -8
  169. data/lib/contrast/components/interface.rb +11 -10
  170. data/lib/contrast/components/logger.rb +3 -68
  171. data/lib/contrast/components/sampling.rb +22 -11
  172. data/lib/contrast/components/settings.rb +18 -5
  173. data/lib/contrast/config/base_configuration.rb +1 -0
  174. data/lib/contrast/config/default_value.rb +1 -0
  175. data/lib/contrast/config/protect_rule_configuration.rb +0 -14
  176. data/lib/contrast/extensions/framework/rails/action_controller_inheritance.rb +39 -0
  177. data/lib/contrast/extensions/framework/rails/active_record_named.rb +13 -8
  178. data/lib/contrast/extensions/ruby_core/assess.rb +1 -2
  179. data/lib/contrast/extensions/ruby_core/assess/assess_extension.rb +27 -22
  180. data/lib/contrast/extensions/ruby_core/assess/erb.rb +0 -8
  181. data/lib/contrast/extensions/ruby_core/assess/exec_trigger.rb +6 -8
  182. data/lib/contrast/extensions/ruby_core/assess/fiber.rb +88 -100
  183. data/lib/contrast/extensions/ruby_core/assess/hash.rb +32 -15
  184. data/lib/contrast/extensions/ruby_core/assess/kernel.rb +28 -27
  185. data/lib/contrast/extensions/ruby_core/assess/regexp.rb +74 -196
  186. data/lib/contrast/extensions/ruby_core/assess/string.rb +15 -7
  187. data/lib/contrast/extensions/ruby_core/assess/tilt_template_trigger.rb +29 -24
  188. data/lib/contrast/extensions/ruby_core/assess/xpath_library_trigger.rb +2 -2
  189. data/lib/contrast/extensions/ruby_core/eval_trigger.rb +0 -1
  190. data/lib/contrast/extensions/ruby_core/inventory/datastores.rb +2 -2
  191. data/lib/contrast/extensions/ruby_core/protect/applies_command_injection_rule.rb +9 -20
  192. data/lib/contrast/extensions/ruby_core/protect/applies_deserialization_rule.rb +9 -19
  193. data/lib/contrast/extensions/ruby_core/protect/applies_no_sqli_rule.rb +10 -27
  194. data/lib/contrast/extensions/ruby_core/protect/applies_path_traversal_rule.rb +13 -21
  195. data/lib/contrast/extensions/ruby_core/protect/applies_sqli_rule.rb +11 -23
  196. data/lib/contrast/extensions/ruby_core/protect/applies_xxe_rule.rb +62 -78
  197. data/lib/contrast/extensions/ruby_core/protect/rule_applicator.rb +50 -0
  198. data/lib/contrast/framework/base_support.rb +10 -0
  199. data/lib/contrast/framework/manager.rb +28 -2
  200. data/lib/contrast/framework/platform_version.rb +1 -0
  201. data/lib/contrast/framework/rails_support.rb +16 -0
  202. data/lib/contrast/framework/sinatra_support.rb +12 -2
  203. data/lib/contrast/framework/view_technologies_descriptor.rb +1 -0
  204. data/lib/contrast/tasks/service.rb +2 -8
  205. data/lib/contrast/utils/assess/sampling_util.rb +4 -9
  206. data/lib/contrast/utils/assess/tracking_util.rb +7 -1
  207. data/lib/contrast/utils/boolean_util.rb +2 -2
  208. data/lib/contrast/utils/cache.rb +0 -11
  209. data/lib/contrast/utils/class_util.rb +20 -1
  210. data/lib/contrast/utils/gemfile_reader.rb +5 -3
  211. data/lib/contrast/utils/hash_digest.rb +0 -4
  212. data/lib/contrast/utils/heap_dump_util.rb +12 -11
  213. data/lib/contrast/utils/invalid_configuration_util.rb +1 -1
  214. data/lib/contrast/utils/inventory_util.rb +2 -2
  215. data/lib/contrast/utils/io_util.rb +1 -11
  216. data/lib/contrast/utils/job_servers_running.rb +2 -2
  217. data/lib/contrast/utils/object_share.rb +1 -27
  218. data/lib/contrast/utils/os.rb +1 -25
  219. data/lib/contrast/utils/rack_assess_session_cookie.rb +3 -3
  220. data/lib/contrast/utils/rails_assess_configuration.rb +3 -3
  221. data/lib/contrast/utils/service_response_util.rb +27 -53
  222. data/lib/contrast/utils/service_sender_util.rb +9 -5
  223. data/lib/contrast/utils/sinatra_helper.rb +0 -6
  224. data/lib/contrast/utils/stack_trace_utils.rb +86 -182
  225. data/lib/contrast/utils/string_utils.rb +18 -2
  226. data/lib/contrast/utils/tag_util.rb +11 -1
  227. data/lib/contrast/utils/thread_tracker.rb +2 -2
  228. data/lib/contrast/utils/timer.rb +0 -40
  229. data/resources/assess/policy.json +33 -21
  230. data/resources/protect/policy.json +9 -9
  231. data/ruby-agent.gemspec +6 -3
  232. metadata +76 -51
  233. data/ext/cs__assess_regexp_track/cs__assess_regexp_track.c +0 -63
  234. data/ext/cs__assess_regexp_track/cs__assess_regexp_track.h +0 -29
  235. data/ext/cs__assess_regexp_track/extconf.rb +0 -2
  236. data/funchook/src/libfunchook.so +0 -0
  237. data/lib/contrast/agent/assess/frozen_properties.rb +0 -41
  238. data/lib/contrast/agent/logger_manager.rb +0 -116
  239. data/lib/contrast/delegators.rb +0 -9
  240. data/lib/contrast/delegators/application_update.rb +0 -32
  241. data/lib/contrast/utils/comment_range.rb +0 -19
  242. data/lib/contrast/utils/environment_util.rb +0 -81
  243. data/lib/contrast/utils/performs_logging.rb +0 -152
  244. data/resources/factory-bot-spec/spec_helper.rb +0 -30
  245. data/resources/rubocops/kernel/catch_cop.rb +0 -37
  246. data/resources/rubocops/kernel/require_cop.rb +0 -37
  247. data/resources/rubocops/kernel/require_relative_cop.rb +0 -33
  248. data/resources/rubocops/module/autoload_cop.rb +0 -37
  249. data/resources/rubocops/module/const_defined_cop.rb +0 -37
  250. data/resources/rubocops/module/const_get_cop.rb +0 -37
  251. data/resources/rubocops/module/const_set_cop.rb +0 -37
  252. data/resources/rubocops/module/constants_cop.rb +0 -37
  253. data/resources/rubocops/module/name_cop.rb +0 -37
  254. data/resources/rubocops/object/class_cop.rb +0 -37
  255. data/resources/rubocops/object/freeze_cop.rb +0 -37
  256. data/resources/rubocops/object/frozen_cop.rb +0 -37
  257. data/resources/rubocops/object/is_a_cop.rb +0 -37
  258. data/resources/rubocops/object/method_cop.rb +0 -37
  259. data/resources/rubocops/object/respond_to_cop.rb +0 -37
  260. data/resources/rubocops/object/singleton_class_cop.rb +0 -37
  261. data/resources/rubocops/regexp/spelling_cop.rb +0 -44
  262. data/resources/rubocops/thread/new_cop.rb +0 -39
  263. data/resources/ruby-spec/ancestors_spec.rb +0 -70
  264. data/resources/ruby-spec/modulo_spec.rb +0 -831
  265. data/resources/ruby-spec/parameters_spec.rb +0 -261
  266. data/resources/ruby-spec/ruby_spec_spec_helper.rb +0 -35
@@ -6,7 +6,8 @@ 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_accessor :mod, :name
9
+ attr_reader :mod, :name
10
+
10
11
  def initialize mod, name = nil
11
12
  @mod = mod
12
13
  @name = name || mod.cs__name
@@ -10,6 +10,7 @@ module Contrast
10
10
  # Used to handle tracking patches that need to apply special instrumentation when a module is loaded
11
11
  class AfterLoadPatch
12
12
  attr_reader :applied, :module_name, :instrumentation_file_path, :method_to_instrument
13
+
13
14
  def initialize module_name, instrumentation_file_path, method_to_instrument: nil
14
15
  @applied = false
15
16
  @module_name = module_name
@@ -18,7 +19,7 @@ module Contrast
18
19
  end
19
20
 
20
21
  def applied?
21
- @applied
22
+ applied
22
23
  end
23
24
 
24
25
  # Modules can be re-opened, so the first load may not
@@ -12,7 +12,7 @@ module Contrast
12
12
  # after they've been loaded
13
13
  module AfterLoadPatcher
14
14
  include Contrast::Components::Interface
15
- access_component :logging, :agent
15
+ access_component :agent, :logging
16
16
 
17
17
  AFTER_LOAD_PATCHES = Set.new([
18
18
  Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
@@ -52,18 +52,36 @@ module Contrast
52
52
 
53
53
  # After initialization run a catchup check to instrument any already loaded modules we care about
54
54
  def catchup_after_load_patches
55
+ apply_direct_patches!
55
56
  applied = []
56
57
  AFTER_LOAD_PATCHES.each do |after_load_patch|
57
58
  next unless after_load_patch.target_defined?
58
59
  next if AGENT.skip_instrumentation?(after_load_patch.module_name)
59
60
 
60
- logger.debug("Detected loading of #{ after_load_patch.module_name } - applying instrumentation")
61
+ logger.trace(
62
+ 'Catching up on already loaded afterload patch - applying instrumentation',
63
+ module: after_load_patch.module_name)
61
64
  after_load_patch.instrument!
62
65
  applied << after_load_patch
63
66
  end
64
67
  AFTER_LOAD_PATCHES.subtract(applied)
65
68
  end
66
69
 
70
+ # These patches need to be applied directly, not from our policy, so
71
+ # do so and do so only once. This should be the new standard so that
72
+ # there are no require time side effects of loading our core
73
+ # extensions.
74
+ def apply_direct_patches!
75
+ @_apply_direct_patches ||= begin
76
+ Contrast::CoreExtensions::Assess::FiberPropagator.instrument_fiber_track
77
+ Contrast::CoreExtensions::Assess::HashPropagator.instrument_hash_track
78
+ Contrast::CoreExtensions::Assess::RegexpPropagator.instrument_regexp_track
79
+ Contrast::CoreExtensions::Assess::StringPropagator.instrument_string
80
+ Contrast::CoreExtensions::Assess::StringPropagator.instrument_string_interpolation
81
+ true
82
+ end
83
+ end
84
+
67
85
  # Use for any checks after we've initialized
68
86
  def load_patches_for_module module_name
69
87
  return if AFTER_LOAD_PATCHES.empty?
@@ -71,8 +89,7 @@ module Contrast
71
89
  patch = AFTER_LOAD_PATCHES.find { |after_load_patch| after_load_patch.applies?(module_name) }
72
90
  return unless patch
73
91
 
74
- logger.debug("Detected loading of #{ module_name } - applying instrumentation")
75
-
92
+ logger.trace('Detected loading of afterload patch - applying instrumentation', module: module_name)
76
93
  patch.instrument!
77
94
  AFTER_LOAD_PATCHES.subtract([patch]) if patch.applied?
78
95
  end
@@ -7,9 +7,9 @@ module Contrast
7
7
  module Policy
8
8
  # This class is used to map each method to the trigger node that applies to it
9
9
  class MethodPolicy
10
- attr_accessor :source_node, :propagation_node, :trigger_node,
11
- :inventory_node, :protect_node, :deadzone_node,
12
- :method_name, :method_visibility, :instance_method
10
+ attr_reader :deadzone_node, :inventory_node, :propagation_node, :protect_node, :trigger_node
11
+ attr_accessor :source_node, :method_name, :method_visibility, :instance_method
12
+
13
13
  def initialize(source_node: nil, propagation_node: nil,
14
14
  trigger_node: nil, inventory_node: nil,
15
15
  protect_node: nil, deadzone_node: nil,
@@ -73,31 +73,6 @@ module Contrast
73
73
  end
74
74
  end
75
75
 
76
- def collect_class_nodes nodes, clazz_name
77
- return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless nodes
78
-
79
- nodes.select { |policy_node| clazz_name == policy_node.class_name }
80
- end
81
-
82
- def collect_method_nodes nodes, method, instance_method
83
- return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless nodes
84
-
85
- nodes.select do |node|
86
- node.instance_method? == instance_method && node.method_name == method
87
- end
88
- end
89
-
90
- def create_method_policy_subset method, instance_method
91
- method_policy = ModulePolicy.new
92
- method_policy.source_nodes = collect_method_nodes(source_nodes, method, instance_method)
93
- method_policy.propagator_nodes = collect_method_nodes(propagator_nodes, method, instance_method)
94
- method_policy.trigger_nodes = collect_method_nodes(trigger_nodes, method, instance_method)
95
- method_policy.inventory_nodes = collect_method_nodes(inventory_nodes, method, instance_method)
96
- method_policy.protect_nodes = collect_method_nodes(protect_nodes, method, instance_method)
97
- method_policy.deadzone_nodes = collect_method_nodes(deadzone_nodes, method, instance_method)
98
- method_policy
99
- end
100
-
101
76
  private
102
77
 
103
78
  def sort_method_names nodes, instance_methods, singleton_methods
@@ -2,28 +2,25 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  cs__scoped_require 'monitor'
5
- # base
6
- cs__scoped_require 'contrast/extensions/ruby_core/module'
5
+ cs__scoped_require 'contrast/components/interface'
6
+
7
+ cs__scoped_require 'contrast/agent'
8
+ cs__scoped_require 'contrast/agent/logger'
7
9
  cs__scoped_require 'contrast/agent/patching/policy/method_policy'
8
10
  cs__scoped_require 'contrast/agent/patching/policy/patch_status'
9
11
  cs__scoped_require 'contrast/agent/patching/policy/trigger_node'
10
- cs__scoped_require 'contrast/components/interface'
11
12
 
12
13
  # assess
13
14
  cs__scoped_require 'contrast/agent/assess/policy/policy'
14
15
  cs__scoped_require 'contrast/agent/assess/policy/propagation_method'
15
16
  cs__scoped_require 'contrast/agent/assess/policy/source_method'
16
17
  cs__scoped_require 'contrast/agent/assess/policy/trigger_method'
17
- cs__scoped_require 'contrast/extensions/ruby_core/assess'
18
-
19
- # inventory
20
- cs__scoped_require 'contrast/agent/inventory/policy/policy'
21
- cs__scoped_require 'contrast/extensions/ruby_core/inventory'
22
18
 
23
19
  # protect
24
20
  cs__scoped_require 'contrast/agent/protect/policy/policy'
25
- cs__scoped_require 'contrast/extensions/ruby_core/protect'
26
- cs__scoped_require 'contrast/extensions/ruby_core/protect/kernel'
21
+
22
+ # inventory
23
+ cs__scoped_require 'contrast/agent/inventory/policy/policy'
27
24
 
28
25
  module Contrast
29
26
  module Agent
@@ -40,7 +37,7 @@ module Contrast
40
37
  include Contrast::Agent::Assess::Policy::TriggerMethod
41
38
 
42
39
  include Contrast::Components::Interface
43
- access_component :logging, :analysis, :agent, :scope
40
+ access_component :agent, :analysis, :logging, :scope
44
41
 
45
42
  POLICIES = [
46
43
  Contrast::Agent::Assess::Policy::Policy,
@@ -86,7 +83,7 @@ module Contrast
86
83
  rescue StandardError => e
87
84
  # Anything else was our bad and we gotta catch that to allow for
88
85
  # normal application flow
89
- logger.error(e, 'Unable to apply pre patch to method.')
86
+ logger.error('Unable to apply pre patch to method.', e)
90
87
  rescue Exception => e # rubocop:disable Lint/RescueException
91
88
  # This is something like NoMemoryError that we can't
92
89
  # hope to handle. Nonetheless, shouldn't leak scope.
@@ -115,7 +112,7 @@ module Contrast
115
112
  def apply_post_patch method_policy, preshift, object, ret, args, block
116
113
  apply_assess(method_policy, preshift, object, ret, args, block)
117
114
  rescue StandardError => e
118
- logger.error(e, 'Unable to apply post patch to method.')
115
+ logger.error('Unable to apply post patch to method.', e)
119
116
  end
120
117
 
121
118
  # Apply the Protect patch which applies to the given method.
@@ -184,14 +181,12 @@ module Contrast
184
181
 
185
182
  trigger_node = method_policy.trigger_node
186
183
  Contrast::Agent::Assess::Policy::TriggerMethod.apply_trigger_rule(trigger_node, object, ret, args) if trigger_node
187
-
188
184
  if method_policy.source_node
189
185
  # If we were given a frozen return, and it was the target of a
190
186
  # source, and we have frozen sources enabled, we'll need to
191
187
  # replace the return. Note, this is not the default case.
192
188
  source_ret = Contrast::Agent::Assess::Policy::SourceMethod.source_patchers(method_policy, object, ret, args)
193
189
  end
194
-
195
190
  if method_policy.propagation_node
196
191
  propagated_ret = Contrast::Agent::Assess::Policy::PropagationMethod.apply_propagation(
197
192
  method_policy,
@@ -201,13 +196,12 @@ module Contrast
201
196
  args,
202
197
  block)
203
198
  end
204
-
205
199
  handle_return(propagated_ret, source_ret, ret)
206
200
  rescue StandardError => e
207
- logger.error(e, 'Unable to assess method call.')
201
+ logger.error('Unable to assess method call.', e)
208
202
  handle_return(propagated_ret, source_ret, ret)
209
203
  rescue Exception => e # rubocop:disable Lint/RescueException
210
- logger.error(e, 'Unable to assess method call.')
204
+ logger.error('Unable to assess method call.', e)
211
205
  handle_return(propagated_ret, source_ret, ret)
212
206
  raise e
213
207
  end
@@ -237,7 +231,6 @@ module Contrast
237
231
  # resembling CUSTOM - they all have a common interface but their
238
232
  # own logic based on what's in the method(s) they've been patched
239
233
  # into.
240
- applicator = trigger_node.applicator
241
234
  # Each patch also knows the method of its applicator. Some
242
235
  # things, like AppliesXxeRule, have different methods depending
243
236
  # on the library patched. This lets us handle the boilerplate of
@@ -247,7 +240,7 @@ module Contrast
247
240
  # By calling send like this, we can reuse all the patching.
248
241
  # We `send` to the given method of the given class
249
242
  # (applicator) since they all accept the same inputs
250
- applicator.send(applicator_method, method, exception, trigger_node.properties, object, args)
243
+ trigger_node.applicator.send(applicator_method, method, exception, trigger_node.properties, object, args)
251
244
  end
252
245
 
253
246
  # Method to choose which replaced return from the post_patch to
@@ -279,6 +272,15 @@ module Contrast
279
272
  patched_method.to_s).to_sym
280
273
  end
281
274
 
275
+ # Given a method, return a symbol in the format
276
+ # <method_start>_unbound_<method_name>
277
+ def build_unbound_method_name patcher_method
278
+ (Contrast::Utils::ObjectShare::CONTRAST_PATCHED_METHOD_START +
279
+ 'unbound' +
280
+ Contrast::Utils::ObjectShare::UNDERSCORE +
281
+ patcher_method.to_s).to_sym
282
+ end
283
+
282
284
  # @param mod [Module] the module in which the patch should be
283
285
  # placed.
284
286
  # @param methods [Array(Symbol)] all the instance or singleton
@@ -297,10 +299,11 @@ module Contrast
297
299
  rescue NameError => e
298
300
  # This shouldn't happen anymore, but just in case calling alias
299
301
  # results in a NameError, we'll be safe here.
300
- logger.debug(
302
+ logger.error(
303
+ 'Attempted to alias a method on a Module that doesn\'t respond to it.',
301
304
  e,
302
- "The class #{ mod.cs__name } does not respond to the "\
303
- "method#{ method_policy.method_name }.")
305
+ module: mod.cs__name,
306
+ method: method_policy.method_name)
304
307
  return false
305
308
  end
306
309
  true
@@ -316,9 +319,79 @@ module Contrast
316
319
  contrast_prepend_method(mod, method_policy)
317
320
  end
318
321
 
322
+ # @param unbound_method [UnboundMethod] An unbound method, that doesn't reference its binding.
323
+ # This method executes C hooking code.
324
+ def register_c_hook unbound_method
325
+ # current binding is as meaningless as any other. but we need something
326
+ unbound_method.bind_call(self)
327
+ end
328
+
329
+ # @param target_module_name [String] Fully-qualified module name, as string, to which the C patch applies.
330
+ # @param unbound_method [UnboundMethod] An unbound method, to be patched into target_module.
331
+ # @param impl [Symbol] Strategy for applying the patch: { :alias_instance, :alias_singleton, or :prepend }:
332
+ # :alias_instance -> alias instance method of module
333
+ # :alias_singleton -> alias instance method of singleton class of module
334
+ # (equivalent to :alias, where `module = module.singleton class`)
335
+ # (this is a.k.a. "class-method patch")
336
+ # :prepend -> prepend instance method of module
337
+ # [prepending singleton is easily supported too, just not implemented yet.]
338
+ # @return [Symbol] new alias for the underlying method (presumably, so the patched method can call it)
339
+ def register_c_patch target_module_name, unbound_method, impl = :alias_instance
340
+ # These could be set as AfterLoadPatches.
341
+ method_name = unbound_method.name.to_sym
342
+ underlying_method_name = build_unbound_method_name(method_name).to_sym
343
+
344
+ target_module = Module.cs__const_get(target_module_name)
345
+
346
+ target_module = target_module.cs__singleton_class if %i[alias_singleton prepend].include? impl
347
+
348
+ visibility = if target_module.private_instance_methods(false).include?(method_name)
349
+ :private
350
+ elsif target_module.protected_instance_methods(false).include?(method_name)
351
+ :protected
352
+ elsif target_module.public_instance_methods(false).include?(method_name)
353
+ :public
354
+ else
355
+ raise NotImplementedError,
356
+ <<~ERR
357
+ Tried to register a C-defined #{ impl } patch for \
358
+ #{ target_module_name }##{ method_name }, but can't find :#{ method_name }.
359
+ ERR
360
+ end
361
+
362
+ case impl
363
+ when :alias_instance, :alias_singleton
364
+ unless target_module.instance_methods(false).include? underlying_method_name
365
+ # alias_method may be private
366
+ target_module.send(:alias_method, underlying_method_name, method_name)
367
+ target_module.send(:define_method, method_name, unbound_method.bind(target_module))
368
+ end
369
+ target_module.send(visibility, method_name) # e.g., module.private(:my_method)
370
+ when :prepend
371
+ prepending_module = Module.new
372
+ prepending_module.send(:define_method, method_name, unbound_method.bind(target_module))
373
+ prepending_module.send(visibility, :method_name)
374
+ # This prepends to the singleton class (it patches a class method)
375
+ target_module.prepend prepending_module
376
+ end
377
+ # Ougai::Logger.create_item_with_2args calls Hash#[]=, so we
378
+ # can't invoke this logging method or we'll seg fault as we'd
379
+ # change the method definition mid-call
380
+ # if method_name != :[]=
381
+ # logger.trace(
382
+ # 'Registered C-defined patch',
383
+ # implementation: impl,
384
+ # module: target_module_name,
385
+ # method: method_name,
386
+ # visibility: visibility)
387
+ # end
388
+ underlying_method_name.to_sym
389
+ end
390
+
319
391
  # @return [Boolean]
320
392
  def skip_contrast_analysis?
321
393
  return true if in_contrast_scope?
394
+ return true unless defined?(Contrast::Agent::REQUEST_TRACKER)
322
395
  return true unless Contrast::Agent::REQUEST_TRACKER.current&.analyze_request?
323
396
 
324
397
  false
@@ -44,14 +44,14 @@ module Contrast
44
44
  extend Contrast::Agent::Patching::Policy::AfterLoadPatcher
45
45
 
46
46
  include Contrast::Components::Interface
47
- access_component :logging, :analysis, :agent, :scope
47
+ access_component :agent, :analysis, :logging, :scope
48
48
 
49
49
  class << self
50
50
  # Hook to install the Contrast changes needed to allow for the
51
51
  # instrumentation of the application - this only occurs once, during
52
52
  # startup to catchup on everything we didn't see get loaded
53
53
  def patch
54
- logger.debug_with_time("\tPatching") do
54
+ logger.debug_with_time('Patching') do
55
55
  catchup_after_load_patches
56
56
  patch_methods
57
57
  Contrast::Agent::Assess::Policy::RewriterPatch.rewrite_interpolations
@@ -91,7 +91,7 @@ module Contrast
91
91
  patch_into_module(module_data)
92
92
  all_module_names.delete(mod_name) if status_type.get_status(mod).patched?
93
93
  rescue StandardError => e
94
- logger.error("Unable to patch into #{ mod_name }", e)
94
+ logger.error('Unable to patch module', e, module: mod_name)
95
95
  end
96
96
 
97
97
  # We did it, team. We found a patcher(s) that applies to the given
@@ -150,20 +150,20 @@ module Contrast
150
150
  # only be invoked by the patch_methods method above in order to
151
151
  # ensure it is wrapped in a synchronize call
152
152
  def synchronized_patch_methods
153
- # logger.trace_with_time("\t\tRunning patching") do # TODO: RUBY-547
154
- patched = []
155
- all_module_names.each do |patchable_name|
156
- next if AGENT.skip_instrumentation?(patchable_name)
153
+ logger.trace_with_time('Running patching') do
154
+ patched = []
155
+ all_module_names.each do |patchable_name|
156
+ next if AGENT.skip_instrumentation?(patchable_name)
157
157
 
158
- patchable_mod = patchable(patchable_name)
159
- next unless patchable_mod
158
+ patchable_mod = patchable(patchable_name)
159
+ next unless patchable_mod
160
160
 
161
- module_data = Contrast::Agent::ModuleData.new(patchable_mod, patchable_name)
162
- patch_into_module(module_data)
163
- patched << patchable_name if status_type.get_status(patchable_mod).patched?
161
+ module_data = Contrast::Agent::ModuleData.new(patchable_mod, patchable_name)
162
+ patch_into_module(module_data)
163
+ patched << patchable_name if status_type.get_status(patchable_mod).patched?
164
+ end
165
+ all_module_names.subtract(patched)
164
166
  end
165
- all_module_names.subtract(patched)
166
- # end
167
167
  end
168
168
 
169
169
  # Given the patchers that apply to this class that may apply, patch
@@ -187,9 +187,6 @@ module Contrast
187
187
 
188
188
  clazz = module_data.mod
189
189
 
190
- status = status_type.get_status(clazz)
191
- return if (status.patched? || status.patching?) && !redo_patch
192
-
193
190
  status.patching!
194
191
  patched = include_module(module_data)
195
192
 
@@ -216,9 +213,12 @@ module Contrast
216
213
  rescue StandardError => e
217
214
  status ||= status_type.get_status(module_data.mod)
218
215
  status.failed_patch!
219
- logger.warn(e, "Unable to patch into #{ module_data.name }")
216
+ logger.warn('Patching failed', e, module: module_data.name)
220
217
  ensure
221
- logger.debug(nil, "Patching #{ module_data.name } resulted in #{ status_type.get_status(module_data.mod).patch_status }")
218
+ logger.trace('Patching complete',
219
+ module: module_data.name,
220
+ result: Contrast::Agent::Patching::Policy::PatchStatus.get_status(
221
+ module_data.mod).patch_status)
222
222
  end
223
223
 
224
224
  # Includes the given module with the
@@ -2,6 +2,8 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  cs__scoped_require 'json'
5
+ cs__scoped_require 'singleton'
6
+
5
7
  cs__scoped_require 'contrast'
6
8
  cs__scoped_require 'contrast/components/interface'
7
9
  cs__scoped_require 'contrast/agent/patching/policy/module_policy'
@@ -33,10 +35,9 @@ module Contrast
33
35
  raise(NotImplementedError, 'specify the concrete node type for this poilcy')
34
36
  end
35
37
 
36
- access_component :logging, :analysis
38
+ access_component :analysis, :logging
37
39
 
38
- attr_accessor :providers, :tracked_classes
39
- attr_reader :sources, :propagators, :triggers, :patched_names
40
+ attr_reader :sources, :propagators, :triggers, :providers, :tracked_classes
40
41
 
41
42
  SOURCES_KEY = 'sources'
42
43
  PROPAGATION_KEY = 'propagators'
@@ -54,7 +55,6 @@ module Contrast
54
55
  @triggers = []
55
56
  @providers = {}
56
57
  @tracked_classes = []
57
- @patched_names = Set.new
58
58
 
59
59
  json = Contrast::Utils::ResourceLoader.load(cs__class.policy_json)
60
60
  from_hash_string(json)
@@ -85,14 +85,14 @@ module Contrast
85
85
 
86
86
  def add_node node, node_type = :trigger
87
87
  unless node
88
- logger.error(nil, 'Node was nil when adding node to policy')
88
+ logger.error('Node was nil when adding node to policy')
89
89
  return
90
90
  end
91
91
 
92
92
  begin
93
93
  node.validate
94
94
  rescue ArgumentError => e
95
- logger.error(e, e.message)
95
+ logger.error('Node failed validation', e)
96
96
  return
97
97
  end
98
98
 
@@ -107,7 +107,7 @@ module Contrast
107
107
  module_names << node.class_name
108
108
  @sources << node
109
109
  else
110
- logger.error(nil, "Invalid node_type: #{ node_type } provided when adding node to policy")
110
+ logger.error('Invalid node_type provided when adding node to policy', node_type: node_type)
111
111
  end
112
112
  end
113
113