contrast-agent 3.14.0 → 4.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (148) hide show
  1. checksums.yaml +4 -4
  2. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +18 -15
  3. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.h +1 -0
  4. data/ext/cs__assess_string/cs__assess_string.c +24 -25
  5. data/ext/cs__assess_string/cs__assess_string.h +3 -1
  6. data/ext/cs__common/cs__common.c +4 -2
  7. data/ext/cs__common/cs__common.h +1 -1
  8. data/lib/contrast.rb +1 -1
  9. data/lib/contrast/agent.rb +4 -12
  10. data/lib/contrast/agent/assess.rb +1 -0
  11. data/lib/contrast/agent/assess/contrast_event.rb +143 -79
  12. data/lib/contrast/agent/assess/events/source_event.rb +1 -1
  13. data/lib/contrast/agent/assess/finalizers/freeze.rb +3 -1
  14. data/lib/contrast/agent/assess/finalizers/hash.rb +45 -1
  15. data/lib/contrast/agent/assess/policy/dynamic_source_factory.rb +10 -3
  16. data/lib/contrast/agent/assess/policy/patcher.rb +1 -1
  17. data/lib/contrast/agent/assess/policy/policy.rb +0 -2
  18. data/lib/contrast/agent/assess/policy/policy_node.rb +15 -10
  19. data/lib/contrast/agent/assess/policy/policy_scanner.rb +19 -3
  20. data/lib/contrast/agent/assess/policy/preshift.rb +7 -11
  21. data/lib/contrast/agent/assess/policy/propagation_method.rb +50 -33
  22. data/lib/contrast/agent/assess/policy/propagator/append.rb +8 -5
  23. data/lib/contrast/agent/assess/policy/propagator/base.rb +1 -1
  24. data/lib/contrast/agent/assess/policy/propagator/center.rb +9 -5
  25. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +5 -3
  26. data/lib/contrast/agent/assess/policy/propagator/insert.rb +7 -4
  27. data/lib/contrast/agent/assess/policy/propagator/keep.rb +4 -1
  28. data/lib/contrast/agent/assess/policy/propagator/match_data.rb +4 -7
  29. data/lib/contrast/agent/assess/policy/propagator/next.rb +7 -5
  30. data/lib/contrast/agent/assess/policy/propagator/prepend.rb +8 -5
  31. data/lib/contrast/agent/assess/policy/propagator/remove.rb +8 -4
  32. data/lib/contrast/agent/assess/policy/propagator/replace.rb +5 -2
  33. data/lib/contrast/agent/assess/policy/propagator/reverse.rb +7 -5
  34. data/lib/contrast/agent/assess/policy/propagator/select.rb +13 -7
  35. data/lib/contrast/agent/assess/policy/propagator/splat.rb +10 -9
  36. data/lib/contrast/agent/assess/policy/propagator/split.rb +24 -19
  37. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +47 -31
  38. data/lib/contrast/agent/assess/policy/propagator/trim.rb +11 -5
  39. data/lib/contrast/agent/assess/policy/source_method.rb +85 -58
  40. data/lib/contrast/agent/assess/policy/trigger/reflected_xss.rb +16 -12
  41. data/lib/contrast/agent/assess/policy/trigger/xpath.rb +1 -1
  42. data/lib/contrast/agent/assess/policy/trigger_method.rb +76 -28
  43. data/lib/contrast/agent/assess/policy/trigger_node.rb +38 -43
  44. data/lib/contrast/agent/assess/policy/trigger_validation/ssrf_validator.rb +2 -1
  45. data/lib/contrast/agent/assess/properties.rb +2 -0
  46. data/lib/contrast/agent/assess/property/evented.rb +5 -18
  47. data/lib/contrast/agent/assess/property/tagged.rb +9 -3
  48. data/lib/contrast/agent/assess/property/updated.rb +131 -0
  49. data/lib/contrast/agent/assess/rule/provider/hardcoded_key.rb +58 -5
  50. data/lib/contrast/agent/assess/rule/provider/hardcoded_password.rb +23 -8
  51. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +83 -14
  52. data/lib/contrast/agent/assess/tag.rb +1 -1
  53. data/lib/contrast/agent/assess/tracker.rb +66 -0
  54. data/lib/contrast/agent/at_exit_hook.rb +5 -5
  55. data/lib/contrast/agent/class_reopener.rb +7 -5
  56. data/lib/contrast/agent/inventory.rb +15 -0
  57. data/lib/contrast/agent/inventory/dependencies.rb +50 -0
  58. data/lib/contrast/agent/inventory/dependency_analysis.rb +37 -0
  59. data/lib/contrast/agent/inventory/dependency_usage_analysis.rb +104 -0
  60. data/lib/contrast/agent/inventory/gemfile_digest_cache.rb +38 -0
  61. data/lib/contrast/agent/middleware.rb +1 -3
  62. data/lib/contrast/agent/patching/policy/after_load_patch.rb +5 -5
  63. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +20 -20
  64. data/lib/contrast/agent/patching/policy/module_policy.rb +10 -10
  65. data/lib/contrast/agent/patching/policy/patch.rb +6 -0
  66. data/lib/contrast/agent/patching/policy/patcher.rb +13 -22
  67. data/lib/contrast/agent/patching/policy/policy.rb +17 -6
  68. data/lib/contrast/agent/protect/policy/applies_command_injection_rule.rb +3 -5
  69. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +4 -3
  70. data/lib/contrast/agent/protect/policy/applies_xxe_rule.rb +1 -1
  71. data/lib/contrast/agent/protect/rule/cmd_injection.rb +9 -25
  72. data/lib/contrast/agent/protect/rule/no_sqli/mongo_no_sql_scanner.rb +1 -0
  73. data/lib/contrast/agent/request.rb +34 -34
  74. data/lib/contrast/agent/request_handler.rb +1 -1
  75. data/lib/contrast/agent/response.rb +17 -6
  76. data/lib/contrast/agent/rewriter.rb +1 -3
  77. data/lib/contrast/agent/scope.rb +59 -53
  78. data/lib/contrast/agent/static_analysis.rb +7 -7
  79. data/lib/contrast/agent/tracepoint_hook.rb +1 -1
  80. data/lib/contrast/agent/version.rb +1 -1
  81. data/lib/contrast/api/communication/messaging_queue.rb +1 -4
  82. data/lib/contrast/api/communication/socket_client.rb +36 -1
  83. data/lib/contrast/api/decorators.rb +3 -0
  84. data/lib/contrast/api/decorators/address.rb +13 -14
  85. data/lib/contrast/api/decorators/application_update.rb +2 -4
  86. data/lib/contrast/api/decorators/library.rb +53 -0
  87. data/lib/contrast/api/decorators/library_usage_update.rb +30 -0
  88. data/lib/contrast/api/decorators/message.rb +1 -0
  89. data/lib/contrast/api/decorators/trace_event.rb +25 -23
  90. data/lib/contrast/common_agent_configuration.rb +2 -1
  91. data/lib/contrast/components/agent.rb +6 -5
  92. data/lib/contrast/components/app_context.rb +49 -38
  93. data/lib/contrast/components/config.rb +30 -48
  94. data/lib/contrast/components/contrast_service.rb +9 -9
  95. data/lib/contrast/components/interface.rb +25 -3
  96. data/lib/contrast/components/inventory.rb +6 -1
  97. data/lib/contrast/components/scope.rb +49 -6
  98. data/lib/contrast/components/settings.rb +23 -23
  99. data/lib/contrast/config/application_configuration.rb +5 -2
  100. data/lib/contrast/config/inventory_configuration.rb +2 -2
  101. data/lib/contrast/config/service_configuration.rb +8 -0
  102. data/lib/contrast/configuration.rb +88 -47
  103. data/lib/contrast/extension/assess.rb +0 -2
  104. data/lib/contrast/extension/assess/array.rb +15 -8
  105. data/lib/contrast/extension/assess/erb.rb +11 -3
  106. data/lib/contrast/extension/assess/eval_trigger.rb +6 -6
  107. data/lib/contrast/extension/assess/exec_trigger.rb +1 -4
  108. data/lib/contrast/extension/assess/fiber.rb +12 -12
  109. data/lib/contrast/extension/assess/hash.rb +5 -6
  110. data/lib/contrast/extension/assess/kernel.rb +28 -23
  111. data/lib/contrast/extension/assess/marshal.rb +11 -6
  112. data/lib/contrast/extension/assess/regexp.rb +8 -7
  113. data/lib/contrast/extension/assess/string.rb +21 -21
  114. data/lib/contrast/extension/protect/kernel.rb +3 -3
  115. data/lib/contrast/framework/base_support.rb +1 -1
  116. data/lib/contrast/framework/manager.rb +3 -3
  117. data/lib/contrast/framework/rack/patch/session_cookie.rb +22 -28
  118. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +13 -13
  119. data/lib/contrast/framework/rails/patch/assess_configuration.rb +5 -11
  120. data/lib/contrast/framework/rails/patch/rails_application_configuration.rb +10 -10
  121. data/lib/contrast/framework/rails/patch/support.rb +1 -1
  122. data/lib/contrast/framework/rails/rewrite/action_controller_railties_helper_inherited.rb +11 -11
  123. data/lib/contrast/framework/rails/rewrite/active_record_attribute_methods_read.rb +12 -12
  124. data/lib/contrast/framework/rails/rewrite/active_record_named.rb +3 -3
  125. data/lib/contrast/framework/rails/rewrite/active_record_time_zone_inherited.rb +12 -12
  126. data/lib/contrast/framework/rails/support.rb +5 -0
  127. data/lib/contrast/framework/sinatra/patch/base.rb +11 -11
  128. data/lib/contrast/framework/sinatra/support.rb +4 -4
  129. data/lib/contrast/logger/application.rb +11 -3
  130. data/lib/contrast/logger/log.rb +7 -2
  131. data/lib/contrast/utils/assess/tracking_util.rb +48 -3
  132. data/lib/contrast/utils/duck_utils.rb +0 -10
  133. data/lib/contrast/utils/env_configuration_item.rb +2 -1
  134. data/lib/contrast/utils/invalid_configuration_util.rb +20 -21
  135. data/lib/contrast/utils/inventory_util.rb +0 -7
  136. data/lib/contrast/utils/sha256_builder.rb +0 -12
  137. data/lib/contrast/utils/string_utils.rb +10 -5
  138. data/resources/assess/policy.json +31 -22
  139. data/ruby-agent.gemspec +21 -18
  140. data/service_executables/VERSION +1 -1
  141. data/service_executables/linux/contrast-service +0 -0
  142. data/service_executables/mac/contrast-service +0 -0
  143. metadata +71 -30
  144. data/lib/contrast/agent/assess/finalizers/finalize.rb +0 -21
  145. data/lib/contrast/extension/assess/assess_extension.rb +0 -145
  146. data/lib/contrast/utils/boolean_util.rb +0 -30
  147. data/lib/contrast/utils/freeze_util.rb +0 -32
  148. data/lib/contrast/utils/gemfile_reader.rb +0 -193
@@ -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
@@ -75,17 +81,24 @@ module Contrast
75
81
  # if it's a string, just ask if it's tracked
76
82
  case args
77
83
  when String
78
- Contrast::Utils::DuckUtils.quacks_to?(args, :cs__tracked?) && args.cs__tracked?
84
+ Contrast::Agent::Assess::Tracker.tracked?(args)
79
85
  # if it's a hash, ask if it has a tracked string
80
86
  when Hash
81
- args.values.any? { |value| value.is_a?(String) && Contrast::Utils::DuckUtils.quacks_to?(value, :cs__tracked?) && value.cs__tracked? }
87
+ args.values.any? { |value| value.is_a?(String) && Contrast::Agent::Assess::Tracker.tracked?(value) }
82
88
  # this should never happen
83
89
  else
84
90
  false
85
91
  end
86
92
  end
87
93
 
88
- 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
+
89
102
  pattern = preshift.args[0]
90
103
  source = preshift.object
91
104
 
@@ -93,13 +106,13 @@ module Contrast
93
106
  # copied from regexp / captures. Trading accuracy for
94
107
  # performance
95
108
  if incoming.match?(CAPTURE_GROUP_REGEXP) || incoming.match?(CAPTURE_NAME_REGEXP)
96
- source.cs__splat_tags(ret) if self_tracked
109
+ properties.splat_from(source, ret) if self_tracked
97
110
  return
98
111
  end
99
112
 
100
113
  # if it's just a straight insert, that we can do
101
114
  # Copy the tags from us to the return
102
- ret.cs__copy_from(source)
115
+ properties.copy_from(source, ret)
103
116
  # Figure out where inserts occurred
104
117
  last_idx = 0
105
118
  ranges = []
@@ -114,54 +127,57 @@ module Contrast
114
127
  ranges << (start_index...end_index)
115
128
  break unless global
116
129
  end
117
- ret.cs__properties.delete_tags_at_ranges(ranges)
118
- ret.cs__properties.shift_tags(ranges)
130
+ properties.delete_tags_at_ranges(ranges)
131
+ properties.shift_tags(ranges)
119
132
  return unless incoming_tracked
133
+ return unless incoming_properties
120
134
 
121
- tags = incoming.cs__properties.tag_keys
135
+ tags = incoming_properties.tag_keys
122
136
  ranges.each do |range|
123
137
  tags.each do |tag|
124
- ret.cs__properties.add_tag(tag, range)
138
+ properties.add_tag(tag, range)
125
139
  end
126
140
  end
127
141
  end
128
142
 
129
143
  def block_sub self_tracked, source, ret
130
- 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
131
146
  end
132
147
 
133
148
  def hash_sub self_tracked, source, ret
134
- 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
135
151
  end
136
152
 
137
- def pattern_gsub preshift, ret
138
- 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
139
156
 
140
157
  source = preshift.object
141
- source.cs__properties.tag_keys.each do |key|
142
- 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)
143
163
  end
164
+ parent_event = source_properties.event
165
+ parent_events << parent_event if parent_event
144
166
  end
145
167
 
146
- def string_build_event patcher, preshift, ret
147
- 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)
148
170
 
171
+ properties = Contrast::Agent::Assess::Tracker.properties(ret)
149
172
  args = preshift.args
150
- if args.length > 1
151
- arg = args[1]
152
- if Contrast::Utils::DuckUtils.quacks_to?(arg, :cs__properties)
153
- arg.cs__properties.events.each do |event|
154
- ret.cs__properties.events << event
155
- end
156
- end
157
- end
158
- ret.cs__properties.build_event(
173
+ properties.build_event(
159
174
  patcher,
160
175
  ret,
161
176
  preshift.object,
162
177
  ret,
163
178
  args,
164
179
  2)
180
+ properties.event.instance_variable_set(:@_parent_events, parent_events)
165
181
  end
166
182
  end
167
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,
@@ -17,7 +17,7 @@ module Contrast
17
17
  # dataflows used in Assess vulnerability detection.
18
18
  module SourceMethod
19
19
  include Contrast::Components::Interface
20
- access_component :logging, :analysis
20
+ access_component :analysis, :logging
21
21
 
22
22
  PARAMETER_TYPE = 'PARAMETER'
23
23
  PARAMETER_KEY_TYPE = 'PARAMETER_KEY'
@@ -44,38 +44,26 @@ module Contrast
44
44
  current_context = Contrast::Agent::REQUEST_TRACKER.current
45
45
  return unless current_context&.analyze_request? && ASSESS.enabled?
46
46
 
47
- replaced_return = nil
48
47
  source_node = method_policy.source_node
49
-
50
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
51
53
 
52
- # We don't propagate to frozen things that haven't been tracked
53
- # before. By definition, something that is a source has not
54
- # previously been tracked; therefore, we can break out early.
55
- if target.cs__frozen?
56
- # That being said, we don't have enough context to know if we
57
- # can make this assumption and still function, so we'll allow for
58
- # source tracking of frozen things by a common config setting.
59
- #
60
- # Rails' StrongParameters make a case for this to be default
61
- # behavior
62
- return replaced_return unless ASSESS.track_frozen_sources?
63
-
64
- # If we're tracking the frozen target, we need to unfreeze
65
- # (dup) it to track and then freeze that result. For
66
- # simplicities sake, we ONLY do this if the return is the
67
- # target (I don't want to have to deal with unfreezing self)
68
- return replaced_return unless source_node.targets[0] == Contrast::Utils::ObjectShare::RETURN_KEY
54
+ dup = safe_dup(ret)
55
+ return unless dup
69
56
 
70
57
  restore_frozen_state = true
71
- ret = Contrast::Utils::FreezeUtil.unfreeze_dup(ret)
58
+ ret = dup
72
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)
73
64
  end
74
-
75
65
  apply_source(current_context, source_node, target, object, ret, source_node.type, nil, *args)
76
-
77
- ret.cs__freeze if restore_frozen_state
78
- ret
66
+ restore_frozen_state ? ret : nil
79
67
  end
80
68
 
81
69
  private
@@ -103,34 +91,10 @@ module Contrast
103
91
  source_name ||= determine_source_name(source_node, object, ret, *args)
104
92
  # We know we only work on certain things.
105
93
  # Skip if this isn't one of them
106
- if Contrast::Utils::DuckUtils.quacks_to?(target, :cs__properties)
94
+ if Contrast::Agent::Assess::Tracker.trackable?(target)
107
95
  apply_tags(source_node, target, object, ret, source_type, source_name, *args)
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
96
  elsif Contrast::Utils::DuckUtils.iterable_hash?(target)
112
- to_replace = []
113
- target.each_pair do |key, value|
114
- # We only do this for Strings b/c of the way Hash lookup works.
115
- # To replace another object would break hash lookup and,
116
- # therefore, the application
117
- if can_track_hash_key?(key, target)
118
- key = Contrast::Utils::FreezeUtil.unfreeze_dup(key)
119
- to_replace << key
120
- end
121
- apply_source(context, source_node, key, object, ret, key_type(source_type), key, *args)
122
- apply_source(context, source_node, value, object, ret, source_type, key, *args)
123
- end
124
-
125
- # Hash is designed to keep one instance of the string key in it.
126
- # We need to remove the existing one and replace it with our new
127
- # tracked one.
128
- to_replace.each do |key|
129
- key.cs__freeze
130
- value = target[key]
131
- target.delete(key)
132
- target[key] = value
133
- end
97
+ apply_hash_tags(context, source_node, target, object, ret, source_type, *args)
134
98
  # While we don't taint arrays themselves, we may taint the things
135
99
  # they hold. Let's pass their keys and values back to ourselves and
136
100
  # try again
@@ -141,17 +105,81 @@ module Contrast
141
105
  logger.warn('Unable to apply source', e, node_id: source_node.id)
142
106
  end
143
107
 
144
- 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
145
148
  ASSESS.track_frozen_sources? &&
149
+ !hash.cs__frozen? &&
146
150
  key.is_a?(String) &&
147
- 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
148
173
  end
149
174
 
150
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)
151
178
  # don't apply second source -- probably needs tuning later if we
152
179
  # use more than 'UNTRUSTED' in our sources
153
- return if target.cs__tracked? || target.cs__frozen?
180
+ return if Contrast::Agent::Assess::Tracker.tracked?(target)
154
181
 
182
+ properties = Contrast::Agent::Assess::Tracker.properties(target)
155
183
  # otherwise for each tag this source_node applies, create a tag range
156
184
  # on the target object
157
185
  # I realize this looping is counter-intuitive from the above
@@ -160,16 +188,15 @@ module Contrast
160
188
  next unless Contrast::Agent::Assess::Policy::SourceValidation.valid?(tag, source_type, source_name)
161
189
 
162
190
  length = Contrast::Utils::StringUtils.ret_length(target)
163
- target.cs__properties.add_tag(tag, 0...length)
164
- target.cs__properties.add_properties(source_node.properties)
191
+ properties.add_tag(tag, 0...length)
192
+ properties.add_properties(source_node.properties)
165
193
  logger.trace('Source detected',
166
194
  node_id: source_node.id,
167
195
  target_id: target.__id__,
168
196
  tag: tag)
169
197
  end
170
-
171
198
  # make a representation of this method that TeamServer can render
172
- 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)
173
200
  end
174
201
 
175
202
  # Find the name of the source
@@ -25,24 +25,26 @@ module Contrast
25
25
  TEMPLATE_PROPAGATION_NODE = Contrast::Agent::Assess::Policy::PropagationNode.new(NODE_HASH)
26
26
 
27
27
  def xss_tilt_trigger context, trigger_node, _source, object, ret, *args
28
- scope = args[0]
28
+ properties = Contrast::Agent::Assess::Tracker.properties(ret)
29
+ return unless properties
29
30
 
31
+ scope = args[0]
30
32
  erb_template_prerender = object.instance_variable_get(:@data)
31
33
  interpolated_inputs = []
32
34
  handle_binding_variables(scope, erb_template_prerender, ret, interpolated_inputs)
33
-
34
35
  handle_local_variables(args, erb_template_prerender, ret, interpolated_inputs)
35
-
36
+ properties.build_event(TEMPLATE_PROPAGATION_NODE, ret, erb_template_prerender, ret, interpolated_inputs)
36
37
  unless interpolated_inputs.empty?
38
+ current_event = properties.event
37
39
  interpolated_inputs.each do |input|
38
- input.cs__properties.events.each do |event|
39
- ret.cs__properties.events << event
40
- end
40
+ input_properties = Contrast::Agent::Assess::Tracker.properties(input)
41
+ next unless input_properties&.event
42
+
43
+ current_event.parent_events << input_properties.event
41
44
  end
42
- ret.cs__properties.build_event(TEMPLATE_PROPAGATION_NODE, ret, erb_template_prerender, ret, interpolated_inputs)
43
45
  end
44
46
 
45
- if ret.cs__tracked?
47
+ if Contrast::Agent::Assess::Tracker.tracked?(ret)
46
48
  Contrast::Agent::Assess::Policy::TriggerMethod.build_finding(context, trigger_node, ret, erb_template_prerender, ret, interpolated_inputs)
47
49
  end
48
50
 
@@ -52,32 +54,34 @@ module Contrast
52
54
  private
53
55
 
54
56
  def handle_binding_variables scope, erb_template_prerender, ret, interpolated_inputs
57
+ properties = Contrast::Agent::Assess::Tracker.properties(ret)
55
58
  binding_variables = scope.instance_variables
56
59
 
57
60
  binding_variables.each do |bound_variable_sym|
58
61
  bound_variable_value = scope.instance_variable_get(bound_variable_sym)
59
62
 
60
- next unless bound_variable_value.cs__respond_to?(:cs__tracked?) && bound_variable_value.cs__tracked?
63
+ next unless Contrast::Agent::Assess::Tracker.tracked?(bound_variable_value)
61
64
  next unless erb_template_prerender.include?(bound_variable_sym.to_s)
62
65
 
63
66
  start_index = ret.index(bound_variable_value)
64
67
  next if start_index.nil?
65
68
 
66
- ret.cs__copy_from(bound_variable_value, start_index)
69
+ properties.copy_from(bound_variable_value, ret, start_index)
67
70
  interpolated_inputs << bound_variable_sym
68
71
  end
69
72
  end
70
73
 
71
74
  def handle_local_variables args, erb_template_prerender, ret, interpolated_inputs
75
+ properties = Contrast::Agent::Assess::Tracker.properties(ret)
72
76
  locals = args[1]
73
77
  locals.each do |local_name, local_value|
74
- next unless local_value.cs__respond_to?(:cs__tracked?) && local_value.cs__tracked?
78
+ next unless Contrast::Agent::Assess::Tracker.tracked?(local_value)
75
79
  next unless erb_template_prerender.include?(local_name.to_s)
76
80
 
77
81
  start_index = ret.index(local_value)
78
82
  next if start_index.nil?
79
83
 
80
- ret.cs__copy_from(local_value, start_index)
84
+ properties.copy_from(local_value, ret, start_index)
81
85
  interpolated_inputs << local_name
82
86
  end
83
87
  end