contrast-agent 5.2.0 → 6.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (228) 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_regexp/cs__assess_regexp.c +15 -2
  10. data/ext/cs__assess_regexp/cs__assess_regexp.h +2 -0
  11. data/ext/cs__assess_string/cs__assess_string.c +21 -1
  12. data/ext/cs__assess_test/cs__assess_test.h +9 -0
  13. data/ext/cs__assess_test/cs__assess_tests.c +22 -0
  14. data/ext/cs__assess_test/extconf.rb +5 -0
  15. data/ext/cs__common/cs__common.c +113 -11
  16. data/ext/cs__common/cs__common.h +29 -5
  17. data/ext/cs__contrast_patch/cs__contrast_patch.c +55 -44
  18. data/ext/cs__os_information/cs__os_information.c +13 -10
  19. data/ext/cs__scope/cs__scope.c +146 -97
  20. data/ext/cs__tests/cs__tests.c +12 -0
  21. data/ext/cs__tests/cs__tests.h +3 -0
  22. data/ext/cs__tests/extconf.rb +5 -0
  23. data/lib/contrast/agent/assess/contrast_object.rb +16 -16
  24. data/lib/contrast/agent/assess/events/source_event.rb +17 -19
  25. data/lib/contrast/agent/assess/finalizers/hash.rb +2 -0
  26. data/lib/contrast/agent/assess/policy/policy_node.rb +50 -27
  27. data/lib/contrast/agent/assess/policy/policy_node_utils.rb +51 -0
  28. data/lib/contrast/agent/assess/policy/policy_scanner.rb +2 -16
  29. data/lib/contrast/agent/assess/policy/preshift.rb +8 -2
  30. data/lib/contrast/agent/assess/policy/propagation_method.rb +47 -13
  31. data/lib/contrast/agent/assess/policy/propagator/buffer.rb +118 -0
  32. data/lib/contrast/agent/assess/policy/propagator/keep.rb +19 -4
  33. data/lib/contrast/agent/assess/policy/propagator/remove.rb +18 -2
  34. data/lib/contrast/agent/assess/policy/propagator/splat.rb +17 -3
  35. data/lib/contrast/agent/assess/policy/propagator/split.rb +15 -19
  36. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +1 -1
  37. data/lib/contrast/agent/assess/policy/propagator/substitution_utils.rb +1 -1
  38. data/lib/contrast/agent/assess/policy/propagator/trim.rb +1 -1
  39. data/lib/contrast/agent/assess/policy/propagator.rb +1 -0
  40. data/lib/contrast/agent/assess/policy/source_method.rb +7 -7
  41. data/lib/contrast/agent/assess/policy/trigger_method.rb +4 -10
  42. data/lib/contrast/agent/assess/property/tagged.rb +1 -1
  43. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +7 -2
  44. data/lib/contrast/agent/assess/rule/response/auto_complete_rule.rb +1 -1
  45. data/lib/contrast/agent/assess/rule/response/base_rule.rb +11 -3
  46. data/lib/contrast/agent/assess/rule/response/body_rule.rb +3 -3
  47. data/lib/contrast/agent/assess/rule/response/cache_control_header_rule.rb +60 -36
  48. data/lib/contrast/agent/assess/rule/response/framework/rails_support.rb +3 -3
  49. data/lib/contrast/agent/assess/rule/response/parameters_pollution_rule.rb +1 -1
  50. data/lib/contrast/agent/assess/rule/response/x_xss_protection_header_rule.rb +1 -2
  51. data/lib/contrast/agent/at_exit_hook.rb +1 -1
  52. data/lib/contrast/agent/deadzone/policy/deadzone_node.rb +0 -7
  53. data/lib/contrast/agent/deadzone/policy/policy.rb +0 -6
  54. data/lib/contrast/agent/exclusion_matcher.rb +3 -3
  55. data/lib/contrast/agent/inventory/database_config.rb +10 -3
  56. data/lib/contrast/agent/middleware.rb +5 -3
  57. data/lib/contrast/agent/patching/policy/after_load_patch.rb +0 -2
  58. data/lib/contrast/agent/patching/policy/patch.rb +13 -12
  59. data/lib/contrast/agent/patching/policy/patcher.rb +4 -4
  60. data/lib/contrast/agent/patching/policy/policy_node.rb +15 -2
  61. data/lib/contrast/agent/protect/exploitable_collection.rb +38 -0
  62. data/lib/contrast/agent/protect/input_analyzer/input_analyzer.rb +66 -9
  63. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +2 -1
  64. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +2 -2
  65. data/lib/contrast/agent/protect/rule/base.rb +37 -5
  66. data/lib/contrast/agent/protect/rule/base_service.rb +3 -1
  67. data/lib/contrast/agent/protect/rule/cmd_injection.rb +13 -0
  68. data/lib/contrast/agent/protect/rule/cmdi/cmdi_input_classification.rb +83 -0
  69. data/lib/contrast/agent/protect/rule/cmdi/cmdi_worth_watching.rb +64 -0
  70. data/lib/contrast/agent/protect/rule/http_method_tampering/http_method_tampering_input_classification.rb +96 -0
  71. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +8 -0
  72. data/lib/contrast/agent/protect/rule/no_sqli/no_sqli_input_classification.rb +231 -0
  73. data/lib/contrast/agent/protect/rule/no_sqli.rb +27 -0
  74. data/lib/contrast/agent/protect/rule/sqli/sqli_input_classification.rb +18 -54
  75. data/lib/contrast/agent/protect/rule/sqli/sqli_worth_watching.rb +1 -4
  76. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_input_classification.rb +82 -0
  77. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_matcher.rb +45 -0
  78. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +42 -0
  79. data/lib/contrast/agent/reporting/attack_result/attack_result.rb +63 -0
  80. data/lib/contrast/agent/reporting/attack_result/rasp_rule_sample.rb +52 -0
  81. data/lib/contrast/agent/reporting/attack_result/response_type.rb +29 -0
  82. data/lib/contrast/agent/reporting/attack_result/user_input.rb +87 -0
  83. data/lib/contrast/agent/reporting/masker/masker.rb +243 -0
  84. data/lib/contrast/agent/reporting/masker/masker_utils.rb +62 -0
  85. data/lib/contrast/agent/reporting/report.rb +2 -0
  86. data/lib/contrast/agent/reporting/reporter.rb +29 -22
  87. data/lib/contrast/agent/reporting/reporter_heartbeat.rb +49 -0
  88. data/lib/contrast/agent/reporting/reporting_events/agent_startup.rb +34 -0
  89. data/lib/contrast/agent/reporting/reporting_events/application_activity.rb +53 -0
  90. data/lib/contrast/agent/reporting/reporting_events/application_defend_activity.rb +48 -0
  91. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_activity.rb +64 -0
  92. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample.rb +70 -0
  93. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample_activity.rb +57 -0
  94. data/lib/contrast/agent/reporting/reporting_events/application_defend_attacker_activity.rb +56 -0
  95. data/lib/contrast/agent/reporting/reporting_events/application_inventory.rb +12 -4
  96. data/lib/contrast/agent/reporting/reporting_events/application_inventory_activity.rb +58 -0
  97. data/lib/contrast/agent/reporting/reporting_events/application_reporting_event.rb +27 -0
  98. data/lib/contrast/agent/reporting/reporting_events/application_startup.rb +50 -0
  99. data/lib/contrast/agent/reporting/reporting_events/application_startup_instrumentation.rb +27 -0
  100. data/lib/contrast/agent/reporting/reporting_events/application_update.rb +7 -12
  101. data/lib/contrast/agent/reporting/reporting_events/finding.rb +10 -4
  102. data/lib/contrast/agent/reporting/reporting_events/finding_event.rb +2 -4
  103. data/lib/contrast/agent/reporting/reporting_events/finding_event_object.rb +3 -3
  104. data/lib/contrast/agent/reporting/reporting_events/library_usage_observation.rb +5 -5
  105. data/lib/contrast/agent/reporting/reporting_events/observed_library_usage.rb +6 -2
  106. data/lib/contrast/agent/reporting/reporting_events/observed_route.rb +16 -12
  107. data/lib/contrast/agent/reporting/reporting_events/poll.rb +6 -2
  108. data/lib/contrast/agent/reporting/reporting_events/preflight.rb +10 -8
  109. data/lib/contrast/agent/reporting/reporting_events/preflight_message.rb +8 -11
  110. data/lib/contrast/agent/reporting/reporting_events/reporting_event.rb +2 -1
  111. data/lib/contrast/agent/reporting/reporting_events/route_coverage.rb +8 -6
  112. data/lib/contrast/agent/reporting/reporting_events/server_activity.rb +12 -20
  113. data/lib/contrast/agent/reporting/reporting_events/server_reporting_event.rb +27 -0
  114. data/lib/contrast/agent/reporting/reporting_utilities/audit.rb +17 -27
  115. data/lib/contrast/agent/reporting/reporting_utilities/build_preflight.rb +38 -0
  116. data/lib/contrast/agent/reporting/reporting_utilities/dtm_message.rb +8 -0
  117. data/lib/contrast/agent/reporting/reporting_utilities/endpoints.rb +6 -0
  118. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +23 -7
  119. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client_utils.rb +64 -76
  120. data/lib/contrast/agent/reporting/reporting_utilities/reporting_storage.rb +1 -1
  121. data/lib/contrast/agent/reporting/reporting_utilities/response.rb +17 -7
  122. data/lib/contrast/agent/reporting/reporting_utilities/response_extractor.rb +100 -0
  123. data/lib/contrast/agent/reporting/reporting_utilities/response_handler.rb +75 -13
  124. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_mode.rb +63 -0
  125. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_utils.rb +154 -113
  126. data/lib/contrast/agent/reporting/settings/application_settings.rb +9 -0
  127. data/lib/contrast/agent/reporting/settings/assess_server_feature.rb +5 -33
  128. data/lib/contrast/agent/reporting/settings/protect.rb +1 -1
  129. data/lib/contrast/agent/reporting/settings/protect_server_feature.rb +2 -2
  130. data/lib/contrast/agent/reporting/settings/sampling.rb +36 -0
  131. data/lib/contrast/agent/reporting/settings/sensitive_data_masking.rb +110 -0
  132. data/lib/contrast/agent/reporting/settings/sensitive_data_masking_rule.rb +58 -0
  133. data/lib/contrast/agent/request.rb +3 -3
  134. data/lib/contrast/agent/request_context.rb +1 -1
  135. data/lib/contrast/agent/request_context_extend.rb +2 -2
  136. data/lib/contrast/agent/request_handler.rb +7 -3
  137. data/lib/contrast/agent/response.rb +2 -0
  138. data/lib/contrast/agent/service_heartbeat.rb +6 -48
  139. data/lib/contrast/agent/static_analysis.rb +1 -1
  140. data/lib/contrast/agent/telemetry/base.rb +151 -0
  141. data/lib/contrast/agent/telemetry/events/event.rb +35 -0
  142. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_base.rb +59 -0
  143. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_event.rb +44 -0
  144. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message.rb +115 -0
  145. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message_exception.rb +83 -0
  146. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_stack_frame.rb +64 -0
  147. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exceptions.rb +20 -0
  148. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exceptions_report.rb +32 -0
  149. data/lib/contrast/agent/telemetry/events/metric_event.rb +28 -0
  150. data/lib/contrast/agent/telemetry/events/startup_metrics_event.rb +123 -0
  151. data/lib/contrast/agent/thread_watcher.rb +52 -68
  152. data/lib/contrast/agent/version.rb +1 -1
  153. data/lib/contrast/agent/worker_thread.rb +8 -0
  154. data/lib/contrast/agent.rb +4 -3
  155. data/lib/contrast/api/communication/messaging_queue.rb +28 -11
  156. data/lib/contrast/api/communication/response_processor.rb +7 -10
  157. data/lib/contrast/api/communication/speedracer.rb +1 -1
  158. data/lib/contrast/api/decorators/activity.rb +33 -0
  159. data/lib/contrast/api/decorators/address.rb +1 -1
  160. data/lib/contrast/api/decorators/http_request.rb +1 -1
  161. data/lib/contrast/api/decorators/response_type.rb +30 -0
  162. data/lib/contrast/api/decorators.rb +1 -0
  163. data/lib/contrast/components/app_context.rb +0 -4
  164. data/lib/contrast/components/assess.rb +14 -0
  165. data/lib/contrast/components/config.rb +13 -22
  166. data/lib/contrast/components/contrast_service.rb +9 -0
  167. data/lib/contrast/components/protect.rb +2 -2
  168. data/lib/contrast/components/sampling.rb +7 -11
  169. data/lib/contrast/components/settings.rb +116 -8
  170. data/lib/contrast/config/agent_configuration.rb +34 -41
  171. data/lib/contrast/config/api_configuration.rb +16 -75
  172. data/lib/contrast/config/api_proxy_configuration.rb +9 -48
  173. data/lib/contrast/config/application_configuration.rb +24 -95
  174. data/lib/contrast/config/assess_configuration.rb +21 -76
  175. data/lib/contrast/config/assess_rules_configuration.rb +13 -38
  176. data/lib/contrast/config/base_configuration.rb +11 -76
  177. data/lib/contrast/config/certification_configuration.rb +15 -68
  178. data/lib/contrast/config/exception_configuration.rb +15 -59
  179. data/lib/contrast/config/heap_dump_configuration.rb +19 -73
  180. data/lib/contrast/config/inventory_configuration.rb +11 -55
  181. data/lib/contrast/config/logger_configuration.rb +8 -41
  182. data/lib/contrast/config/protect_configuration.rb +23 -10
  183. data/lib/contrast/config/protect_rule_configuration.rb +23 -37
  184. data/lib/contrast/config/protect_rules_configuration.rb +39 -43
  185. data/lib/contrast/config/request_audit_configuration.rb +16 -55
  186. data/lib/contrast/config/root_configuration.rb +70 -13
  187. data/lib/contrast/config/ruby_configuration.rb +14 -47
  188. data/lib/contrast/config/sampling_configuration.rb +12 -65
  189. data/lib/contrast/config/server_configuration.rb +13 -45
  190. data/lib/contrast/config/service_configuration.rb +36 -17
  191. data/lib/contrast/configuration.rb +21 -13
  192. data/lib/contrast/extension/assess/string.rb +20 -1
  193. data/lib/contrast/extension/module.rb +0 -1
  194. data/lib/contrast/framework/manager.rb +2 -2
  195. data/lib/contrast/framework/rails/patch/support.rb +13 -45
  196. data/lib/contrast/logger/aliased_logging.rb +87 -0
  197. data/lib/contrast/logger/application.rb +1 -5
  198. data/lib/contrast/logger/cef_log.rb +1 -1
  199. data/lib/contrast/tasks/config.rb +100 -4
  200. data/lib/contrast/utils/assess/object_store.rb +36 -0
  201. data/lib/contrast/utils/assess/propagation_method_utils.rb +6 -0
  202. data/lib/contrast/utils/class_util.rb +5 -18
  203. data/lib/contrast/utils/input_classification.rb +73 -0
  204. data/lib/contrast/utils/invalid_configuration_util.rb +1 -1
  205. data/lib/contrast/utils/log_utils.rb +3 -1
  206. data/lib/contrast/utils/middleware_utils.rb +10 -9
  207. data/lib/contrast/utils/net_http_base.rb +1 -1
  208. data/lib/contrast/utils/object_share.rb +2 -1
  209. data/lib/contrast/utils/os.rb +0 -5
  210. data/lib/contrast/utils/patching/policy/patch_utils.rb +4 -5
  211. data/lib/contrast/utils/response_utils.rb +18 -33
  212. data/lib/contrast/utils/telemetry.rb +20 -2
  213. data/lib/contrast/utils/telemetry_client.rb +23 -11
  214. data/lib/contrast/utils/telemetry_hash.rb +41 -0
  215. data/lib/contrast/utils/telemetry_identifier.rb +16 -1
  216. data/lib/contrast.rb +9 -0
  217. data/resources/assess/policy.json +98 -0
  218. data/resources/deadzone/policy.json +0 -86
  219. data/ruby-agent.gemspec +10 -9
  220. data/service_executables/VERSION +1 -1
  221. data/service_executables/linux/contrast-service +0 -0
  222. data/service_executables/mac/contrast-service +0 -0
  223. metadata +98 -30
  224. data/lib/contrast/agent/metric_telemetry_event.rb +0 -26
  225. data/lib/contrast/agent/startup_metrics_telemetry_event.rb +0 -121
  226. data/lib/contrast/agent/telemetry.rb +0 -137
  227. data/lib/contrast/agent/telemetry_event.rb +0 -33
  228. data/lib/contrast/utils/exclude_key.rb +0 -20
@@ -3,6 +3,8 @@
3
3
 
4
4
  require 'contrast/agent/patching/policy/policy_node'
5
5
  require 'contrast/api/decorators/trace_taint_range_tags'
6
+ require 'contrast/utils/object_share'
7
+ require 'contrast/agent/assess/policy/policy_node_utils'
6
8
 
7
9
  module Contrast
8
10
  module Agent
@@ -11,9 +13,30 @@ module Contrast
11
13
  # This class functions to translate our policy.json into an actionable
12
14
  # Ruby object, allowing for dynamic patching over hardcoded patching.
13
15
  class PolicyNode < Contrast::Agent::Patching::Policy::PolicyNode
16
+ include PolicyNodeUtils
17
+
14
18
  attr_accessor :tags, :type
15
19
  attr_reader :sources, :targets, :source_string, :target_string
16
20
 
21
+ # Here are all methods that can use original objects without mutation the source.
22
+ # For methods with REMOVE action, their (!) bang alternative is not listed, since
23
+ # this methods tends to mutate the original (shortens it's length, remove special
24
+ # symbols..) and causes mismatch in tags range representation of the target. In
25
+ # short preshift is needed.
26
+ ORIGINAL_OBJECT_METHODS = %i[
27
+ capitalize
28
+ capitalize!
29
+ chomp
30
+ to_s
31
+ downcase
32
+ downcase!
33
+ lstrip
34
+ strip
35
+ upcase!
36
+ upcase
37
+ ].cs__freeze
38
+ TO_S = 'to_s'
39
+
17
40
  def initialize policy_hash = {}
18
41
  super(policy_hash)
19
42
  @source_string = policy_hash[JSON_SOURCE]
@@ -21,6 +44,20 @@ module Contrast
21
44
  @tags = Set.new(policy_hash[JSON_TAGS])
22
45
  @sources = convert_policy_markers(source_string)
23
46
  @targets = convert_policy_markers(target_string)
47
+ @_use_original_object = ORIGINAL_OBJECT_METHODS.include?(@method_name)
48
+ @_use_original_on_bang_method = assign_on_bang_check policy_hash
49
+ end
50
+
51
+ def assign_on_bang_check policy_hash
52
+ # If we have KEEP action on String, and the method is to_s, that method would return self:
53
+ # String#to_s => self or string. This method is included here to cover the situations such as
54
+ # String.to_s.html_safe, where normally the dynamic sources properties get lost. To solve this
55
+ # we will simply return the original object here.
56
+ return true if @_use_original_object && policy_hash[JSON_METHOD_NAME] == TO_S
57
+
58
+ @_use_original_object &&
59
+ # Check if method name ends with a (!) bang unless is the to_s method:
60
+ policy_hash[JSON_METHOD_NAME].end_with?(Contrast::Utils::ObjectShare::BANG)
24
61
  end
25
62
 
26
63
  def feature
@@ -119,36 +156,22 @@ module Contrast
119
156
  JSON_TAGS = 'tags'
120
157
  JSON_DATAFLOW = 'dataflow'
121
158
 
122
- private
123
-
124
- # Given a policy string in the format A,B,C, populate the given array
125
- # 1) Split on ','
126
- # 2) If 'O' or 'R', add the array, else it's P and needs to be
127
- # converted. P type will either be P# where # is the index
128
- # of the parameter. Drop the P and store the # as an int.
159
+ # This method will check if a method is fit to use it's original object and
160
+ # that the method is without bang - it does not change the source, but rather
161
+ # creates a copy of it.
129
162
  #
130
- # @param markers [String] the String from the policy to parse
131
- # @return [Array] the array generated by converting the marker string
132
- def convert_policy_markers markers
133
- return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless markers
134
- return Contrast::Utils::ObjectShare::EMPTY_ARRAY if markers.empty?
135
-
136
- converted = []
137
- markers.split(Contrast::Utils::ObjectShare::COMMA).each do |t|
138
- converted << case t
139
- when Contrast::Utils::ObjectShare::OBJECT_KEY,
140
- Contrast::Utils::ObjectShare::RETURN_KEY
141
-
142
- t
143
- else
144
- Integer(t[1..])
145
- end
146
- end
147
- converted
163
+ # @return true | false
164
+ def use_original_object?
165
+ @_use_original_object && Contrast::ASSESS.track_original_object?
148
166
  end
149
167
 
150
- def all_type? marker
151
- marker.include?(Contrast::Utils::ObjectShare::COMMA)
168
+ # This method will check if a method is fit to use it's original object and
169
+ # that the target return is the same as object - a bang method modifying the
170
+ # source.
171
+ #
172
+ # @return true | false
173
+ def use_original_on_bang_method?
174
+ @_use_original_on_bang_method && Contrast::ASSESS.track_original_object?
152
175
  end
153
176
  end
154
177
  end
@@ -0,0 +1,51 @@
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/patching/policy/policy_node'
5
+ require 'contrast/api/decorators/trace_taint_range_tags'
6
+ require 'contrast/utils/object_share'
7
+
8
+ module Contrast
9
+ module Agent
10
+ module Assess
11
+ module Policy
12
+ # This module will extend the PolicyNode, which functions is to translate
13
+ # our policy.json into an actionable Ruby object, allowing for dynamic
14
+ # patching over hardcoded patching.
15
+ module PolicyNodeUtils
16
+ private
17
+
18
+ # Given a policy string in the format A,B,C, populate the given array
19
+ # 1) Split on ','
20
+ # 2) If 'O' or 'R', add the array, else it's P and needs to be
21
+ # converted. P type will either be P# where # is the index
22
+ # of the parameter. Drop the P and store the # as an int.
23
+ #
24
+ # @param markers [String] the String from the policy to parse
25
+ # @return [Array] the array generated by converting the marker string
26
+ def convert_policy_markers markers
27
+ return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless markers
28
+ return Contrast::Utils::ObjectShare::EMPTY_ARRAY if markers.empty?
29
+
30
+ converted = []
31
+ markers.split(Contrast::Utils::ObjectShare::COMMA).each do |maker|
32
+ converted << case maker
33
+ when Contrast::Utils::ObjectShare::OBJECT_KEY,
34
+ Contrast::Utils::ObjectShare::RETURN_KEY
35
+
36
+ maker
37
+ else
38
+ Integer(maker[1..])
39
+ end
40
+ end
41
+ converted
42
+ end
43
+
44
+ def all_type? marker
45
+ marker.include?(Contrast::Utils::ObjectShare::COMMA)
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -32,22 +32,8 @@ module Contrast
32
32
  mod = trace_point.self
33
33
  return if mod.cs__frozen? || mod.singleton_class?
34
34
 
35
- different_ruby_version trace_point, provider_values, mod
36
- end
37
-
38
- def different_ruby_version trace_point, provider_values, mod
39
- # TODO: RUBY-1014 - remove non-AST approach
40
- if RUBY_VERSION >= '2.6.0'
41
- # TODO: RUBY-714 EOL 2.5. That check will be removed and the code will be brought back in here.
42
- ast = RubyVM::AbstractSyntaxTree.parse_file(trace_point.path)
43
- provider_values.each do |provider|
44
- provider.parse(trace_point, ast)
45
- end
46
- else
47
- provider_values.each do |provider|
48
- provider.analyze(mod)
49
- end
50
- end
35
+ ast = RubyVM::AbstractSyntaxTree.parse_file(trace_point.path)
36
+ provider_values.each { |provider| provider.parse(trace_point, ast) }
51
37
  end
52
38
 
53
39
  def policy
@@ -14,9 +14,12 @@ module Contrast
14
14
  extend Contrast::Components::Logger::InstanceMethods
15
15
 
16
16
  @lru_cache = Contrast::Utils::LRUCache.new
17
- UNDUPLICABLE_MODULES = [
17
+ UNDUPLICABLE_MODULES = [ # rubocop: disable Style/MutableConstant
18
18
  Enumerator # dup'ing results in 'can't copy execution context'
19
- ].cs__freeze
19
+ ]
20
+ # dup'ing IO::Buffer results in null buffer.
21
+ UNDUPLICABLE_MODULES << IO::Buffer if RUBY_VERSION >= '3.1.0'
22
+ UNDUPLICABLE_MODULES.cs__freeze
20
23
 
21
24
  attr_accessor :object, :object_length, :args, :arg_lengths
22
25
 
@@ -41,6 +44,9 @@ module Contrast
41
44
  return unless propagation_node
42
45
  return unless ::Contrast::ASSESS.enabled?
43
46
 
47
+ # don't do preshift just use the original object
48
+ return if propagation_node.use_original_object?
49
+
44
50
  initializing = propagation_node.method_name == :initialize
45
51
  return if unsafe_io_object?(object, initializing)
46
52
 
@@ -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
@@ -178,27 +178,23 @@ module Contrast
178
178
  end
179
179
  end
180
180
 
181
- if RUBY_VERSION >= '2.6.0'
182
- # Special class to handle String#split in 2.6 which, when given a block,
183
- # propagates each split piece directly
184
- class String
185
- alias_method :cs__patched_string_split_special, :split
186
-
187
- # Override of the the standard split method to handle the 2.6 direct yield case.
188
- #
189
- # Note: because this patch is applied before our standard propagation, this
190
- # call is wrapped in it. As such, any call here happens in scope, so there is
191
- # no need to manage it on our own.
192
- def split *args, &block
193
- if block
194
- Contrast::Agent::Assess::Policy::Propagator::Split.wrap_split(self, args) do
195
- cs__patched_string_split_special(*args, &block)
196
- end
197
- else
181
+ # Special class to handle String#split in which, when given a block, propagates each split piece directly.
182
+ class String
183
+ alias_method :cs__patched_string_split_special, :split
184
+
185
+ # Override of the the standard split method to handle the direct yield case.
186
+ #
187
+ # Note: because this patch is applied before our standard propagation, this call is wrapped in it. As such, any call
188
+ # here happens in scope, so there is no need to manage it on our own.
189
+ def split *args, &block
190
+ if block
191
+ Contrast::Agent::Assess::Policy::Propagator::Split.wrap_split(self, args) do
198
192
  cs__patched_string_split_special(*args, &block)
199
193
  end
194
+ else
195
+ cs__patched_string_split_special(*args, &block)
200
196
  end
201
197
  end
202
-
203
- Contrast::Agent::Assess::Policy::Propagator::Split.instrument_string_split
204
198
  end
199
+
200
+ Contrast::Agent::Assess::Policy::Propagator::Split.instrument_string_split
@@ -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