contrast-agent 3.13.2 → 4.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (250) hide show
  1. checksums.yaml +4 -4
  2. data/exe/contrast_service +1 -7
  3. data/ext/cs__assess_active_record_named/cs__active_record_named.c +8 -7
  4. data/ext/cs__assess_array/cs__assess_array.c +6 -5
  5. data/ext/cs__assess_basic_object/cs__assess_basic_object.c +5 -5
  6. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.c +2 -1
  7. data/ext/cs__assess_hash/cs__assess_hash.c +18 -17
  8. data/ext/cs__assess_hash/cs__assess_hash.h +2 -1
  9. data/ext/cs__assess_kernel/cs__assess_kernel.c +7 -8
  10. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +18 -16
  11. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.h +1 -0
  12. data/ext/cs__assess_module/cs__assess_module.c +6 -6
  13. data/ext/cs__assess_regexp/cs__assess_regexp.c +4 -4
  14. data/ext/cs__assess_string/cs__assess_string.c +31 -16
  15. data/ext/cs__assess_string/cs__assess_string.h +6 -1
  16. data/ext/cs__assess_string_interpolation26/cs__assess_string_interpolation26.c +4 -2
  17. data/ext/cs__assess_yield_track/cs__assess_yield_track.c +2 -2
  18. data/ext/cs__common/cs__common.c +48 -39
  19. data/ext/cs__common/cs__common.h +16 -21
  20. data/ext/cs__contrast_patch/cs__contrast_patch.c +27 -25
  21. data/ext/cs__contrast_patch/cs__contrast_patch.h +5 -7
  22. data/ext/cs__protect_kernel/cs__protect_kernel.c +11 -12
  23. data/ext/cs__protect_kernel/cs__protect_kernel.h +2 -2
  24. data/lib/contrast-agent.rb +1 -1
  25. data/lib/contrast.rb +13 -23
  26. data/lib/contrast/agent.rb +39 -47
  27. data/lib/contrast/agent/assess.rb +12 -12
  28. data/lib/contrast/agent/assess/contrast_event.rb +151 -85
  29. data/lib/contrast/agent/assess/events/event_factory.rb +2 -2
  30. data/lib/contrast/agent/assess/events/source_event.rb +3 -3
  31. data/lib/contrast/agent/assess/finalizers/freeze.rb +15 -0
  32. data/lib/contrast/agent/assess/finalizers/hash.rb +97 -0
  33. data/lib/contrast/agent/assess/policy/dynamic_source_factory.rb +11 -4
  34. data/lib/contrast/agent/assess/policy/patcher.rb +6 -6
  35. data/lib/contrast/agent/assess/policy/policy.rb +9 -11
  36. data/lib/contrast/agent/assess/policy/policy_node.rb +17 -12
  37. data/lib/contrast/agent/assess/policy/policy_scanner.rb +21 -5
  38. data/lib/contrast/agent/assess/policy/preshift.rb +13 -7
  39. data/lib/contrast/agent/assess/policy/propagation_method.rb +64 -44
  40. data/lib/contrast/agent/assess/policy/propagation_node.rb +2 -2
  41. data/lib/contrast/agent/assess/policy/propagator.rb +18 -18
  42. data/lib/contrast/agent/assess/policy/propagator/append.rb +8 -5
  43. data/lib/contrast/agent/assess/policy/propagator/base.rb +1 -1
  44. data/lib/contrast/agent/assess/policy/propagator/center.rb +9 -5
  45. data/lib/contrast/agent/assess/policy/propagator/custom.rb +1 -1
  46. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +6 -4
  47. data/lib/contrast/agent/assess/policy/propagator/insert.rb +7 -4
  48. data/lib/contrast/agent/assess/policy/propagator/keep.rb +4 -1
  49. data/lib/contrast/agent/assess/policy/propagator/match_data.rb +7 -9
  50. data/lib/contrast/agent/assess/policy/propagator/next.rb +7 -5
  51. data/lib/contrast/agent/assess/policy/propagator/prepend.rb +13 -5
  52. data/lib/contrast/agent/assess/policy/propagator/remove.rb +8 -4
  53. data/lib/contrast/agent/assess/policy/propagator/replace.rb +5 -2
  54. data/lib/contrast/agent/assess/policy/propagator/reverse.rb +7 -5
  55. data/lib/contrast/agent/assess/policy/propagator/select.rb +13 -7
  56. data/lib/contrast/agent/assess/policy/propagator/splat.rb +10 -9
  57. data/lib/contrast/agent/assess/policy/propagator/split.rb +27 -22
  58. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +52 -35
  59. data/lib/contrast/agent/assess/policy/propagator/trim.rb +11 -5
  60. data/lib/contrast/agent/assess/policy/rewriter_patch.rb +5 -5
  61. data/lib/contrast/agent/assess/policy/source_method.rb +90 -72
  62. data/lib/contrast/agent/assess/policy/source_validation/cross_site_validator.rb +1 -1
  63. data/lib/contrast/agent/assess/policy/source_validation/source_validation.rb +1 -1
  64. data/lib/contrast/agent/assess/policy/trigger/reflected_xss.rb +16 -12
  65. data/lib/contrast/agent/assess/policy/trigger/xpath.rb +2 -2
  66. data/lib/contrast/agent/assess/policy/trigger_method.rb +81 -33
  67. data/lib/contrast/agent/assess/policy/trigger_node.rb +41 -46
  68. data/lib/contrast/agent/assess/policy/trigger_validation/ssrf_validator.rb +2 -1
  69. data/lib/contrast/agent/assess/policy/trigger_validation/trigger_validation.rb +2 -2
  70. data/lib/contrast/agent/assess/properties.rb +15 -5
  71. data/lib/contrast/agent/assess/property/evented.rb +7 -20
  72. data/lib/contrast/agent/assess/property/tagged.rb +13 -7
  73. data/lib/contrast/agent/assess/property/updated.rb +131 -0
  74. data/lib/contrast/agent/assess/rule.rb +2 -2
  75. data/lib/contrast/agent/assess/rule/base.rb +3 -4
  76. data/lib/contrast/agent/assess/rule/provider.rb +3 -3
  77. data/lib/contrast/agent/assess/rule/provider/hardcoded_key.rb +58 -5
  78. data/lib/contrast/agent/assess/rule/provider/hardcoded_password.rb +24 -9
  79. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +85 -16
  80. data/lib/contrast/agent/assess/tag.rb +1 -1
  81. data/lib/contrast/agent/assess/tracker.rb +66 -0
  82. data/lib/contrast/agent/at_exit_hook.rb +6 -6
  83. data/lib/contrast/agent/class_reopener.rb +14 -11
  84. data/lib/contrast/agent/deadzone/policy/deadzone_node.rb +1 -1
  85. data/lib/contrast/agent/deadzone/policy/policy.rb +2 -2
  86. data/lib/contrast/agent/disable_reaction.rb +1 -1
  87. data/lib/contrast/agent/exclusion_matcher.rb +1 -1
  88. data/lib/contrast/agent/inventory.rb +15 -0
  89. data/lib/contrast/agent/inventory/dependencies.rb +50 -0
  90. data/lib/contrast/agent/inventory/dependency_analysis.rb +37 -0
  91. data/lib/contrast/agent/inventory/dependency_usage_analysis.rb +104 -0
  92. data/lib/contrast/agent/inventory/gemfile_digest_cache.rb +38 -0
  93. data/lib/contrast/agent/inventory/policy/datastores.rb +2 -2
  94. data/lib/contrast/agent/inventory/policy/policy.rb +3 -3
  95. data/lib/contrast/agent/inventory/policy/trigger_node.rb +1 -1
  96. data/lib/contrast/agent/middleware.rb +33 -34
  97. data/lib/contrast/agent/patching/policy/after_load_patch.rb +9 -9
  98. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +23 -22
  99. data/lib/contrast/agent/patching/policy/module_policy.rb +11 -11
  100. data/lib/contrast/agent/patching/policy/patch.rb +15 -15
  101. data/lib/contrast/agent/patching/policy/patcher.rb +43 -44
  102. data/lib/contrast/agent/patching/policy/policy.rb +23 -12
  103. data/lib/contrast/agent/patching/policy/policy_node.rb +1 -1
  104. data/lib/contrast/agent/patching/policy/trigger_node.rb +2 -2
  105. data/lib/contrast/agent/protect/policy/applies_command_injection_rule.rb +6 -8
  106. data/lib/contrast/agent/protect/policy/applies_deserialization_rule.rb +2 -2
  107. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +2 -2
  108. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +7 -6
  109. data/lib/contrast/agent/protect/policy/applies_sqli_rule.rb +2 -2
  110. data/lib/contrast/agent/protect/policy/applies_xxe_rule.rb +4 -4
  111. data/lib/contrast/agent/protect/policy/policy.rb +8 -8
  112. data/lib/contrast/agent/protect/policy/rule_applicator.rb +1 -1
  113. data/lib/contrast/agent/protect/policy/trigger_node.rb +1 -1
  114. data/lib/contrast/agent/protect/rule.rb +18 -18
  115. data/lib/contrast/agent/protect/rule/base.rb +4 -3
  116. data/lib/contrast/agent/protect/rule/base_service.rb +1 -1
  117. data/lib/contrast/agent/protect/rule/cmd_injection.rb +5 -5
  118. data/lib/contrast/agent/protect/rule/deserialization.rb +1 -1
  119. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +1 -1
  120. data/lib/contrast/agent/protect/rule/no_sqli.rb +1 -1
  121. data/lib/contrast/agent/protect/rule/no_sqli/mongo_no_sql_scanner.rb +1 -0
  122. data/lib/contrast/agent/protect/rule/path_traversal.rb +4 -5
  123. data/lib/contrast/agent/protect/rule/sqli.rb +2 -2
  124. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +1 -1
  125. data/lib/contrast/agent/protect/rule/xss.rb +1 -1
  126. data/lib/contrast/agent/protect/rule/xxe.rb +3 -5
  127. data/lib/contrast/agent/protect/rule/xxe/entity_wrapper.rb +2 -2
  128. data/lib/contrast/agent/railtie.rb +1 -1
  129. data/lib/contrast/agent/reaction_processor.rb +2 -2
  130. data/lib/contrast/agent/request.rb +45 -43
  131. data/lib/contrast/agent/request_context.rb +10 -6
  132. data/lib/contrast/agent/request_handler.rb +1 -1
  133. data/lib/contrast/agent/response.rb +23 -12
  134. data/lib/contrast/agent/rewriter.rb +6 -9
  135. data/lib/contrast/agent/service_heartbeat.rb +2 -2
  136. data/lib/contrast/agent/static_analysis.rb +9 -9
  137. data/lib/contrast/agent/thread.rb +1 -1
  138. data/lib/contrast/agent/thread_watcher.rb +2 -2
  139. data/lib/contrast/agent/tracepoint_hook.rb +2 -2
  140. data/lib/contrast/agent/version.rb +1 -1
  141. data/lib/contrast/api.rb +4 -4
  142. data/lib/contrast/api/communication.rb +9 -9
  143. data/lib/contrast/api/communication/messaging_queue.rb +3 -6
  144. data/lib/contrast/api/communication/response_processor.rb +1 -1
  145. data/lib/contrast/api/communication/socket_client.rb +41 -6
  146. data/lib/contrast/api/communication/speedracer.rb +1 -1
  147. data/lib/contrast/api/communication/tcp_socket.rb +1 -1
  148. data/lib/contrast/api/communication/unix_socket.rb +1 -1
  149. data/lib/contrast/api/decorators.rb +17 -14
  150. data/lib/contrast/api/decorators/address.rb +20 -20
  151. data/lib/contrast/api/decorators/application_settings.rb +3 -2
  152. data/lib/contrast/api/decorators/application_update.rb +7 -8
  153. data/lib/contrast/api/decorators/http_request.rb +13 -12
  154. data/lib/contrast/api/decorators/input_analysis.rb +3 -2
  155. data/lib/contrast/api/decorators/library.rb +53 -0
  156. data/lib/contrast/api/decorators/library_usage_update.rb +30 -0
  157. data/lib/contrast/api/decorators/message.rb +4 -2
  158. data/lib/contrast/api/decorators/rasp_rule_sample.rb +2 -1
  159. data/lib/contrast/api/decorators/route_coverage.rb +3 -2
  160. data/lib/contrast/api/decorators/server_features.rb +3 -2
  161. data/lib/contrast/api/decorators/trace_event.rb +28 -25
  162. data/lib/contrast/api/decorators/trace_event_object.rb +6 -5
  163. data/lib/contrast/api/decorators/trace_event_signature.rb +5 -4
  164. data/lib/contrast/api/decorators/trace_taint_range.rb +4 -3
  165. data/lib/contrast/api/decorators/user_input.rb +4 -4
  166. data/lib/contrast/common_agent_configuration.rb +2 -2
  167. data/lib/contrast/components/agent.rb +8 -7
  168. data/lib/contrast/components/app_context.rb +50 -39
  169. data/lib/contrast/components/config.rb +32 -50
  170. data/lib/contrast/components/contrast_service.rb +10 -10
  171. data/lib/contrast/components/interface.rb +39 -17
  172. data/lib/contrast/components/inventory.rb +6 -1
  173. data/lib/contrast/components/logger.rb +1 -1
  174. data/lib/contrast/components/scope.rb +3 -3
  175. data/lib/contrast/components/settings.rb +20 -23
  176. data/lib/contrast/config.rb +18 -18
  177. data/lib/contrast/config/application_configuration.rb +5 -2
  178. data/lib/contrast/config/base_configuration.rb +2 -2
  179. data/lib/contrast/config/inventory_configuration.rb +2 -2
  180. data/lib/contrast/config/protect_rule_configuration.rb +1 -1
  181. data/lib/contrast/config/service_configuration.rb +8 -0
  182. data/lib/contrast/configuration.rb +93 -52
  183. data/lib/contrast/extension/assess.rb +21 -22
  184. data/lib/contrast/extension/assess/array.rb +18 -11
  185. data/lib/contrast/extension/assess/erb.rb +11 -3
  186. data/lib/contrast/extension/assess/eval_trigger.rb +7 -7
  187. data/lib/contrast/extension/assess/exec_trigger.rb +2 -2
  188. data/lib/contrast/extension/assess/fiber.rb +14 -14
  189. data/lib/contrast/extension/assess/hash.rb +7 -6
  190. data/lib/contrast/extension/assess/kernel.rb +34 -28
  191. data/lib/contrast/extension/assess/marshal.rb +67 -0
  192. data/lib/contrast/extension/assess/regexp.rb +10 -9
  193. data/lib/contrast/extension/assess/string.rb +23 -23
  194. data/lib/contrast/extension/inventory.rb +4 -4
  195. data/lib/contrast/extension/kernel.rb +1 -1
  196. data/lib/contrast/extension/module.rb +1 -1
  197. data/lib/contrast/extension/protect.rb +3 -3
  198. data/lib/contrast/extension/protect/kernel.rb +4 -4
  199. data/lib/contrast/extension/protect/psych.rb +2 -2
  200. data/lib/contrast/framework/base_support.rb +1 -1
  201. data/lib/contrast/framework/manager.rb +10 -11
  202. data/lib/contrast/framework/rack/patch/session_cookie.rb +23 -29
  203. data/lib/contrast/framework/rack/patch/support.rb +1 -1
  204. data/lib/contrast/framework/rack/support.rb +2 -2
  205. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +13 -13
  206. data/lib/contrast/framework/rails/patch/assess_configuration.rb +7 -13
  207. data/lib/contrast/framework/rails/patch/rails_application_configuration.rb +11 -11
  208. data/lib/contrast/framework/rails/patch/support.rb +4 -4
  209. data/lib/contrast/framework/rails/rewrite/action_controller_railties_helper_inherited.rb +11 -11
  210. data/lib/contrast/framework/rails/rewrite/active_record_attribute_methods_read.rb +12 -12
  211. data/lib/contrast/framework/rails/rewrite/active_record_named.rb +4 -4
  212. data/lib/contrast/framework/rails/rewrite/active_record_time_zone_inherited.rb +12 -12
  213. data/lib/contrast/framework/rails/support.rb +67 -14
  214. data/lib/contrast/framework/sinatra/patch/base.rb +12 -12
  215. data/lib/contrast/framework/sinatra/patch/support.rb +1 -1
  216. data/lib/contrast/framework/sinatra/support.rb +6 -6
  217. data/lib/contrast/funchook/funchook.rb +1 -1
  218. data/lib/contrast/logger/application.rb +13 -5
  219. data/lib/contrast/logger/format.rb +22 -9
  220. data/lib/contrast/logger/log.rb +17 -10
  221. data/lib/contrast/logger/request.rb +30 -0
  222. data/lib/contrast/tasks/config.rb +1 -1
  223. data/lib/contrast/tasks/service.rb +2 -2
  224. data/lib/contrast/utils/assess/sampling_util.rb +2 -2
  225. data/lib/contrast/utils/assess/tracking_util.rb +49 -4
  226. data/lib/contrast/utils/class_util.rb +2 -2
  227. data/lib/contrast/utils/duck_utils.rb +0 -10
  228. data/lib/contrast/utils/env_configuration_item.rb +2 -1
  229. data/lib/contrast/utils/hash_digest.rb +2 -1
  230. data/lib/contrast/utils/heap_dump_util.rb +2 -2
  231. data/lib/contrast/utils/invalid_configuration_util.rb +21 -22
  232. data/lib/contrast/utils/inventory_util.rb +3 -10
  233. data/lib/contrast/utils/io_util.rb +1 -1
  234. data/lib/contrast/utils/os.rb +1 -1
  235. data/lib/contrast/utils/ruby_ast_rewriter.rb +1 -1
  236. data/lib/contrast/utils/sha256_builder.rb +2 -14
  237. data/lib/contrast/utils/stack_trace_utils.rb +2 -2
  238. data/lib/contrast/utils/string_utils.rb +11 -6
  239. data/resources/assess/policy.json +31 -22
  240. data/ruby-agent.gemspec +21 -19
  241. data/service_executables/VERSION +1 -1
  242. data/service_executables/linux/contrast-service +0 -0
  243. data/service_executables/mac/contrast-service +0 -0
  244. metadata +73 -30
  245. data/lib/contrast/agent/assess/insulator.rb +0 -49
  246. data/lib/contrast/agent/require_state.rb +0 -61
  247. data/lib/contrast/extension/assess/assess_extension.rb +0 -147
  248. data/lib/contrast/utils/boolean_util.rb +0 -30
  249. data/lib/contrast/utils/freeze_util.rb +0 -32
  250. data/lib/contrast/utils/gemfile_reader.rb +0 -193
@@ -17,11 +17,11 @@ module Contrast
17
17
  propagation_node.sources.each do |source|
18
18
  case source
19
19
  when Contrast::Utils::ObjectShare::OBJECT_KEY
20
- tracked_inputs << preshift.object if preshift.object.cs__tracked?
20
+ tracked_inputs << preshift.object if Contrast::Agent::Assess::Tracker.tracked?(preshift.object)
21
21
  else
22
22
  arg = preshift.args[source]
23
23
  if arg.is_a?(String)
24
- tracked_inputs << arg if arg.cs__tracked?
24
+ tracked_inputs << arg if Contrast::Agent::Assess::Tracker.tracked?(arg)
25
25
  elsif Contrast::Utils::DuckUtils.iterable_hash?(arg)
26
26
  arg.each_pair do |key, value|
27
27
  tracked_inputs << key if tracked_value?(key)
@@ -36,19 +36,20 @@ module Contrast
36
36
  end
37
37
 
38
38
  splat_tags(tracked_inputs, target)
39
- target.cs__properties.cleanup_tags
39
+ Contrast::Agent::Assess::Tracker.properties(target).cleanup_tags
40
40
  end
41
41
 
42
42
  def splat_tags tracked_inputs, target
43
43
  return if tracked_inputs.empty?
44
44
 
45
+ properties = Contrast::Agent::Assess::Tracker.properties(target)
46
+ return unless properties
47
+
45
48
  tracked_inputs.each do |input|
46
- unless input == target
47
- input.cs__properties.events.each do |event|
48
- target.cs__properties.events << event
49
- end
50
- end
51
- input.cs__splat_tags(target)
49
+ input_properties = Contrast::Agent::Assess::Tracker.properties(input)
50
+ next unless input_properties
51
+
52
+ properties.splat_from(input, target)
52
53
  end
53
54
  end
54
55
  end
@@ -1,9 +1,9 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- cs__scoped_require 'contrast/agent/assess/policy/preshift'
5
- cs__scoped_require 'contrast/components/interface'
6
- cs__scoped_require 'contrast/utils/thread_tracker'
4
+ require 'contrast/agent/assess/policy/preshift'
5
+ require 'contrast/components/interface'
6
+ require 'contrast/utils/thread_tracker'
7
7
 
8
8
  module Contrast
9
9
  module Agent
@@ -36,6 +36,8 @@ module Contrast
36
36
  target_id: target.__id__)
37
37
  unless target.is_a?(Array)
38
38
  Contrast::Agent::Assess::Policy::Propagator::Keep.propagate(propagation_node, preshift, target)
39
+ properties = Contrast::Agent::Assess::Tracker.properties(target)
40
+ properties.build_event(propagation_node, target, object, ret, args)
39
41
  return
40
42
  end
41
43
 
@@ -53,17 +55,17 @@ module Contrast
53
55
  target.each do |elem|
54
56
  elem_length = elem.length
55
57
  range = current_index...(current_index + elem_length)
56
- tags = source.cs__properties.tags_at_range(range)
58
+ elem_properties = Contrast::Agent::Assess::Tracker.properties(elem)
59
+ next unless elem_properties
57
60
 
58
- elem.cs__properties.clear_tags
61
+ source_properties = Contrast::Agent::Assess::Tracker.properties(source)
62
+ tags = source_properties.tags_at_range(range)
63
+ elem_properties.clear_tags
59
64
  tags.each_pair do |key, value|
60
- elem.cs__properties.set_tags(key, value)
65
+ elem_properties.set_tags(key, value)
61
66
  end
62
- source.cs__properties.events.each do |event|
63
- elem.cs__properties.add_event(event)
64
- end
65
- elem.cs__properties.build_event(propagation_node, elem, preshift.object, target, preshift.args, 0)
66
- elem.cs__properties.add_properties(propagation_node.properties)
67
+ elem_properties.build_event(propagation_node, elem, preshift.object, target, preshift.args, 0)
68
+ elem_properties.add_properties(propagation_node.properties)
67
69
  current_index = current_index + elem_length + separator_length
68
70
  end
69
71
  nil
@@ -125,8 +127,11 @@ module Contrast
125
127
  index = SPLIT_TRACKER.get(:split_index)&.fetch(depth)
126
128
  return unless index
127
129
 
130
+ properties = Contrast::Agent::Assess::Tracker.properties(target)
131
+ return unless properties
132
+
128
133
  true_source = source[index]
129
- target.cs__copy_from(true_source)
134
+ properties.copy_from(true_source, target)
130
135
  rescue StandardError => e
131
136
  logger.warn('Unable to track within split context', e)
132
137
  ensure
@@ -139,12 +144,12 @@ module Contrast
139
144
  def instrument_string_split
140
145
  if @_instrument_string_split.nil?
141
146
  @_instrument_string_split = begin
142
- cs__scoped_require 'cs__assess_yield_track/cs__assess_yield_track' if AGENT.patch_yield? && Funchook.available?
143
- true
144
- rescue StandardError => e
145
- logger.error('Error loading split rb_yield patch', e)
146
- false
147
- end
147
+ require 'cs__assess_yield_track/cs__assess_yield_track' if AGENT.patch_yield? && Funchook.available?
148
+ true
149
+ rescue StandardError => e
150
+ logger.error('Error loading split rb_yield patch', e)
151
+ false
152
+ end
148
153
  end
149
154
  @_instrument_string_split
150
155
  end
@@ -191,10 +196,10 @@ module Contrast
191
196
  # String#split node
192
197
  def split_node
193
198
  @_split_node ||= begin
194
- Contrast::Agent::Assess::Policy::Policy.instance.propagators.find do |node|
195
- node.class_name == 'String' && node.method_name == :split && node.instance_method?
196
- end
197
- end
199
+ Contrast::Agent::Assess::Policy::Policy.instance.propagators.find do |node|
200
+ node.class_name == 'String' && node.method_name == :split && node.instance_method?
201
+ end
202
+ end
198
203
  end
199
204
  end
200
205
  end
@@ -1,8 +1,8 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- cs__scoped_require 'contrast/components/interface'
5
- cs__scoped_require 'contrast/utils/duck_utils'
4
+ require 'contrast/components/interface'
5
+ require 'contrast/utils/duck_utils'
6
6
 
7
7
  module Contrast
8
8
  module Agent
@@ -46,22 +46,28 @@ module Contrast
46
46
 
47
47
  begin
48
48
  source = preshift.object
49
- self_tracked = Contrast::Utils::DuckUtils.quacks_to?(source, :cs__tracked?) &&
50
- source.cs__tracked?
49
+ self_tracked = Contrast::Agent::Assess::Tracker.tracked?(source)
51
50
  args = preshift.args[1]
52
51
  incoming_tracked = args && determine_tracked(args)
53
52
  return ret unless self_tracked || incoming_tracked
54
53
 
54
+ parent_events = []
55
55
  if block
56
56
  block_sub(self_tracked, source, ret)
57
57
  elsif args.is_a?(String)
58
- string_sub(self_tracked, preshift, ret, args, incoming_tracked, global)
58
+ string_sub(parent_events, self_tracked, preshift, ret, args, incoming_tracked, global)
59
59
  elsif args.is_a?(Hash)
60
60
  hash_sub(self_tracked, source, ret)
61
61
  else # Enumerator, only for gsub
62
- pattern_gsub(preshift, ret)
62
+ pattern_gsub(parent_events, preshift, ret)
63
63
  end
64
- string_build_event(patcher, preshift, ret)
64
+
65
+ if self_tracked
66
+ source_properties = Contrast::Agent::Assess::Tracker.properties(source)
67
+ parent_event = source_properties&.event
68
+ parent_events.prepend(parent_event) if parent_event
69
+ end
70
+ string_build_event(parent_events, patcher, preshift, ret)
65
71
  rescue StandardError => e
66
72
  logger.error('Unable to apply gsub propagator', e)
67
73
  end
@@ -73,18 +79,26 @@ module Contrast
73
79
  return false unless args
74
80
 
75
81
  # if it's a string, just ask if it's tracked
76
- if args.is_a?(String)
77
- Contrast::Utils::DuckUtils.quacks_to?(args, :cs__tracked?) && args.cs__tracked?
82
+ case args
83
+ when String
84
+ Contrast::Agent::Assess::Tracker.tracked?(args)
78
85
  # if it's a hash, ask if it has a tracked string
79
- elsif args.is_a?(Hash)
80
- args.values.any? { |value| value.is_a?(String) && Contrast::Utils::DuckUtils.quacks_to?(value, :cs__tracked?) && value.cs__tracked? }
86
+ when Hash
87
+ args.values.any? { |value| value.is_a?(String) && Contrast::Agent::Assess::Tracker.tracked?(value) }
81
88
  # this should never happen
82
89
  else
83
90
  false
84
91
  end
85
92
  end
86
93
 
87
- def string_sub self_tracked, preshift, ret, incoming, incoming_tracked, global
94
+ def string_sub parent_events, self_tracked, preshift, ret, incoming, incoming_tracked, global
95
+ properties = Contrast::Agent::Assess::Tracker.properties(ret)
96
+ return unless properties
97
+
98
+ incoming_properties = Contrast::Agent::Assess::Tracker.properties(incoming)
99
+ parent_event = incoming_properties&.event
100
+ parent_events << parent_event if parent_event
101
+
88
102
  pattern = preshift.args[0]
89
103
  source = preshift.object
90
104
 
@@ -92,13 +106,13 @@ module Contrast
92
106
  # copied from regexp / captures. Trading accuracy for
93
107
  # performance
94
108
  if incoming.match?(CAPTURE_GROUP_REGEXP) || incoming.match?(CAPTURE_NAME_REGEXP)
95
- source.cs__splat_tags(ret) if self_tracked
109
+ properties.splat_from(source, ret) if self_tracked
96
110
  return
97
111
  end
98
112
 
99
113
  # if it's just a straight insert, that we can do
100
114
  # Copy the tags from us to the return
101
- ret.cs__copy_from(source)
115
+ properties.copy_from(source, ret)
102
116
  # Figure out where inserts occurred
103
117
  last_idx = 0
104
118
  ranges = []
@@ -113,54 +127,57 @@ module Contrast
113
127
  ranges << (start_index...end_index)
114
128
  break unless global
115
129
  end
116
- ret.cs__properties.delete_tags_at_ranges(ranges)
117
- ret.cs__properties.shift_tags(ranges)
130
+ properties.delete_tags_at_ranges(ranges)
131
+ properties.shift_tags(ranges)
118
132
  return unless incoming_tracked
133
+ return unless incoming_properties
119
134
 
120
- tags = incoming.cs__properties.tag_keys
135
+ tags = incoming_properties.tag_keys
121
136
  ranges.each do |range|
122
137
  tags.each do |tag|
123
- ret.cs__properties.add_tag(tag, range)
138
+ properties.add_tag(tag, range)
124
139
  end
125
140
  end
126
141
  end
127
142
 
128
143
  def block_sub self_tracked, source, ret
129
- source.cs__splat_tags(ret) if self_tracked
144
+ properties = Contrast::Agent::Assess::Tracker.properties(ret)
145
+ properties&.splat_from(source, ret) if self_tracked
130
146
  end
131
147
 
132
148
  def hash_sub self_tracked, source, ret
133
- source.cs__splat_tags(ret) if self_tracked
149
+ properties = Contrast::Agent::Assess::Tracker.properties(ret)
150
+ properties&.splat_from(source, ret) if self_tracked
134
151
  end
135
152
 
136
- def pattern_gsub preshift, ret
137
- return unless Contrast::Utils::DuckUtils.trackable?(ret)
153
+ def pattern_gsub parent_events, preshift, ret
154
+ properties = Contrast::Agent::Assess::Tracker.properties(ret)
155
+ return unless properties
138
156
 
139
157
  source = preshift.object
140
- source.cs__properties.tag_keys.each do |key|
141
- ret.cs__properties.add_tag(key, 0...1)
158
+ source_properties = Contrast::Agent::Assess::Tracker.properties(source)
159
+ return unless source_properties
160
+
161
+ source_properties.tag_keys.each do |key|
162
+ properties.add_tag(key, 0...1)
142
163
  end
164
+ parent_event = source_properties.event
165
+ parent_events << parent_event if parent_event
143
166
  end
144
167
 
145
- def string_build_event patcher, preshift, ret
146
- return unless Contrast::Utils::DuckUtils.quacks_to?(ret, :cs__tracked?) && ret.cs__tracked?
168
+ def string_build_event parent_events, patcher, preshift, ret
169
+ return unless Contrast::Agent::Assess::Tracker.tracked?(ret)
147
170
 
171
+ properties = Contrast::Agent::Assess::Tracker.properties(ret)
148
172
  args = preshift.args
149
- if args.length > 1
150
- arg = args[1]
151
- if Contrast::Utils::DuckUtils.quacks_to?(arg, :cs__properties)
152
- arg.cs__properties.events.each do |event|
153
- ret.cs__properties.events << event
154
- end
155
- end
156
- end
157
- ret.cs__properties.build_event(
173
+ properties.build_event(
158
174
  patcher,
159
175
  ret,
160
176
  preshift.object,
161
177
  ret,
162
178
  args,
163
179
  2)
180
+ properties.event.instance_variable_set(:@_parent_events, parent_events)
164
181
  end
165
182
  end
166
183
  end
@@ -16,9 +16,12 @@ module Contrast
16
16
  def tr_tagger patcher, preshift, ret, _block
17
17
  return ret unless ret && !ret.empty?
18
18
 
19
+ properties = Contrast::Agent::Assess::Tracker.properties(ret)
20
+ return unless properties
21
+
19
22
  source = preshift.object
20
23
  args = preshift.args
21
- ret.cs__copy_from(source)
24
+ properties.copy_from(source, ret)
22
25
  replace_string = args[1]
23
26
  source_chars = source.chars
24
27
  # if the replace string is empty, then there's a bunch of deletes. this
@@ -41,10 +44,10 @@ module Contrast
41
44
  end
42
45
  # account for the last char being different
43
46
  remove_ranges << (start...source_chars.length) if start
44
- ret.cs__properties.delete_tags_at_ranges(remove_ranges, false)
47
+ properties.delete_tags_at_ranges(remove_ranges, false)
45
48
  end
46
49
 
47
- ret.cs__properties.build_event(
50
+ properties.build_event(
48
51
  patcher,
49
52
  ret,
50
53
  source,
@@ -57,10 +60,13 @@ module Contrast
57
60
  def tr_s_tagger patcher, preshift, ret, _block
58
61
  return unless ret && !ret.empty?
59
62
 
63
+ properties = Contrast::Agent::Assess::Tracker.properties(ret)
64
+ return unless properties
65
+
60
66
  source = preshift.object
61
67
  args = preshift.args
62
- source.cs__splat_tags(ret)
63
- ret.cs__properties.build_event(
68
+ properties.splat_from(source, ret)
69
+ properties.build_event(
64
70
  patcher,
65
71
  ret,
66
72
  source,
@@ -1,11 +1,11 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- cs__scoped_require 'contrast/utils/object_share'
5
- cs__scoped_require 'contrast/agent/patching/policy/patch_status'
6
- cs__scoped_require 'contrast/agent/module_data'
7
- cs__scoped_require 'contrast/agent/rewriter'
8
- cs__scoped_require 'contrast/components/interface'
4
+ require 'contrast/agent/patching/policy/patch_status'
5
+ require 'contrast/agent/module_data'
6
+ require 'contrast/agent/rewriter'
7
+ require 'contrast/components/interface'
8
+ require 'contrast/utils/object_share'
9
9
 
10
10
  module Contrast
11
11
  module Agent
@@ -1,20 +1,11 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- # This class is responsible for the origination of traces. A Source is any method
5
- # that returns an untrusted value. In general, these sources are request methods
6
- # as the request object is user controlled.
7
- #
8
- # Going forward, we may add in Dynamic sources (determined at runtime) based on
9
- # database configs or other variables (used for Stored XSS or other persisted
10
- # vulnerability detection rules)
11
- cs__scoped_require 'set'
12
-
13
- cs__scoped_require 'contrast/utils/object_share'
14
- cs__scoped_require 'contrast/utils/sha256_builder'
15
- cs__scoped_require 'contrast/agent/assess/policy/source_validation/source_validation'
16
-
17
- cs__scoped_require 'contrast/components/interface'
4
+ require 'set'
5
+ require 'contrast/agent/assess/policy/source_validation/source_validation'
6
+ require 'contrast/components/interface'
7
+ require 'contrast/utils/object_share'
8
+ require 'contrast/utils/sha256_builder'
18
9
 
19
10
  module Contrast
20
11
  module Agent
@@ -26,7 +17,7 @@ module Contrast
26
17
  # dataflows used in Assess vulnerability detection.
27
18
  module SourceMethod
28
19
  include Contrast::Components::Interface
29
- access_component :logging, :analysis
20
+ access_component :analysis, :logging
30
21
 
31
22
  PARAMETER_TYPE = 'PARAMETER'
32
23
  PARAMETER_KEY_TYPE = 'PARAMETER_KEY'
@@ -53,38 +44,26 @@ module Contrast
53
44
  current_context = Contrast::Agent::REQUEST_TRACKER.current
54
45
  return unless current_context&.analyze_request? && ASSESS.enabled?
55
46
 
56
- replaced_return = nil
57
47
  source_node = method_policy.source_node
58
-
59
48
  target = determine_target(source_node, object, ret, args)
49
+ restore_frozen_state = false
50
+ if target.cs__frozen? && !Contrast::Agent::Assess::Tracker.trackable?(target)
51
+ return unless ASSESS.track_frozen_sources?
52
+ return unless source_node.targets[0] == Contrast::Utils::ObjectShare::RETURN_KEY
60
53
 
61
- # We don't propagate to frozen things that haven't been tracked
62
- # before. By definition, something that is a source has not
63
- # previously been tracked; therefore, we can break out early.
64
- if target.cs__frozen?
65
- # That being said, we don't have enough context to know if we
66
- # can make this assumption and still function, so we'll allow for
67
- # source tracking of frozen things by a common config setting.
68
- #
69
- # Rails' StrongParameters make a case for this to be default
70
- # behavior
71
- return replaced_return unless ASSESS.track_frozen_sources?
72
-
73
- # If we're tracking the frozen target, we need to unfreeze
74
- # (dup) it to track and then freeze that result. For
75
- # simplicities sake, we ONLY do this if the return is the
76
- # target (I don't want to have to deal with unfreezing self)
77
- return replaced_return unless source_node.targets[0] == Contrast::Utils::ObjectShare::RETURN_KEY
54
+ dup = safe_dup(ret)
55
+ return unless dup
78
56
 
79
57
  restore_frozen_state = true
80
- ret = Contrast::Utils::FreezeUtil.unfreeze_dup(ret)
58
+ ret = dup
81
59
  target = ret
60
+ Contrast::Agent::Assess::Tracker.pre_freeze(ret)
61
+ ret.cs__freeze
62
+ # double check that we were able to finalize the replaced return
63
+ return unless Contrast::Agent::Assess::Tracker.trackable?(target)
82
64
  end
83
-
84
65
  apply_source(current_context, source_node, target, object, ret, source_node.type, nil, *args)
85
-
86
- ret.cs__freeze if restore_frozen_state
87
- ret
66
+ restore_frozen_state ? ret : nil
88
67
  end
89
68
 
90
69
  private
@@ -112,34 +91,10 @@ module Contrast
112
91
  source_name ||= determine_source_name(source_node, object, ret, *args)
113
92
  # We know we only work on certain things.
114
93
  # Skip if this isn't one of them
115
- if Contrast::Utils::DuckUtils.quacks_to?(target, :cs__properties)
94
+ if Contrast::Agent::Assess::Tracker.trackable?(target)
116
95
  apply_tags(source_node, target, object, ret, source_type, source_name, *args)
117
- # While we don't taint hashes themselves, we may taint the things
118
- # they hold. Let's pass their keys and values back to ourselves and
119
- # try again
120
96
  elsif Contrast::Utils::DuckUtils.iterable_hash?(target)
121
- to_replace = []
122
- target.each_pair do |key, value|
123
- # We only do this for Strings b/c of the way Hash lookup works.
124
- # To replace another object would break hash lookup and,
125
- # therefore, the application
126
- if can_track_hash_key?(key, target)
127
- key = Contrast::Utils::FreezeUtil.unfreeze_dup(key)
128
- to_replace << key
129
- end
130
- apply_source(context, source_node, key, object, ret, key_type(source_type), key, *args)
131
- apply_source(context, source_node, value, object, ret, source_type, key, *args)
132
- end
133
-
134
- # Hash is designed to keep one instance of the string key in it.
135
- # We need to remove the existing one and replace it with our new
136
- # tracked one.
137
- to_replace.each do |key|
138
- key.cs__freeze
139
- value = target[key]
140
- target.delete(key)
141
- target[key] = value
142
- end
97
+ apply_hash_tags(context, source_node, target, object, ret, source_type, *args)
143
98
  # While we don't taint arrays themselves, we may taint the things
144
99
  # they hold. Let's pass their keys and values back to ourselves and
145
100
  # try again
@@ -150,17 +105,81 @@ module Contrast
150
105
  logger.warn('Unable to apply source', e, node_id: source_node.id)
151
106
  end
152
107
 
153
- def can_track_hash_key? key, target
108
+ # While we don't taint hashes themselves, we may taint the things
109
+ # they hold. Let's pass their keys and values back to ourselves and
110
+ # try again
111
+ #
112
+ # @param context [Contrast::Utils::ThreadTracker] the current request
113
+ # context
114
+ # @param source_node [Contrast::Agent::Assess::Policy::SourceNode]
115
+ # the node to direct applying this source event
116
+ # @param target [Object] the target of the Source Event
117
+ # @param object [Object] the Object on which the method was invoked
118
+ # @param ret [Object] the Return of the invoked method
119
+ # @param source_type [String] the type of this source, from the
120
+ # source_node, or a KEY_TYPE if invoked for a map
121
+ # @param args [Array<Object>] the Arguments with which the method
122
+ # was invoked
123
+ def apply_hash_tags context, source_node, target, object, ret, source_type, *args
124
+ to_replace = []
125
+ target.each_pair do |key, value|
126
+ # We only do this for Strings b/c of the way Hash lookup works.
127
+ # To replace another object would break hash lookup and,
128
+ # therefore, the application
129
+ if replace_hash_key?(key, target)
130
+ key = key.dup
131
+ to_replace << key
132
+ end
133
+ apply_source(context, source_node, key, object, ret, key_type(source_type), key, *args)
134
+ apply_source(context, source_node, value, object, ret, source_type, key, *args)
135
+ end
136
+ handle_hash_key(target, to_replace)
137
+ end
138
+
139
+ # Given an unfrozen hash, if the key is a String, we should replace
140
+ # it with one that we can finalize, allowing us to track that key.
141
+ # This method handles checking if that replace can and should
142
+ # occur.
143
+ #
144
+ # @param key [Object] the key in the hash that may need replacing.
145
+ # @param hash [Hash] the hash to which the key belongs.
146
+ # @return [Boolean] whether replace the key in the hash or not.
147
+ def replace_hash_key? key, hash
154
148
  ASSESS.track_frozen_sources? &&
149
+ !hash.cs__frozen? &&
155
150
  key.is_a?(String) &&
156
- Contrast::Utils::DuckUtils.quacks_to?(target, :delete)
151
+ !Contrast::Agent::Assess::Tracker.trackable?(key)
152
+ end
153
+
154
+ # Safely duplicate the target, or return nil
155
+ #
156
+ # @param target [Object] the thing to check for duplication
157
+ def safe_dup target
158
+ target.dup
159
+ rescue StandardError => _e
160
+ nil
161
+ end
162
+
163
+ # Hash is designed to keep one instance of the string key in it.
164
+ # We need to remove the existing one and replace it with our new
165
+ # tracked one.
166
+ def handle_hash_key target, to_replace
167
+ to_replace.each do |key|
168
+ Contrast::Agent::Assess::Tracker.pre_freeze(key)
169
+ key.cs__freeze
170
+ value = target.delete(key)
171
+ target[key] = value
172
+ end
157
173
  end
158
174
 
159
175
  def apply_tags source_node, target, object, ret, source_type, source_name, *args
176
+ # don't apply tags if we can't track the thing
177
+ return unless Contrast::Agent::Assess::Tracker.trackable?(target)
160
178
  # don't apply second source -- probably needs tuning later if we
161
179
  # use more than 'UNTRUSTED' in our sources
162
- return if target.cs__tracked? || target.cs__frozen?
180
+ return if Contrast::Agent::Assess::Tracker.tracked?(target)
163
181
 
182
+ properties = Contrast::Agent::Assess::Tracker.properties(target)
164
183
  # otherwise for each tag this source_node applies, create a tag range
165
184
  # on the target object
166
185
  # I realize this looping is counter-intuitive from the above
@@ -169,16 +188,15 @@ module Contrast
169
188
  next unless Contrast::Agent::Assess::Policy::SourceValidation.valid?(tag, source_type, source_name)
170
189
 
171
190
  length = Contrast::Utils::StringUtils.ret_length(target)
172
- target.cs__properties.add_tag(tag, 0...length)
173
- target.cs__properties.add_properties(source_node.properties)
191
+ properties.add_tag(tag, 0...length)
192
+ properties.add_properties(source_node.properties)
174
193
  logger.trace('Source detected',
175
194
  node_id: source_node.id,
176
195
  target_id: target.__id__,
177
196
  tag: tag)
178
197
  end
179
-
180
198
  # make a representation of this method that TeamServer can render
181
- target.cs__properties.build_event(source_node, target, object, ret, args, source_type, source_name)
199
+ properties.build_event(source_node, target, object, ret, args, source_type, source_name)
182
200
  end
183
201
 
184
202
  # Find the name of the source