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
@@ -0,0 +1,77 @@
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/patching/policy/patch'
5
+ cs__scoped_require 'contrast/agent/patching/policy/patcher'
6
+ cs__scoped_require 'contrast/components/interface'
7
+
8
+ module Contrast
9
+ module Extension
10
+ module Assess
11
+ # This is our patch of the Array class required to handle propagation
12
+ # Disclaimer: there may be a better way, but we're in a 'get it work' state.
13
+ # Hopefully, we'll be in a 'get it right' state soon.
14
+ class ArrayPropagator
15
+ include Contrast::Components::Interface
16
+
17
+ access_component :scope
18
+
19
+ ARRAY_JOIN_HASH = {
20
+ 'class_name' => 'Array',
21
+ 'instance_method' => true,
22
+ 'method_visibility' => 'public',
23
+ 'method_name' => 'join',
24
+ 'action' => 'CUSTOM',
25
+ 'source' => 'O',
26
+ 'target' => 'R',
27
+ 'patch_class' => 'NOOP',
28
+ 'patch_method' => 'cs__track_join'
29
+ }.cs__freeze
30
+ ARRAY_JOIN_NODE = Contrast::Agent::Assess::Policy::PropagationNode.new(ARRAY_JOIN_HASH)
31
+
32
+ class << self
33
+ # When you call join, they use an internal thing, so there's no good way to get at the thing being returned.
34
+ # Multiple Strings are appended with the #join method. Because that
35
+ # operation happens in C, we have to do it here rather than rely on the
36
+ # patch of our String append or concat methods.
37
+ def cs__track_join ary, separator, ret
38
+ return unless ary
39
+ return ret if Contrast::Agent::Patching::Policy::Patch.skip_assess_analysis?
40
+
41
+ with_contrast_scope do
42
+ shift = 0
43
+ separator_length = separator.nil? ? 0 : separator.to_s.length
44
+ ary.each do |obj|
45
+ if obj # skip nil here
46
+ ret.cs__copy_from(obj, shift)
47
+ shift += obj.to_s.length
48
+ end
49
+ shift += separator_length
50
+ end
51
+ return ret unless ret.cs__tracked?
52
+
53
+ ret.cs__properties.cleanup_tags
54
+ ret.cs__properties.build_event(
55
+ ARRAY_JOIN_NODE,
56
+ ret,
57
+ ary,
58
+ ret,
59
+ [separator])
60
+ ret
61
+ end
62
+ end
63
+
64
+ def instrument_array_track
65
+ @_instrument_array_track ||= begin
66
+ cs__scoped_require 'cs__assess_array/cs__assess_array'
67
+ true
68
+ end
69
+ rescue StandardError, LoadError => e
70
+ logger.error('Error loading assess track patch', e)
71
+ false
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -5,13 +5,13 @@ cs__scoped_require 'contrast/agent/assess/properties'
5
5
  cs__scoped_require 'contrast/agent/assess/insulator'
6
6
 
7
7
  module Contrast
8
- module CoreExtensions
8
+ module Extension
9
9
  module Assess
10
10
  # This module is responsible for maintaining the data we need to
11
11
  # construct a trace event for the object in which it is included. Rather
12
12
  # than have this code all over the place, any class that wants to use
13
13
  # dataflow features should be sent
14
- # 'include Contrast::CoreExtensions::Assess::AssessExtension'
14
+ # 'include Contrast::Extension::Assess::AssessExtension'
15
15
  module AssessExtension
16
16
  # Lazily build properties object. Only objects that have been tracked
17
17
  # will have the @_cs__properties, but all will respond to the
@@ -0,0 +1,78 @@
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/components/interface'
5
+
6
+ module Contrast
7
+ module Extension
8
+ module Assess
9
+ # This Module allows us to track calls to the BasicObject#eval method,
10
+ # which violates the design of most methods we track in that we have to
11
+ # apply the trigger in a custom patch over one of the generic triggers in
12
+ # TriggerMethod.
13
+ class EvalTrigger
14
+ include Contrast::Components::Interface
15
+ access_component :logging
16
+
17
+ class << self
18
+ def instance_eval_trigger_check obj, source, ret
19
+ apply_trigger(obj, source, ret, 'BasicObject', :instance_eval)
20
+ end
21
+
22
+ def eval_trigger_check obj, source, ret, method
23
+ apply_trigger(obj, source, ret, 'Module', method)
24
+ end
25
+
26
+ def apply_trigger obj, source, ret, clazz, method
27
+ current_context = Contrast::Agent::REQUEST_TRACKER.current
28
+ return unless current_context
29
+
30
+ # Since we know this is the source of the trigger, we can do some
31
+ # optimization here and return when it is not tracked
32
+ return unless Contrast::Utils::Assess::TrackingUtil.tracked?(source)
33
+
34
+ # source might not be all the args passed in, but it is the one we care
35
+ # about. we could pass in all the args in the last param here if it
36
+ # becomes an issue in rendering on TS
37
+ Contrast::Agent::Assess::Policy::TriggerMethod.apply_eval_trigger(
38
+ current_context,
39
+ trigger_node(clazz, method),
40
+ source,
41
+ obj,
42
+ ret,
43
+ source)
44
+ end
45
+
46
+ def instrument_basic_object_track
47
+ @_instrument_basic_object_track ||= begin
48
+ cs__scoped_require 'cs__assess_basic_object/cs__assess_basic_object'
49
+ true
50
+ end
51
+ rescue StandardError, LoadError => e
52
+ logger.error('Error loading basic object track patch', e)
53
+ false
54
+ end
55
+
56
+ def instrument_module_track
57
+ @_instrument_module_track ||= begin
58
+ cs__scoped_require 'cs__assess_module/cs__assess_module'
59
+ true
60
+ end
61
+ rescue StandardError, LoadError => e
62
+ logger.error('Error loading module track patch', e)
63
+ false
64
+ end
65
+
66
+ private
67
+
68
+ def trigger_node clazz, method
69
+ triggers = Contrast::Agent::Assess::Policy::Policy.instance.triggers
70
+ return unless triggers
71
+
72
+ triggers.find { |node| node.class_name == clazz && node.method_name == method }
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -4,7 +4,7 @@
4
4
  cs__scoped_require 'contrast/agent/at_exit_hook'
5
5
 
6
6
  module Contrast
7
- module CoreExtensions
7
+ module Extension
8
8
  module Assess
9
9
  # This Module allows us to track calls to the Kernel#exec method, which
10
10
  # violates the design of most methods we track in that we have to apply
@@ -9,7 +9,7 @@ cs__scoped_require 'contrast/components/interface'
9
9
  # Specifically, we instrument 'rb_fiber_yield' and 'rb_fiber_new' in
10
10
  # order to track what happens within Enumerator#next.
11
11
  module Contrast
12
- module CoreExtensions
12
+ module Extension
13
13
  module Assess
14
14
  # This Class provides us with a way to invoke Regexp propagation for those
15
15
  # methods which are too complex to fit into one of the standard
@@ -20,15 +20,16 @@ module Contrast
20
20
 
21
21
  access_component :analysis, :logging, :scope
22
22
 
23
+ # we use funchook to patch rb_fiber_new the initialize method is not exposed by Ruby core
23
24
  FIBER_NEW_NODE_HASH = {
24
25
  'class_name' => 'Fiber',
25
26
  'instance_method' => true,
26
27
  'method_visibility' => 'public',
27
- 'method_name' => 'c_new', # TODO: Why do we patch new here and not initialize? Historically, new has been problematic.
28
+ 'method_name' => 'c_new',
28
29
  'action' => 'CUSTOM',
29
30
  'source' => 'O',
30
31
  'target' => 'R',
31
- 'patch_class' => 'Contrast::CoreExtensions::Assess::FiberPropagator',
32
+ 'patch_class' => 'Contrast::Extension::Assess::FiberPropagator',
32
33
  'patch_method' => 'track_rb_fiber_new'
33
34
  }.cs__freeze
34
35
  FIBER_NEW_NODE = Contrast::Agent::Assess::Policy::PropagationNode.new(FIBER_NEW_NODE_HASH)
@@ -43,7 +44,7 @@ module Contrast
43
44
  'action' => 'CUSTOM',
44
45
  'source' => 'O',
45
46
  'target' => 'R',
46
- 'patch_class' => 'Contrast::CoreExtensions::Assess::FiberPropagator',
47
+ 'patch_class' => 'Contrast::Extension::Assess::FiberPropagator',
47
48
  'patch_method' => 'track_rb_fiber_yield'
48
49
  }.cs__freeze
49
50
  FIBER_YIELD_NODE = Contrast::Agent::Assess::Policy::PropagationNode.new(FIBER_YIELD_NODE_HASH)
@@ -101,7 +102,7 @@ module Contrast
101
102
  cs__scoped_require 'cs__assess_fiber_track/cs__assess_fiber_track'
102
103
  true
103
104
  end
104
- rescue StandardError => e
105
+ rescue StandardError, LoadError => e
105
106
  logger.error('Error loading fiber track patch', e)
106
107
  false
107
108
  end
@@ -4,7 +4,7 @@
4
4
  cs__scoped_require 'contrast/components/interface'
5
5
 
6
6
  module Contrast
7
- module CoreExtensions
7
+ module Extension
8
8
  module Assess
9
9
  # This Class provides us with a way to invoke Hash propagation for those
10
10
  # methods which are too complex to fit into one of the standard
@@ -28,7 +28,7 @@ module Contrast
28
28
  cs__scoped_require 'cs__assess_hash/cs__assess_hash'
29
29
  true
30
30
  end
31
- rescue StandardError => e
31
+ rescue StandardError, LoadError => e
32
32
  logger.error('Error loading hash track patch', e)
33
33
  false
34
34
  end
@@ -0,0 +1,110 @@
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/components/interface'
5
+ cs__scoped_require 'contrast/extension/assess/exec_trigger'
6
+
7
+ module Contrast
8
+ module Extension
9
+ module Assess
10
+ # This module provides us with a way to invoke Kernel propagation for those
11
+ # methods which are too complex to fit into one of the standard
12
+ # Contrast::Agent::Assess::Policy::Propagator molds without cluttering up the
13
+ # Kernel Module or exposing our methods there.
14
+ module KernelPropagator
15
+ class << self
16
+ include Contrast::Components::Interface
17
+ include Contrast::Extension::Assess::ExecTrigger
18
+
19
+ access_component :logging
20
+
21
+ # We're 'tracking' sprintf now, meaning if anything is tracked on the way
22
+ # in, the entire result will be tracked out. We're going to take this
23
+ # approach for now b/c it's fast and easy. I don't super love it, and by
24
+ # that I mean I hate it.
25
+ #
26
+ # To actually track this, we'd have to find the index of the new things
27
+ # being added, then remove the tags at the range of the format marker,
28
+ # which is some arbitrary length thing, and add the new tags from the
29
+ # inserted string, shifted down by the length of the aforementioned
30
+ # marker.
31
+ #
32
+ # marker is in the format %[flags][width][.precision]type, type being a
33
+ # single character. We could regexp this with %.+[bBdiouxXeEfgGaAcps%]
34
+ #
35
+ # also, b/c Ruby hates us, there are things called absolute markers,
36
+ # (digit)$, that go in the flags section. These cannot be mixed w/ the
37
+ # order assumed type
38
+ #
39
+ # oh, and there's also %<name>type and %{name}... b/c of course there is
40
+ # -HM
41
+ def sprintf_tagger patcher, preshift, ret, _block
42
+ format_string = preshift.args[0]
43
+ args = preshift.args[1]
44
+
45
+ track_sprintf(ret, format_string, args)
46
+
47
+ ret.cs__properties.build_event(
48
+ patcher,
49
+ ret,
50
+ preshift.object,
51
+ ret,
52
+ preshift.args,
53
+ 1)
54
+ ret
55
+ end
56
+
57
+ def track_sprintf result, format_string, args
58
+ handle_sprintf_value(format_string, result)
59
+ if args.is_a?(String)
60
+ handle_sprintf_value(args, result)
61
+ elsif args.is_a?(Hash)
62
+ handle_sprintf_hash(args, result)
63
+ elsif args.is_a?(Array)
64
+ handle_sprintf_array args, result
65
+ end
66
+
67
+ result
68
+ rescue StandardError => e
69
+ logger.error(
70
+ 'Unable to track dataflow through sprintf', e)
71
+ result
72
+ end
73
+
74
+ def instrument_kernel_track
75
+ @_instrument_fiber_variables ||= begin
76
+ cs__scoped_require 'cs__assess_kernel/cs__assess_kernel'
77
+ true
78
+ end
79
+ rescue StandardError, LoadError => e
80
+ logger.error('Error loading kernel track patch', e)
81
+ false
82
+ end
83
+
84
+ private
85
+
86
+ def handle_sprintf_value value, result
87
+ return unless Contrast::Utils::DuckUtils.trackable?(value) && value.cs__tracked?
88
+
89
+ value.cs__properties.events.each do |event|
90
+ result.cs__properties.events << event
91
+ end
92
+ value.cs__splat_tags(result)
93
+ end
94
+
95
+ def handle_sprintf_array args, result
96
+ args.each do |value|
97
+ handle_sprintf_value(value, result)
98
+ end
99
+ end
100
+
101
+ def handle_sprintf_hash args, result
102
+ args.each_value do |value|
103
+ handle_sprintf_value(value, result)
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
@@ -5,7 +5,7 @@ cs__scoped_require 'contrast/agent/assess/policy/propagation_node'
5
5
  cs__scoped_require 'contrast/components/interface'
6
6
 
7
7
  module Contrast
8
- module CoreExtensions
8
+ module Extension
9
9
  module Assess
10
10
  # This Class provides us with a way to invoke Regexp propagation for those
11
11
  # methods which are too complex to fit into one of the standard
@@ -25,7 +25,7 @@ module Contrast
25
25
  'action' => 'CUSTOM',
26
26
  'source' => 'P0',
27
27
  'target' => 'R',
28
- 'patch_class' => 'Contrast::CoreExtensions::Assess::RegexpPropagator',
28
+ 'patch_class' => 'Contrast::Extension::Assess::RegexpPropagator',
29
29
  'patch_method' => 'track_equal_squiggle'
30
30
  }.cs__freeze
31
31
  REGEXP_EQUAL_SQUIGGLE_NODE = Contrast::Agent::Assess::Policy::PropagationNode.new(REGEXP_EQUAL_SQUIGGLE_HASH)
@@ -43,7 +43,7 @@ module Contrast
43
43
  # As such, a scope of 1 here indicates that,
44
44
  # so we know that we're in the top level call for this method.
45
45
  # normal patch [-alias-]> special case patch [-alias-]> original method
46
- # TODO: RUBY-686
46
+ # TODO: RUBY-686
47
47
  return if scope_for_current_ec.instance_variable_get(:@contrast_scope) > 1
48
48
 
49
49
  target = info_hash[:back_ref]
@@ -73,7 +73,7 @@ module Contrast
73
73
  cs__scoped_require 'cs__assess_regexp/cs__assess_regexp'
74
74
  true
75
75
  end
76
- rescue StandardError => e
76
+ rescue StandardError, LoadError => e
77
77
  logger.error('Error loading regexp track patch', e)
78
78
  false
79
79
  end
@@ -3,16 +3,16 @@
3
3
 
4
4
  cs__scoped_require 'contrast/agent/assess/policy/propagation_node'
5
5
  cs__scoped_require 'contrast/components/interface'
6
- cs__scoped_require 'contrast/extensions/ruby_core/assess/assess_extension'
6
+ cs__scoped_require 'contrast/extension/assess/assess_extension'
7
7
 
8
8
  # This patch installs our extension as early as possible. The alternative is to
9
9
  # litter our code with Contrast::Utils::DuckUtils.trackable? checks.
10
10
  class String
11
- include Contrast::CoreExtensions::Assess::AssessExtension
11
+ include Contrast::Extension::Assess::AssessExtension
12
12
  end
13
13
 
14
14
  module Contrast
15
- module CoreExtensions
15
+ module Extension
16
16
  module Assess
17
17
  # This Class provides us with a way to invoke String propagation for those
18
18
  # methods which are too complex to fit into one of the standard
@@ -57,7 +57,7 @@ module Contrast
57
57
  cs__scoped_require 'cs__assess_string/cs__assess_string'
58
58
  true
59
59
  end
60
- rescue StandardError => e
60
+ rescue StandardError, LoadError => e
61
61
  logger.error('Error loading hash track patch', e)
62
62
  false
63
63
  end
@@ -69,7 +69,7 @@ module Contrast
69
69
  cs__scoped_require 'cs__assess_string_interpolation26/cs__assess_string_interpolation26'
70
70
  end
71
71
  true
72
- rescue StandardError => e
72
+ rescue StandardError, LoadError => e
73
73
  logger.error('Error loading interpolation patch', e)
74
74
  false
75
75
  end
@@ -2,8 +2,8 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Contrast
5
- module CoreExtensions
6
- # Our top level Inventory namespace in the Core Extensions section of our
5
+ module Extension
6
+ # Our top level Inventory namespace in the Core Extension section of our
7
7
  # code. These patches are those that are invoked directly from a patched
8
8
  # Class.
9
9
  #
@@ -0,0 +1,54 @@
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/components/scope'
5
+
6
+ # This is a reasonable place for the Kernel#catch hook to live.
7
+ # No current plans for component re-design, but if we had some kind of
8
+ # "do this when a component is hooked in" thing, this would live there.
9
+ # For now, it's over-engineering to live anywhere else. -ajm
10
+ module Kernel # :nodoc:
11
+ alias_method :cs__catch, :catch
12
+
13
+ # In the event of a `throw`, we need to override `catch`
14
+ # to save & restore scope state:
15
+ #
16
+ # scope_level == 0
17
+ #
18
+ # catch(:abc) do
19
+ # with_contrast_scope do
20
+ # throw :abc # will leak
21
+ # end
22
+ # end
23
+ #
24
+ # scope_level == 1
25
+ #
26
+ # Frankly, this isn't how scope should be used. This is in place of
27
+ # proper `ensure` blocks within the instrumentation call stack.
28
+ # This will actually /create/ scope leaks if you're doing something like:
29
+ #
30
+ # catch(:ohno) do
31
+ # enter scope
32
+ # end
33
+ #
34
+ # abc()
35
+ #
36
+ # exit scope
37
+ #
38
+ # i.e. if you intend to change net scope across a catch block boundary.
39
+
40
+ private
41
+
42
+ def catch *args, &block
43
+ # Save current scope level
44
+ scope_level = Contrast::Components::Scope::COMPONENT_INTERFACE.scope_for_current_ec.instance_variable_get(:@contrast_scope)
45
+
46
+ # Run original catch with block.
47
+ retval = cs__catch(*args, &block)
48
+
49
+ # Restore scope.
50
+ Contrast::Components::Scope::COMPONENT_INTERFACE.scope_for_current_ec.instance_variable_set(:@contrast_scope, scope_level)
51
+
52
+ retval
53
+ end
54
+ end