contrast-agent 3.11.0 → 3.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (217) hide show
  1. checksums.yaml +4 -4
  2. data/.flayignore +1 -0
  3. data/ext/cs__assess_active_record_named/cs__active_record_named.c +7 -2
  4. data/ext/cs__assess_active_record_named/cs__active_record_named.h +1 -0
  5. data/ext/cs__assess_array/cs__assess_array.c +2 -1
  6. data/ext/cs__assess_array/cs__assess_array.h +1 -0
  7. data/ext/cs__assess_basic_object/cs__assess_basic_object.c +3 -7
  8. data/ext/cs__assess_basic_object/cs__assess_basic_object.h +2 -1
  9. data/ext/cs__assess_kernel/cs__assess_kernel.c +1 -1
  10. data/ext/cs__assess_module/cs__assess_module.c +5 -7
  11. data/ext/cs__assess_module/cs__assess_module.h +3 -0
  12. data/ext/cs__common/cs__common.c +1 -1
  13. data/ext/cs__protect_kernel/cs__protect_kernel.c +4 -2
  14. data/ext/cs__protect_kernel/cs__protect_kernel.h +1 -0
  15. data/funchook/autom4te.cache/output.0 +13 -1
  16. data/funchook/autom4te.cache/requests +49 -48
  17. data/funchook/autom4te.cache/traces.0 +3 -0
  18. data/funchook/config.log +217 -378
  19. data/funchook/config.status +24 -23
  20. data/funchook/configure +13 -1
  21. data/funchook/src/Makefile +7 -7
  22. data/funchook/src/config.h +2 -2
  23. data/funchook/src/decoder.o +0 -0
  24. data/funchook/src/distorm.o +0 -0
  25. data/funchook/src/funchook.o +0 -0
  26. data/funchook/src/funchook_io.o +0 -0
  27. data/funchook/src/funchook_syscall.o +0 -0
  28. data/funchook/src/funchook_unix.o +0 -0
  29. data/funchook/src/funchook_x86.o +0 -0
  30. data/funchook/src/instructions.o +0 -0
  31. data/funchook/src/insts.o +0 -0
  32. data/funchook/src/libfunchook.so +0 -0
  33. data/funchook/src/mnemonics.o +0 -0
  34. data/funchook/src/operands.o +0 -0
  35. data/funchook/src/os_func.o +0 -0
  36. data/funchook/src/os_func_unix.o +0 -0
  37. data/funchook/src/prefix.o +0 -0
  38. data/funchook/src/printf_base.o +0 -0
  39. data/funchook/src/textdefs.o +0 -0
  40. data/funchook/src/wstring.o +0 -0
  41. data/funchook/test/Makefile +2 -2
  42. data/funchook/test/funchook_test +0 -0
  43. data/funchook/test/libfunchook_test.so +0 -0
  44. data/funchook/test/test_main.o +0 -0
  45. data/funchook/test/x86_64_test.o +0 -0
  46. data/lib/contrast.rb +0 -1
  47. data/lib/contrast/agent.rb +19 -22
  48. data/lib/contrast/agent/assess.rb +0 -9
  49. data/lib/contrast/agent/assess/policy/patcher.rb +1 -0
  50. data/lib/contrast/agent/assess/policy/policy_node.rb +1 -1
  51. data/lib/contrast/agent/assess/policy/policy_scanner.rb +1 -1
  52. data/lib/contrast/agent/assess/policy/propagation_method.rb +3 -0
  53. data/lib/contrast/agent/assess/policy/propagator/custom.rb +1 -1
  54. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +1 -3
  55. data/lib/contrast/agent/assess/policy/trigger/reflected_xss.rb +90 -0
  56. data/lib/contrast/agent/assess/policy/trigger/xpath.rb +57 -0
  57. data/lib/contrast/agent/assess/policy/trigger_method.rb +3 -7
  58. data/lib/contrast/agent/assess/policy/trigger_node.rb +4 -1
  59. data/lib/contrast/agent/assess/rule/base.rb +0 -15
  60. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +22 -5
  61. data/lib/contrast/agent/assess/rule/redos.rb +0 -1
  62. data/lib/contrast/agent/at_exit_hook.rb +2 -2
  63. data/lib/contrast/agent/class_reopener.rb +9 -4
  64. data/lib/contrast/agent/exclusion_matcher.rb +0 -1
  65. data/lib/contrast/agent/inventory/policy/datastores.rb +54 -0
  66. data/lib/contrast/agent/inventory/policy/policy.rb +1 -1
  67. data/lib/contrast/agent/middleware.rb +38 -34
  68. data/lib/contrast/agent/patching/policy/after_load_patch.rb +11 -2
  69. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +51 -56
  70. data/lib/contrast/agent/patching/policy/patch.rb +2 -1
  71. data/lib/contrast/agent/patching/policy/patcher.rb +10 -12
  72. data/lib/contrast/agent/patching/policy/policy_node.rb +1 -1
  73. data/lib/contrast/agent/patching/policy/trigger_node.rb +1 -1
  74. data/lib/contrast/agent/protect/policy/applies_command_injection_rule.rb +63 -0
  75. data/lib/contrast/agent/protect/policy/applies_deserialization_rule.rb +52 -0
  76. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +68 -0
  77. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +117 -0
  78. data/lib/contrast/agent/protect/policy/applies_sqli_rule.rb +54 -0
  79. data/lib/contrast/agent/protect/policy/applies_xxe_rule.rb +129 -0
  80. data/lib/contrast/agent/protect/policy/policy.rb +6 -6
  81. data/lib/contrast/agent/protect/policy/rule_applicator.rb +51 -0
  82. data/lib/contrast/agent/protect/rule.rb +0 -5
  83. data/lib/contrast/agent/protect/rule/base.rb +6 -5
  84. data/lib/contrast/agent/protect/rule/cmd_injection.rb +3 -3
  85. data/lib/contrast/agent/protect/rule/path_traversal.rb +2 -7
  86. data/lib/contrast/agent/protect/rule/sqli.rb +4 -4
  87. data/lib/contrast/agent/railtie.rb +1 -0
  88. data/lib/contrast/agent/request.rb +2 -6
  89. data/lib/contrast/agent/request_context.rb +5 -6
  90. data/lib/contrast/agent/request_handler.rb +2 -2
  91. data/lib/contrast/agent/response.rb +0 -69
  92. data/lib/contrast/agent/service_heartbeat.rb +2 -2
  93. data/lib/contrast/agent/socket_client.rb +8 -8
  94. data/lib/contrast/agent/static_analysis.rb +2 -3
  95. data/lib/contrast/agent/version.rb +1 -1
  96. data/lib/contrast/api/decorators/application_settings.rb +1 -1
  97. data/lib/contrast/api/speedracer.rb +1 -1
  98. data/lib/contrast/components/agent.rb +17 -12
  99. data/lib/contrast/components/app_context.rb +33 -1
  100. data/lib/contrast/components/assess.rb +25 -15
  101. data/lib/contrast/components/contrast_service.rb +23 -67
  102. data/lib/contrast/components/interface.rb +4 -12
  103. data/lib/contrast/components/inventory.rb +5 -1
  104. data/lib/contrast/components/logger.rb +2 -2
  105. data/lib/contrast/components/protect.rb +40 -4
  106. data/lib/contrast/components/scope.rb +2 -52
  107. data/lib/contrast/components/settings.rb +24 -18
  108. data/lib/contrast/config/protect_rules_configuration.rb +0 -1
  109. data/lib/contrast/{extensions/ruby_core → extension}/assess.rb +12 -14
  110. data/lib/contrast/extension/assess/array.rb +77 -0
  111. data/lib/contrast/{extensions/ruby_core → extension}/assess/assess_extension.rb +2 -2
  112. data/lib/contrast/{extensions/ruby_core → extension}/assess/erb.rb +0 -0
  113. data/lib/contrast/extension/assess/eval_trigger.rb +78 -0
  114. data/lib/contrast/{extensions/ruby_core → extension}/assess/exec_trigger.rb +1 -1
  115. data/lib/contrast/{extensions/ruby_core → extension}/assess/fiber.rb +6 -5
  116. data/lib/contrast/{extensions/ruby_core → extension}/assess/hash.rb +2 -2
  117. data/lib/contrast/extension/assess/kernel.rb +110 -0
  118. data/lib/contrast/{extensions/ruby_core → extension}/assess/regexp.rb +4 -4
  119. data/lib/contrast/{extensions/ruby_core → extension}/assess/string.rb +5 -5
  120. data/lib/contrast/{extensions/ruby_core → extension}/delegator.rb +0 -0
  121. data/lib/contrast/{extensions/ruby_core → extension}/inventory.rb +2 -2
  122. data/lib/contrast/extension/kernel.rb +54 -0
  123. data/lib/contrast/{extensions/ruby_core → extension}/module.rb +0 -0
  124. data/lib/contrast/{extensions/ruby_core → extension}/protect.rb +2 -2
  125. data/lib/contrast/extension/protect/kernel.rb +44 -0
  126. data/lib/contrast/{extensions/ruby_core → extension}/protect/psych.rb +1 -1
  127. data/lib/contrast/{extensions/ruby_core → extension}/thread.rb +0 -0
  128. data/lib/contrast/framework/base_support.rb +22 -0
  129. data/lib/contrast/framework/manager.rb +33 -8
  130. data/lib/contrast/framework/rack/patch/session_cookie.rb +126 -0
  131. data/lib/contrast/framework/rack/patch/support.rb +24 -0
  132. data/lib/contrast/framework/rack/support.rb +22 -0
  133. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +43 -0
  134. data/lib/contrast/framework/rails/patch/assess_configuration.rb +103 -0
  135. data/lib/contrast/framework/rails/patch/rails_application_configuration.rb +31 -0
  136. data/lib/contrast/framework/rails/patch/support.rb +67 -0
  137. data/lib/contrast/framework/rails/rewrite/action_controller_railties_helper_inherited.rb +34 -0
  138. data/lib/contrast/framework/rails/rewrite/active_record_attribute_methods_read.rb +39 -0
  139. data/lib/contrast/framework/rails/rewrite/active_record_named.rb +73 -0
  140. data/lib/contrast/framework/rails/rewrite/active_record_time_zone_inherited.rb +33 -0
  141. data/lib/contrast/framework/rails/support.rb +115 -0
  142. data/lib/contrast/framework/sinatra/application_helper.rb +51 -0
  143. data/lib/contrast/framework/sinatra/patch/base.rb +83 -0
  144. data/lib/contrast/framework/sinatra/patch/support.rb +27 -0
  145. data/lib/contrast/framework/sinatra/support.rb +109 -0
  146. data/lib/contrast/logger/application.rb +80 -0
  147. data/lib/contrast/{agent/logger.rb → logger/log.rb} +23 -54
  148. data/lib/contrast/logger/time.rb +50 -0
  149. data/lib/contrast/tasks/config.rb +54 -0
  150. data/lib/contrast/tasks/service.rb +1 -5
  151. data/lib/contrast/utils/class_util.rb +1 -1
  152. data/lib/contrast/utils/gemfile_reader.rb +2 -2
  153. data/lib/contrast/utils/hash_digest.rb +2 -7
  154. data/lib/contrast/utils/invalid_configuration_util.rb +3 -3
  155. data/lib/contrast/utils/job_servers_running.rb +4 -2
  156. data/lib/contrast/utils/object_share.rb +0 -1
  157. data/lib/contrast/utils/service_response_util.rb +14 -12
  158. data/lib/contrast/utils/service_sender_util.rb +78 -21
  159. data/resources/assess/policy.json +9 -50
  160. data/resources/inventory/policy.json +2 -2
  161. data/resources/protect/policy.json +6 -6
  162. data/ruby-agent.gemspec +5 -1
  163. data/service_executables/VERSION +1 -1
  164. data/service_executables/linux/contrast-service +0 -0
  165. data/service_executables/mac/contrast-service +0 -0
  166. metadata +69 -83
  167. data/funchook/src/libfunchook.dylib +0 -0
  168. data/funchook/test/libfunchook_test.so.dSYM/Contents/Info.plist +0 -20
  169. data/funchook/test/libfunchook_test.so.dSYM/Contents/Resources/DWARF/libfunchook_test.so +0 -0
  170. data/lib/contrast/agent/assess/rule/csrf.rb +0 -66
  171. data/lib/contrast/agent/assess/rule/csrf/csrf_action.rb +0 -28
  172. data/lib/contrast/agent/assess/rule/csrf/csrf_applicator.rb +0 -53
  173. data/lib/contrast/agent/assess/rule/csrf/csrf_watcher.rb +0 -136
  174. data/lib/contrast/agent/assess/rule/response_scanning_rule.rb +0 -47
  175. data/lib/contrast/agent/assess/rule/response_watcher.rb +0 -36
  176. data/lib/contrast/agent/assess/rule/watcher.rb +0 -36
  177. data/lib/contrast/agent/feature_state.rb +0 -346
  178. data/lib/contrast/agent/protect/rule/csrf.rb +0 -119
  179. data/lib/contrast/agent/protect/rule/csrf/csrf_evaluator.rb +0 -100
  180. data/lib/contrast/agent/protect/rule/csrf/csrf_token_injector.rb +0 -85
  181. data/lib/contrast/agent/settings_state.rb +0 -88
  182. data/lib/contrast/api/decorators/exclusion.rb +0 -20
  183. data/lib/contrast/extensions/framework/rack/cookie.rb +0 -24
  184. data/lib/contrast/extensions/framework/rack/request.rb +0 -24
  185. data/lib/contrast/extensions/framework/rack/response.rb +0 -23
  186. data/lib/contrast/extensions/framework/rails/action_controller_inheritance.rb +0 -39
  187. data/lib/contrast/extensions/framework/rails/action_controller_railties_helper_inherited.rb +0 -20
  188. data/lib/contrast/extensions/framework/rails/active_record.rb +0 -26
  189. data/lib/contrast/extensions/framework/rails/active_record_named.rb +0 -58
  190. data/lib/contrast/extensions/framework/rails/active_record_time_zone_inherited.rb +0 -21
  191. data/lib/contrast/extensions/framework/rails/buffer.rb +0 -28
  192. data/lib/contrast/extensions/framework/rails/configuration.rb +0 -27
  193. data/lib/contrast/extensions/framework/sinatra/base.rb +0 -59
  194. data/lib/contrast/extensions/ruby_core/assess/array.rb +0 -59
  195. data/lib/contrast/extensions/ruby_core/assess/basic_object.rb +0 -15
  196. data/lib/contrast/extensions/ruby_core/assess/kernel.rb +0 -96
  197. data/lib/contrast/extensions/ruby_core/assess/module.rb +0 -14
  198. data/lib/contrast/extensions/ruby_core/assess/tilt_template_trigger.rb +0 -78
  199. data/lib/contrast/extensions/ruby_core/assess/xpath_library_trigger.rb +0 -40
  200. data/lib/contrast/extensions/ruby_core/eval_trigger.rb +0 -51
  201. data/lib/contrast/extensions/ruby_core/inventory/datastores.rb +0 -37
  202. data/lib/contrast/extensions/ruby_core/protect/applies_command_injection_rule.rb +0 -61
  203. data/lib/contrast/extensions/ruby_core/protect/applies_deserialization_rule.rb +0 -50
  204. data/lib/contrast/extensions/ruby_core/protect/applies_no_sqli_rule.rb +0 -66
  205. data/lib/contrast/extensions/ruby_core/protect/applies_path_traversal_rule.rb +0 -115
  206. data/lib/contrast/extensions/ruby_core/protect/applies_sqli_rule.rb +0 -53
  207. data/lib/contrast/extensions/ruby_core/protect/applies_xxe_rule.rb +0 -127
  208. data/lib/contrast/extensions/ruby_core/protect/kernel.rb +0 -30
  209. data/lib/contrast/extensions/ruby_core/protect/rule_applicator.rb +0 -50
  210. data/lib/contrast/framework/rails_support.rb +0 -104
  211. data/lib/contrast/framework/sinatra_application_helper.rb +0 -49
  212. data/lib/contrast/framework/sinatra_support.rb +0 -104
  213. data/lib/contrast/utils/data_store_util.rb +0 -23
  214. data/lib/contrast/utils/rack_assess_session_cookie.rb +0 -104
  215. data/lib/contrast/utils/rails_assess_configuration.rb +0 -95
  216. data/lib/contrast/utils/random_util.rb +0 -22
  217. data/resources/csrf/inject.js +0 -44
@@ -1,6 +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/components/interface'
5
+ cs__scoped_require 'contrast/extension/module'
4
6
  cs__scoped_require 'contrast/utils/class_util'
5
7
 
6
8
  module Contrast
@@ -9,13 +11,16 @@ module Contrast
9
11
  module Policy
10
12
  # Used to handle tracking patches that need to apply special instrumentation when a module is loaded
11
13
  class AfterLoadPatch
12
- attr_reader :applied, :module_name, :instrumentation_file_path, :method_to_instrument
14
+ include Contrast::Components::Interface
15
+ access_component :scope
16
+ attr_reader :applied, :module_name, :instrumentation_file_path, :method_to_instrument, :instrumenting_module
13
17
 
14
- def initialize module_name, instrumentation_file_path, method_to_instrument: nil
18
+ def initialize module_name, instrumentation_file_path, method_to_instrument: nil, instrumenting_module:
15
19
  @applied = false
16
20
  @module_name = module_name
17
21
  @method_to_instrument = method_to_instrument
18
22
  @instrumentation_file_path = instrumentation_file_path
23
+ @instrumenting_module = instrumenting_module
19
24
  end
20
25
 
21
26
  def applied?
@@ -55,6 +60,10 @@ module Contrast
55
60
 
56
61
  def instrument!
57
62
  cs__scoped_require instrumentation_file_path
63
+ if instrumenting_module
64
+ mod = Module.cs__const_get(instrumenting_module)
65
+ with_contrast_scope { mod.instrument } if mod
66
+ end
58
67
  @applied = true
59
68
  end
60
69
 
@@ -3,6 +3,7 @@
3
3
 
4
4
  cs__scoped_require 'contrast/components/interface'
5
5
  cs__scoped_require 'contrast/agent/patching/policy/after_load_patch'
6
+ cs__scoped_require 'contrast/framework/manager'
6
7
 
7
8
  module Contrast
8
9
  module Agent
@@ -14,84 +15,78 @@ module Contrast
14
15
  include Contrast::Components::Interface
15
16
  access_component :agent, :logging
16
17
 
17
- AFTER_LOAD_PATCHES = Set.new([
18
- Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
19
- 'ActiveRecord::AttributeMethods::TimeZoneConversion::ClassMethods',
20
- 'contrast/extensions/framework/rails/active_record_time_zone_inherited',
21
- method_to_instrument: :inherited),
22
- Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
23
- 'ActionController::Railties::Helper::ClassMethods',
24
- 'contrast/extensions/framework/rails/action_controller_railties_helper_inherited',
25
- method_to_instrument: :inherited),
26
- Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
27
- 'ActiveRecord::Scoping::Named::ClassMethods',
28
- 'contrast/extensions/framework/rails/active_record_named'),
29
- Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
30
- 'ActiveRecord::AttributeMethods::Read::ClassMethods',
31
- 'contrast/extensions/framework/rails/active_record'),
32
- Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
33
- 'Sinatra::Base',
34
- 'contrast/extensions/framework/sinatra/base'),
35
- Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
36
- 'Rails::Application::Configuration',
37
- 'contrast/extensions/framework/rails/configuration',
38
- method_to_instrument: :session_store),
39
- Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
40
- 'Rack::Request',
41
- 'contrast/extensions/framework/rack/request'),
42
- Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
43
- 'Rack::Response',
44
- 'contrast/extensions/framework/rack/response'),
45
- Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
46
- 'ActionController::Live::Buffer',
47
- 'contrast/extensions/framework/rails/buffer'),
48
- Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
49
- 'Rack::Session::Cookie',
50
- 'contrast/extensions/framework/rack/cookie')
51
- ])
52
-
53
18
  # After initialization run a catchup check to instrument any already loaded modules we care about
54
19
  def catchup_after_load_patches
20
+ apply_require_patches!
55
21
  apply_direct_patches!
56
- applied = []
57
- AFTER_LOAD_PATCHES.each do |after_load_patch|
58
- next unless after_load_patch.target_defined?
59
- next if AGENT.skip_instrumentation?(after_load_patch.module_name)
60
-
61
- logger.trace(
62
- 'Catching up on already loaded afterload patch - applying instrumentation',
63
- module: after_load_patch.module_name)
64
- after_load_patch.instrument!
65
- applied << after_load_patch
66
- end
67
- AFTER_LOAD_PATCHES.subtract(applied)
22
+ apply_load_patches!
68
23
  end
69
24
 
25
+ private
26
+
70
27
  # These patches need to be applied directly, not from our policy, so
71
28
  # do so and do so only once. This should be the new standard so that
72
29
  # there are no require time side effects of loading our core
73
30
  # extensions.
74
31
  def apply_direct_patches!
75
32
  @_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
33
+ Contrast::Extension::Assess::ArrayPropagator.instrument_array_track
34
+ Contrast::Extension::Assess::EvalTrigger.instrument_basic_object_track
35
+ Contrast::Extension::Assess::EvalTrigger.instrument_module_track
36
+ Contrast::Extension::Assess::FiberPropagator.instrument_fiber_track
37
+ Contrast::Extension::Assess::HashPropagator.instrument_hash_track
38
+ Contrast::Extension::Assess::KernelPropagator.instrument_kernel_track
39
+ Contrast::Extension::Assess::RegexpPropagator.instrument_regexp_track
40
+ Contrast::Extension::Assess::StringPropagator.instrument_string
41
+ Contrast::Extension::Assess::StringPropagator.instrument_string_interpolation
42
+
43
+ Contrast::Extension::Protect::Kernel.instrument
81
44
  true
82
45
  end
83
46
  end
84
47
 
48
+ def apply_load_patches!
49
+ after_load_patches.each do |after_load_patch|
50
+ next unless after_load_patch.target_defined?
51
+ next if AGENT.skip_instrumentation?(after_load_patch.module_name)
52
+
53
+ logger.trace(
54
+ 'Catching up on already loaded afterload patch - applying instrumentation',
55
+ module: after_load_patch.module_name)
56
+ after_load_patch.instrument!
57
+ end
58
+ after_load_patches.delete_if(&:applied?)
59
+ end
60
+
61
+ # These patches need to be applied directly, not from our policy, and
62
+ # are applied as a result of requiring the file as they alias methods
63
+ # directly, allowing us to control things like scope and exception
64
+ # handling
65
+ def apply_require_patches!
66
+ @_apply_require_patches ||= begin
67
+ cs__scoped_require 'contrast/extension/thread'
68
+ cs__scoped_require 'contrast/extension/kernel'
69
+ true
70
+ rescue LoadError, StandardError => e
71
+ logger.error('failed instrumenting apply_require_patches!', e)
72
+ false
73
+ end
74
+ end
75
+
76
+ def after_load_patches
77
+ @_after_load_patches ||= Contrast::Agent.framework_manager.find_after_load_patches
78
+ end
79
+
85
80
  # Use for any checks after we've initialized
86
81
  def load_patches_for_module module_name
87
- return if AFTER_LOAD_PATCHES.empty?
82
+ return if after_load_patches.empty?
88
83
 
89
- patch = AFTER_LOAD_PATCHES.find { |after_load_patch| after_load_patch.applies?(module_name) }
84
+ patch = after_load_patches.find { |after_load_patch| after_load_patch.applies?(module_name) }
90
85
  return unless patch
91
86
 
92
87
  logger.trace('Detected loading of afterload patch - applying instrumentation', module: module_name)
93
88
  patch.instrument!
94
- AFTER_LOAD_PATCHES.subtract([patch]) if patch.applied?
89
+ after_load_patches.delete_if(&:applied?)
95
90
  end
96
91
  end
97
92
  end
@@ -5,7 +5,7 @@ cs__scoped_require 'monitor'
5
5
  cs__scoped_require 'contrast/components/interface'
6
6
 
7
7
  cs__scoped_require 'contrast/agent'
8
- cs__scoped_require 'contrast/agent/logger'
8
+ cs__scoped_require 'contrast/logger/log'
9
9
  cs__scoped_require 'contrast/agent/patching/policy/method_policy'
10
10
  cs__scoped_require 'contrast/agent/patching/policy/patch_status'
11
11
  cs__scoped_require 'contrast/agent/patching/policy/trigger_node'
@@ -127,6 +127,7 @@ module Contrast
127
127
  # @param args [Array<Object>] The arguments passed to the method
128
128
  # being invoked.
129
129
  def apply_protect method_policy, method, exception, object, args
130
+ return unless AGENT.enabled?
130
131
  return unless PROTECT.enabled?
131
132
 
132
133
  apply_trigger_only(method_policy&.protect_node,
@@ -51,11 +51,9 @@ module Contrast
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('Patching') do
55
- catchup_after_load_patches
56
- patch_methods
57
- Contrast::Agent::Assess::Policy::RewriterPatch.rewrite_interpolations
58
- end
54
+ catchup_after_load_patches
55
+ patch_methods
56
+ Contrast::Agent::Assess::Policy::RewriterPatch.rewrite_interpolations
59
57
  end
60
58
 
61
59
  # Hook to only monkeypatch Contrast. This will not trigger any
@@ -222,13 +220,13 @@ module Contrast
222
220
  end
223
221
 
224
222
  # Includes the given module with the
225
- # Contrast::CoreExtensions::Assess::AssessExtension
223
+ # Contrast::Extension::Assess::AssessExtension
226
224
  # @param module_data [Contrast::Agent::ModuleData] the module, and
227
225
  # its name, that's being patched into
228
226
  def include_module module_data
229
227
  return false unless Contrast::Agent::Assess::Policy::Policy.instance.tracked_classes.include?(module_data.name)
230
228
 
231
- module_data.mod.send(:include, Contrast::CoreExtensions::Assess::AssessExtension)
229
+ module_data.mod.send(:include, Contrast::Extension::Assess::AssessExtension)
232
230
  true
233
231
  end
234
232
 
@@ -300,10 +298,10 @@ module Contrast
300
298
  end
301
299
 
302
300
  # core extensions
303
- cs__scoped_require 'contrast/extensions/ruby_core/module'
304
- cs__scoped_require 'contrast/extensions/ruby_core/assess'
305
- cs__scoped_require 'contrast/extensions/ruby_core/inventory'
306
- cs__scoped_require 'contrast/extensions/ruby_core/protect'
307
- cs__scoped_require 'contrast/extensions/ruby_core/protect/kernel'
301
+ cs__scoped_require 'contrast/extension/module'
302
+ cs__scoped_require 'contrast/extension/assess'
303
+ cs__scoped_require 'contrast/extension/inventory'
304
+ cs__scoped_require 'contrast/extension/protect'
305
+ cs__scoped_require 'contrast/extension/protect/kernel'
308
306
 
309
307
  cs__scoped_require 'cs__contrast_patch/cs__contrast_patch'
@@ -13,7 +13,7 @@ module Contrast
13
13
  # @abstract
14
14
  class PolicyNode
15
15
  include Contrast::Components::Interface
16
- access_component :scope
16
+ access_component :analysis, :scope
17
17
 
18
18
  attr_accessor :class_name, :instance_method, :method_name, :method_visibility
19
19
  attr_reader :properties, :method_scope
@@ -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/extensions/ruby_core/module'
4
+ cs__scoped_require 'contrast/extension/module'
5
5
  cs__scoped_require 'contrast/agent/patching/policy/policy_node'
6
6
 
7
7
  module Contrast
@@ -0,0 +1,63 @@
1
+ # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ cs__scoped_require 'contrast/agent/protect/rule/cmd_injection'
5
+ cs__scoped_require 'contrast/agent/protect/policy/applies_deserialization_rule'
6
+ cs__scoped_require 'contrast/agent/protect/policy/rule_applicator'
7
+
8
+ module Contrast
9
+ module Agent
10
+ module Protect
11
+ module Policy
12
+ # This Module is how we apply the Command Injection rule. It is called
13
+ # from our patches of the targeted methods in which command execution
14
+ # occurs. It is responsible for deciding if the infilter methods of the
15
+ # rule should be invoked.
16
+ # In addition, b/c of the nature of Deserialization's sand boxing
17
+ # function, this Module's apply methods call through to the
18
+ # {#apply_deserialization_command_check} method of the
19
+ # Deserialization applicator.
20
+ module AppliesCommandInjectionRule
21
+ extend Contrast::Agent::Protect::Policy::RuleApplicator
22
+
23
+ CS__SEMICOLON = '; '
24
+
25
+ class << self
26
+ def invoke method, _exception, _properties, object, args
27
+ return unless valid_command?(args)
28
+
29
+ command = build_command(args)
30
+ Contrast::Agent::Protect::Policy::AppliesDeserializationRule.apply_deserialization_command_check(command)
31
+ return if skip_analysis?
32
+
33
+ begin
34
+ clazz = object.is_a?(Module) ? object : object.cs__class
35
+ class_name = clazz.cs__name
36
+ rule.infilter(Contrast::Agent::REQUEST_TRACKER.current, class_name, method, command)
37
+ end
38
+ end
39
+
40
+ protected
41
+
42
+ def name
43
+ Contrast::Agent::Protect::Rule::CmdInjection::NAME
44
+ end
45
+
46
+ private
47
+
48
+ def valid_command? command
49
+ command && (command.is_a?(String) || command.is_a?(Array))
50
+ end
51
+
52
+ def build_command command
53
+ return command if command.is_a?(String)
54
+
55
+ command = command.drop(1) if command.length > 1
56
+ command.join(CS__SEMICOLON)
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,52 @@
1
+ # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ cs__scoped_require 'contrast/agent/protect/rule/deserialization'
5
+ cs__scoped_require 'contrast/agent/protect/policy/rule_applicator'
6
+
7
+ module Contrast
8
+ module Agent
9
+ module Protect
10
+ module Policy
11
+ # This Module is how we apply the Deserialization rule. It is called from
12
+ # our patches of the targeted methods in which deserialization occurs.
13
+ # It is responsible for deciding if the infilter methods of the rule
14
+ # should be invoked.
15
+ module AppliesDeserializationRule
16
+ extend Contrast::Agent::Protect::Policy::RuleApplicator
17
+
18
+ class << self
19
+ def invoke _method, _exception, _properties, _object, args
20
+ return unless valid_input?(args)
21
+ return if skip_analysis?
22
+
23
+ rule.infilter(Contrast::Agent::REQUEST_TRACKER.current, args[0])
24
+ end
25
+
26
+ def apply_deserialization_command_check command
27
+ return unless command
28
+ return if skip_analysis?
29
+
30
+ rule.check_command_scope(command)
31
+ end
32
+
33
+ protected
34
+
35
+ def name
36
+ Contrast::Agent::Protect::Rule::Deserialization::NAME
37
+ end
38
+
39
+ private
40
+
41
+ def valid_input? args
42
+ return false unless args&.any?
43
+
44
+ input = args[0]
45
+ input.is_a?(String)
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,68 @@
1
+ # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ cs__scoped_require 'contrast/agent/protect/rule/no_sqli'
5
+ cs__scoped_require 'contrast/agent/protect/policy/rule_applicator'
6
+
7
+ module Contrast
8
+ module Agent
9
+ module Protect
10
+ module Policy
11
+ # This Module is how we apply the NoSQL Injection rule. It is called from
12
+ # our patches of the targeted methods in which the execution of String
13
+ # based NoSQL queries occur. It is responsible for deciding if the
14
+ # infilter methods of the rule should be invoked.
15
+ module AppliesNoSqliRule
16
+ extend Contrast::Agent::Protect::Policy::RuleApplicator
17
+
18
+ class << self
19
+ def invoke method, _exception, properties, _object, args
20
+ return unless valid_input?(args)
21
+ return if skip_analysis?
22
+
23
+ database = properties['database']
24
+ operations = args[0]
25
+ context = Contrast::Agent::REQUEST_TRACKER.current
26
+ if operations.is_a?(Array)
27
+ operations.each do |m|
28
+ handle_operation(context, database, method, m)
29
+ end
30
+ else
31
+ handle_operation(context, database, method, operations)
32
+ end
33
+ end
34
+
35
+ protected
36
+
37
+ def name
38
+ Contrast::Agent::Protect::Rule::NoSqli::NAME
39
+ end
40
+
41
+ private
42
+
43
+ def valid_input? args
44
+ return false unless args&.any?
45
+
46
+ args[0]
47
+ end
48
+
49
+ def handle_operation context, database, _action, operation
50
+ data = extract_mongo_data(operation)
51
+ return unless data && !data.empty?
52
+
53
+ rule.infilter(context, database, data)
54
+ end
55
+
56
+ def extract_mongo_data operation
57
+ if operation.cs__respond_to? :selector
58
+ operation.selector
59
+ elsif operation.cs__respond_to? :documents
60
+ operation.documents
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end