contrast-agent 4.11.0 → 4.14.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +4 -4
  2. data/.simplecov +1 -0
  3. data/ext/cs__assess_module/cs__assess_module.c +48 -0
  4. data/ext/cs__assess_module/cs__assess_module.h +7 -0
  5. data/ext/cs__common/cs__common.c +24 -7
  6. data/ext/cs__common/cs__common.h +12 -2
  7. data/ext/cs__contrast_patch/cs__contrast_patch.c +48 -11
  8. data/ext/cs__contrast_patch/cs__contrast_patch.h +5 -2
  9. data/ext/cs__os_information/cs__os_information.c +31 -0
  10. data/ext/cs__os_information/cs__os_information.h +7 -0
  11. data/ext/{cs__protect_kernel → cs__os_information}/extconf.rb +0 -0
  12. data/lib/contrast/agent/assess/contrast_event.rb +1 -1
  13. data/lib/contrast/agent/assess/contrast_object.rb +1 -1
  14. data/lib/contrast/agent/assess/policy/dynamic_source_factory.rb +2 -0
  15. data/lib/contrast/agent/assess/policy/policy_node.rb +6 -6
  16. data/lib/contrast/agent/assess/policy/policy_scanner.rb +5 -0
  17. data/lib/contrast/agent/assess/policy/preshift.rb +19 -6
  18. data/lib/contrast/agent/assess/policy/propagation_method.rb +2 -116
  19. data/lib/contrast/agent/assess/policy/propagation_node.rb +4 -4
  20. data/lib/contrast/agent/assess/policy/propagator/center.rb +1 -1
  21. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +2 -0
  22. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +2 -154
  23. data/lib/contrast/agent/assess/policy/source_method.rb +2 -71
  24. data/lib/contrast/agent/assess/policy/trigger_method.rb +45 -110
  25. data/lib/contrast/agent/assess/policy/trigger_node.rb +62 -21
  26. data/lib/contrast/agent/assess/policy/trigger_validation/xss_validator.rb +1 -1
  27. data/lib/contrast/agent/assess/property/tagged.rb +66 -189
  28. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +40 -6
  29. data/lib/contrast/agent/deadzone/policy/policy.rb +6 -0
  30. data/lib/contrast/agent/inventory/dependency_usage_analysis.rb +1 -0
  31. data/lib/contrast/agent/metric_telemetry_event.rb +26 -0
  32. data/lib/contrast/agent/middleware.rb +14 -62
  33. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +0 -1
  34. data/lib/contrast/agent/patching/policy/method_policy.rb +3 -44
  35. data/lib/contrast/agent/patching/policy/method_policy_extend.rb +111 -0
  36. data/lib/contrast/agent/patching/policy/patch.rb +37 -238
  37. data/lib/contrast/agent/patching/policy/patcher.rb +15 -50
  38. data/lib/contrast/agent/reporting/report.rb +21 -0
  39. data/lib/contrast/agent/reporting/reporter.rb +142 -0
  40. data/lib/contrast/agent/reporting/reporting_events/finding.rb +90 -0
  41. data/lib/contrast/agent/reporting/reporting_events/preflight.rb +25 -0
  42. data/lib/contrast/agent/reporting/reporting_events/preflight_message.rb +56 -0
  43. data/lib/contrast/agent/reporting/reporting_events/reporting_event.rb +37 -0
  44. data/lib/contrast/agent/reporting/reporting_utilities/audit.rb +127 -0
  45. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +168 -0
  46. data/lib/contrast/agent/reporting/reporting_utilities/reporting_storage.rb +66 -0
  47. data/lib/contrast/agent/request.rb +2 -81
  48. data/lib/contrast/agent/request_context.rb +18 -126
  49. data/lib/contrast/agent/request_context_extend.rb +138 -0
  50. data/lib/contrast/agent/request_handler.rb +7 -3
  51. data/lib/contrast/agent/response.rb +2 -73
  52. data/lib/contrast/agent/rule_set.rb +2 -4
  53. data/lib/contrast/agent/startup_metrics_telemetry_event.rb +94 -0
  54. data/lib/contrast/agent/static_analysis.rb +5 -3
  55. data/lib/contrast/agent/telemetry.rb +137 -0
  56. data/lib/contrast/agent/telemetry_event.rb +33 -0
  57. data/lib/contrast/agent/thread_watcher.rb +66 -11
  58. data/lib/contrast/agent/version.rb +1 -1
  59. data/lib/contrast/agent.rb +21 -1
  60. data/lib/contrast/api/communication/connection_status.rb +10 -7
  61. data/lib/contrast/api/communication/messaging_queue.rb +37 -3
  62. data/lib/contrast/api/communication/response_processor.rb +15 -8
  63. data/lib/contrast/api/communication/service_lifecycle.rb +13 -3
  64. data/lib/contrast/api/communication/socket.rb +6 -8
  65. data/lib/contrast/api/communication/socket_client.rb +29 -12
  66. data/lib/contrast/api/communication/speedracer.rb +37 -1
  67. data/lib/contrast/api/communication/tcp_socket.rb +4 -3
  68. data/lib/contrast/api/communication/unix_socket.rb +1 -0
  69. data/lib/contrast/api/decorators/finding.rb +45 -0
  70. data/lib/contrast/components/api.rb +90 -0
  71. data/lib/contrast/components/app_context.rb +10 -41
  72. data/lib/contrast/components/app_context_extend.rb +78 -0
  73. data/lib/contrast/components/assess.rb +7 -0
  74. data/lib/contrast/components/base.rb +23 -0
  75. data/lib/contrast/components/config.rb +92 -13
  76. data/lib/contrast/components/contrast_service.rb +11 -0
  77. data/lib/contrast/components/sampling.rb +2 -2
  78. data/lib/contrast/config/agent_configuration.rb +1 -1
  79. data/lib/contrast/config/api_configuration.rb +27 -0
  80. data/lib/contrast/config/api_proxy_configuration.rb +14 -0
  81. data/lib/contrast/config/application_configuration.rb +2 -3
  82. data/lib/contrast/config/assess_configuration.rb +3 -2
  83. data/lib/contrast/config/base_configuration.rb +17 -28
  84. data/lib/contrast/config/certification_configuration.rb +15 -0
  85. data/lib/contrast/config/env_variables.rb +18 -0
  86. data/lib/contrast/config/heap_dump_configuration.rb +6 -6
  87. data/lib/contrast/config/inventory_configuration.rb +1 -5
  88. data/lib/contrast/config/protect_rule_configuration.rb +1 -1
  89. data/lib/contrast/config/request_audit_configuration.rb +18 -0
  90. data/lib/contrast/config/root_configuration.rb +1 -0
  91. data/lib/contrast/config/ruby_configuration.rb +6 -6
  92. data/lib/contrast/config/service_configuration.rb +2 -2
  93. data/lib/contrast/config.rb +1 -1
  94. data/lib/contrast/configuration.rb +4 -2
  95. data/lib/contrast/extension/assess/array.rb +5 -7
  96. data/lib/contrast/framework/manager.rb +22 -44
  97. data/lib/contrast/framework/manager_extend.rb +50 -0
  98. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +9 -6
  99. data/lib/contrast/framework/rails/patch/support.rb +31 -29
  100. data/lib/contrast/framework/rails/railtie.rb +1 -1
  101. data/lib/contrast/framework/sinatra/support.rb +2 -1
  102. data/lib/contrast/logger/application.rb +4 -0
  103. data/lib/contrast/logger/log.rb +8 -103
  104. data/lib/contrast/utils/assess/propagation_method_utils.rb +129 -0
  105. data/lib/contrast/utils/assess/property/tagged_utils.rb +165 -0
  106. data/lib/contrast/utils/assess/source_method_utils.rb +83 -0
  107. data/lib/contrast/utils/assess/tracking_util.rb +20 -15
  108. data/lib/contrast/utils/assess/trigger_method_utils.rb +138 -0
  109. data/lib/contrast/utils/class_util.rb +65 -54
  110. data/lib/contrast/utils/exclude_key.rb +20 -0
  111. data/lib/contrast/utils/findings.rb +62 -0
  112. data/lib/contrast/utils/hash_digest.rb +10 -73
  113. data/lib/contrast/utils/hash_digest_extend.rb +86 -0
  114. data/lib/contrast/utils/head_dump_utils_extend.rb +74 -0
  115. data/lib/contrast/utils/heap_dump_util.rb +2 -65
  116. data/lib/contrast/utils/invalid_configuration_util.rb +29 -0
  117. data/lib/contrast/utils/io_util.rb +1 -1
  118. data/lib/contrast/utils/log_utils.rb +108 -0
  119. data/lib/contrast/utils/lru_cache.rb +4 -2
  120. data/lib/contrast/utils/metrics_hash.rb +59 -0
  121. data/lib/contrast/utils/middleware_utils.rb +87 -0
  122. data/lib/contrast/utils/net_http_base.rb +158 -0
  123. data/lib/contrast/utils/object_share.rb +1 -0
  124. data/lib/contrast/utils/os.rb +23 -0
  125. data/lib/contrast/utils/patching/policy/patch_utils.rb +232 -0
  126. data/lib/contrast/utils/patching/policy/patcher_utils.rb +54 -0
  127. data/lib/contrast/utils/request_utils.rb +88 -0
  128. data/lib/contrast/utils/response_utils.rb +97 -0
  129. data/lib/contrast/utils/substitution_utils.rb +167 -0
  130. data/lib/contrast/utils/tag_util.rb +9 -9
  131. data/lib/contrast/utils/telemetry.rb +79 -0
  132. data/lib/contrast/utils/telemetry_client.rb +90 -0
  133. data/lib/contrast/utils/telemetry_identifier.rb +130 -0
  134. data/lib/contrast.rb +19 -1
  135. data/resources/assess/policy.json +12 -6
  136. data/resources/deadzone/policy.json +86 -5
  137. data/ruby-agent.gemspec +7 -6
  138. data/service_executables/VERSION +1 -1
  139. data/service_executables/linux/contrast-service +0 -0
  140. data/service_executables/mac/contrast-service +0 -0
  141. metadata +68 -26
  142. data/ext/cs__protect_kernel/cs__protect_kernel.c +0 -47
  143. data/ext/cs__protect_kernel/cs__protect_kernel.h +0 -12
  144. data/lib/contrast/config/default_value.rb +0 -17
  145. data/lib/contrast/extension/protect/kernel.rb +0 -29
@@ -24,7 +24,7 @@ module Contrast
24
24
  end
25
25
 
26
26
  # find original in the target, copy tags to the new position in target
27
- original_start_index = target[0..target.length / 2 + 1].rindex(source1)
27
+ original_start_index = target[0..(target.length / 2) + 1].rindex(source1)
28
28
  original_start_index ||= 1
29
29
  properties.copy_from(source1, target, original_start_index, propagation_node.untags)
30
30
 
@@ -13,6 +13,8 @@ module Contrast
13
13
  class DatabaseWrite < Contrast::Agent::Assess::Policy::Propagator::Base
14
14
  class << self
15
15
  def propagate propagation_node, preshift, target
16
+ return unless Contrast::ASSESS.require_dynamic_sources?
17
+
16
18
  class_type = preshift.object.cs__class
17
19
  class_name = class_type.cs__name
18
20
  tainted_columns = {}
@@ -3,6 +3,7 @@
3
3
 
4
4
  require 'contrast/components/logger'
5
5
  require 'contrast/utils/duck_utils'
6
+ require 'contrast/utils/substitution_utils'
6
7
 
7
8
  module Contrast
8
9
  module Agent
@@ -17,9 +18,7 @@ module Contrast
17
18
  class Substitution
18
19
  include Contrast::Components::Logger::InstanceMethods
19
20
  extend Contrast::Components::Logger::InstanceMethods
20
-
21
- CAPTURE_GROUP_REGEXP = /\\[[:digit:]]/.cs__freeze
22
- CAPTURE_NAME_REGEXP = /\\k<[[:alpha:]]/.cs__freeze
21
+ extend Contrast::Utils::SubstitutionUtils
23
22
 
24
23
  class << self
25
24
  # gsub is hard. there are four versions of this method
@@ -38,157 +37,6 @@ module Contrast
38
37
  def sub_tagger patcher, preshift, ret, block
39
38
  substitution_tagger(patcher, preshift, ret, !block.nil?, false)
40
39
  end
41
-
42
- private
43
-
44
- def substitution_tagger patcher, preshift, ret, block, global = true
45
- return ret unless ret
46
-
47
- begin
48
- source = preshift.object
49
- self_tracked = Contrast::Agent::Assess::Tracker.tracked?(source)
50
- args = preshift.args[1]
51
- incoming_tracked = args && determine_tracked(args)
52
- return ret unless self_tracked || incoming_tracked
53
-
54
- parent_events = []
55
- if block
56
- block_sub(self_tracked, source, ret)
57
- elsif args.is_a?(String)
58
- string_sub(parent_events, self_tracked, preshift, ret, args, incoming_tracked, global)
59
- elsif args.is_a?(Hash)
60
- hash_sub(self_tracked, source, ret)
61
- else # Enumerator, only for gsub
62
- pattern_gsub(parent_events, preshift, ret)
63
- end
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)
71
- rescue StandardError => e
72
- logger.error('Unable to apply gsub propagator', e)
73
- end
74
- ret
75
- end
76
-
77
- def determine_tracked args
78
- # if there's no arg, it can't be tracked
79
- return false unless args
80
-
81
- # if it's a string, just ask if it's tracked
82
- case args
83
- when String
84
- Contrast::Agent::Assess::Tracker.tracked?(args)
85
- # if it's a hash, ask if it has a tracked string
86
- when Hash
87
- args.values.any? { |value| value.is_a?(String) && Contrast::Agent::Assess::Tracker.tracked?(value) }
88
- # this should never happen
89
- else
90
- false
91
- end
92
- end
93
-
94
- def string_sub parent_events, self_tracked, preshift, ret, incoming, incoming_tracked, global
95
- return unless (properties = Contrast::Agent::Assess::Tracker.properties!(ret))
96
-
97
- incoming_properties = Contrast::Agent::Assess::Tracker.properties(incoming)
98
- parent_events << incoming_properties&.event if incoming_properties&.event
99
-
100
- source = preshift.object
101
-
102
- # We can't efficiently find the places that things were
103
- # copied from regexp / captures. Trading accuracy for
104
- # performance
105
- if incoming.match?(CAPTURE_GROUP_REGEXP) || incoming.match?(CAPTURE_NAME_REGEXP)
106
- properties.splat_from(source, ret) if self_tracked
107
- return
108
- end
109
-
110
- # if it's just a straight insert, that we can do
111
- # Copy the tags from us to the return
112
- ranges = find_string_sub_insert(properties, preshift, incoming, ret, global)
113
-
114
- properties.delete_tags_at_ranges(ranges)
115
- properties.shift_tags(ranges)
116
- return unless incoming_tracked
117
- return unless incoming_properties
118
-
119
- tags = incoming_properties.tag_keys
120
- ranges.each do |range|
121
- tags.each do |tag|
122
- properties.add_tag(tag, range)
123
- end
124
- end
125
- end
126
-
127
- # Find the points at which the new String was placed into the original
128
- #
129
- # @param properties [Contrast::Agent::Assess::Properties] the Properties of the ret
130
- # @param preshift [Contrast::Agent::Assess::PreShift] the capture of the state of the code just prior to
131
- # the invocation of the patched method
132
- # @param incoming [String] the new String going into the substitution
133
- # @param ret [String] the result of the substitution
134
- # @param global [Boolean] if this was a global or single substitution
135
- # @return [Array<Range>] the Ranges where substitution occurred
136
- def find_string_sub_insert properties, preshift, incoming, ret, global
137
- pattern = preshift.args[0]
138
- source = preshift.object
139
-
140
- properties.copy_from(source, ret)
141
- # Figure out where inserts occurred
142
- last_idx = 0
143
- ranges = []
144
- # For each insert, move the tag ranges
145
- while last_idx
146
- idx = source.index(pattern, last_idx)
147
- break unless idx
148
-
149
- last_idx = idx ? idx + 1 : nil
150
- start_index = idx
151
- end_index = idx + incoming.length
152
- ranges << (start_index...end_index)
153
- break unless global
154
- end
155
- ranges
156
- end
157
-
158
- def block_sub self_tracked, source, ret
159
- return unless self_tracked
160
-
161
- properties = Contrast::Agent::Assess::Tracker.properties!(ret)
162
- properties&.splat_from(source, ret)
163
- end
164
-
165
- def hash_sub self_tracked, source, ret
166
- return unless self_tracked
167
-
168
- properties = Contrast::Agent::Assess::Tracker.properties!(ret)
169
- properties&.splat_from(source, ret)
170
- end
171
-
172
- def pattern_gsub parent_events, preshift, ret
173
- source = preshift.object
174
- return unless (source_properties = Contrast::Agent::Assess::Tracker.properties(source))
175
- return unless (properties = Contrast::Agent::Assess::Tracker.properties!(ret))
176
-
177
- source_properties.tag_keys.each do |key|
178
- properties.add_tag(key, 0...1)
179
- end
180
- parent_event = source_properties.event
181
- parent_events << parent_event if parent_event
182
- end
183
-
184
- def string_build_event parent_events, patcher, preshift, ret
185
- return unless Contrast::Agent::Assess::Tracker.tracked?(ret)
186
-
187
- properties = Contrast::Agent::Assess::Tracker.properties(ret)
188
- args = preshift.args
189
- properties.build_event(patcher, ret, preshift.object, ret, args, 2)
190
- properties.event.instance_variable_set(:@_parent_events, parent_events)
191
- end
192
40
  end
193
41
  end
194
42
  end
@@ -6,6 +6,7 @@ require 'contrast/agent/assess/policy/source_validation/source_validation'
6
6
  require 'contrast/components/logger'
7
7
  require 'contrast/utils/object_share'
8
8
  require 'contrast/utils/sha256_builder'
9
+ require 'contrast/utils/assess/source_method_utils'
9
10
 
10
11
  module Contrast
11
12
  module Agent
@@ -16,6 +17,7 @@ module Contrast
16
17
  # used in Assess vulnerability detection.
17
18
  module SourceMethod
18
19
  extend Contrast::Components::Logger::InstanceMethods
20
+ extend Contrast::Utils::Assess::SourceMethodUtils
19
21
 
20
22
  PARAMETER_TYPE = 'PARAMETER'
21
23
  PARAMETER_KEY_TYPE = 'PARAMETER_KEY'
@@ -133,15 +135,6 @@ module Contrast
133
135
  !Contrast::Agent::Assess::Tracker.trackable?(key)
134
136
  end
135
137
 
136
- # Safely duplicate the target, or return nil
137
- #
138
- # @param target [Object] the thing to check for duplication
139
- def safe_dup target
140
- target.dup
141
- rescue StandardError => _e
142
- nil
143
- end
144
-
145
138
  # Hash is designed to keep one instance of the string key in it. We need to remove the existing one and
146
139
  # replace it with our new tracked one.
147
140
  def handle_hash_key target, to_replace
@@ -174,68 +167,6 @@ module Contrast
174
167
  properties.build_event(source_node, target, object, ret, args, source_type, source_name)
175
168
  end
176
169
 
177
- # Find the name of the source
178
- #
179
- # @param source_node [Contrast::Agent::Assess::Policy::SourceNode] the node to direct applying this source
180
- # event
181
- # @param object [Object] the Object on which the method was invoked
182
- # @param ret [Object] the Return of the invoked method
183
- # @param args [Array<Object>] the Arguments with which the method was invoked
184
- # @return [String, nil] the human readable name of the target to which this source event applies, or nil if
185
- # none provided by the node
186
- def determine_source_name source_node, object, ret, *args
187
- return source_node.get_property('dynamic_source_name') if source_node.type == 'UNTRUSTED_DATABASE'
188
-
189
- source_node_source = source_node.sources[0]
190
- case source_node_source
191
- when nil
192
- nil
193
- when Contrast::Utils::ObjectShare::RETURN_KEY
194
- ret
195
- when Contrast::Utils::ObjectShare::OBJECT_KEY
196
- object
197
- else
198
- args[source_node_source]
199
- end
200
- end
201
-
202
- # Determine if we should analyze this method invocation for a Source or not. We should if we have enough
203
- # information to build the context of this invocation, we're not disabled, and we can't immediately
204
- # determine the invocation was done safely.
205
- #
206
- # @param method_policy [Contrast::Agent::Patching::Policy::MethodPolicy] the policy that applies to the
207
- # method being called
208
- # @param object [Object] the Object on which the method was invoked
209
- # @param ret [Object] the Return of the invoked method
210
- # @param args [Array<Object>] the Arguments with which the method was invoked
211
- # @return [boolean] if the invocation of this method should be analyzed
212
- def analyze? method_policy, object, ret, args
213
- return false unless method_policy&.source_node
214
- return false unless ::Contrast::ASSESS.enabled?
215
- return false unless Contrast::Agent::REQUEST_TRACKER.current&.analyze_request?
216
-
217
- !safe_invocation?(method_policy.source_node, object, ret, args)
218
- end
219
-
220
- # Determine if the method was invoked safely.
221
- #
222
- # @param source_node [Contrast::Agent::Assess::Policy::SourceNode] the node to direct applying this source
223
- # event
224
- # @param _object [Object] the Object on which the method was invoked
225
- # @param _ret [Object] the Return of the invoked method
226
- # @param args [Array<Object>] the Arguments with which the method was invoked
227
- # @return [boolean] if the invocation of this method was safe
228
- def safe_invocation? source_node, _object, _ret, args
229
- # According the the Rack Specification https://github.com/rack/rack/blob/master/SPEC.rdoc, any header
230
- # from the Request will start with HTTP_. As such, only Headers with that key should be considered for
231
- # tracking, as the others have come from the Framework or Middleware stashing in the ENV. Rails, for
232
- # instance, uses action_dispatch. to store several values. Technically, you can't call
233
- # Rack::Request#get_header without a parameter, and that parameter should be a String, but trust no one.
234
- source_node.id == 'Assess:Source:Rack::Request::Env#get_header' &&
235
- args&.any? &&
236
- !args[0].to_s.start_with?('HTTP_')
237
- end
238
-
239
170
  # Find the literal target of the propagation
240
171
  #
241
172
  # @param source_node [Contrast::Agent::Assess::Policy::SourceNode] the node to direct applying this source
@@ -6,6 +6,8 @@ require 'contrast/agent/assess/policy/trigger_validation/trigger_validation'
6
6
  require 'contrast/components/logger'
7
7
  require 'contrast/utils/object_share'
8
8
  require 'contrast/utils/sha256_builder'
9
+ require 'contrast/utils/assess/trigger_method_utils'
10
+ require 'contrast/agent/reporting/reporting_utilities/reporting_storage'
9
11
 
10
12
  module Contrast
11
13
  module Agent
@@ -15,8 +17,9 @@ module Contrast
15
17
  # Contrast::Agent::Assess::Policy::TriggerNode class. Each such method will call to this module just after
16
18
  # invocation in order to determine if the call was done safely. In those cases where it was not, a Finding
17
19
  # report is issued to the Service.
18
- module TriggerMethod
20
+ module TriggerMethod # rubocop:disable Metrics/ModuleLength
19
21
  extend Contrast::Components::Logger::InstanceMethods
22
+ extend Contrast::Utils::Assess::TriggerMethodUtils
20
23
 
21
24
  # The level of TeamServer compliance our traces meet when in the abnormal condition of being dataflow rules
22
25
  # without routes.
@@ -50,6 +53,14 @@ module Contrast
50
53
  apply_trigger(trigger_node, source, object, ret, *args)
51
54
  end
52
55
 
56
+ def append_to_finding finding, trigger_node, source, object, ret, request, *args
57
+ finding.rule_id = Contrast::Utils::StringUtils.protobuf_safe_string(trigger_node.rule_id)
58
+ finding.version = determine_compliance_version(finding)
59
+ append_events(finding, trigger_node, source, object, ret, args)
60
+ append_route(finding, request)
61
+ append_hash(finding, source, request)
62
+ end
63
+
53
64
  # This converts the source of the finding, and the events leading up to it into a Finding
54
65
  #
55
66
  # @param trigger_node [Contrast::Agent::Assess::Policy::TriggerNode] the node to direct applying this
@@ -61,18 +72,21 @@ module Contrast
61
72
  # @return [Contrast::Api::Dtm::Finding, nil] the Contrast::Api::Dtm::Finding to send to TeamServer or
62
73
  # nil if conditions were not met
63
74
  def build_finding trigger_node, source, object, ret, *args
75
+ content_type = Contrast::Agent::REQUEST_TRACKER.current&.response&.content_type
76
+
77
+ if content_type.nil? && trigger_node.collectable?
78
+ Contrast::Agent::FINDINGS.collect_finding trigger_node, source, object, ret, *args
79
+ return
80
+ end
81
+
64
82
  return unless Contrast::Agent::Assess::Policy::TriggerValidation.valid?(trigger_node, object, ret, args)
65
83
 
66
84
  request = find_request(source)
67
85
  return unless reportable?(request&.env)
68
86
 
87
+ handle_new_finding trigger_node, source, object, ret, request, *args
69
88
  finding = Contrast::Api::Dtm::Finding.new
70
- finding.rule_id = Contrast::Utils::StringUtils.protobuf_safe_string(trigger_node.rule_id)
71
-
72
- append_events(finding, trigger_node, source, object, ret, args)
73
- append_route(finding, request)
74
- append_hash(finding, source, request)
75
- finding.version = determine_compliance_version(finding)
89
+ append_to_finding finding, trigger_node, source, object, ret, request, *args
76
90
  logger.trace('Finding created', node_id: trigger_node.id, source_id: source.__id__,
77
91
  rule: trigger_node.rule_id)
78
92
  report_finding(finding, request)
@@ -84,6 +98,8 @@ module Contrast
84
98
  # activity message does not exist, b/c we're invoked outside of a request context, build an activity and
85
99
  # immediately report it with the finding.
86
100
  #
101
+ # TODO: RUBY-1351
102
+ #
87
103
  # @param finding [Contrast::Api::Dtm::Finding] the Finding to report.
88
104
  # @param request [Contrast::Agent::Request] our wrapper around the Rack::Request.
89
105
  def report_finding finding, request = nil
@@ -106,66 +122,37 @@ module Contrast
106
122
  Contrast::Agent.messaging_queue.send_event_eventually(activity)
107
123
  end
108
124
 
109
- private
125
+ def handle_new_finding trigger_node, source, object, ret, request, *args
126
+ return unless Contrast::Agent::Reporter.enabled?
110
127
 
111
- # A request is reportable if it is not from ActionController::Live
112
- #
113
- # @param env [Hash] the env of the Request
114
- # @return [Boolean]
115
- def reportable? env
116
- !(defined?(ActionController::Live) &&
117
- env &&
118
- env['action_controller.instance'].cs__class.included_modules.include?(ActionController::Live))
128
+ new_finding_and_reporting trigger_node, source, object, ret, request, *args
119
129
  end
120
130
 
121
- # Find the request for this finding. This assumes, for now, that if there is an active request, then that
122
- # is the request to report. Otherwise, we'll use the first request found in the events of the
123
- # source_object.
124
- #
125
- # @param source [Object,nil] some Object used as the source of a trigger event
126
- # @return [Contrast::Agent::Request,nil] the request from which the dataflow on the request originated.
127
- def find_request source
128
- return Contrast::Agent::REQUEST_TRACKER.current.request if Contrast::Agent::REQUEST_TRACKER.current
129
- return unless (properties = Contrast::Agent::Assess::Tracker.properties(source))
130
-
131
- properties.events.each do |event|
132
- next unless event.cs__is_a?(Contrast::Agent::Assess::Events::SourceEvent)
133
-
134
- return event.request if event.request
135
- end
136
- nil
131
+ def new_finding_and_reporting trigger_node, source, object, ret, request, *args
132
+ # sent to reporter
133
+ # here we will generate new type of finding
134
+ ruby_finding = Contrast::Agent::Reporting::Finding.new trigger_node.rule_id
135
+ ruby_finding.attach_data trigger_node, source, object, ret, request, *args
136
+ hash_code = Contrast::Utils::HashDigest.generate_event_hash(ruby_finding, source, request)
137
+ ruby_finding.hash_code = hash_code
138
+ # save the current finding
139
+ Contrast::Agent::Reporting::ReportingStorage[hash_code] = ruby_finding
140
+
141
+ new_preflight = Contrast::Agent::Reporting::Preflight.new
142
+ new_preflight_message = Contrast::Agent::Reporting::PreflightMessage.new
143
+ new_preflight_message.routes << request
144
+ new_preflight_message.hash_code = hash_code
145
+ new_preflight_message.data = "#{ trigger_node.rule_id },#{ hash_code }"
146
+ new_preflight.messages << new_preflight_message
147
+ Contrast::Agent.reporter_queue.send_event_immediately(new_preflight)
137
148
  end
138
149
 
150
+ private
151
+
139
152
  def settings
140
153
  Contrast::Agent::FeatureState.instance
141
154
  end
142
155
 
143
- # This is our method that actually checks the taint on the object our trigger_node targets.
144
- #
145
- # @param trigger_node [Contrast::Agent::Assess::Policy::TriggerNode] the node to direct applying this
146
- # trigger event
147
- # @param source [Object] the source of the Trigger Event
148
- # @param object [Object] the Object on which the method was invoked
149
- # @param ret [Object] the Return of the invoked method
150
- # @param args [Array<Object>] the Arguments with which the method was invoked
151
- def apply_trigger trigger_node, source, object, ret, *args
152
- return unless trigger_node
153
- return if trigger_node.rule_disabled?
154
- return if trigger_node.dataflow? && source.nil?
155
-
156
- if trigger_node.regexp_rule?
157
- apply_regexp_rule(trigger_node, source, object, ret, *args)
158
- elsif trigger_node.custom_trigger?
159
- trigger_node.apply_custom_trigger(trigger_node, source, object, ret, *args)
160
- elsif trigger_node.dataflow?
161
- apply_dataflow_rule(trigger_node, source, object, ret, *args)
162
- else # trigger rule - just calling the method is dangerous
163
- build_finding(trigger_node, source, object, ret, *args)
164
- end
165
- rescue StandardError => e
166
- logger.warn('Unable to apply trigger', e, node_id: trigger_node.id)
167
- end
168
-
169
156
  # Given the marker from the trigger_node (the pointer indicating the entity from which the taint
170
157
  # originated), return the entity on which this trigger needs to operate.
171
158
  #
@@ -199,58 +186,6 @@ module Contrast
199
186
  end
200
187
  end
201
188
 
202
- # This is our method that actually checks the taint on the object our trigger_node targets for our Regexp
203
- # based rules.
204
- #
205
- # @param trigger_node [Contrast::Agent::Assess::Policy::TriggerNode] the node to direct applying this
206
- # trigger event
207
- # @param source [Object] the source of the Trigger Event
208
- # @param object [Object] the Object on which the method was invoked
209
- # @param ret [Object] the Return of the invoked method
210
- # @param args [Array<Object>] the Arguments with which the method was invoked
211
- def apply_regexp_rule trigger_node, source, object, ret, *args
212
- return unless source.is_a?(String)
213
- return if trigger_node.good_value && source.match?(trigger_node.good_value)
214
- return if trigger_node.bad_value && source !~ trigger_node.bad_value
215
-
216
- build_finding(trigger_node, source, object, ret, *args)
217
- end
218
-
219
- # This is our method that actually checks the taint on the object our trigger_node targets for our Dataflow
220
- # based rules.
221
- #
222
- # @param trigger_node [Contrast::Agent::Assess::Policy::TriggerNode] the node to direct applying this
223
- # trigger event
224
- # @param source [Object] the source of the Trigger Event
225
- # @param object [Object] the Object on which the method was invoked
226
- # @param ret [Object] the Return of the invoked method
227
- # @param args [Array<Object>] the Arguments with which the method was invoked
228
- def apply_dataflow_rule trigger_node, source, object, ret, *args
229
- return unless source
230
-
231
- if Contrast::Agent::Assess::Tracker.trackable?(source)
232
- return unless Contrast::Agent::Assess::Tracker.tracked?(source)
233
- return unless trigger_node.violated?(source)
234
-
235
- build_finding(trigger_node, source, object, ret, *args)
236
- elsif Contrast::Utils::DuckUtils.iterable_hash?(source)
237
- source.each_pair do |key, value|
238
- apply_dataflow_rule(trigger_node, key, object, ret, *args)
239
- apply_dataflow_rule(trigger_node, value, object, ret, *args)
240
- end
241
- elsif Contrast::Utils::DuckUtils.iterable_enumerable?(source)
242
- source.each do |value|
243
- apply_dataflow_rule(trigger_node, value, object, ret, *args)
244
- end
245
- else
246
- logger.debug('Trigger source is untrackable. Unable to inspect.',
247
- node_id: trigger_node.id,
248
- source_id: source.__id__,
249
- source_type: source.cs__class.cs__name,
250
- frozen: source.cs__frozen?)
251
- end
252
- end
253
-
254
189
  def append_events finding, trigger_node, source, object, ret, args
255
190
  append_from_source(finding, source)
256
191
  finding.events << Contrast::Agent::Assess::Events::EventFactory.build(trigger_node, source, object, ret,
@@ -14,7 +14,7 @@ module Contrast
14
14
  # specifically for those methods which result in the trigger of a
15
15
  # vulnerability (indicate points in the application where uncontrolled
16
16
  # user input can do damage).
17
- class TriggerNode < PolicyNode
17
+ class TriggerNode < PolicyNode # rubocop:disable Metrics/ClassLength
18
18
  JSON_BAD_VALUE = 'bad_value'
19
19
  JSON_GOOD_VALUE = 'good_value'
20
20
  JSON_DISALLOWED_TAGS = 'disallowed_tags'
@@ -22,6 +22,10 @@ module Contrast
22
22
  JSON_RULE_NAME = 'name'
23
23
  JSON_CUSTOM_PATCH = 'custom_patch'
24
24
 
25
+ # Our list with rules to be collected and reported back when we have response
26
+ # from the application. Some rules rely on Content-Type validation.
27
+ COLLECTABLE_RULES = %w[reflected-xss].cs__freeze
28
+
25
29
  attr_reader :rule_id, :required_tags, :disallowed_tags, :good_value, :bad_value
26
30
 
27
31
  def initialize trigger_hash = {}, rule_hash = {}
@@ -67,6 +71,10 @@ module Contrast
67
71
  :TYPE_METHOD
68
72
  end
69
73
 
74
+ def collectable?
75
+ COLLECTABLE_RULES.include?(rule_id)
76
+ end
77
+
70
78
  def rule_disabled?
71
79
  ::Contrast::ASSESS.rule_disabled?(rule_id)
72
80
  end
@@ -104,8 +112,7 @@ module Contrast
104
112
 
105
113
  properties = Contrast::Agent::Assess::Tracker.properties(source)
106
114
  # find the ranges that violate the rule (untrusted, etc)
107
- vulnerable_ranges = ranges_with_all_tags(Contrast::Utils::StringUtils.ret_length(source), properties,
108
- required_tags)
115
+ vulnerable_ranges = ranges_with_all_tags(properties, required_tags)
109
116
  # if there aren't any vulnerable ranges, nope out
110
117
  return false if vulnerable_ranges.empty?
111
118
 
@@ -161,8 +168,8 @@ module Contrast
161
168
  @disallowed_tags << LIMITED_CHARS
162
169
  @disallowed_tags << CUSTOM_ENCODED
163
170
  @disallowed_tags << CUSTOM_VALIDATED
164
- @disallowed_tags << ENCODER_START + loud_name
165
- @disallowed_tags << VALIDATOR_START + loud_name
171
+ @disallowed_tags << (ENCODER_START + loud_name)
172
+ @disallowed_tags << (VALIDATOR_START + loud_name)
166
173
  end
167
174
 
168
175
  def validate_rule_tags tags
@@ -170,49 +177,56 @@ module Contrast
170
177
 
171
178
  tags.each do |tag|
172
179
  raise(ArgumentError, "Rule #{ rule_id } had an invalid tag. #{ tag } is not a known value.") unless
173
- Contrast::Api::Decorators::TraceTaintRangeTags::VALID_TAGS.include?(tag) ||
174
- Contrast::Api::Decorators::TraceTaintRangeTags::VALID_SOURCE_TAGS.include?(tag)
180
+ Contrast::Api::Decorators::TraceTaintRangeTags::VALID_TAGS.include?(tag) ||
181
+ Contrast::Api::Decorators::TraceTaintRangeTags::VALID_SOURCE_TAGS.include?(tag)
175
182
  end
176
183
  end
177
184
 
178
185
  # Find the ranges that satisfy all of the given tags.
179
186
  #
180
- # @param length [Integer] the length of the object which may have the
181
- # given tags -- used as the maximum index to search for all of the
182
- # tags.
183
187
  # @param properties [Contrast::Agent::Assess::Properties] the
184
188
  # properties to check for the tags
185
189
  # @param required_tags [Set<String>] the list of tags on which to match
186
190
  # @return [Array<Contrast::Agent::Assess::Tag>] the ranges satisfied
187
191
  # by the given conditions
188
- def ranges_with_all_tags length, properties, required_tags
192
+ def ranges_with_all_tags properties, required_tags
189
193
  return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless matches_tags?(properties, required_tags)
190
194
 
191
- ranges = []
192
195
  chunking = false
196
+ ranges = []
197
+ # find the start and end range of required tags:
198
+ search_ranges = find_required_ranges properties, required_tags
199
+ start_range = search_ranges.first
200
+ end_range = search_ranges.last + 1
201
+
193
202
  # find all the indicies on the source that have all the given tags
194
- (0..length).each do |idx|
195
- tags_at = properties.tags_at(idx).to_a
203
+ while start_range < end_range
204
+
205
+ tags_at = properties.tags_at(start_range).to_a
196
206
  # only those that have all the required tags in the tags_at
197
207
  # satisfy the requirement
198
208
  satisfied = tags_at.any? && required_tags.all? { |tag| tags_at.any? { |found| found.label == tag } }
199
209
  # if this range matches all the required tags and we're already
200
210
  # chunking, meaning the previous range matched, do nothing
201
- next if satisfied && chunking
202
211
 
203
212
  # if we are satisfied and we were not chunking, this represents
204
213
  # the start of the next range, so create a new entry.
205
214
  if satisfied
206
- ranges << Contrast::Agent::Assess::Tag.new('required', 0, idx)
215
+ if chunking
216
+ start_range += 1
217
+ next
218
+ end
219
+ ranges << Contrast::Agent::Assess::Tag.new('required', 0, start_range)
207
220
  chunking = true
208
- # if we are chunking and not satisfied, this represents the end
209
- # of the range, meaning the last index is what satisfied the
210
- # range. Because the range is exclusive end, we can just use this
211
- # index.
221
+ # if we are chunking and not satisfied, this represents the end
222
+ # of the range, meaning the last index is what satisfied the
223
+ # range. Because the range is exclusive end, we can just use this
224
+ # index.
212
225
  elsif chunking
213
- ranges[-1]&.update_end(idx)
226
+ ranges[-1]&.update_end(start_range)
214
227
  chunking = false
215
228
  end
229
+ start_range += 1
216
230
  end
217
231
  ranges
218
232
  end
@@ -265,6 +279,33 @@ module Contrast
265
279
 
266
280
  true
267
281
  end
282
+
283
+ # Range finder helper for #ranges_with_all_tags
284
+ #
285
+ # @param properties [Contrast::Agent::Assess::Properties] the properties to check for the tags
286
+ # @param required_tags [Set<String>] the list of tags on which to match
287
+ # @return [Array] of required tags ranges to search
288
+ def find_required_ranges properties, required_tags
289
+ start_range = 0
290
+ end_range = 0
291
+ required_tags_arr = required_tags.to_a
292
+ idx = 0
293
+
294
+ while idx < required_tags_arr.length
295
+ # find the start and end range of required tags:
296
+ start_temp = properties.fetch_tag(required_tags_arr[idx])[0].start_idx
297
+ end_temp = properties.fetch_tag(required_tags_arr[idx])[0].end_idx
298
+ # first iteration only
299
+ start_range = start_temp if idx.zero?
300
+ end_range = end_temp if idx.zero?
301
+
302
+ # find the tag with smallest ranges
303
+ start_range = start_temp if start_range < start_temp
304
+ end_range = end_temp if end_range > end_temp
305
+ idx += 1
306
+ end
307
+ [start_range, end_range]
308
+ end
268
309
  end
269
310
  end
270
311
  end