contrast-agent 3.12.2 → 3.15.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (306) hide show
  1. checksums.yaml +4 -4
  2. data/.dockerignore +0 -1
  3. data/.gitignore +1 -1
  4. data/.simplecov +1 -1
  5. data/Rakefile +31 -0
  6. data/exe/contrast_service +1 -7
  7. data/ext/build_funchook.rb +0 -2
  8. data/ext/cs__assess_active_record_named/cs__active_record_named.c +8 -7
  9. data/ext/cs__assess_array/cs__assess_array.c +6 -5
  10. data/ext/cs__assess_basic_object/cs__assess_basic_object.c +5 -5
  11. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.c +4 -9
  12. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.h +0 -1
  13. data/ext/cs__assess_hash/cs__assess_hash.c +18 -17
  14. data/ext/cs__assess_hash/cs__assess_hash.h +2 -1
  15. data/ext/cs__assess_kernel/cs__assess_kernel.c +7 -8
  16. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +18 -16
  17. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.h +1 -0
  18. data/ext/cs__assess_module/cs__assess_module.c +6 -6
  19. data/ext/cs__assess_regexp/cs__assess_regexp.c +4 -4
  20. data/ext/cs__assess_string/cs__assess_string.c +31 -16
  21. data/ext/cs__assess_string/cs__assess_string.h +6 -1
  22. data/ext/cs__assess_string_interpolation26/cs__assess_string_interpolation26.c +4 -7
  23. data/ext/cs__assess_yield_track/cs__assess_yield_track.c +3 -7
  24. data/ext/cs__assess_yield_track/cs__assess_yield_track.h +0 -1
  25. data/ext/cs__common/cs__common.c +63 -30
  26. data/ext/cs__common/cs__common.h +19 -21
  27. data/ext/cs__common/extconf.rb +0 -14
  28. data/ext/cs__contrast_patch/cs__contrast_patch.c +27 -25
  29. data/ext/cs__contrast_patch/cs__contrast_patch.h +5 -7
  30. data/ext/cs__protect_kernel/cs__protect_kernel.c +11 -12
  31. data/ext/cs__protect_kernel/cs__protect_kernel.h +2 -2
  32. data/ext/extconf_common.rb +0 -28
  33. data/lib/contrast-agent.rb +1 -1
  34. data/lib/contrast.rb +15 -23
  35. data/lib/contrast/agent.rb +51 -39
  36. data/lib/contrast/agent/assess.rb +12 -12
  37. data/lib/contrast/agent/assess/contrast_event.rb +40 -185
  38. data/lib/contrast/agent/assess/events/event_factory.rb +2 -2
  39. data/lib/contrast/agent/assess/events/source_event.rb +5 -9
  40. data/lib/contrast/agent/assess/finalizers/freeze.rb +15 -0
  41. data/lib/contrast/agent/assess/finalizers/hash.rb +97 -0
  42. data/lib/contrast/agent/assess/policy/dynamic_source_factory.rb +2 -2
  43. data/lib/contrast/agent/assess/policy/patcher.rb +6 -6
  44. data/lib/contrast/agent/assess/policy/policy.rb +9 -11
  45. data/lib/contrast/agent/assess/policy/policy_node.rb +5 -99
  46. data/lib/contrast/agent/assess/policy/policy_scanner.rb +2 -3
  47. data/lib/contrast/agent/assess/policy/preshift.rb +13 -7
  48. data/lib/contrast/agent/assess/policy/propagation_method.rb +64 -45
  49. data/lib/contrast/agent/assess/policy/propagation_node.rb +6 -2
  50. data/lib/contrast/agent/assess/policy/propagator.rb +18 -18
  51. data/lib/contrast/agent/assess/policy/propagator/append.rb +8 -5
  52. data/lib/contrast/agent/assess/policy/propagator/base.rb +2 -2
  53. data/lib/contrast/agent/assess/policy/propagator/center.rb +9 -5
  54. data/lib/contrast/agent/assess/policy/propagator/custom.rb +1 -1
  55. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +6 -4
  56. data/lib/contrast/agent/assess/policy/propagator/insert.rb +7 -7
  57. data/lib/contrast/agent/assess/policy/propagator/keep.rb +4 -1
  58. data/lib/contrast/agent/assess/policy/propagator/match_data.rb +18 -9
  59. data/lib/contrast/agent/assess/policy/propagator/next.rb +7 -5
  60. data/lib/contrast/agent/assess/policy/propagator/prepend.rb +13 -5
  61. data/lib/contrast/agent/assess/policy/propagator/remove.rb +14 -15
  62. data/lib/contrast/agent/assess/policy/propagator/replace.rb +5 -2
  63. data/lib/contrast/agent/assess/policy/propagator/reverse.rb +7 -5
  64. data/lib/contrast/agent/assess/policy/propagator/select.rb +19 -11
  65. data/lib/contrast/agent/assess/policy/propagator/splat.rb +14 -8
  66. data/lib/contrast/agent/assess/policy/propagator/split.rb +19 -13
  67. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +36 -26
  68. data/lib/contrast/agent/assess/policy/propagator/trim.rb +17 -15
  69. data/lib/contrast/agent/assess/policy/rewriter_patch.rb +5 -5
  70. data/lib/contrast/agent/assess/policy/source_method.rb +90 -73
  71. data/lib/contrast/agent/assess/policy/source_validation/cross_site_validator.rb +1 -1
  72. data/lib/contrast/agent/assess/policy/source_validation/source_validation.rb +1 -1
  73. data/lib/contrast/agent/assess/policy/trigger/reflected_xss.rb +16 -11
  74. data/lib/contrast/agent/assess/policy/trigger/xpath.rb +2 -2
  75. data/lib/contrast/agent/assess/policy/trigger_method.rb +41 -26
  76. data/lib/contrast/agent/assess/policy/trigger_node.rb +30 -17
  77. data/lib/contrast/agent/assess/policy/trigger_validation/ssrf_validator.rb +2 -1
  78. data/lib/contrast/agent/assess/policy/trigger_validation/trigger_validation.rb +2 -2
  79. data/lib/contrast/agent/assess/properties.rb +15 -383
  80. data/lib/contrast/agent/assess/property/evented.rb +78 -0
  81. data/lib/contrast/agent/assess/property/tagged.rb +339 -0
  82. data/lib/contrast/agent/assess/property/updated.rb +136 -0
  83. data/lib/contrast/agent/assess/rule.rb +2 -2
  84. data/lib/contrast/agent/assess/rule/base.rb +3 -4
  85. data/lib/contrast/agent/assess/rule/provider.rb +3 -3
  86. data/lib/contrast/agent/assess/rule/provider/hardcoded_password.rb +1 -1
  87. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +4 -22
  88. data/lib/contrast/agent/assess/tag.rb +27 -12
  89. data/lib/contrast/agent/assess/tracker.rb +66 -0
  90. data/lib/contrast/agent/at_exit_hook.rb +4 -2
  91. data/lib/contrast/agent/class_reopener.rb +14 -11
  92. data/lib/contrast/agent/deadzone/policy/deadzone_node.rb +1 -1
  93. data/lib/contrast/agent/deadzone/policy/policy.rb +2 -2
  94. data/lib/contrast/agent/disable_reaction.rb +1 -1
  95. data/lib/contrast/agent/exclusion_matcher.rb +3 -3
  96. data/lib/contrast/agent/inventory/policy/datastores.rb +2 -3
  97. data/lib/contrast/agent/inventory/policy/policy.rb +3 -3
  98. data/lib/contrast/agent/inventory/policy/trigger_node.rb +1 -1
  99. data/lib/contrast/agent/middleware.rb +36 -48
  100. data/lib/contrast/agent/patching/policy/after_load_patch.rb +4 -4
  101. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +6 -5
  102. data/lib/contrast/agent/patching/policy/module_policy.rb +1 -1
  103. data/lib/contrast/agent/patching/policy/patch.rb +16 -16
  104. data/lib/contrast/agent/patching/policy/patcher.rb +43 -44
  105. data/lib/contrast/agent/patching/policy/policy.rb +10 -13
  106. data/lib/contrast/agent/patching/policy/policy_node.rb +3 -3
  107. data/lib/contrast/agent/patching/policy/trigger_node.rb +2 -2
  108. data/lib/contrast/agent/protect/policy/applies_command_injection_rule.rb +3 -3
  109. data/lib/contrast/agent/protect/policy/applies_deserialization_rule.rb +2 -2
  110. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +2 -2
  111. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +3 -3
  112. data/lib/contrast/agent/protect/policy/applies_sqli_rule.rb +2 -2
  113. data/lib/contrast/agent/protect/policy/applies_xxe_rule.rb +3 -3
  114. data/lib/contrast/agent/protect/policy/policy.rb +8 -8
  115. data/lib/contrast/agent/protect/policy/rule_applicator.rb +3 -3
  116. data/lib/contrast/agent/protect/policy/trigger_node.rb +1 -1
  117. data/lib/contrast/agent/protect/rule.rb +18 -18
  118. data/lib/contrast/agent/protect/rule/base.rb +21 -32
  119. data/lib/contrast/agent/protect/rule/base_service.rb +2 -2
  120. data/lib/contrast/agent/protect/rule/cmd_injection.rb +5 -5
  121. data/lib/contrast/agent/protect/rule/deserialization.rb +1 -1
  122. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +3 -8
  123. data/lib/contrast/agent/protect/rule/no_sqli.rb +1 -1
  124. data/lib/contrast/agent/protect/rule/path_traversal.rb +4 -5
  125. data/lib/contrast/agent/protect/rule/sqli.rb +2 -2
  126. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +1 -1
  127. data/lib/contrast/agent/protect/rule/xss.rb +1 -1
  128. data/lib/contrast/agent/protect/rule/xxe.rb +4 -5
  129. data/lib/contrast/agent/protect/rule/xxe/entity_wrapper.rb +2 -2
  130. data/lib/contrast/agent/railtie.rb +1 -1
  131. data/lib/contrast/agent/reaction_processor.rb +5 -5
  132. data/lib/contrast/agent/request.rb +103 -340
  133. data/lib/contrast/agent/request_context.rb +25 -21
  134. data/lib/contrast/agent/request_handler.rb +1 -1
  135. data/lib/contrast/agent/response.rb +25 -26
  136. data/lib/contrast/agent/rewriter.rb +6 -9
  137. data/lib/contrast/agent/scope.rb +1 -1
  138. data/lib/contrast/agent/service_heartbeat.rb +8 -10
  139. data/lib/contrast/agent/static_analysis.rb +4 -4
  140. data/lib/contrast/agent/thread.rb +1 -1
  141. data/lib/contrast/agent/thread_watcher.rb +49 -0
  142. data/lib/contrast/agent/tracepoint_hook.rb +1 -1
  143. data/lib/contrast/agent/version.rb +1 -1
  144. data/lib/contrast/agent/worker_thread.rb +24 -0
  145. data/lib/contrast/api.rb +4 -6
  146. data/lib/contrast/api/communication.rb +20 -0
  147. data/lib/contrast/api/communication/connection_status.rb +41 -0
  148. data/lib/contrast/api/communication/messaging_queue.rb +76 -0
  149. data/lib/contrast/{utils/service_response_util.rb → api/communication/response_processor.rb} +10 -19
  150. data/lib/contrast/api/communication/service_lifecycle.rb +61 -0
  151. data/lib/contrast/api/communication/socket.rb +45 -0
  152. data/lib/contrast/api/communication/socket_client.rb +76 -0
  153. data/lib/contrast/api/communication/speedracer.rb +111 -0
  154. data/lib/contrast/api/communication/tcp_socket.rb +31 -0
  155. data/lib/contrast/api/communication/unix_socket.rb +27 -0
  156. data/lib/contrast/api/decorators.rb +14 -4
  157. data/lib/contrast/api/decorators/address.rb +61 -0
  158. data/lib/contrast/api/decorators/application_settings.rb +10 -5
  159. data/lib/contrast/api/decorators/application_update.rb +7 -17
  160. data/lib/contrast/api/decorators/http_request.rb +140 -0
  161. data/lib/contrast/api/decorators/input_analysis.rb +3 -2
  162. data/lib/contrast/api/decorators/message.rb +76 -0
  163. data/lib/contrast/api/decorators/rasp_rule_sample.rb +29 -0
  164. data/lib/contrast/api/decorators/route_coverage.rb +58 -0
  165. data/lib/contrast/api/decorators/server_features.rb +3 -2
  166. data/lib/contrast/api/decorators/trace_event.rb +100 -0
  167. data/lib/contrast/api/decorators/trace_event_object.rb +58 -0
  168. data/lib/contrast/api/decorators/trace_event_signature.rb +47 -0
  169. data/lib/contrast/api/decorators/trace_taint_range.rb +52 -0
  170. data/lib/contrast/api/decorators/trace_taint_range_tags.rb +109 -0
  171. data/lib/contrast/api/decorators/user_input.rb +40 -0
  172. data/lib/contrast/common_agent_configuration.rb +2 -2
  173. data/lib/contrast/components/agent.rb +2 -2
  174. data/lib/contrast/components/app_context.rb +12 -17
  175. data/lib/contrast/components/config.rb +8 -23
  176. data/lib/contrast/components/contrast_service.rb +3 -3
  177. data/lib/contrast/components/interface.rb +16 -16
  178. data/lib/contrast/components/logger.rb +1 -1
  179. data/lib/contrast/components/scope.rb +3 -3
  180. data/lib/contrast/components/settings.rb +0 -6
  181. data/lib/contrast/config.rb +18 -18
  182. data/lib/contrast/config/application_configuration.rb +5 -2
  183. data/lib/contrast/config/base_configuration.rb +2 -2
  184. data/lib/contrast/config/protect_rule_configuration.rb +1 -1
  185. data/lib/contrast/config/service_configuration.rb +8 -2
  186. data/lib/contrast/configuration.rb +93 -52
  187. data/lib/contrast/extension/assess.rb +21 -23
  188. data/lib/contrast/extension/assess/array.rb +12 -9
  189. data/lib/contrast/extension/assess/erb.rb +6 -3
  190. data/lib/contrast/extension/assess/eval_trigger.rb +3 -3
  191. data/lib/contrast/extension/assess/exec_trigger.rb +1 -1
  192. data/lib/contrast/extension/assess/fiber.rb +12 -12
  193. data/lib/contrast/extension/assess/hash.rb +5 -4
  194. data/lib/contrast/extension/assess/kernel.rb +19 -11
  195. data/lib/contrast/extension/assess/marshal.rb +63 -0
  196. data/lib/contrast/extension/assess/regexp.rb +8 -7
  197. data/lib/contrast/extension/assess/string.rb +13 -15
  198. data/lib/contrast/extension/inventory.rb +4 -5
  199. data/lib/contrast/extension/kernel.rb +1 -1
  200. data/lib/contrast/extension/module.rb +1 -1
  201. data/lib/contrast/extension/protect.rb +3 -3
  202. data/lib/contrast/extension/protect/kernel.rb +2 -2
  203. data/lib/contrast/extension/protect/psych.rb +2 -2
  204. data/lib/contrast/framework/base_support.rb +0 -23
  205. data/lib/contrast/framework/manager.rb +7 -17
  206. data/lib/contrast/framework/rack/patch/session_cookie.rb +13 -19
  207. data/lib/contrast/framework/rack/patch/support.rb +1 -1
  208. data/lib/contrast/framework/rack/support.rb +2 -2
  209. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +1 -3
  210. data/lib/contrast/framework/rails/patch/assess_configuration.rb +6 -13
  211. data/lib/contrast/framework/rails/patch/rails_application_configuration.rb +1 -1
  212. data/lib/contrast/framework/rails/patch/support.rb +3 -3
  213. data/lib/contrast/framework/rails/rewrite/action_controller_railties_helper_inherited.rb +2 -2
  214. data/lib/contrast/framework/rails/rewrite/active_record_named.rb +2 -2
  215. data/lib/contrast/framework/rails/rewrite/active_record_time_zone_inherited.rb +2 -2
  216. data/lib/contrast/framework/rails/support.rb +58 -37
  217. data/lib/contrast/framework/sinatra/patch/base.rb +2 -2
  218. data/lib/contrast/framework/sinatra/patch/support.rb +1 -1
  219. data/lib/contrast/framework/sinatra/support.rb +13 -24
  220. data/lib/contrast/funchook/funchook.rb +45 -0
  221. data/lib/contrast/logger/application.rb +13 -5
  222. data/lib/contrast/logger/format.rb +64 -0
  223. data/lib/contrast/logger/log.rb +17 -9
  224. data/lib/contrast/logger/request.rb +30 -0
  225. data/lib/contrast/tasks/config.rb +1 -1
  226. data/lib/contrast/tasks/service.rb +2 -2
  227. data/lib/contrast/utils/assess/sampling_util.rb +2 -2
  228. data/lib/contrast/utils/assess/tracking_util.rb +89 -19
  229. data/lib/contrast/utils/boolean_util.rb +1 -1
  230. data/lib/contrast/utils/class_util.rb +2 -2
  231. data/lib/contrast/utils/duck_utils.rb +0 -10
  232. data/lib/contrast/utils/env_configuration_item.rb +2 -1
  233. data/lib/contrast/utils/gemfile_reader.rb +5 -5
  234. data/lib/contrast/utils/hash_digest.rb +13 -3
  235. data/lib/contrast/utils/heap_dump_util.rb +2 -2
  236. data/lib/contrast/utils/invalid_configuration_util.rb +21 -35
  237. data/lib/contrast/utils/inventory_util.rb +6 -11
  238. data/lib/contrast/utils/io_util.rb +1 -1
  239. data/lib/contrast/utils/object_share.rb +0 -1
  240. data/lib/contrast/utils/os.rb +16 -4
  241. data/lib/contrast/utils/ruby_ast_rewriter.rb +1 -1
  242. data/lib/contrast/utils/sha256_builder.rb +2 -2
  243. data/lib/contrast/utils/stack_trace_utils.rb +2 -3
  244. data/lib/contrast/utils/string_utils.rb +11 -6
  245. data/lib/contrast/utils/tag_util.rb +1 -1
  246. data/lib/contrast/utils/thread_tracker.rb +1 -14
  247. data/lib/contrast/utils/timer.rb +1 -17
  248. data/resources/assess/policy.json +0 -10
  249. data/resources/deadzone/policy.json +5 -0
  250. data/ruby-agent.gemspec +24 -23
  251. data/service_executables/VERSION +1 -1
  252. data/service_executables/linux/contrast-service +0 -0
  253. data/service_executables/mac/contrast-service +0 -0
  254. metadata +92 -92
  255. data/funchook/Makefile +0 -29
  256. data/funchook/autom4te.cache/output.0 +0 -4964
  257. data/funchook/autom4te.cache/requests +0 -77
  258. data/funchook/autom4te.cache/traces.0 +0 -361
  259. data/funchook/config.log +0 -651
  260. data/funchook/config.status +0 -1015
  261. data/funchook/configure +0 -4964
  262. data/funchook/src/Makefile +0 -70
  263. data/funchook/src/config.h +0 -101
  264. data/funchook/src/config.h.in +0 -100
  265. data/funchook/src/decoder.o +0 -0
  266. data/funchook/src/distorm.o +0 -0
  267. data/funchook/src/funchook.o +0 -0
  268. data/funchook/src/funchook_io.o +0 -0
  269. data/funchook/src/funchook_syscall.o +0 -0
  270. data/funchook/src/funchook_unix.o +0 -0
  271. data/funchook/src/funchook_x86.o +0 -0
  272. data/funchook/src/instructions.o +0 -0
  273. data/funchook/src/insts.o +0 -0
  274. data/funchook/src/libfunchook.dylib +0 -0
  275. data/funchook/src/mnemonics.o +0 -0
  276. data/funchook/src/operands.o +0 -0
  277. data/funchook/src/os_func.o +0 -0
  278. data/funchook/src/os_func_unix.o +0 -0
  279. data/funchook/src/prefix.o +0 -0
  280. data/funchook/src/printf_base.o +0 -0
  281. data/funchook/src/textdefs.o +0 -0
  282. data/funchook/src/wstring.o +0 -0
  283. data/funchook/test/Makefile +0 -43
  284. data/funchook/test/funchook_test +0 -0
  285. data/funchook/test/libfunchook_test.so +0 -0
  286. data/funchook/test/libfunchook_test.so.dSYM/Contents/Info.plist +0 -20
  287. data/funchook/test/libfunchook_test.so.dSYM/Contents/Resources/DWARF/libfunchook_test.so +0 -0
  288. data/funchook/test/test_main.o +0 -0
  289. data/funchook/test/x86_64_test.o +0 -0
  290. data/lib/contrast/agent/assess/adjusted_span.rb +0 -27
  291. data/lib/contrast/agent/assess/insulator.rb +0 -49
  292. data/lib/contrast/agent/require_state.rb +0 -61
  293. data/lib/contrast/agent/socket_client.rb +0 -134
  294. data/lib/contrast/api/connection_status.rb +0 -49
  295. data/lib/contrast/api/socket.rb +0 -43
  296. data/lib/contrast/api/speedracer.rb +0 -188
  297. data/lib/contrast/api/tcp_socket.rb +0 -29
  298. data/lib/contrast/api/unix_socket.rb +0 -25
  299. data/lib/contrast/extension/assess/assess_extension.rb +0 -148
  300. data/lib/contrast/framework/sinatra/application_helper.rb +0 -51
  301. data/lib/contrast/framework/view_technologies_descriptor.rb +0 -21
  302. data/lib/contrast/internal_exception.rb +0 -8
  303. data/lib/contrast/utils/cache.rb +0 -58
  304. data/lib/contrast/utils/freeze_util.rb +0 -32
  305. data/lib/contrast/utils/service_sender_util.rb +0 -167
  306. data/lib/contrast/utils/sinatra_helper.rb +0 -49
@@ -1,8 +1,9 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- cs__scoped_require 'contrast/agent/assess/policy/trigger/reflected_xss'
5
- cs__scoped_require 'contrast/agent/assess/policy/trigger/xpath'
4
+ require 'contrast/agent/assess/policy/trigger/reflected_xss'
5
+ require 'contrast/agent/assess/policy/trigger/xpath'
6
+ require 'contrast/api/decorators/trace_taint_range_tags'
6
7
 
7
8
  module Contrast
8
9
  module Agent
@@ -99,16 +100,17 @@ module Contrast
99
100
  # if the source isn't tracked, there can't be a violation
100
101
  # this condition may not hold true forever, but for now it's
101
102
  # a nice optimization
102
- return false unless source.cs__tracked?
103
+ return false unless Contrast::Agent::Assess::Tracker.tracked?(source)
103
104
 
105
+ properties = Contrast::Agent::Assess::Tracker.properties(source)
104
106
  # find the ranges that violate the rule (untrusted, etc)
105
- vulnerable_ranges = find_ranges_by_all_tags(Contrast::Utils::StringUtils.ret_length(source), source.cs__properties, required_tags)
107
+ vulnerable_ranges = find_ranges_by_all_tags(Contrast::Utils::StringUtils.ret_length(source), properties, required_tags)
106
108
  # if there aren't any vulnerable ranges, nope out
107
109
  return false if vulnerable_ranges.empty?
108
110
 
109
111
  # find the ranges that are exempt from the rule
110
112
  # (validated, sanitized, etc)
111
- secure_ranges = find_ranges_by_any_tag(source.cs__properties, disallowed_tags)
113
+ secure_ranges = find_ranges_by_any_tag(properties, disallowed_tags)
112
114
  # if there are vulnerable ranges and no secure, report
113
115
  return true if secure_ranges.empty?
114
116
 
@@ -167,8 +169,8 @@ module Contrast
167
169
 
168
170
  tags.each do |tag|
169
171
  raise(ArgumentError, "Rule #{ rule_id } had an invalid tag. #{ tag } is not a known value.") unless
170
- Contrast::Agent::Assess::Policy::PolicyNode::VALID_TAGS.include?(tag) ||
171
- Contrast::Agent::Assess::Policy::PolicyNode::VALID_SOURCE_TAGS.include?(tag)
172
+ Contrast::Api::Decorators::TraceTaintRangeTags::VALID_TAGS.include?(tag) ||
173
+ Contrast::Api::Decorators::TraceTaintRangeTags::VALID_SOURCE_TAGS.include?(tag)
172
174
  end
173
175
  end
174
176
 
@@ -177,24 +179,35 @@ module Contrast
177
179
  # @param length [Integer] the length of the object which may have the
178
180
  # given tags -- used as the maximum index to search for all of the
179
181
  # tags.
180
- # @param cs__properties [Contrast::Agent::Assess::Properties] the
182
+ # @param properties [Contrast::Agent::Assess::Properties] the
181
183
  # properties to check for the tags
182
184
  # @param tags [Set<String>] the list of tags on which to match
183
185
  # @return [Array<Contrast::Agent::Assess::Tag>] the ranges satisfied
184
186
  # by the given conditions
185
- def find_ranges_by_all_tags length, cs__properties, tags
187
+ def find_ranges_by_all_tags length, properties, tags
186
188
  # if there aren't any all_tags or tags, break early
187
- return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless cs__properties.tracked?
189
+ return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless properties.tracked?
188
190
  return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless tags&.any?
189
191
 
190
192
  # :zap: faster to treat all as any if there's only one tag
191
- return find_ranges_by_any_tag(cs__properties, tags) if tags.length == 1
193
+ return find_ranges_by_any_tag(properties, tags) if tags.length == 1
192
194
 
193
195
  ranges = []
196
+ # TODO: RUBY-946 clean this up, perhaps with
197
+ # tags.each { |tag| applicable << properties.fetch_tag(tag) }
198
+ # return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless applicable.length == tags.length
199
+ # ...
194
200
  # find all the indicies on the source that have all the given tags
195
201
  (0..length).each do |idx|
196
- tags_at = cs__properties.tag_names_at(idx)
197
- ranges << idx if tags.all? { |tag| tags_at.include?(tag) }
202
+ tags_at = properties.tags_at(idx)
203
+ ranges << idx if tags.all? do |tag|
204
+ found = false
205
+ tags_at.each do |tag_at|
206
+ found = tag_at.label == tag
207
+ break if found
208
+ end
209
+ found
210
+ end
198
211
  end
199
212
  # break early if no indicies satisfy all the tags
200
213
  return Contrast::Utils::ObjectShare::EMPTY_ARRAY if ranges.empty?
@@ -215,19 +228,19 @@ module Contrast
215
228
 
216
229
  # Find the ranges that satisfy any of the given tags.
217
230
  #
218
- # @param cs__properties [Contrast::Agent::Assess::Properties] the
231
+ # @param properties [Contrast::Agent::Assess::Properties] the
219
232
  # properties to check for the tags
220
233
  # @param tags [Set<String>] the list of tags on which to match
221
234
  # @return [Array<Contrast::Agent::Assess::Tag>] the ranges satisfied
222
235
  # by the given conditions
223
- def find_ranges_by_any_tag cs__properties, tags
236
+ def find_ranges_by_any_tag properties, tags
224
237
  # if there aren't any all_tags or tags, break early
225
- return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless cs__properties.tracked?
238
+ return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless properties.tracked?
226
239
  return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless tags&.any?
227
240
 
228
241
  ranges = []
229
242
  tags.each do |desired|
230
- found = cs__properties.fetch_tag(desired)
243
+ found = properties.fetch_tag(desired)
231
244
  next unless found
232
245
 
233
246
  # we need to dup here so that we don't change the tags if target is
@@ -38,7 +38,8 @@ module Contrast
38
38
  finish = match.begin(:path)
39
39
  finish ||= url.length
40
40
 
41
- args[0].cs__properties.any_tags_between?(start, finish)
41
+ properties = Contrast::Agent::Assess::Tracker.properties(args[0])
42
+ properties.any_tags_between?(start, finish)
42
43
  end
43
44
  end
44
45
  end
@@ -1,8 +1,8 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- cs__scoped_require 'contrast/agent/assess/policy/trigger_validation/ssrf_validator'
5
- cs__scoped_require 'contrast/agent/assess/policy/trigger_validation/xss_validator'
4
+ require 'contrast/agent/assess/policy/trigger_validation/ssrf_validator'
5
+ require 'contrast/agent/assess/policy/trigger_validation/xss_validator'
6
6
 
7
7
  module Contrast
8
8
  module Agent
@@ -1,12 +1,12 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- cs__scoped_require 'set'
5
- cs__scoped_require 'base64'
6
- cs__scoped_require 'contrast/utils/prevent_serialization'
7
- cs__scoped_require 'contrast/utils/tag_util'
8
- cs__scoped_require 'contrast/agent/assess/events/event_factory'
9
- cs__scoped_require 'contrast/agent/assess/events/source_event'
4
+ require 'base64'
5
+ require 'set'
6
+ require 'contrast/agent/assess/property/evented'
7
+ require 'contrast/agent/assess/property/tagged'
8
+ require 'contrast/agent/assess/property/updated'
9
+ require 'contrast/utils/prevent_serialization'
10
10
 
11
11
  module Contrast
12
12
  module Agent
@@ -19,6 +19,11 @@ module Contrast
19
19
  # user input.
20
20
  class Properties
21
21
  include Contrast::Utils::PreventSerialization
22
+ include Contrast::Agent::Assess::Property::Evented
23
+ include Contrast::Agent::Assess::Property::Tagged
24
+ include Contrast::Agent::Assess::Property::Updated
25
+
26
+ attr_accessor :dupped_from
22
27
 
23
28
  # CONTRAST-36937
24
29
  # Creating these on Properties is expensive. We want to delay this for
@@ -27,18 +32,6 @@ module Contrast
27
32
  @_properties ||= {}
28
33
  end
29
34
 
30
- def events
31
- @_events ||= []
32
- end
33
-
34
- def tracked?
35
- tags? && tags.any?
36
- end
37
-
38
- def tagged? label
39
- tags? && tags.key?(label)
40
- end
41
-
42
35
  def add_properties hash
43
36
  return unless hash
44
37
 
@@ -51,371 +44,10 @@ module Contrast
51
44
  properties[name] = value
52
45
  end
53
46
 
54
- def any_tags_between? start, finish
55
- return false unless tags?
56
-
57
- range = Contrast::Agent::Assess::Tag.new(start + finish, start)
58
- tags.each_value do |tag_array|
59
- return true if tag_array.any? { |tag| tag.overlaps?(range) }
60
- end
61
- false
62
- end
63
-
64
- # Find all of the ranges that span a given index. This is used
65
- # in propagation when we need to shift tags about. For instance, in
66
- # the append method when we need to see if any tag at the end needs
67
- # to be expanded out to the size of the new String.
68
- #
69
- # Note: Tags do not know their key, so this is only the range covered
70
- #
71
- # @param idx [Integer] the index to check for tags
72
- # @return [Array<Contrast::Agent::Assess::Tag>] a set of all the Tags
73
- # covering the given index. This is only the ranges, not the names.
74
- def tags_at idx
75
- return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless tags?
76
-
77
- at = []
78
- tags.each_value do |tag_array|
79
- tag_array.each do |tag|
80
- if tag.covers?(idx)
81
- at << tag
82
- elsif tag.above?(idx)
83
- break
84
- end
85
- end
86
- end
87
- at
88
- end
89
-
90
- # Find all of the tag names that span a given index.
91
- #
92
- # @param idx [Integer] the index to check for tags
93
- # @return [Set<String>] a set of all the tags covering the given index.
94
- # This is only the names of the tags, not their ranges.
95
- def tag_names_at idx
96
- return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless tags?
97
-
98
- at = Set.new
99
- tags.each_pair do |tag_name, tag_array|
100
- tag_array.each do |tag|
101
- if tag.covers?(idx)
102
- at << tag_name
103
- elsif tag.above?(idx)
104
- break
105
- end
106
- end
107
- end
108
- at
109
- end
110
-
111
- # given a range, select all tags in that range the selected tags are
112
- # shifted such that the start index of the new tag (0) aligns with the
113
- # given start index in the range
114
- #
115
- # current tags: 5-15
116
- # range : 5-10
117
- # result : 0-05
118
- def tags_at_range range
119
- return Contrast::Utils::ObjectShare::EMPTY_HASH unless tags?
120
-
121
- at = Hash.new { |h, k| h[k] = [] }
122
- length = range.stop - range.start
123
- tags.each_pair do |key, value|
124
- add = []
125
- value.each do |tag|
126
- comparison = tag.compare_range(range.start, range.stop)
127
- # BELOW and ABOVE are applicable to this check and are removed.
128
- case comparison
129
- # part of the tag is being selected
130
- when Contrast::Agent::Assess::Tag::LOW_SPAN
131
- add << Contrast::Agent::Assess::Tag.new(length)
132
- # the tag exists in the requested range, figure out the boundaries
133
- when Contrast::Agent::Assess::Tag::WITHIN
134
- start = tag.start_idx - range.start
135
- finish = length - start
136
- add << Contrast::Agent::Assess::Tag.new(finish, start)
137
- # the tag spans the requested range.
138
- when Contrast::Agent::Assess::Tag::WITHOUT
139
- add << Contrast::Agent::Assess::Tag.new(length)
140
- # part of the tag is being selected
141
- when Contrast::Agent::Assess::Tag::HIGH_SPAN
142
- add << Contrast::Agent::Assess::Tag.new(length)
143
- end
144
- end
145
- next if add.empty?
146
-
147
- at[key] = add
148
- end
149
- at
150
- end
151
-
152
- # Given a tag name and range object, add a new tag to this
153
- # collection. If the given range touches an existing tag,
154
- # we'll combine the two, adjusting the existing one and
155
- # dropping this new one.
156
- #
157
- # @param label [String] the name of the tag
158
- # @param range [Contrast::Agent::Assess::AdjustedSpan] the span that
159
- # the tag covers
160
- def add_tag label, range
161
- length = range.stop - range.start
162
- tag = Contrast::Agent::Assess::Tag.new(length, range.start)
163
- existing = fetch_tag(label)
164
- tags[label] = Contrast::Utils::TagUtil.ordered_merge(existing, tag)
165
- end
166
-
167
- def set_tags label, tag_ranges
168
- tags[label] = tag_ranges
169
- end
170
-
171
- # Remove all tags with a given label
172
- def delete_tags label
173
- return unless tags?
174
-
175
- tags.delete(label)
176
- end
177
-
178
- # Reset the tag hash
179
- def clear_tags
180
- return unless tags?
181
-
182
- tags.clear
183
- end
184
-
185
- # Returns a list of all current tag labels, most likely to be used for
186
- # a splat operation
187
- def tag_keys
188
- return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless tags?
189
-
190
- tags.keys
191
- end
192
-
193
- # Calls merge to combine touching or overlapping tags
194
- # Deletes empty tags
195
- def cleanup_tags
196
- return unless tags?
197
-
198
- Contrast::Utils::TagUtil.merge_tags(tags)
199
- tags.delete_if { |_, value| value.empty? }
200
- end
201
-
202
- # We'll use this as a helper method to retrieve tags from the hash.
203
- # Because the hash auto-populates an empty array when we try to access
204
- # a tag in it, we cannot use the [] method without side effect. To get
205
- # around this, we'll use a fetch work around.
206
- def fetch_tag label
207
- return unless tags?
208
-
209
- tags.fetch(label, nil)
210
- end
211
-
212
- # Convert the tags of this object into the TraceTaintRange requried to
213
- # be sent to the service
214
- def tags_to_dtm
215
- return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless tags?
216
-
217
- ranges = []
218
- tags.each_pair do |key, value|
219
- next if value.empty?
220
-
221
- value.each do |tag|
222
- range = Contrast::Api::Dtm::TraceTaintRange.new
223
- range.tag = Contrast::Utils::StringUtils.protobuf_safe_string(key)
224
- range.range = tag.start_idx.to_s + Contrast::Utils::ObjectShare::COLON + tag.end_idx.to_s
225
- ranges << range
226
- end
227
- end
228
- ranges
229
- end
230
-
231
- # Remove all tags within the given ranges.
232
- # This does not delete an entire tag if part of that tag is
233
- # outside this range, meaning we may reduce sizes of tags
234
- # or split them.
235
- #
236
- # If shift is true, it is assumed the characters at those ranges were
237
- # removed. If shift is false, it is assumed those ranges were replaced
238
- # by the same number of characters and no shift is needed.
239
- #
240
- # current tags: 0-15
241
- # range: 5-10
242
- # result: 0-5, 10-15
243
- def delete_tags_at_ranges ranges, shift = true
244
- return unless tags?
245
-
246
- # Stage one - delete the tags w/o changing their
247
- # location.
248
- ranges.each do |range|
249
- remove_tags(range)
250
- end
251
-
252
- return unless shift
253
-
254
- # the amount we've already removed from the string
255
- shift = 0
256
- # Stage two - shift the tags to the left to account
257
- # for the sections that were deleted.
258
- ranges.each do |range|
259
- shift_tags_for_deletion(range, shift)
260
- shift += (range.stop - range.start)
261
- end
262
-
263
- # Clean up and merge any touching tags
264
- Contrast::Utils::TagUtil.merge_tags(tags)
265
- end
266
-
267
- # Shift all the tags in this object by the given ranges.
268
- # This method assumes the ranges are sorted, meaning
269
- # the leftmost (lowest) range is first
270
- #
271
- # current tags: 0-15
272
- # range: 5-10
273
- # result: 0-5, 10-20
274
- def shift_tags ranges
275
- return unless tags?
276
-
277
- ranges.each do |range|
278
- shift_tags_for_insertion(range)
279
- end
280
- end
281
-
282
- # Add an event to these properties. It will be used to build
283
- # a trace if this object ends up in a trigger.
284
- def add_event event
285
- events << event
286
- self
287
- end
288
-
289
- def build_event policy_node, tagged, object, ret, args, source_type = nil, source_name = nil
290
- event = Contrast::Agent::Assess::Events::EventFactory.build(policy_node, tagged, object, ret, args, source_type, source_name)
291
- add_event(event)
292
- report_sources(tagged, event)
293
- end
294
-
295
- private
296
-
297
- def report_sources tagged, event
298
- return unless tagged && !tagged.to_s.empty?
299
- return unless event.cs__is_a?(Contrast::Agent::Assess::Events::SourceEvent)
300
- return unless event&.source_type
301
-
302
- current_request = Contrast::Agent::REQUEST_TRACKER.current
303
- return unless current_request
304
- return if current_request.observed_route.sources.any? { |source| source.type == event.forced_source_type && source.name == event.forced_source_name }
305
-
306
- event_source_dtm = event.build_event_source_dtm
307
- return unless event_source_dtm
308
-
309
- current_request.observed_route.sources << event_source_dtm
310
- end
311
-
312
- # Because of the auto-fill thing, we should not allow direct access to
313
- # the tags hash. Instead, the methods above should be used to do
314
- # operations like add, delete, and fetch.
315
- #
316
- # CONTRAST-22914
317
- # please do NOT expose this w/ an attr_reader / accessor. there are
318
- # helper methods in this class that safely access the hash. the tags
319
- # method is private to avoid the side effect of a direct lookup with
320
- # `[]` adding an empty array to the hash.
321
- def tags
322
- @_tags ||= Hash.new { |h, k| h[k] = [] }
323
- end
324
-
325
- # Creating Tags is expensive and we check for Tags all the time on
326
- # untracked things. ALWAYS!!! call this method before checking if
327
- # an object has tags
328
- def tags?
329
- instance_variable_defined?(:@_tags)
330
- end
331
-
332
- # Remove the tag ranges covering the given range
333
- def remove_tags range
334
- return unless tags?
335
-
336
- full_delete = []
337
- tags.each_pair do |key, value|
338
- remove = []
339
- add = []
340
- value.each do |tag|
341
- comparison = tag.compare_range(range.start, range.stop)
342
- # ABOVE and BELOW are not affected by this check
343
- case comparison
344
- when Contrast::Agent::Assess::Tag::LOW_SPAN
345
- tag.update_end(range.start)
346
- when Contrast::Agent::Assess::Tag::WITHIN
347
- remove << tag
348
- when Contrast::Agent::Assess::Tag::WITHOUT
349
- new_tag = tag.clone
350
- new_tag.update_start(range.stop)
351
- add << new_tag
352
- tag.update_end(range.start)
353
- when Contrast::Agent::Assess::Tag::HIGH_SPAN
354
- tag.update_start(range.stop)
355
- end
356
- end
357
- value.delete_if { |tag| remove.include?(tag) }
358
- Contrast::Utils::TagUtil.ordered_merge(value, add)
359
- full_delete << key if value.empty?
360
- end
361
- full_delete.each { |key| tags.delete(key) }
362
- end
363
-
364
- # Shift the tag ranges covering the given range
365
- # We assume this is for a deletion, meaning we
366
- # have to move tags to the left
367
- def shift_tags_for_deletion range, shift
368
- return unless tags?
369
-
370
- tags.each_value do |value|
371
- value.each do |tag|
372
- comparison = tag.compare_range(range.start - shift, range.stop - shift)
373
- length = range.stop - range.start
374
- case comparison
375
- # this is really the only thing we need to shift
376
- when Contrast::Agent::Assess::Tag::ABOVE
377
- tag.shift(0 - length)
378
- end
379
- end
380
- end
381
- end
382
-
383
- # Shift the tag ranges covering the given range
384
- # We assume this is for a insertion, meaning we
385
- # have to move tags to the right
386
- def shift_tags_for_insertion range
387
- return unless tags?
388
-
389
- tags.each_value do |value|
390
- add = []
391
- value.each do |tag|
392
- comparison = tag.compare_range(range.start, range.stop)
393
- length = range.stop - range.start
394
- # BELOW is not affected by this check
395
- case comparison
396
- # part of the tag is being inserted on
397
- when Contrast::Agent::Assess::Tag::LOW_SPAN
398
- new_tag = tag.clone
399
- new_tag.update_start(range.start)
400
- new_tag.shift(length)
401
- add << new_tag
402
- tag.update_end(range.start)
403
- # the tag exists in the inserted range. it is partially shifted
404
- when Contrast::Agent::Assess::Tag::WITHIN
405
- tag.shift(length)
406
- # the tag spans the range. leave the part below alone
407
- when Contrast::Agent::Assess::Tag::WITHOUT
408
- new_tag = tag.clone
409
- new_tag.update_start(range.start)
410
- new_tag.shift(length)
411
- add << new_tag
412
- tag.update_end(range.start)
413
- when Contrast::Agent::Assess::Tag::HIGH_SPAN, Contrast::Agent::Assess::Tag::ABOVE
414
- tag.shift(length)
415
- end
416
- end
417
- Contrast::Utils::TagUtil.ordered_merge(value, add)
418
- end
47
+ def dup
48
+ ret = super
49
+ ret.dupped_from = __id__
50
+ ret
419
51
  end
420
52
  end
421
53
  end