contrast-agent 4.6.0 → 4.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (190) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +6 -1
  3. data/.gitmodules +1 -1
  4. data/.simplecov +1 -0
  5. data/Rakefile +1 -2
  6. data/ext/build_funchook.rb +3 -3
  7. data/ext/extconf_common.rb +1 -5
  8. data/lib/contrast.rb +24 -14
  9. data/lib/contrast/agent/assess.rb +1 -1
  10. data/lib/contrast/agent/assess/contrast_event.rb +1 -4
  11. data/lib/contrast/agent/assess/contrast_object.rb +2 -2
  12. data/lib/contrast/agent/assess/events/event_factory.rb +2 -1
  13. data/lib/contrast/agent/assess/finalizers/hash.rb +2 -4
  14. data/lib/contrast/agent/assess/policy/dynamic_source_factory.rb +6 -3
  15. data/lib/contrast/agent/assess/policy/patcher.rb +16 -21
  16. data/lib/contrast/agent/assess/policy/policy.rb +1 -1
  17. data/lib/contrast/agent/assess/policy/policy_node.rb +25 -33
  18. data/lib/contrast/agent/assess/policy/policy_scanner.rb +3 -5
  19. data/lib/contrast/agent/assess/policy/preshift.rb +7 -5
  20. data/lib/contrast/agent/assess/policy/propagation_method.rb +10 -19
  21. data/lib/contrast/agent/assess/policy/propagation_node.rb +19 -8
  22. data/lib/contrast/agent/assess/policy/propagator.rb +1 -0
  23. data/lib/contrast/agent/assess/policy/propagator/center.rb +2 -1
  24. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +3 -6
  25. data/lib/contrast/agent/assess/policy/propagator/insert.rb +3 -1
  26. data/lib/contrast/agent/assess/policy/propagator/match_data.rb +2 -1
  27. data/lib/contrast/agent/assess/policy/propagator/rack_protection.rb +73 -0
  28. data/lib/contrast/agent/assess/policy/propagator/select.rb +2 -12
  29. data/lib/contrast/agent/assess/policy/propagator/split.rb +12 -13
  30. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +3 -10
  31. data/lib/contrast/agent/assess/policy/propagator/trim.rb +3 -15
  32. data/lib/contrast/agent/assess/policy/rewriter_patch.rb +13 -10
  33. data/lib/contrast/agent/assess/policy/source_method.rb +12 -12
  34. data/lib/contrast/agent/assess/policy/source_validation/source_validation.rb +1 -3
  35. data/lib/contrast/agent/assess/policy/trigger/reflected_xss.rb +5 -1
  36. data/lib/contrast/agent/assess/policy/trigger/xpath.rb +0 -3
  37. data/lib/contrast/agent/assess/policy/trigger_method.rb +8 -18
  38. data/lib/contrast/agent/assess/policy/trigger_node.rb +3 -2
  39. data/lib/contrast/agent/assess/policy/trigger_validation/redos_validator.rb +4 -3
  40. data/lib/contrast/agent/assess/policy/trigger_validation/ssrf_validator.rb +1 -2
  41. data/lib/contrast/agent/assess/policy/trigger_validation/xss_validator.rb +1 -8
  42. data/lib/contrast/agent/assess/property/evented.rb +8 -5
  43. data/lib/contrast/agent/assess/rule/provider/hardcoded_key.rb +11 -5
  44. data/lib/contrast/agent/assess/rule/provider/hardcoded_password.rb +4 -1
  45. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +7 -9
  46. data/lib/contrast/agent/at_exit_hook.rb +3 -3
  47. data/lib/contrast/agent/class_reopener.rb +9 -6
  48. data/lib/contrast/agent/disable_reaction.rb +4 -7
  49. data/lib/contrast/agent/exclusion_matcher.rb +7 -14
  50. data/lib/contrast/agent/inventory/dependencies.rb +2 -0
  51. data/lib/contrast/agent/inventory/dependency_analysis.rb +2 -6
  52. data/lib/contrast/agent/inventory/dependency_usage_analysis.rb +3 -5
  53. data/lib/contrast/agent/inventory/policy/datastores.rb +3 -4
  54. data/lib/contrast/agent/inventory/policy/policy.rb +1 -1
  55. data/lib/contrast/agent/middleware.rb +17 -18
  56. data/lib/contrast/agent/module_data.rb +3 -3
  57. data/lib/contrast/agent/patching/policy/after_load_patch.rb +3 -3
  58. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +9 -9
  59. data/lib/contrast/agent/patching/policy/method_policy.rb +6 -2
  60. data/lib/contrast/agent/patching/policy/module_policy.rb +14 -7
  61. data/lib/contrast/agent/patching/policy/patch.rb +20 -25
  62. data/lib/contrast/agent/patching/policy/patch_status.rb +6 -7
  63. data/lib/contrast/agent/patching/policy/patcher.rb +21 -18
  64. data/lib/contrast/agent/patching/policy/policy.rb +2 -4
  65. data/lib/contrast/agent/patching/policy/policy_node.rb +16 -7
  66. data/lib/contrast/agent/patching/policy/trigger_node.rb +21 -8
  67. data/lib/contrast/agent/protect/policy/applies_command_injection_rule.rb +1 -1
  68. data/lib/contrast/agent/protect/policy/applies_deserialization_rule.rb +1 -1
  69. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +1 -1
  70. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +2 -3
  71. data/lib/contrast/agent/protect/policy/applies_sqli_rule.rb +1 -1
  72. data/lib/contrast/agent/protect/policy/applies_xxe_rule.rb +5 -9
  73. data/lib/contrast/agent/protect/policy/policy.rb +1 -1
  74. data/lib/contrast/agent/protect/policy/rule_applicator.rb +7 -9
  75. data/lib/contrast/agent/protect/rule/base.rb +20 -23
  76. data/lib/contrast/agent/protect/rule/base_service.rb +9 -5
  77. data/lib/contrast/agent/protect/rule/cmd_injection.rb +18 -23
  78. data/lib/contrast/agent/protect/rule/deserialization.rb +6 -13
  79. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +3 -14
  80. data/lib/contrast/agent/protect/rule/no_sqli.rb +6 -2
  81. data/lib/contrast/agent/protect/rule/no_sqli/mongo_no_sql_scanner.rb +1 -3
  82. data/lib/contrast/agent/protect/rule/path_traversal.rb +6 -10
  83. data/lib/contrast/agent/protect/rule/sqli.rb +1 -1
  84. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +1 -1
  85. data/lib/contrast/agent/protect/rule/xss.rb +1 -1
  86. data/lib/contrast/agent/protect/rule/xxe.rb +5 -12
  87. data/lib/contrast/agent/protect/rule/xxe/entity_wrapper.rb +1 -2
  88. data/lib/contrast/agent/reaction_processor.rb +13 -13
  89. data/lib/contrast/agent/request.rb +27 -26
  90. data/lib/contrast/agent/request_context.rb +17 -22
  91. data/lib/contrast/agent/request_handler.rb +5 -3
  92. data/lib/contrast/agent/response.rb +2 -3
  93. data/lib/contrast/agent/rewriter.rb +9 -6
  94. data/lib/contrast/agent/rule_set.rb +5 -4
  95. data/lib/contrast/agent/service_heartbeat.rb +4 -6
  96. data/lib/contrast/agent/static_analysis.rb +6 -5
  97. data/lib/contrast/agent/thread.rb +2 -4
  98. data/lib/contrast/agent/thread_watcher.rb +3 -4
  99. data/lib/contrast/agent/tracepoint_hook.rb +5 -5
  100. data/lib/contrast/agent/version.rb +1 -1
  101. data/lib/contrast/api/communication/messaging_queue.rb +4 -5
  102. data/lib/contrast/api/communication/response_processor.rb +11 -13
  103. data/lib/contrast/api/communication/service_lifecycle.rb +9 -6
  104. data/lib/contrast/api/communication/socket_client.rb +22 -31
  105. data/lib/contrast/api/communication/speedracer.rb +8 -13
  106. data/lib/contrast/api/decorators/address.rb +2 -3
  107. data/lib/contrast/api/decorators/agent_startup.rb +7 -9
  108. data/lib/contrast/api/decorators/application_startup.rb +12 -10
  109. data/lib/contrast/api/decorators/application_update.rb +0 -4
  110. data/lib/contrast/api/decorators/http_request.rb +3 -7
  111. data/lib/contrast/api/decorators/instrumentation_mode.rb +3 -5
  112. data/lib/contrast/api/decorators/library.rb +8 -6
  113. data/lib/contrast/api/decorators/message.rb +9 -9
  114. data/lib/contrast/api/decorators/trace_event.rb +3 -1
  115. data/lib/contrast/api/decorators/trace_event_object.rb +3 -6
  116. data/lib/contrast/api/decorators/trace_taint_range_tags.rb +1 -6
  117. data/lib/contrast/components/agent.rb +17 -17
  118. data/lib/contrast/components/app_context.rb +11 -15
  119. data/lib/contrast/components/assess.rb +16 -16
  120. data/lib/contrast/components/base.rb +40 -0
  121. data/lib/contrast/components/config.rb +2 -3
  122. data/lib/contrast/components/contrast_service.rb +12 -18
  123. data/lib/contrast/components/heap_dump.rb +5 -4
  124. data/lib/contrast/components/inventory.rb +2 -7
  125. data/lib/contrast/components/logger.rb +1 -2
  126. data/lib/contrast/components/protect.rb +10 -13
  127. data/lib/contrast/components/sampling.rb +13 -7
  128. data/lib/contrast/components/scope.rb +0 -4
  129. data/lib/contrast/components/settings.rb +5 -7
  130. data/lib/contrast/config/assess_rules_configuration.rb +1 -3
  131. data/lib/contrast/config/base_configuration.rb +4 -5
  132. data/lib/contrast/config/exception_configuration.rb +1 -5
  133. data/lib/contrast/config/heap_dump_configuration.rb +12 -6
  134. data/lib/contrast/config/logger_configuration.rb +1 -5
  135. data/lib/contrast/configuration.rb +6 -18
  136. data/lib/contrast/extension/assess/array.rb +3 -10
  137. data/lib/contrast/extension/assess/erb.rb +1 -7
  138. data/lib/contrast/extension/assess/eval_trigger.rb +4 -9
  139. data/lib/contrast/extension/assess/exec_trigger.rb +3 -9
  140. data/lib/contrast/extension/assess/fiber.rb +8 -17
  141. data/lib/contrast/extension/assess/hash.rb +3 -3
  142. data/lib/contrast/extension/assess/kernel.rb +4 -13
  143. data/lib/contrast/extension/assess/marshal.rb +6 -10
  144. data/lib/contrast/extension/assess/regexp.rb +6 -10
  145. data/lib/contrast/extension/assess/string.rb +8 -6
  146. data/lib/contrast/extension/kernel.rb +2 -2
  147. data/lib/contrast/extension/protect/kernel.rb +0 -5
  148. data/lib/contrast/framework/manager.rb +3 -5
  149. data/lib/contrast/framework/rack/patch/session_cookie.rb +11 -24
  150. data/lib/contrast/framework/rack/patch/support.rb +6 -4
  151. data/lib/contrast/framework/rails/patch/assess_configuration.rb +12 -9
  152. data/lib/contrast/framework/rails/patch/support.rb +41 -35
  153. data/lib/contrast/framework/rails/railtie.rb +34 -0
  154. data/lib/contrast/framework/rails/rewrite/action_controller_railties_helper_inherited.rb +4 -1
  155. data/lib/contrast/framework/rails/rewrite/active_record_attribute_methods_read.rb +2 -0
  156. data/lib/contrast/framework/rails/rewrite/active_record_named.rb +5 -4
  157. data/lib/contrast/framework/rails/rewrite/active_record_time_zone_inherited.rb +2 -0
  158. data/lib/contrast/framework/rails/support.rb +2 -2
  159. data/lib/contrast/framework/sinatra/support.rb +3 -1
  160. data/lib/contrast/funchook/funchook.rb +5 -8
  161. data/lib/contrast/logger/application.rb +13 -15
  162. data/lib/contrast/logger/format.rb +2 -5
  163. data/lib/contrast/logger/log.rb +26 -9
  164. data/lib/contrast/logger/request.rb +1 -6
  165. data/lib/contrast/security_exception.rb +1 -1
  166. data/lib/contrast/tasks/config.rb +0 -1
  167. data/lib/contrast/tasks/service.rb +6 -7
  168. data/lib/contrast/utils/assess/sampling_util.rb +2 -3
  169. data/lib/contrast/utils/assess/tracking_util.rb +3 -6
  170. data/lib/contrast/utils/class_util.rb +0 -8
  171. data/lib/contrast/utils/hash_digest.rb +2 -5
  172. data/lib/contrast/utils/heap_dump_util.rb +5 -3
  173. data/lib/contrast/utils/invalid_configuration_util.rb +4 -3
  174. data/lib/contrast/utils/inventory_util.rb +2 -3
  175. data/lib/contrast/utils/io_util.rb +3 -5
  176. data/lib/contrast/utils/job_servers_running.rb +13 -7
  177. data/lib/contrast/utils/os.rb +4 -4
  178. data/lib/contrast/utils/ruby_ast_rewriter.rb +2 -1
  179. data/lib/contrast/utils/string_utils.rb +2 -3
  180. data/lib/contrast/utils/tag_util.rb +25 -19
  181. data/resources/assess/policy.json +55 -0
  182. data/ruby-agent.gemspec +17 -16
  183. data/service_executables/VERSION +1 -1
  184. data/service_executables/linux/contrast-service +0 -0
  185. data/service_executables/mac/contrast-service +0 -0
  186. data/sonar-project.properties +9 -0
  187. metadata +61 -46
  188. data/lib/contrast/agent/railtie.rb +0 -31
  189. data/lib/contrast/components/interface.rb +0 -195
  190. data/lib/contrast/delegators/input_analysis.rb +0 -12
@@ -1,7 +1,6 @@
1
1
  # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'contrast/components/interface'
5
4
  require 'contrast/utils/object_share'
6
5
 
7
6
  module Contrast
@@ -13,8 +12,7 @@ module Contrast
13
12
  # of a file vs data flow, such as the detection of Hardcoded Passwords
14
13
  # or Keys.
15
14
  module PolicyScanner
16
- include Contrast::Components::Interface
17
- access_component :analysis
15
+
18
16
 
19
17
  class << self
20
18
  # Use the given trace_point, built from an :end event, to determine
@@ -24,8 +22,8 @@ module Contrast
24
22
  # @param trace_point [TracePoint] the TracePoint generated by an
25
23
  # :end event at the end of a Module definition.
26
24
  def scan trace_point
27
- return unless ASSESS.enabled?
28
- return unless ASSESS.require_scan?
25
+ return unless ::Contrast::ASSESS.enabled?
26
+ return unless ::Contrast::ASSESS.require_scan?
29
27
 
30
28
  provider_values = policy.providers.values
31
29
  return if provider_values.all?(&:disabled?)
@@ -1,7 +1,7 @@
1
1
  # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'contrast/components/interface'
4
+ require 'contrast/components/logger'
5
5
 
6
6
  module Contrast
7
7
  module Agent
@@ -9,8 +9,8 @@ module Contrast
9
9
  # In order to properly shift tags to account for the changes this method
10
10
  # caused, we'll need to store the state before the change occurred.
11
11
  class PreShift
12
- include Contrast::Components::Interface
13
- access_component :analysis, :logging
12
+ include Contrast::Components::Logger::InstanceMethods
13
+
14
14
 
15
15
  UNDUPLICABLE_MODULES = [
16
16
  Enumerator # dup'ing results in 'can't copy execution context'
@@ -37,7 +37,7 @@ module Contrast
37
37
  # being called or nil if one is not required.
38
38
  def build_preshift propagation_node, object, args
39
39
  return unless propagation_node
40
- return unless ASSESS.enabled?
40
+ return unless ::Contrast::ASSESS.enabled?
41
41
 
42
42
  initializing = propagation_node.method_name == :initialize
43
43
  return if unsafe_io_object?(object, initializing)
@@ -91,7 +91,9 @@ module Contrast
91
91
 
92
92
  Contrast::Agent::Assess::Tracker.copy(original_arg, preshift_arg)
93
93
  end
94
- preshift.arg_lengths = preshift.args.map { |preshift_arg| Contrast::Utils::DuckUtils.quacks_to?(preshift_arg, :length) ? preshift_arg.length : 0 }
94
+ preshift.arg_lengths = preshift.args.map do |preshift_arg|
95
+ Contrast::Utils::DuckUtils.quacks_to?(preshift_arg, :length) ? preshift_arg.length : 0
96
+ end
95
97
  end
96
98
  end
97
99
  end
@@ -4,7 +4,7 @@
4
4
  require 'set'
5
5
 
6
6
  require 'contrast/agent/assess/policy/propagator'
7
- require 'contrast/components/interface'
7
+ require 'contrast/components/logger'
8
8
  require 'contrast/utils/object_share'
9
9
  require 'contrast/utils/sha256_builder'
10
10
 
@@ -17,8 +17,8 @@ module Contrast
17
17
  # general, these methods work on the String class or a holder of
18
18
  # Strings
19
19
  module PropagationMethod
20
- include Contrast::Components::Interface
21
- access_component :analysis, :logging
20
+ extend Contrast::Components::Logger::InstanceMethods
21
+
22
22
 
23
23
  APPEND_ACTION = 'APPEND'
24
24
  CENTER_ACTION = 'CENTER'
@@ -140,13 +140,7 @@ module Contrast
140
140
  !!target
141
141
  end
142
142
 
143
- ZERO_LENGTH_ACTIONS = [
144
- DB_WRITE_ACTION,
145
- CUSTOM_ACTION,
146
- KEEP_ACTION,
147
- REPLACE_ACTION,
148
- SPLAT_ACTION
149
- ].cs__freeze
143
+ ZERO_LENGTH_ACTIONS = [DB_WRITE_ACTION, CUSTOM_ACTION, KEEP_ACTION, REPLACE_ACTION, SPLAT_ACTION].cs__freeze
150
144
  # If the action required needs a length and the target does not have
151
145
  # one, the length is not valid
152
146
  def valid_length? target, action
@@ -245,7 +239,7 @@ module Contrast
245
239
 
246
240
  def handle_enumerable_propagation propagation_node, preshift, target, object, ret, args, block
247
241
  target.each do |value|
248
- next if target == value # Some Enumerable#each are overridden to return self the first time which leads to infinite propagation
242
+ next if target == value
249
243
 
250
244
  apply_propagator(propagation_node, preshift, value, object, ret, args, block)
251
245
  end
@@ -283,9 +277,7 @@ module Contrast
283
277
 
284
278
  properties.add_properties(propagation_node.properties)
285
279
  properties.build_event(propagation_node, target, object, ret, args)
286
- logger.trace('Propagation detected',
287
- node_id: propagation_node.id,
288
- target_id: target.__id__)
280
+ logger.trace('Propagation detected', node_id: propagation_node.id, target_id: target.__id__)
289
281
  restore_frozen_state ? ret : nil
290
282
  end
291
283
 
@@ -296,10 +288,9 @@ module Contrast
296
288
  # @return [Contrast::Agent::Assess::Policy::Propagator, nil]
297
289
  def find_propagation_class propagation_node
298
290
  unless (propagation_class = PROPAGATION_ACTIONS.fetch(propagation_node.action, nil))
299
- logger.warn(
300
- 'Unknown propagation action received. Unable to propagate.',
301
- node_id: propagation_node.id,
302
- action: propagation_node.action)
291
+ logger.warn('Unknown propagation action received. Unable to propagate.',
292
+ node_id: propagation_node.id,
293
+ action: propagation_node.action)
303
294
  end
304
295
  propagation_class
305
296
  end
@@ -311,7 +302,7 @@ module Contrast
311
302
  # propagation event.
312
303
  # @return [Boolean]
313
304
  def can_handle_frozen? propagation_node
314
- ASSESS.track_frozen_sources? && propagation_node.targets[0] == Contrast::Utils::ObjectShare::RETURN_KEY
305
+ ::Contrast::ASSESS.track_frozen_sources? && propagation_node.targets[0] == Contrast::Utils::ObjectShare::RETURN_KEY
315
306
  end
316
307
  end
317
308
  end
@@ -62,11 +62,19 @@ module Contrast
62
62
  raise(ArgumentError, "Propagator #{ id } did not have a proper action. Unable to create.") unless action
63
63
 
64
64
  if @action == 'CUSTOM'
65
- raise(ArgumentError, "Propagator #{ id } did not have a proper patch_class. Unable to create.") unless patch_class
66
- raise(ArgumentError, "Propagator #{ id } did not have a proper patch_method. Unable to create.") unless patch_method.is_a?(Symbol)
65
+ unless patch_class
66
+ raise(ArgumentError, "Propagator #{ id } did not have a proper patch_class. Unable to create.")
67
+ end
68
+ unless patch_method.is_a?(Symbol)
69
+ raise(ArgumentError, "Propagator #{ id } did not have a proper patch_method. Unable to create.")
70
+ end
67
71
  else
68
- raise(ArgumentError, "Propagator #{ id } did not have a proper target. Unable to create.") unless targets&.any?
69
- raise(ArgumentError, "Propagator #{ id } did not have a proper source. Unable to create.") unless sources&.any?
72
+ unless targets&.any?
73
+ raise(ArgumentError, "Propagator #{ id } did not have a proper target. Unable to create.")
74
+ end
75
+ unless sources&.any?
76
+ raise(ArgumentError, "Propagator #{ id } did not have a proper source. Unable to create.")
77
+ end
70
78
  end
71
79
  validate_untags
72
80
  end
@@ -76,9 +84,12 @@ module Contrast
76
84
 
77
85
  untags.each do |tag|
78
86
  unless Contrast::Api::Decorators::TraceTaintRangeTags::VALID_TAGS.include?(tag)
79
- raise(ArgumentError, "#{ node_type } #{ id } did not have a valid untag. #{ tag } is not a known value.")
87
+ raise(ArgumentError,
88
+ "#{ node_type } #{ id } did not have a valid untag. #{ tag } is not a known value.")
89
+ end
90
+ if tags&.include?(tag)
91
+ raise(ArgumentError, "#{ node_type } #{ id } had the same tag and untag, #{ tag }.")
80
92
  end
81
- raise(ArgumentError, "#{ node_type } #{ id } had the same tag and untag, #{ tag }.") if tags&.include?(tag)
82
93
  end
83
94
  end
84
95
 
@@ -86,8 +97,8 @@ module Contrast
86
97
  if @_needs_object.nil?
87
98
  @_needs_object = action == Contrast::Agent::Assess::Policy::PropagationMethod::CUSTOM_ACTION ||
88
99
  action == Contrast::Agent::Assess::Policy::PropagationMethod::DB_WRITE_ACTION ||
89
- sources.any? { |source| source == Contrast::Utils::ObjectShare::OBJECT_KEY } ||
90
- targets.any? { |target| target == Contrast::Utils::ObjectShare::OBJECT_KEY }
100
+ sources.any?(Contrast::Utils::ObjectShare::OBJECT_KEY) ||
101
+ targets.any?(Contrast::Utils::ObjectShare::OBJECT_KEY)
91
102
  end
92
103
  @_needs_object
93
104
  end
@@ -21,6 +21,7 @@ module Contrast
21
21
  require 'contrast/agent/assess/policy/propagator/match_data'
22
22
  require 'contrast/agent/assess/policy/propagator/next'
23
23
  require 'contrast/agent/assess/policy/propagator/prepend'
24
+ require 'contrast/agent/assess/policy/propagator/rack_protection'
24
25
  require 'contrast/agent/assess/policy/propagator/remove'
25
26
  require 'contrast/agent/assess/policy/propagator/replace'
26
27
  require 'contrast/agent/assess/policy/propagator/reverse'
@@ -31,7 +31,8 @@ module Contrast
31
31
  return unless sources[1]
32
32
 
33
33
  original_end_index = original_start_index + source1.length - 1
34
- handle_incoming_tags(target, propagation_node, sources[1], preshift, original_start_index, original_end_index)
34
+ handle_incoming_tags(target, propagation_node, sources[1], preshift, original_start_index,
35
+ original_end_index)
35
36
  end
36
37
 
37
38
  private
@@ -1,8 +1,6 @@
1
1
  # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'contrast/components/interface'
5
-
6
4
  module Contrast
7
5
  module Agent
8
6
  module Assess
@@ -13,8 +11,7 @@ module Contrast
13
11
  # results in new source nodes to track which columns in the database
14
12
  # have been tainted.
15
13
  class DatabaseWrite < Contrast::Agent::Assess::Policy::Propagator::Base
16
- include Contrast::Components::Interface
17
- access_component :analysis
14
+
18
15
 
19
16
  class << self
20
17
  def propagate propagation_node, preshift, target
@@ -22,7 +19,7 @@ module Contrast
22
19
  class_name = class_type.cs__name
23
20
  tainted_columns = {}
24
21
 
25
- known_tainted = ASSESS.tainted_columns[class_name]
22
+ known_tainted = ::Contrast::ASSESS.tainted_columns[class_name]
26
23
  propagation_node.sources.each do |source|
27
24
  handle_write(propagation_node, source, preshift, target, known_tainted, tainted_columns)
28
25
  end
@@ -31,7 +28,7 @@ module Contrast
31
28
  if known_tainted
32
29
  known_tainted.concat(tainted_columns.keys)
33
30
  else
34
- ASSESS.tainted_columns[class_name] = tainted_columns.keys
31
+ ::Contrast::ASSESS.tainted_columns[class_name] = tainted_columns.keys
35
32
  end
36
33
 
37
34
  Contrast::Agent::Assess::Policy::DynamicSourceFactory.create_sources class_type, tainted_columns
@@ -33,7 +33,9 @@ module Contrast
33
33
  # point on which all tags need to be adjusted
34
34
  # If the insertion point is the end of the string, preshift length is returned
35
35
  # https://stackoverflow.com/questions/31714522/find-the-first-differing-character-between-two-strings-in-ruby
36
- insert_point = (0...preshift_target.length).find { |i| preshift_target[i] != target[i] } || preshift_target.length
36
+ insert_point = (0...preshift_target.length).find do |i|
37
+ preshift_target[i] != target[i]
38
+ end || preshift_target.length
37
39
  # Depending what's inserted, we might be wrong. For instance, inserting 'foo'
38
40
  # into 'asdfasdf' could result in 'asdfoofasdf'. we'd be off by one b/c of the 'f'
39
41
  insert_point = target.rindex(source, insert_point)
@@ -76,7 +76,8 @@ module Contrast
76
76
  applicable_tags.each do |tag_name, tag_ranges|
77
77
  return_properties.set_tags(tag_name, tag_ranges)
78
78
  end
79
- return_properties.build_event(propagation_node, return_value, preshift.object, return_value, preshift.args)
79
+ return_properties.build_event(propagation_node, return_value, preshift.object, return_value,
80
+ preshift.args)
80
81
  end
81
82
  end
82
83
  end
@@ -0,0 +1,73 @@
1
+ # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ module Contrast
5
+ module Agent
6
+ module Assess
7
+ module Policy
8
+ module Propagator
9
+ # Rack::Protection offers several protections against vulnerabilities. Of these, some apply to dataflow and
10
+ # need to be accounted for in order to properly tag data. Others apply to configurations and may be used to
11
+ # suppress configuration vulnerabilities in the future.
12
+ class RackProtection < Contrast::Agent::Assess::Policy::Propagator::Base
13
+ class << self
14
+ # Our custom instrumentation for the Rack::Protection::EscapedParams#escape_string method
15
+ # @param propagation_node [Contrast::Agent::Assess::Policy::PropagationNode] the node that governs this
16
+ # propagation event.
17
+ # @param preshift [Contrast::Agent::Assess::PreShift] The capture of the state of the code just prior to
18
+ # the invocation of the patched method.
19
+ # @param ret [nil, String] the target to which to propagate.
20
+ # @return [nil, String] ret
21
+ def escaped_params propagation_node, preshift, ret, _block
22
+ Contrast::Agent::Assess::Policy::Propagator::Splat.propagate(propagation_node, preshift, ret)
23
+ apply_escaper_tags(preshift.object, ret)
24
+ ret
25
+ end
26
+
27
+ private
28
+
29
+ # Rack::Protection::EscapedParams can be configured such that it only applies certain escape. We need
30
+ # to account for the configuration of the individual escapes when applying tags.
31
+ #
32
+ # @param escaper [Rack::Protection::EscapedParams] the instance of Rack::Protection::EscapedParams
33
+ # applying the escape_string
34
+ # @param ret [String] the result of the escape
35
+ def apply_escaper_tags escaper, ret
36
+ # I don't know how this could not be an instance of Rack::Protection::EscapedParams, but I don't want
37
+ # to chance it.
38
+ return unless escaper.cs__is_a?(Rack::Protection::EscapedParams)
39
+ return unless (properties = Contrast::Agent::Assess::Tracker.properties(ret))
40
+
41
+ tags = []
42
+ untags = []
43
+ if escaper.instance_variable_get(:@html)
44
+ tags << 'HTML_ENCODED'
45
+ untags << 'HTML_DECODED'
46
+ end
47
+
48
+ if escaper.instance_variable_get(:@javascript)
49
+ tags << 'JAVASCRIPT_ENCODED'
50
+ untags << 'JAVASCRIPT_DECODED'
51
+ end
52
+
53
+ if escaper.instance_variable_get(:@url)
54
+ tags << 'URL_ENCODED'
55
+ untags << 'URL_DECODED'
56
+ end
57
+
58
+ length = Contrast::Utils::StringUtils.ret_length(ret)
59
+ tags.each do |tag|
60
+ properties.add_tag(tag, 0...length)
61
+ end
62
+
63
+ untags.each do |tag|
64
+ properties.delete_tags(tag)
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -21,22 +21,12 @@ module Contrast
21
21
  # Additionally, an empty string is returned when the starting index for
22
22
  # a character range is at the end of the string. Let's just skip that
23
23
  # and only track a string that has length
24
- unless ret &&
25
- !ret.empty? &&
26
- Contrast::Agent::Assess::Tracker.tracked?(source)
27
-
28
- return
29
- end
24
+ return unless ret && !ret.empty? && Contrast::Agent::Assess::Tracker.tracked?(source)
30
25
 
31
26
  return unless (source_properties = Contrast::Agent::Assess::Tracker.properties(source))
32
27
  return unless (properties = Contrast::Agent::Assess::Tracker.properties!(ret))
33
28
 
34
- properties.build_event(
35
- patcher,
36
- ret,
37
- source,
38
- ret,
39
- args)
29
+ properties.build_event(patcher, ret, source, ret, args)
40
30
 
41
31
  range = determine_select_range(source, args)
42
32
  return unless range
@@ -2,7 +2,9 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/agent/assess/policy/preshift'
5
- require 'contrast/components/interface'
5
+ require 'contrast/components/agent'
6
+ require 'contrast/components/logger'
7
+ require 'contrast/components/scope'
6
8
  require 'contrast/utils/thread_tracker'
7
9
 
8
10
  module Contrast
@@ -13,9 +15,9 @@ module Contrast
13
15
  # This class is specifically for String#split & String#grapheme_clusters propagation
14
16
  # it propagates tag ranges from a string to elements within an untracked array
15
17
  class Split < Contrast::Agent::Assess::Policy::Propagator::Base
16
- include Contrast::Components::Interface
17
-
18
- access_component :agent, :logging, :scope
18
+ extend Contrast::Components::Scope::InstanceMethods
19
+ extend Contrast::Components::Logger::InstanceMethods
20
+ #cs__const_set('AGENT', Contrast::AGENT)
19
21
 
20
22
  SPLIT_TRACKER = Contrast::Utils::ThreadTracker.new
21
23
 
@@ -29,11 +31,10 @@ module Contrast
29
31
  # patched method.
30
32
  # @param target [Array, String] the target to which to propagate.
31
33
  # @return [nil] so as not to risk changing the result of the propagation.
32
-
33
34
  def propagate propagation_node, preshift, target
34
- logger.trace('Propagation detected',
35
- node_id: propagation_node.id,
36
- target_id: target.__id__)
35
+ return unless target.is_a?(Array) # apply_post_patch is called, but split with block returns a string.
36
+
37
+ logger.trace('Propagation detected', node_id: propagation_node.id, target_id: target.__id__)
37
38
 
38
39
  source = find_source(propagation_node.sources[0], preshift)
39
40
  return unless (source_properties = Contrast::Agent::Assess::Tracker.properties(source))
@@ -110,7 +111,7 @@ module Contrast
110
111
  # Load patch.
111
112
  def instrument_string_split
112
113
  @_instrument_string_split ||= begin
113
- require 'cs__assess_yield_track/cs__assess_yield_track' if AGENT.patch_yield? && Funchook.available?
114
+ require 'cs__assess_yield_track/cs__assess_yield_track' if ::Contrast::AGENT.patch_yield? && Funchook.available?
114
115
  true
115
116
  rescue StandardError => e
116
117
  logger.error('Error loading split rb_yield patch', e)
@@ -162,10 +163,8 @@ module Contrast
162
163
  #
163
164
  # @return [Contrast::Agent::Assess::Policy::PropagationNode] String#split node
164
165
  def split_node
165
- @_split_node ||= begin
166
- Contrast::Agent::Assess::Policy::Policy.instance.propagators.find do |node|
167
- node.class_name == 'String' && node.method_name == :split && node.instance_method?
168
- end
166
+ @_split_node ||= Contrast::Agent::Assess::Policy::Policy.instance.propagators.find do |node|
167
+ node.class_name == 'String' && node.method_name == :split && node.instance_method?
169
168
  end
170
169
  end
171
170
  end
@@ -1,7 +1,7 @@
1
1
  # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'contrast/components/interface'
4
+ require 'contrast/components/logger'
5
5
  require 'contrast/utils/duck_utils'
6
6
 
7
7
  module Contrast
@@ -15,8 +15,7 @@ module Contrast
15
15
  # in a 'get it work' state. hopefully, we'll be in
16
16
  # a 'get it right' state soon.
17
17
  class Substitution
18
- include Contrast::Components::Interface
19
- access_component :logging
18
+ include Contrast::Components::Logger::InstanceMethods
20
19
 
21
20
  CAPTURE_GROUP_REGEXP = /\\[[:digit:]]/.cs__freeze
22
21
  CAPTURE_NAME_REGEXP = /\\k<[[:alpha:]]/.cs__freeze
@@ -186,13 +185,7 @@ module Contrast
186
185
 
187
186
  properties = Contrast::Agent::Assess::Tracker.properties(ret)
188
187
  args = preshift.args
189
- properties.build_event(
190
- patcher,
191
- ret,
192
- preshift.object,
193
- ret,
194
- args,
195
- 2)
188
+ properties.build_event(patcher, ret, preshift.object, ret, args, 2)
196
189
  properties.event.instance_variable_set(:@_parent_events, parent_events)
197
190
  end
198
191
  end