contrast-agent 5.3.0 → 6.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (161) hide show
  1. checksums.yaml +4 -4
  2. data/ext/cs__assess_array/cs__assess_array.c +7 -0
  3. data/ext/cs__assess_basic_object/cs__assess_basic_object.c +19 -5
  4. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.c +1 -1
  5. data/ext/cs__assess_hash/cs__assess_hash.c +3 -4
  6. data/ext/cs__assess_kernel/cs__assess_kernel.c +1 -2
  7. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +26 -12
  8. data/ext/cs__assess_module/cs__assess_module.c +1 -1
  9. data/ext/cs__assess_string/cs__assess_string.c +13 -1
  10. data/ext/cs__common/cs__common.c +12 -11
  11. data/ext/cs__contrast_patch/cs__contrast_patch.c +54 -43
  12. data/ext/cs__os_information/cs__os_information.c +13 -10
  13. data/ext/cs__scope/cs__scope.c +146 -97
  14. data/lib/contrast/agent/assess/finalizers/hash.rb +2 -0
  15. data/lib/contrast/agent/assess/policy/policy_node.rb +50 -27
  16. data/lib/contrast/agent/assess/policy/policy_node_utils.rb +51 -0
  17. data/lib/contrast/agent/assess/policy/preshift.rb +8 -2
  18. data/lib/contrast/agent/assess/policy/propagation_method.rb +47 -13
  19. data/lib/contrast/agent/assess/policy/propagator/buffer.rb +118 -0
  20. data/lib/contrast/agent/assess/policy/propagator/keep.rb +19 -4
  21. data/lib/contrast/agent/assess/policy/propagator/remove.rb +18 -2
  22. data/lib/contrast/agent/assess/policy/propagator/splat.rb +17 -3
  23. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +1 -1
  24. data/lib/contrast/agent/assess/policy/propagator/substitution_utils.rb +1 -1
  25. data/lib/contrast/agent/assess/policy/propagator/trim.rb +1 -1
  26. data/lib/contrast/agent/assess/policy/propagator.rb +1 -0
  27. data/lib/contrast/agent/assess/policy/source_method.rb +7 -7
  28. data/lib/contrast/agent/assess/policy/trigger_method.rb +3 -1
  29. data/lib/contrast/agent/assess/property/tagged.rb +1 -1
  30. data/lib/contrast/agent/assess/rule/response/auto_complete_rule.rb +1 -1
  31. data/lib/contrast/agent/assess/rule/response/body_rule.rb +3 -3
  32. data/lib/contrast/agent/assess/rule/response/cache_control_header_rule.rb +1 -1
  33. data/lib/contrast/agent/assess/rule/response/parameters_pollution_rule.rb +1 -1
  34. data/lib/contrast/agent/deadzone/policy/deadzone_node.rb +0 -7
  35. data/lib/contrast/agent/deadzone/policy/policy.rb +0 -6
  36. data/lib/contrast/agent/exclusion_matcher.rb +3 -3
  37. data/lib/contrast/agent/middleware.rb +3 -1
  38. data/lib/contrast/agent/patching/policy/patcher.rb +3 -3
  39. data/lib/contrast/agent/patching/policy/policy_node.rb +15 -2
  40. data/lib/contrast/agent/protect/exploitable_collection.rb +38 -0
  41. data/lib/contrast/agent/protect/input_analyzer/input_analyzer.rb +61 -8
  42. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +2 -1
  43. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +2 -2
  44. data/lib/contrast/agent/protect/rule/base.rb +37 -5
  45. data/lib/contrast/agent/protect/rule/base_service.rb +3 -1
  46. data/lib/contrast/agent/protect/rule/cmd_injection.rb +13 -0
  47. data/lib/contrast/agent/protect/rule/cmdi/cmdi_input_classification.rb +83 -0
  48. data/lib/contrast/agent/protect/rule/cmdi/cmdi_worth_watching.rb +64 -0
  49. data/lib/contrast/agent/protect/rule/http_method_tampering/http_method_tampering_input_classification.rb +96 -0
  50. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +8 -0
  51. data/lib/contrast/agent/protect/rule/no_sqli/no_sqli_input_classification.rb +231 -0
  52. data/lib/contrast/agent/protect/rule/no_sqli.rb +27 -0
  53. data/lib/contrast/agent/protect/rule/sqli/sqli_input_classification.rb +18 -54
  54. data/lib/contrast/agent/protect/rule/sqli/sqli_worth_watching.rb +1 -4
  55. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_input_classification.rb +82 -0
  56. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_matcher.rb +45 -0
  57. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +42 -0
  58. data/lib/contrast/agent/reporting/attack_result/attack_result.rb +63 -0
  59. data/lib/contrast/agent/reporting/attack_result/rasp_rule_sample.rb +52 -0
  60. data/lib/contrast/agent/reporting/attack_result/response_type.rb +29 -0
  61. data/lib/contrast/agent/reporting/attack_result/user_input.rb +87 -0
  62. data/lib/contrast/agent/reporting/masker/masker.rb +246 -0
  63. data/lib/contrast/agent/reporting/masker/masker_utils.rb +58 -0
  64. data/lib/contrast/agent/reporting/report.rb +2 -0
  65. data/lib/contrast/agent/reporting/reporter.rb +23 -11
  66. data/lib/contrast/agent/reporting/reporting_events/agent_startup.rb +30 -0
  67. data/lib/contrast/agent/reporting/reporting_events/application_inventory.rb +7 -3
  68. data/lib/contrast/agent/reporting/reporting_events/application_startup.rb +40 -0
  69. data/lib/contrast/agent/reporting/reporting_events/application_startup_instrumentation.rb +27 -0
  70. data/lib/contrast/agent/reporting/reporting_events/finding.rb +1 -1
  71. data/lib/contrast/agent/reporting/reporting_events/library_usage_observation.rb +5 -5
  72. data/lib/contrast/agent/reporting/reporting_events/observed_route.rb +9 -9
  73. data/lib/contrast/agent/reporting/reporting_events/preflight_message.rb +2 -1
  74. data/lib/contrast/agent/reporting/reporting_events/reporting_event.rb +2 -1
  75. data/lib/contrast/agent/reporting/reporting_events/route_coverage.rb +8 -6
  76. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +16 -5
  77. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client_utils.rb +27 -26
  78. data/lib/contrast/agent/reporting/reporting_utilities/reporting_storage.rb +1 -1
  79. data/lib/contrast/agent/reporting/reporting_utilities/response_extractor.rb +97 -0
  80. data/lib/contrast/agent/reporting/reporting_utilities/response_handler.rb +68 -6
  81. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_mode.rb +63 -0
  82. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_utils.rb +122 -96
  83. data/lib/contrast/agent/reporting/settings/application_settings.rb +9 -0
  84. data/lib/contrast/agent/reporting/settings/assess_server_feature.rb +5 -33
  85. data/lib/contrast/agent/reporting/settings/protect_server_feature.rb +1 -1
  86. data/lib/contrast/agent/reporting/settings/sampling.rb +36 -0
  87. data/lib/contrast/agent/reporting/settings/sensitive_data_masking.rb +110 -0
  88. data/lib/contrast/agent/reporting/settings/sensitive_data_masking_rule.rb +58 -0
  89. data/lib/contrast/agent/request_context.rb +1 -1
  90. data/lib/contrast/agent/request_context_extend.rb +1 -1
  91. data/lib/contrast/agent/request_handler.rb +4 -0
  92. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_base.rb +51 -0
  93. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_event.rb +36 -0
  94. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message.rb +97 -0
  95. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message_exception.rb +65 -0
  96. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_stack_frame.rb +47 -0
  97. data/lib/contrast/agent/{metric_telemetry_event.rb → telemetry/events/metric_telemetry_event.rb} +1 -1
  98. data/lib/contrast/agent/{startup_metrics_telemetry_event.rb → telemetry/events/startup_metrics_telemetry_event.rb} +3 -3
  99. data/lib/contrast/agent/{telemetry_event.rb → telemetry/events/telemetry_event.rb} +1 -1
  100. data/lib/contrast/agent/{telemetry.rb → telemetry/telemetry.rb} +32 -19
  101. data/lib/contrast/agent/thread_watcher.rb +1 -1
  102. data/lib/contrast/agent/version.rb +1 -1
  103. data/lib/contrast/agent.rb +3 -0
  104. data/lib/contrast/api/communication/speedracer.rb +1 -1
  105. data/lib/contrast/api/decorators/address.rb +1 -1
  106. data/lib/contrast/api/decorators/response_type.rb +30 -0
  107. data/lib/contrast/api/decorators.rb +1 -0
  108. data/lib/contrast/components/app_context.rb +0 -4
  109. data/lib/contrast/components/assess.rb +14 -0
  110. data/lib/contrast/components/protect.rb +2 -2
  111. data/lib/contrast/components/sampling.rb +7 -11
  112. data/lib/contrast/components/settings.rb +106 -8
  113. data/lib/contrast/config/agent_configuration.rb +13 -30
  114. data/lib/contrast/config/api_configuration.rb +4 -67
  115. data/lib/contrast/config/api_proxy_configuration.rb +2 -45
  116. data/lib/contrast/config/application_configuration.rb +9 -84
  117. data/lib/contrast/config/assess_configuration.rb +10 -69
  118. data/lib/contrast/config/assess_rules_configuration.rb +9 -38
  119. data/lib/contrast/config/base_configuration.rb +17 -50
  120. data/lib/contrast/config/certification_configuration.rb +6 -63
  121. data/lib/contrast/config/exception_configuration.rb +5 -52
  122. data/lib/contrast/config/heap_dump_configuration.rb +6 -64
  123. data/lib/contrast/config/inventory_configuration.rb +2 -49
  124. data/lib/contrast/config/logger_configuration.rb +0 -36
  125. data/lib/contrast/config/protect_configuration.rb +17 -7
  126. data/lib/contrast/config/protect_rule_configuration.rb +12 -30
  127. data/lib/contrast/config/protect_rules_configuration.rb +21 -26
  128. data/lib/contrast/config/request_audit_configuration.rb +6 -48
  129. data/lib/contrast/config/root_configuration.rb +52 -12
  130. data/lib/contrast/config/ruby_configuration.rb +0 -36
  131. data/lib/contrast/config/sampling_configuration.rb +1 -57
  132. data/lib/contrast/config/server_configuration.rb +0 -36
  133. data/lib/contrast/config/service_configuration.rb +5 -44
  134. data/lib/contrast/configuration.rb +2 -3
  135. data/lib/contrast/extension/assess/string.rb +20 -1
  136. data/lib/contrast/extension/module.rb +0 -1
  137. data/lib/contrast/framework/manager.rb +2 -2
  138. data/lib/contrast/logger/application.rb +1 -1
  139. data/lib/contrast/logger/cef_log.rb +1 -1
  140. data/lib/contrast/tasks/config.rb +90 -3
  141. data/lib/contrast/utils/assess/object_store.rb +36 -0
  142. data/lib/contrast/utils/assess/propagation_method_utils.rb +6 -0
  143. data/lib/contrast/utils/class_util.rb +3 -12
  144. data/lib/contrast/utils/input_classification.rb +73 -0
  145. data/lib/contrast/utils/log_utils.rb +1 -1
  146. data/lib/contrast/utils/middleware_utils.rb +9 -8
  147. data/lib/contrast/utils/net_http_base.rb +1 -1
  148. data/lib/contrast/utils/object_share.rb +2 -1
  149. data/lib/contrast/utils/os.rb +0 -5
  150. data/lib/contrast/utils/patching/policy/patch_utils.rb +4 -5
  151. data/lib/contrast/utils/response_utils.rb +18 -33
  152. data/lib/contrast/utils/telemetry.rb +1 -1
  153. data/lib/contrast/utils/telemetry_client.rb +1 -1
  154. data/lib/contrast/utils/telemetry_identifier.rb +1 -1
  155. data/resources/assess/policy.json +98 -0
  156. data/resources/deadzone/policy.json +0 -86
  157. data/ruby-agent.gemspec +9 -8
  158. data/service_executables/VERSION +1 -1
  159. data/service_executables/linux/contrast-service +0 -0
  160. data/service_executables/mac/contrast-service +0 -0
  161. metadata +73 -28
@@ -9,6 +9,7 @@ require 'contrast/utils/object_share'
9
9
  require 'contrast/utils/sha256_builder'
10
10
  require 'contrast/utils/assess/propagation_method_utils'
11
11
  require 'contrast/agent/assess/events/event_data'
12
+ require 'contrast/utils/assess/object_store'
12
13
 
13
14
  module Contrast
14
15
  module Agent
@@ -21,6 +22,8 @@ module Contrast
21
22
  extend Contrast::Components::Logger::InstanceMethods
22
23
  extend Contrast::Utils::Assess::PropagationMethodUtils
23
24
 
25
+ @properties = Contrast::Utils::Assess::ObjectStore.new
26
+
24
27
  class << self
25
28
  # @param method_policy [Contrast::Agent::Patching::Policy::MethodPolicy] the policy that governs the
26
29
  # patches to this method
@@ -33,10 +36,8 @@ module Contrast
33
36
  # @return [Object, nil] the tracked Return or nil if no changes were made; will replace the return of the
34
37
  # original function if not nil
35
38
  def apply_propagation method_policy, preshift, object, ret, args, block
36
- return unless method_policy.propagation_node
37
- return unless preshift
38
-
39
- propagation_node = method_policy.propagation_node
39
+ return unless (propagation_node = method_policy.propagation_node)
40
+ return unless propagation_node.use_original_object? || preshift
40
41
 
41
42
  target = determine_target(propagation_node, ret, object, args)
42
43
  propagation_data = Contrast::Agent::Assess::Events::EventData.new(nil, nil, object, ret, args)
@@ -211,20 +212,39 @@ module Contrast
211
212
  # double check that we were able to finalize the replaced return
212
213
  return unless Contrast::Agent::Assess::Tracker.trackable?(target)
213
214
  end
214
- propagation_class.propagate(propagation_node, preshift, target)
215
+
216
+ # If we are using the original object tracking, the preshift object is not created.
217
+ # Instead identify the source as the original object itself and propagate with it.
218
+ source = propagation_node.use_original_object? ? propagation_data.object : preshift
219
+ handle_propagation propagation_class, propagation_node, source, target
215
220
  update_properties restore_frozen_state, propagation_node, target, propagation_data, ret
216
221
  end
217
222
 
223
+ def handle_propagation propagation_class, propagation_node, source, target
224
+ if propagation_node.patch_method
225
+ propagation_class.send(propagation_node.patch_method, propagation_node, source, target)
226
+ else
227
+ propagation_class.propagate(propagation_node, source, target)
228
+ end
229
+ end
230
+
218
231
  def update_properties restore_frozen_state, propagation_node, target, propagation_data, ret
219
- # Once we've propagated, attempt to tag the target if there is a tag(s) to be applied
220
- apply_tags(propagation_node, target)
221
- # Even though we skipped propagating tags from the source if they were included in untags, the target may
222
- # have already had some on it. Let's go ahead and remove them. In this order, untags takes precedent over
223
- # tags; but we control both and there should never be a propagator that has a tag in its untag.
224
- apply_untags(propagation_node, target)
225
- return unless (properties = Contrast::Agent::Assess::Tracker.properties!(target))
232
+ if propagation_node.use_original_on_bang_method?
233
+ properties = use_original_object_properties propagation_data
226
234
 
227
- properties.add_properties(propagation_node.properties)
235
+ return unless properties
236
+ else
237
+ # Once we've propagated, attempt to tag the target if there is a tag(s) to be applied
238
+ apply_tags(propagation_node, target)
239
+ # Even though we skipped propagating tags from the source if they were included in untags, the target
240
+ # may have already had some on it. Let's go ahead and remove them. In this order, untags takes
241
+ # precedent over tags; but we control both and there should never be a propagator that has a
242
+ # tag in its untag.
243
+ apply_untags(propagation_node, target)
244
+ return unless (properties = Contrast::Agent::Assess::Tracker.properties!(target))
245
+
246
+ properties.add_properties(propagation_node.properties)
247
+ end
228
248
  event_data = Contrast::Agent::Assess::Events::EventData.new(propagation_node,
229
249
  target,
230
250
  propagation_data.object,
@@ -259,6 +279,20 @@ module Contrast
259
279
  ::Contrast::ASSESS.track_frozen_sources? &&
260
280
  propagation_node.targets[0] == Contrast::Utils::ObjectShare::RETURN_KEY
261
281
  end
282
+
283
+ # For certain bang methods we return the same object, no need to create expensive new properties.
284
+ #
285
+ # @param propagation_data [Contrast::Agent::Assess::Events::EventData] used to hold object, args
286
+ # and ret.
287
+ # @return properties [Contrast::Agent::Assess::Properties] the original properties transfered to
288
+ # target.
289
+ def use_original_object_properties propagation_data
290
+ unless @properties[propagation_data.object.__id__]
291
+ @properties[propagation_data.object.__id__] = Contrast::Agent::Assess::Tracker.properties!(
292
+ propagation_data.object)
293
+ end
294
+ @properties[propagation_data.object.__id__]
295
+ end
262
296
  end
263
297
  end
264
298
  # rubocop:enable Metrics/ModuleLength
@@ -0,0 +1,118 @@
1
+ # Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ require 'contrast/agent/assess/policy/propagator/select'
5
+
6
+ module Contrast
7
+ module Agent
8
+ module Assess
9
+ module Policy
10
+ module Propagator
11
+ # Propagation that results in all the tags of the source being
12
+ # applied to the target at the point of insertion. The target's
13
+ # preexisting tags are shifted to account for this insertion.
14
+ class Buffer < Contrast::Agent::Assess::Policy::Propagator::Base
15
+ class << self
16
+ # For the source, append its tags to the target.
17
+ # Once the tag is applied, shift it to the location of the insert
18
+ # Unlike additive propagation, this currently only supports one source
19
+ # We assume that insert changes the preshift target
20
+ def propagate_insert propagation_node, preshift, target
21
+ return unless (properties = Contrast::Agent::Assess::Tracker.properties!(target))
22
+
23
+ source = find_source(propagation_node.sources[0], preshift)
24
+
25
+ patcher_target = propagation_node.targets[0]
26
+ preshift_target = case patcher_target
27
+ when Contrast::Utils::ObjectShare::OBJECT_KEY
28
+ preshift.object
29
+ else
30
+ # this is hardly reached b/c insert supports only one source
31
+ # changed to second argument: string.insert[idx, string_to_insert]
32
+ # previously was args[int] => produces exception
33
+ preshift.args[1]
34
+ end
35
+
36
+ # in case of situations, where buffer is initialized with X size and it's not fully
37
+ preshift_target = preshift_target.get_string
38
+ target = target.get_string
39
+ # Find the first difference between the source to which
40
+ # we inserted and the result. That is the insertion
41
+ # point on which all tags need to be adjusted
42
+ # If the insertion point is the end of the string, preshift length is returned
43
+ # https://stackoverflow.com/questions/31714522/find-the-first-differing-character-between-two-strings-in-ruby
44
+ insert_point = (0...preshift_target.length).find do |i|
45
+ preshift_target[i] != target[i]
46
+ end || preshift_target.length
47
+ # Depending what's inserted, we might be wrong. For instance, inserting 'foo'
48
+ # into 'asdfasdf' could result in 'asdfoofasdf'. we'd be off by one b/c of the 'f'
49
+ insert_point = target.rindex(source, insert_point)
50
+ overflow = insert_point...(insert_point + source.length)
51
+
52
+ # handle shifting the inserted range
53
+ properties.shift_tags([overflow])
54
+
55
+ properties.copy_from(source, target, insert_point, propagation_node.untags)
56
+ properties.cleanup_tags
57
+ end
58
+
59
+ # This method will implement both KEEP and SPLAT actions.
60
+ #
61
+ # @param propagation_node [Contrast::Agent::Assess::Policy::PropagationNode]
62
+ # @param preshift [Contrast::Agent::Assess::Preshift]
63
+ # @param ret [Object] Return target from method invocation.
64
+ def buffer_keep_splat propagation_node, preshift, ret
65
+ # If IO::Buffer gets duplicated the dup buffer is not allocated,
66
+ # it's empty, So a way of doing it is to use the original object
67
+ # itself. The class is included in the preshift's UNDUPLICABLE_MODULES.
68
+ return unless preshift.object
69
+ # We need the return in both propagation cases. So this propagation is
70
+ # applied in the Contrast::Utils::Patching::PatchUtils.apply_post_patch.
71
+ # we use this for the IO::Buffer#get_value and it requires 2 args always.
72
+ return unless ret || Contrast::Agent::Assess::Tracker.tracked?(preshift.object)
73
+
74
+ if preshift.args[1] == 0 # rubocop:disable Style/NumericPredicate
75
+ # KEEP
76
+ Contrast::Agent::Assess::Policy::Propagator::Keep.propagate(propagation_node, preshift, ret)
77
+ else
78
+ # SPLAT
79
+ Contrast::Agent::Assess::Policy::Propagator::Splat.propagate(propagation_node, preshift, ret)
80
+ end
81
+ end
82
+
83
+ # This method will implement both SELECT and KEEP actions for
84
+ # IO::Buffer
85
+ #
86
+ # @param propagation_node [Contrast::Agent::Assess::Policy::PropagationNode]
87
+ # @param preshift [Contrast::Agent::Assess::Preshift]
88
+ # @param ret [Object] Return targer from method invocation.
89
+ # @param _block [nil, {}] block passed.
90
+ def propagate_keep_select propagation_node, preshift, ret, _block
91
+ # We need the return in both propagation cases. So this propagation is
92
+ # applied in the Contrast::Utils::Patching::PatchUtils.apply_post_patch.
93
+ # If IO::Buffer gets duplicated the dup buffer is not allocated,
94
+ # it's empty, So a way of doing it is to use the original object
95
+ # itself. The class is handled with no dup in preshift.
96
+ return unless (properties = Contrast::Agent::Assess::Tracker.properties!(ret)) &&
97
+ (source = preshift.object)
98
+
99
+ # KEEP means the tags just pass from the source to the target as is.
100
+ # There is a case with IO::Buffer use, when we don't pass any args,
101
+ # and the target (ret) in this case is the whole strings. We should
102
+ # use Keep action. Otherwise the SELECT action covers all other cases.
103
+ if preshift.args.empty?
104
+ # KEEP
105
+ properties.copy_from(source, ret, 0, propagation_node.untags)
106
+ ret
107
+ else
108
+ # SELECT
109
+ Contrast::Agent::Assess::Policy::Propagator::Select.select_tagger propagation_node, preshift, ret, nil
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
@@ -11,12 +11,27 @@ module Contrast
11
11
  # are unaffected beyond any merging of overlapping tags.
12
12
  class Keep < Contrast::Agent::Assess::Policy::Propagator::Base
13
13
  class << self
14
- # Keep means the tags just pass from the source to the target
15
- # as is.
16
- def propagate propagation_node, preshift, target
14
+ # Keep means the tags just pass from the source to the target as is.
15
+ #
16
+ # @param propagation_node [Contrast::Agent::Assess::Policy::PropagationNode] Current node
17
+ # governing the propagation.
18
+ # @param preshift_or_object [Contrast::Agent::Assess::Preshift] current preshift state or
19
+ # source object passed if track_original_object flag is active.
20
+ # @param target [Object] To where the tainted data flows.
21
+ def propagate propagation_node, preshift_or_object, target
17
22
  return unless (properties = Contrast::Agent::Assess::Tracker.properties!(target))
18
23
 
19
- source = find_source(propagation_node.sources[0], preshift)
24
+ source = if propagation_node.use_original_object?
25
+ # for now this is used with string based object and with methods not mutating
26
+ # original object, so we will always return 'O' key which is the object itself.
27
+ # Because preshift is disabled for this methods we use it's parameter to pass
28
+ # in the original object, preshift = object. To see list of methods in which
29
+ # this is used:
30
+ # Contrast::Utils::MethodCheck::ORIGINAL_OBJECT_METHODS
31
+ preshift_or_object
32
+ else
33
+ find_source(propagation_node.sources[0], preshift_or_object)
34
+ end
20
35
  properties.copy_from(source, target, 0, propagation_node.untags)
21
36
  end
22
37
  end
@@ -13,10 +13,26 @@ module Contrast
13
13
  class << self
14
14
  # For the source, append its tags to the target. Once the tag is applied, remove the section that was
15
15
  # removed by the delete. Unlike additive propagation, this currently only supports one source.
16
- def propagate propagation_node, preshift, target
16
+ #
17
+ # @param propagation_node [Contrast::Agent::Assess::Policy::PropagationNode] Current node
18
+ # governing the propagation.
19
+ # @param preshift_or_object [Contrast::Agent::Assess::Preshift] current preshift state or
20
+ # source object passed if track_original_object flag is active.
21
+ # @param target [Object] To where the tainted data flows.
22
+ def propagate propagation_node, preshift_or_object, target
17
23
  return unless (properties = Contrast::Agent::Assess::Tracker.properties!(target))
18
24
 
19
- source = find_source(propagation_node.sources[0], preshift)
25
+ source = if propagation_node.use_original_object?
26
+ # for now this is used with string based object and with methods not mutating
27
+ # original object (clearing empty spaces, \n..) so we will always return 'O' key
28
+ # which is the object itself. Because preshift is disabled for this methods
29
+ # we use it's parameter to pass in the original object( in this case just a unchained
30
+ # source), preshift = object. To see list of methods in which this is used:
31
+ # Contrast::Utils::MethodCheck::ORIGINAL_OBJECT_METHODS
32
+ preshift_or_object
33
+ else
34
+ find_source(propagation_node.sources[0], preshift_or_object)
35
+ end
20
36
  properties.copy_from(source, target, 0, propagation_node.untags)
21
37
  handle_removal(propagation_node, source, target)
22
38
  end
@@ -19,6 +19,7 @@ module Contrast
19
19
  when Contrast::Utils::ObjectShare::OBJECT_KEY
20
20
  tracked_inputs << preshift.object if Contrast::Agent::Assess::Tracker.tracked?(preshift.object)
21
21
  else
22
+ check_for_buffer(tracked_inputs, preshift.args[source])
22
23
  find_argument_inputs(tracked_inputs, preshift.args[source])
23
24
  end
24
25
  end
@@ -47,9 +48,15 @@ module Contrast
47
48
  # @param tracked_inputs [Array] storage for the inputs to act on later
48
49
  # @param arg [Object] an input to the method which act as sources for this propagation.
49
50
  def find_argument_inputs tracked_inputs, arg
50
- if arg.is_a?(String)
51
- tracked_inputs << arg if Contrast::Agent::Assess::Tracker.tracked?(arg)
52
- elsif Contrast::Utils::DuckUtils.iterable_hash?(arg)
51
+ if arg.is_a?(String) || arg.is_a?(File)
52
+ tracked_inputs << arg if tracked_value?(arg)
53
+ else
54
+ iterable_arg tracked_inputs, arg
55
+ end
56
+ end
57
+
58
+ def iterable_arg tracked_inputs, arg
59
+ if Contrast::Utils::DuckUtils.iterable_hash?(arg)
53
60
  arg.each_pair do |key, value|
54
61
  tracked_inputs << key if tracked_value?(key)
55
62
  tracked_inputs << value if tracked_value?(value)
@@ -60,6 +67,13 @@ module Contrast
60
67
  end
61
68
  end
62
69
  end
70
+
71
+ def check_for_buffer tracked_inputs, arg
72
+ return if RUBY_VERSION < '3.1.0'
73
+ return unless arg.is_a?(IO::Buffer) && tracked_value?(arg)
74
+
75
+ tracked_inputs << arg
76
+ end
63
77
  end
64
78
  end
65
79
  end
@@ -33,7 +33,7 @@ module Contrast
33
33
  end
34
34
 
35
35
  def sub_tagger patcher, preshift, ret, block
36
- substitution_tagger(patcher, preshift, ret, !block.nil?, false)
36
+ substitution_tagger(patcher, preshift, ret, !block.nil?, global: false)
37
37
  end
38
38
  end
39
39
  end
@@ -29,7 +29,7 @@ module Contrast
29
29
  # @param ret [String] the result of the substitution
30
30
  # @param block, the given block
31
31
  # @param global [Boolean] if this was a global or single substitution, optional
32
- def substitution_tagger patcher, preshift, ret, block, global = true
32
+ def substitution_tagger patcher, preshift, ret, block, global: true
33
33
  return ret unless ret
34
34
 
35
35
  begin
@@ -92,7 +92,7 @@ module Contrast
92
92
  end
93
93
  # Be sure to capture the last range in the holder.
94
94
  remove_ranges << (start...stop)
95
- properties.delete_tags_at_ranges(remove_ranges, false)
95
+ properties.delete_tags_at_ranges(remove_ranges, shift: false)
96
96
  end
97
97
  end
98
98
  end
@@ -30,6 +30,7 @@ module Contrast
30
30
  require 'contrast/agent/assess/policy/propagator/split'
31
31
  require 'contrast/agent/assess/policy/propagator/substitution'
32
32
  require 'contrast/agent/assess/policy/propagator/trim'
33
+ require 'contrast/agent/assess/policy/propagator/buffer'
33
34
  end
34
35
  end
35
36
  end
@@ -36,7 +36,7 @@ module Contrast
36
36
  # @param ret [Object] the Return of the invoked method
37
37
  # @param args [Array<Object>] the Arguments with which the method was invoked
38
38
  # @return [Object, nil] the tracked Return or nil if no changes were made
39
- def source_patchers method_policy, object, ret, args
39
+ def apply_source method_policy, object, ret, args
40
40
  return unless analyze?(method_policy, object, ret, args)
41
41
  return unless (source_node = method_policy.source_node)
42
42
 
@@ -56,11 +56,11 @@ module Contrast
56
56
  target = dup
57
57
  end
58
58
 
59
- apply_source(source_node, target, source_data, source_node.type, nil, *args)
59
+ process_source(source_node, target, source_data, source_node.type, nil, *args)
60
60
 
61
61
  return_val
62
62
  end
63
- Contrast::Components::Logger.add_trace_log_timing_for(SourceMethod, :source_patchers)
63
+ Contrast::Components::Logger.add_trace_log_timing_for(SourceMethod, :apply_source)
64
64
 
65
65
  private
66
66
 
@@ -75,7 +75,7 @@ module Contrast
75
75
  # @param source_name [String, nil] the name of this source, i.e. the key used to accessed if from a map or
76
76
  # nil if a type like BODY
77
77
  # @param args [Array<Object>] the Arguments with which the method was invoked
78
- def apply_source source_node, target, source_data, source_type, source_name = nil, *args
78
+ def process_source source_node, target, source_data, source_type, source_name = nil, *args
79
79
  context = Contrast::Agent::REQUEST_TRACKER.current
80
80
  return unless context && source_node && target
81
81
 
@@ -90,7 +90,7 @@ module Contrast
90
90
  # values back to ourselves and try again
91
91
  elsif Contrast::Utils::DuckUtils.iterable_enumerable?(target)
92
92
  target.each do |value|
93
- apply_source(source_node, value, source_data, source_type, source_name, *args)
93
+ process_source(source_node, value, source_data, source_type, source_name, *args)
94
94
  end
95
95
  end
96
96
  rescue StandardError => e
@@ -116,8 +116,8 @@ module Contrast
116
116
  key = key.dup
117
117
  to_replace << key
118
118
  end
119
- apply_source(source_node, key, source_data, key_type(source_type), key, *args)
120
- apply_source(source_node, value, source_data, source_type, key, *args)
119
+ process_source(source_node, key, source_data, key_type(source_type), key, *args)
120
+ process_source(source_node, value, source_data, source_type, key, *args)
121
121
  end
122
122
  handle_hash_key(target, to_replace)
123
123
  end
@@ -157,7 +157,9 @@ module Contrast
157
157
 
158
158
  new_preflight = Contrast::Agent::Reporting::Preflight.new
159
159
  new_preflight_message = Contrast::Agent::Reporting::PreflightMessage.new
160
- new_preflight_message.routes << Contrast::Agent::Reporting::RouteDiscovery.convert(request.route)
160
+ if request.route
161
+ new_preflight_message.routes << Contrast::Agent::Reporting::RouteDiscovery.convert(request.route)
162
+ end
161
163
  new_preflight_message.hash_code = hash_code
162
164
  new_preflight_message.data = "#{ trigger_node.rule_id },#{ hash_code }"
163
165
  new_preflight.messages << new_preflight_message
@@ -40,7 +40,7 @@ module Contrast
40
40
  # @param ranges [Array<Range>] the ranges to delete
41
41
  # @param shift [Boolean] move remaining tags to the left to account
42
42
  # for the deletion
43
- def delete_tags_at_ranges ranges, shift = true
43
+ def delete_tags_at_ranges ranges, shift: true
44
44
  return unless tracked?
45
45
 
46
46
  # Stage one - delete the tags w/o changing their
@@ -32,7 +32,7 @@ module Contrast
32
32
  # @return [Hash, nil] the evidence required to prove the violation of the rule
33
33
  def violated? response
34
34
  body = response.body
35
- forms = html_elements(body, FORM_START_REGEXP, true)
35
+ forms = html_elements(body, FORM_START_REGEXP, capture_overflow: true)
36
36
  forms.each do |form|
37
37
  # Because TeamServer will reject any subsequent form on the same page due to deduplication, we can
38
38
  # skip out on the first violation.
@@ -45,7 +45,7 @@ module Contrast
45
45
  # @param section [String,nil] html section to find element
46
46
  # @param element_start_str [String] element to find in html section
47
47
  # @return [Array<Hash>] the found elements of this section, as well as their start and end indexes.
48
- def html_elements section, element_start_str = '', capture_overflow = false
48
+ def html_elements section, element_start_str = '', capture_overflow: false
49
49
  elements = []
50
50
  section_start = 0
51
51
  return [] unless section
@@ -61,7 +61,7 @@ module Contrast
61
61
  next unless element_stop
62
62
 
63
63
  section_close = section_start + 6 + element_stop
64
- elements << capture(section, section_start, section_close, element_stop, capture_overflow)
64
+ elements << capture(section, section_start, section_close, element_stop, overflow: capture_overflow)
65
65
  section_start = section_close
66
66
  end
67
67
  elements
@@ -82,7 +82,7 @@ module Contrast
82
82
  # @param body_close [Integer] the end of the range to take from the body
83
83
  # @param tag_stop [Integer] the index of the end of the html tag from its start
84
84
  # @return [Hash]
85
- def capture body, body_start, body_close, tag_stop, overflow = false
85
+ def capture body, body_start, body_close, tag_stop, overflow: false
86
86
  tag = {}
87
87
  # Capture the 50 characters in front of the form, or up to the start if the form starts before 50.
88
88
  if overflow
@@ -54,7 +54,7 @@ module Contrast
54
54
  # rack response, we should use that value
55
55
  if framework_supported?
56
56
  cache_control = response.rack_response.cache_control
57
- has_header = !cache_control.empty?
57
+ has_header = !cache_control.blank?
58
58
  not_valid = has_header && !((cache_control[:no_cache]) || (cache_control[:no_store]))
59
59
  # evidence requires header value string, pull directly instead of rebuilding from hash
60
60
  return has_header, evidence(HEADER_TYPE, NAME, cache_control_to_s(cache_control)) if not_valid
@@ -32,7 +32,7 @@ module Contrast
32
32
  # @return [Hash, nil] the evidence required to prove the violation of the rule
33
33
  def violated? response
34
34
  body = response.body
35
- forms = html_elements(body, FORM_START_REGEXP, true)
35
+ forms = html_elements(body, FORM_START_REGEXP, capture_overflow: true)
36
36
  forms.each do |form|
37
37
  # Because TeamServer will reject any subsequent form on the same page due to deduplication, we can
38
38
  # skip out on the first violation.
@@ -24,13 +24,6 @@ module Contrast
24
24
  def method_scope
25
25
  :contrast
26
26
  end
27
-
28
- # TODO: RUBY-99999 remove, used to clean up logs while debugging
29
- # def validate
30
- # return if class_name
31
- #
32
- # raise(ArgumentError, "#{ node_class } #{ id } did not have a proper class name. Unable to create.")
33
- # end
34
27
  end
35
28
  end
36
29
  end
@@ -35,12 +35,6 @@ module Contrast
35
35
  end
36
36
  end
37
37
 
38
- def validate
39
- return if class_name
40
-
41
- raise(ArgumentError, "#{ node_class } #{ id } did not have a proper class name. Unable to create.")
42
- end
43
-
44
38
  def module_names
45
39
  @_module_names ||= Set.new(deadzones.map(&:class_name))
46
40
  end
@@ -51,7 +51,7 @@ module Contrast
51
51
 
52
52
  @urls = []
53
53
  @exclusion.urls.each do |url|
54
- url_pattern = build_regexp(url, true, true)
54
+ url_pattern = build_regexp(url, start_anchor: true, end_anchor: true)
55
55
  @urls << url_pattern if url_pattern
56
56
  end
57
57
  end
@@ -66,7 +66,7 @@ module Contrast
66
66
  @wildcard_exclusions = []
67
67
  @exclusion.denylist.each do |code|
68
68
  class_name, method_name = code.split(Contrast::Utils::ObjectShare::COLON)
69
- class_pattern = build_regexp(class_name, false, true)
69
+ class_pattern = build_regexp(class_name, start_anchor: false, end_anchor: true)
70
70
  method_pattern = build_regexp(method_name)
71
71
  next unless class_pattern && method_pattern
72
72
 
@@ -74,7 +74,7 @@ module Contrast
74
74
  end
75
75
  end
76
76
 
77
- def build_regexp pattern, start_anchor = false, end_anchor = false
77
+ def build_regexp pattern, start_anchor: false, end_anchor: false
78
78
  pattern = Contrast::Utils::ObjectShare::CARROT + pattern if start_anchor
79
79
  pattern += Contrast::Utils::ObjectShare::DOLLAR_SIGN if end_anchor
80
80
  Regexp.compile(pattern)
@@ -13,7 +13,7 @@ require 'contrast/utils/heap_dump_util'
13
13
  require 'contrast/utils/telemetry'
14
14
  require 'contrast/agent/request_handler'
15
15
  require 'contrast/agent/static_analysis'
16
- require 'contrast/agent/startup_metrics_telemetry_event'
16
+ require 'contrast/agent/telemetry/events/startup_metrics_telemetry_event'
17
17
  require 'contrast/utils/middleware_utils'
18
18
 
19
19
  require 'contrast/utils/timer'
@@ -163,6 +163,8 @@ module Contrast
163
163
 
164
164
  # Build and report all collected findings prior response
165
165
  Contrast::Agent::FINDINGS.report_collected_findings unless Contrast::Agent::FINDINGS.collection.empty?
166
+ # All protect rules, which are trigger but require response to be reported
167
+ Contrast::Agent::EXPLOITS.report_recorded_exploits context unless Contrast::Agent::EXPLOITS.collection.empty?
166
168
 
167
169
  if Contrast::Agent.framework_manager.streaming?(env)
168
170
  context.reset_activity
@@ -133,7 +133,7 @@ module Contrast
133
133
  # @param module_data [Contrast::Agent::ModuleData] the module, and its name, that's being patched into
134
134
  # @param redo_patch [Boolean] a trigger to force patching regardless of the state of the
135
135
  # Contrast::Agent::Patching::Policy::PatchStatus status on the Module
136
- def patch_into_module module_data, redo_patch = false
136
+ def patch_into_module module_data, redo_patch: false
137
137
  status = Contrast::Agent::Patching::Policy::PatchStatus.get_status(module_data.mod)
138
138
  return if (status&.patched? || status&.patching?) && !redo_patch
139
139
 
@@ -179,7 +179,7 @@ module Contrast
179
179
  # @param private [Boolean] Indicate if the query should include
180
180
  # private, as well as public, instance methods
181
181
  # @return [Array<Symbol>]
182
- def all_instance_methods mod, private = false
182
+ def all_instance_methods mod, private: false
183
183
  instance_methods = mod.instance_methods(false)
184
184
  # C magic rb_define_global_function creates private instance
185
185
  # methods. We need to instrument those dudes
@@ -206,7 +206,7 @@ module Contrast
206
206
  # this module, sorted by type.
207
207
  def patch_into_instance_methods module_data, module_policy
208
208
  mod = module_data.mod
209
- methods = all_instance_methods(mod, true)
209
+ methods = all_instance_methods(mod, private: true)
210
210
  methods.delete(:initialize) if mod.to_s.starts_with?('RSpec') && mod.to_s.include?('Matchers')
211
211
  patch_into_methods(mod, methods, module_policy, true)
212
212
  end
@@ -2,6 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/components/scope'
5
+ require 'contrast/utils/object_share'
5
6
 
6
7
  module Contrast
7
8
  module Agent
@@ -14,8 +15,20 @@ module Contrast
14
15
  class PolicyNode
15
16
  include Contrast::Components::Scope::InstanceMethods
16
17
 
17
- attr_accessor :class_name, :instance_method, :method_name, :method_visibility
18
- attr_reader :properties, :method_scope
18
+ # Name of the class in which the method is being invoked.
19
+ attr_accessor :class_name
20
+ # Check for instance method.
21
+ #
22
+ # @return true | false
23
+ attr_accessor :instance_method
24
+ # The symbol representation of the invoked method.
25
+ attr_accessor :method_name
26
+ # Visibility of the invoked method [Private, Public, Protected]
27
+ attr_accessor :method_visibility
28
+ # Properties parsed from our JSON policy.
29
+ attr_reader :properties
30
+ # Scope of the method parsed from our JSON policy.
31
+ attr_reader :method_scope
19
32
 
20
33
  def node_class
21
34
  raise NoMethodError, 'specify the type of the feature for which this node patches'