contrast-agent 3.9.1 → 3.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (319) hide show
  1. checksums.yaml +4 -4
  2. data/.flayignore +1 -0
  3. data/.simplecov +5 -2
  4. data/ext/build_funchook.rb +12 -7
  5. data/ext/cs__assess_active_record_named/cs__active_record_named.c +12 -14
  6. data/ext/cs__assess_active_record_named/cs__active_record_named.h +1 -0
  7. data/ext/cs__assess_active_record_named/extconf.rb +3 -0
  8. data/ext/cs__assess_array/cs__assess_array.c +5 -6
  9. data/ext/cs__assess_array/cs__assess_array.h +1 -0
  10. data/ext/cs__assess_array/extconf.rb +3 -0
  11. data/ext/cs__assess_basic_object/cs__assess_basic_object.c +13 -11
  12. data/ext/cs__assess_basic_object/cs__assess_basic_object.h +2 -1
  13. data/ext/cs__assess_basic_object/extconf.rb +3 -0
  14. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.c +4 -3
  15. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.h +3 -3
  16. data/ext/cs__assess_fiber_track/extconf.rb +3 -0
  17. data/ext/cs__assess_hash/cs__assess_hash.c +40 -17
  18. data/ext/cs__assess_hash/cs__assess_hash.h +4 -6
  19. data/ext/cs__assess_hash/extconf.rb +3 -0
  20. data/ext/cs__assess_kernel/cs__assess_kernel.c +11 -9
  21. data/ext/cs__assess_kernel/cs__assess_kernel.h +1 -0
  22. data/ext/cs__assess_kernel/extconf.rb +3 -0
  23. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +3 -6
  24. data/ext/cs__assess_marshal_module/extconf.rb +3 -0
  25. data/ext/cs__assess_module/cs__assess_module.c +16 -14
  26. data/ext/cs__assess_module/cs__assess_module.h +3 -0
  27. data/ext/cs__assess_module/extconf.rb +3 -0
  28. data/ext/cs__assess_regexp/cs__assess_regexp.c +13 -9
  29. data/ext/cs__assess_regexp/cs__assess_regexp.h +1 -0
  30. data/ext/cs__assess_regexp/extconf.rb +3 -0
  31. data/ext/cs__assess_string/cs__assess_string.c +5 -8
  32. data/ext/cs__assess_string/cs__assess_string.h +2 -1
  33. data/ext/cs__assess_string/extconf.rb +3 -0
  34. data/ext/cs__assess_string_interpolation26/cs__assess_string_interpolation26.c +2 -2
  35. data/ext/cs__assess_string_interpolation26/cs__assess_string_interpolation26.h +3 -3
  36. data/ext/cs__assess_string_interpolation26/extconf.rb +3 -0
  37. data/ext/cs__assess_yield_track/cs__assess_yield_track.h +1 -1
  38. data/ext/cs__assess_yield_track/extconf.rb +3 -0
  39. data/ext/cs__common/cs__common.c +80 -1
  40. data/ext/cs__common/cs__common.h +34 -0
  41. data/ext/cs__common/extconf.rb +9 -8
  42. data/ext/cs__contrast_patch/cs__contrast_patch.h +1 -6
  43. data/ext/cs__contrast_patch/extconf.rb +3 -0
  44. data/ext/cs__protect_kernel/cs__protect_kernel.c +23 -12
  45. data/ext/cs__protect_kernel/cs__protect_kernel.h +1 -0
  46. data/ext/cs__protect_kernel/extconf.rb +3 -0
  47. data/ext/extconf_common.rb +10 -8
  48. data/funchook/autom4te.cache/requests +48 -48
  49. data/funchook/config.log +4 -4
  50. data/lib/contrast.rb +1 -1
  51. data/lib/contrast/agent.rb +32 -29
  52. data/lib/contrast/agent/assess.rb +1 -11
  53. data/lib/contrast/agent/assess/adjusted_span.rb +3 -1
  54. data/lib/contrast/agent/assess/contrast_event.rb +20 -68
  55. data/lib/contrast/agent/assess/events/event_factory.rb +25 -0
  56. data/lib/contrast/agent/assess/events/source_event.rb +83 -0
  57. data/lib/contrast/agent/assess/insulator.rb +0 -4
  58. data/lib/contrast/agent/assess/policy/patcher.rb +6 -2
  59. data/lib/contrast/agent/assess/policy/policy_node.rb +1 -8
  60. data/lib/contrast/agent/assess/policy/policy_scanner.rb +2 -2
  61. data/lib/contrast/agent/assess/policy/preshift.rb +1 -1
  62. data/lib/contrast/agent/assess/policy/propagation_method.rb +68 -33
  63. data/lib/contrast/agent/assess/policy/propagation_node.rb +2 -1
  64. data/lib/contrast/agent/assess/policy/propagator.rb +1 -0
  65. data/lib/contrast/agent/assess/policy/propagator/custom.rb +1 -1
  66. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +1 -3
  67. data/lib/contrast/agent/assess/policy/propagator/match_data.rb +80 -0
  68. data/lib/contrast/agent/assess/policy/propagator/select.rb +35 -22
  69. data/lib/contrast/agent/assess/policy/propagator/split.rb +26 -6
  70. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +2 -0
  71. data/lib/contrast/agent/assess/policy/rewriter_patch.rb +40 -27
  72. data/lib/contrast/agent/assess/policy/source_method.rb +20 -20
  73. data/lib/contrast/agent/assess/policy/source_node.rb +0 -15
  74. data/lib/contrast/agent/assess/policy/trigger/reflected_xss.rb +90 -0
  75. data/lib/contrast/agent/assess/policy/trigger/xpath.rb +57 -0
  76. data/lib/contrast/agent/assess/policy/trigger_method.rb +30 -45
  77. data/lib/contrast/agent/assess/policy/trigger_node.rb +7 -7
  78. data/lib/contrast/agent/assess/policy/trigger_validation/ssrf_validator.rb +2 -31
  79. data/lib/contrast/agent/assess/properties.rb +5 -3
  80. data/lib/contrast/agent/assess/rule/base.rb +1 -20
  81. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +23 -6
  82. data/lib/contrast/agent/assess/rule/redos.rb +4 -5
  83. data/lib/contrast/agent/assess/tag.rb +24 -14
  84. data/lib/contrast/agent/at_exit_hook.rb +16 -13
  85. data/lib/contrast/agent/class_reopener.rb +24 -10
  86. data/lib/contrast/agent/deadzone/policy/policy.rb +2 -2
  87. data/lib/contrast/agent/disable_reaction.rb +3 -4
  88. data/lib/contrast/agent/exclusion_matcher.rb +7 -48
  89. data/lib/contrast/agent/inventory/policy/datastores.rb +54 -0
  90. data/lib/contrast/agent/inventory/policy/policy.rb +1 -1
  91. data/lib/contrast/agent/middleware.rb +101 -260
  92. data/lib/contrast/agent/module_data.rb +2 -1
  93. data/lib/contrast/agent/patching/policy/after_load_patch.rb +13 -3
  94. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +59 -47
  95. data/lib/contrast/agent/patching/policy/method_policy.rb +3 -3
  96. data/lib/contrast/agent/patching/policy/module_policy.rb +0 -25
  97. data/lib/contrast/agent/patching/policy/patch.rb +97 -23
  98. data/lib/contrast/agent/patching/policy/patcher.rb +28 -30
  99. data/lib/contrast/agent/patching/policy/policy.rb +7 -7
  100. data/lib/contrast/agent/patching/policy/policy_node.rb +3 -11
  101. data/lib/contrast/agent/patching/policy/trigger_node.rb +2 -5
  102. data/lib/contrast/agent/protect/policy/applies_command_injection_rule.rb +63 -0
  103. data/lib/contrast/agent/protect/policy/applies_deserialization_rule.rb +52 -0
  104. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +68 -0
  105. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +117 -0
  106. data/lib/contrast/agent/protect/policy/applies_sqli_rule.rb +54 -0
  107. data/lib/contrast/agent/protect/policy/applies_xxe_rule.rb +129 -0
  108. data/lib/contrast/agent/protect/policy/policy.rb +6 -6
  109. data/lib/contrast/agent/protect/policy/rule_applicator.rb +51 -0
  110. data/lib/contrast/agent/protect/rule.rb +0 -5
  111. data/lib/contrast/agent/protect/rule/base.rb +19 -37
  112. data/lib/contrast/agent/protect/rule/base_service.rb +3 -1
  113. data/lib/contrast/agent/protect/rule/cmd_injection.rb +12 -15
  114. data/lib/contrast/agent/protect/rule/default_scanner.rb +0 -13
  115. data/lib/contrast/agent/protect/rule/deserialization.rb +2 -0
  116. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +2 -2
  117. data/lib/contrast/agent/protect/rule/no_sqli.rb +4 -4
  118. data/lib/contrast/agent/protect/rule/path_traversal.rb +6 -10
  119. data/lib/contrast/agent/protect/rule/sqli.rb +5 -4
  120. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +2 -0
  121. data/lib/contrast/agent/protect/rule/xss.rb +2 -0
  122. data/lib/contrast/agent/protect/rule/xxe.rb +10 -4
  123. data/lib/contrast/agent/railtie.rb +3 -8
  124. data/lib/contrast/agent/reaction_processor.rb +5 -5
  125. data/lib/contrast/agent/request.rb +11 -18
  126. data/lib/contrast/agent/request_context.rb +16 -19
  127. data/lib/contrast/agent/request_handler.rb +35 -0
  128. data/lib/contrast/agent/response.rb +39 -86
  129. data/lib/contrast/agent/rewriter.rb +25 -11
  130. data/lib/contrast/agent/rule_set.rb +49 -0
  131. data/lib/contrast/agent/scope.rb +4 -12
  132. data/lib/contrast/agent/service_heartbeat.rb +3 -4
  133. data/lib/contrast/agent/socket_client.rb +25 -19
  134. data/lib/contrast/agent/static_analysis.rb +41 -0
  135. data/lib/contrast/agent/thread.rb +1 -1
  136. data/lib/contrast/agent/tracepoint_hook.rb +1 -5
  137. data/lib/contrast/agent/version.rb +1 -1
  138. data/lib/contrast/api.rb +1 -1
  139. data/lib/contrast/api/decorators.rb +14 -0
  140. data/lib/contrast/api/decorators/application_settings.rb +37 -0
  141. data/lib/contrast/api/decorators/application_update.rb +66 -0
  142. data/lib/contrast/api/decorators/input_analysis.rb +17 -0
  143. data/lib/contrast/api/decorators/server_features.rb +24 -0
  144. data/lib/contrast/api/speedracer.rb +32 -30
  145. data/lib/contrast/api/tcp_socket.rb +0 -2
  146. data/lib/contrast/components/agent.rb +34 -24
  147. data/lib/contrast/components/app_context.rb +45 -38
  148. data/lib/contrast/components/assess.rb +25 -15
  149. data/lib/contrast/components/config.rb +7 -5
  150. data/lib/contrast/components/contrast_service.rb +23 -71
  151. data/lib/contrast/components/heap_dump.rb +12 -8
  152. data/lib/contrast/components/interface.rb +15 -22
  153. data/lib/contrast/components/inventory.rb +5 -1
  154. data/lib/contrast/components/logger.rb +3 -68
  155. data/lib/contrast/components/protect.rb +40 -4
  156. data/lib/contrast/components/sampling.rb +22 -11
  157. data/lib/contrast/components/scope.rb +2 -52
  158. data/lib/contrast/components/settings.rb +42 -23
  159. data/lib/contrast/config/base_configuration.rb +1 -0
  160. data/lib/contrast/config/default_value.rb +1 -0
  161. data/lib/contrast/config/protect_rule_configuration.rb +0 -14
  162. data/lib/contrast/config/protect_rules_configuration.rb +0 -1
  163. data/lib/contrast/configuration.rb +3 -5
  164. data/lib/contrast/{extensions/ruby_core → extension}/assess.rb +12 -15
  165. data/lib/contrast/extension/assess/array.rb +77 -0
  166. data/lib/contrast/{extensions/ruby_core → extension}/assess/assess_extension.rb +29 -24
  167. data/lib/contrast/{extensions/ruby_core → extension}/assess/erb.rb +0 -8
  168. data/lib/contrast/extension/assess/eval_trigger.rb +78 -0
  169. data/lib/contrast/{extensions/ruby_core → extension}/assess/exec_trigger.rb +7 -9
  170. data/lib/contrast/extension/assess/fiber.rb +113 -0
  171. data/lib/contrast/extension/assess/hash.rb +39 -0
  172. data/lib/contrast/extension/assess/kernel.rb +110 -0
  173. data/lib/contrast/extension/assess/regexp.rb +84 -0
  174. data/lib/contrast/{extensions/ruby_core → extension}/assess/string.rb +18 -10
  175. data/lib/contrast/{extensions/ruby_core → extension}/delegator.rb +0 -0
  176. data/lib/contrast/{extensions/ruby_core → extension}/inventory.rb +2 -2
  177. data/lib/contrast/extension/kernel.rb +54 -0
  178. data/lib/contrast/{extensions/ruby_core → extension}/module.rb +0 -0
  179. data/lib/contrast/{extensions/ruby_core → extension}/protect.rb +2 -2
  180. data/lib/contrast/extension/protect/kernel.rb +44 -0
  181. data/lib/contrast/{extensions/ruby_core → extension}/protect/psych.rb +1 -1
  182. data/lib/contrast/{extensions/ruby_core → extension}/thread.rb +0 -0
  183. data/lib/contrast/framework/base_support.rb +32 -0
  184. data/lib/contrast/framework/manager.rb +59 -8
  185. data/lib/contrast/framework/platform_version.rb +1 -0
  186. data/lib/contrast/framework/rack/patch/session_cookie.rb +126 -0
  187. data/lib/contrast/framework/rack/patch/support.rb +24 -0
  188. data/lib/contrast/framework/rack/support.rb +22 -0
  189. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +43 -0
  190. data/lib/contrast/framework/rails/patch/assess_configuration.rb +103 -0
  191. data/lib/contrast/framework/rails/patch/rails_application_configuration.rb +31 -0
  192. data/lib/contrast/framework/rails/patch/support.rb +67 -0
  193. data/lib/contrast/framework/rails/rewrite/action_controller_railties_helper_inherited.rb +34 -0
  194. data/lib/contrast/framework/rails/rewrite/active_record_attribute_methods_read.rb +39 -0
  195. data/lib/contrast/framework/rails/rewrite/active_record_named.rb +73 -0
  196. data/lib/contrast/framework/rails/rewrite/active_record_time_zone_inherited.rb +33 -0
  197. data/lib/contrast/framework/rails/support.rb +115 -0
  198. data/lib/contrast/framework/sinatra/application_helper.rb +51 -0
  199. data/lib/contrast/framework/sinatra/patch/base.rb +83 -0
  200. data/lib/contrast/framework/sinatra/patch/support.rb +27 -0
  201. data/lib/contrast/framework/sinatra/support.rb +109 -0
  202. data/lib/contrast/framework/view_technologies_descriptor.rb +1 -0
  203. data/lib/contrast/logger/application.rb +80 -0
  204. data/lib/contrast/logger/log.rb +142 -0
  205. data/lib/contrast/logger/time.rb +50 -0
  206. data/lib/contrast/tasks/config.rb +54 -0
  207. data/lib/contrast/tasks/service.rb +3 -13
  208. data/lib/contrast/utils/assess/sampling_util.rb +4 -9
  209. data/lib/contrast/utils/assess/tracking_util.rb +7 -1
  210. data/lib/contrast/utils/boolean_util.rb +2 -5
  211. data/lib/contrast/utils/cache.rb +0 -11
  212. data/lib/contrast/utils/class_util.rb +21 -2
  213. data/lib/contrast/utils/gemfile_reader.rb +7 -5
  214. data/lib/contrast/utils/hash_digest.rb +2 -11
  215. data/lib/contrast/utils/heap_dump_util.rb +12 -11
  216. data/lib/contrast/utils/invalid_configuration_util.rb +4 -4
  217. data/lib/contrast/utils/inventory_util.rb +2 -2
  218. data/lib/contrast/utils/io_util.rb +1 -11
  219. data/lib/contrast/utils/job_servers_running.rb +6 -4
  220. data/lib/contrast/utils/object_share.rb +1 -38
  221. data/lib/contrast/utils/os.rb +1 -25
  222. data/lib/contrast/utils/ruby_ast_rewriter.rb +5 -1
  223. data/lib/contrast/utils/service_response_util.rb +36 -60
  224. data/lib/contrast/utils/service_sender_util.rb +84 -23
  225. data/lib/contrast/utils/sinatra_helper.rb +0 -6
  226. data/lib/contrast/utils/stack_trace_utils.rb +86 -182
  227. data/lib/contrast/utils/string_utils.rb +18 -2
  228. data/lib/contrast/utils/tag_util.rb +11 -1
  229. data/lib/contrast/utils/thread_tracker.rb +2 -2
  230. data/lib/contrast/utils/timer.rb +0 -40
  231. data/resources/assess/policy.json +42 -71
  232. data/resources/inventory/policy.json +2 -2
  233. data/resources/protect/policy.json +15 -15
  234. data/ruby-agent.gemspec +12 -5
  235. data/service_executables/VERSION +1 -1
  236. data/service_executables/linux/contrast-service +0 -0
  237. data/service_executables/mac/contrast-service +0 -0
  238. metadata +123 -112
  239. data/ext/cs__assess_regexp_track/cs__assess_regexp_track.c +0 -63
  240. data/ext/cs__assess_regexp_track/cs__assess_regexp_track.h +0 -29
  241. data/ext/cs__assess_regexp_track/extconf.rb +0 -2
  242. data/lib/contrast/agent/assess/frozen_properties.rb +0 -41
  243. data/lib/contrast/agent/assess/rule/csrf.rb +0 -66
  244. data/lib/contrast/agent/assess/rule/csrf/csrf_action.rb +0 -28
  245. data/lib/contrast/agent/assess/rule/csrf/csrf_applicator.rb +0 -73
  246. data/lib/contrast/agent/assess/rule/csrf/csrf_watcher.rb +0 -132
  247. data/lib/contrast/agent/assess/rule/response_scanning_rule.rb +0 -47
  248. data/lib/contrast/agent/assess/rule/response_watcher.rb +0 -36
  249. data/lib/contrast/agent/assess/rule/watcher.rb +0 -36
  250. data/lib/contrast/agent/feature_state.rb +0 -379
  251. data/lib/contrast/agent/logger_manager.rb +0 -116
  252. data/lib/contrast/agent/protect/rule/csrf.rb +0 -118
  253. data/lib/contrast/agent/protect/rule/csrf/csrf_evaluator.rb +0 -103
  254. data/lib/contrast/agent/protect/rule/csrf/csrf_token_injector.rb +0 -85
  255. data/lib/contrast/agent/settings_state.rb +0 -152
  256. data/lib/contrast/delegators.rb +0 -9
  257. data/lib/contrast/delegators/application_update.rb +0 -32
  258. data/lib/contrast/extensions/framework/rack/cookie.rb +0 -24
  259. data/lib/contrast/extensions/framework/rack/request.rb +0 -24
  260. data/lib/contrast/extensions/framework/rack/response.rb +0 -23
  261. data/lib/contrast/extensions/framework/rails/action_controller_railties_helper_inherited.rb +0 -20
  262. data/lib/contrast/extensions/framework/rails/active_record.rb +0 -26
  263. data/lib/contrast/extensions/framework/rails/active_record_named.rb +0 -53
  264. data/lib/contrast/extensions/framework/rails/active_record_time_zone_inherited.rb +0 -21
  265. data/lib/contrast/extensions/framework/rails/buffer.rb +0 -28
  266. data/lib/contrast/extensions/framework/rails/configuration.rb +0 -27
  267. data/lib/contrast/extensions/framework/sinatra/base.rb +0 -59
  268. data/lib/contrast/extensions/ruby_core/assess/array.rb +0 -59
  269. data/lib/contrast/extensions/ruby_core/assess/basic_object.rb +0 -15
  270. data/lib/contrast/extensions/ruby_core/assess/fiber.rb +0 -124
  271. data/lib/contrast/extensions/ruby_core/assess/hash.rb +0 -22
  272. data/lib/contrast/extensions/ruby_core/assess/kernel.rb +0 -95
  273. data/lib/contrast/extensions/ruby_core/assess/module.rb +0 -14
  274. data/lib/contrast/extensions/ruby_core/assess/regexp.rb +0 -206
  275. data/lib/contrast/extensions/ruby_core/assess/tilt_template_trigger.rb +0 -73
  276. data/lib/contrast/extensions/ruby_core/assess/xpath_library_trigger.rb +0 -40
  277. data/lib/contrast/extensions/ruby_core/eval_trigger.rb +0 -52
  278. data/lib/contrast/extensions/ruby_core/inventory/datastores.rb +0 -37
  279. data/lib/contrast/extensions/ruby_core/protect/applies_command_injection_rule.rb +0 -72
  280. data/lib/contrast/extensions/ruby_core/protect/applies_deserialization_rule.rb +0 -60
  281. data/lib/contrast/extensions/ruby_core/protect/applies_no_sqli_rule.rb +0 -83
  282. data/lib/contrast/extensions/ruby_core/protect/applies_path_traversal_rule.rb +0 -123
  283. data/lib/contrast/extensions/ruby_core/protect/applies_sqli_rule.rb +0 -65
  284. data/lib/contrast/extensions/ruby_core/protect/applies_xxe_rule.rb +0 -143
  285. data/lib/contrast/extensions/ruby_core/protect/kernel.rb +0 -30
  286. data/lib/contrast/framework/rails_support.rb +0 -88
  287. data/lib/contrast/framework/sinatra_application_helper.rb +0 -49
  288. data/lib/contrast/framework/sinatra_support.rb +0 -94
  289. data/lib/contrast/utils/comment_range.rb +0 -19
  290. data/lib/contrast/utils/data_store_util.rb +0 -23
  291. data/lib/contrast/utils/environment_util.rb +0 -82
  292. data/lib/contrast/utils/performs_logging.rb +0 -152
  293. data/lib/contrast/utils/rack_assess_session_cookie.rb +0 -104
  294. data/lib/contrast/utils/rails_assess_configuration.rb +0 -95
  295. data/lib/contrast/utils/random_util.rb +0 -22
  296. data/resources/csrf/inject.js +0 -44
  297. data/resources/factory-bot-spec/spec_helper.rb +0 -30
  298. data/resources/rubocops/kernel/catch_cop.rb +0 -37
  299. data/resources/rubocops/kernel/require_cop.rb +0 -37
  300. data/resources/rubocops/kernel/require_relative_cop.rb +0 -33
  301. data/resources/rubocops/module/autoload_cop.rb +0 -37
  302. data/resources/rubocops/module/const_defined_cop.rb +0 -37
  303. data/resources/rubocops/module/const_get_cop.rb +0 -37
  304. data/resources/rubocops/module/const_set_cop.rb +0 -37
  305. data/resources/rubocops/module/constants_cop.rb +0 -37
  306. data/resources/rubocops/module/name_cop.rb +0 -37
  307. data/resources/rubocops/object/class_cop.rb +0 -37
  308. data/resources/rubocops/object/freeze_cop.rb +0 -37
  309. data/resources/rubocops/object/frozen_cop.rb +0 -37
  310. data/resources/rubocops/object/is_a_cop.rb +0 -37
  311. data/resources/rubocops/object/method_cop.rb +0 -37
  312. data/resources/rubocops/object/respond_to_cop.rb +0 -37
  313. data/resources/rubocops/object/singleton_class_cop.rb +0 -37
  314. data/resources/rubocops/regexp/spelling_cop.rb +0 -44
  315. data/resources/rubocops/thread/new_cop.rb +0 -39
  316. data/resources/ruby-spec/ancestors_spec.rb +0 -70
  317. data/resources/ruby-spec/modulo_spec.rb +0 -831
  318. data/resources/ruby-spec/parameters_spec.rb +0 -261
  319. data/resources/ruby-spec/ruby_spec_spec_helper.rb +0 -35
@@ -12,9 +12,6 @@ module Contrast
12
12
  # in a 'get it work' state. hopefully, we'll be in
13
13
  # a 'get it right' state soon.
14
14
  class Select
15
- include Contrast::Components::Interface
16
- access_component :logging
17
-
18
15
  class << self
19
16
  def select_tagger patcher, preshift, ret, _block
20
17
  source = preshift.object
@@ -49,32 +46,48 @@ module Contrast
49
46
 
50
47
  private
51
48
 
49
+ def handle_integer args, arg, source
50
+ length = args[1] || 1
51
+ # (void) negative range
52
+ arg += source.length if arg.negative?
53
+ Contrast::Agent::Assess::AdjustedSpan.new(arg, arg + length)
54
+ end
55
+
56
+ def handle_string arg, source
57
+ idx = source.index(arg)
58
+ Contrast::Agent::Assess::AdjustedSpan.new(idx, idx + arg.length)
59
+ end
60
+
61
+ def handle_regexp args, arg, source
62
+ match_data = arg.match(source)
63
+ # nil has the same meaning as 0. use full match
64
+ group = args[1] || 0
65
+ Contrast::Agent::Assess::AdjustedSpan.new(match_data.begin(group), match_data.end(group))
66
+ end
67
+
68
+ def handle_range arg, source
69
+ start = arg.begin
70
+ finish = arg.end
71
+
72
+ # (void) negative range
73
+ start += source.length if start.negative?
74
+ finish += source.length if finish.negative?
75
+ finish += 1 unless arg.exclude_end?
76
+
77
+ Contrast::Agent::Assess::AdjustedSpan.new(start, finish)
78
+ end
79
+
52
80
  def determine_select_range source, args
53
81
  arg = args[0]
54
82
  case arg
55
83
  when Integer
56
- length = args[1] || 1
57
- # (void) negative range
58
- arg += source.length if arg.negative?
59
- Contrast::Agent::Assess::AdjustedSpan.new(arg, arg + length)
84
+ handle_integer(args, arg, source)
60
85
  when String
61
- idx = source.index(arg)
62
- Contrast::Agent::Assess::AdjustedSpan.new(idx, idx + arg.length)
86
+ handle_string(arg, source)
63
87
  when Regexp
64
- match_data = arg.match(source)
65
- # nil has the same meaning as 0. use full match
66
- group = args[1] || 0
67
- Contrast::Agent::Assess::AdjustedSpan.new(match_data.begin(group), match_data.end(group))
88
+ handle_regexp(args, arg, source)
68
89
  when Range
69
- start = arg.begin
70
- finish = arg.end
71
-
72
- # (void) negative range
73
- start += source.length if start.negative?
74
- finish += source.length if finish.negative?
75
- finish += 1 unless arg.exclude_end?
76
-
77
- Contrast::Agent::Assess::AdjustedSpan.new(start, finish)
90
+ handle_range(arg, source)
78
91
  end
79
92
  end
80
93
  end
@@ -19,8 +19,25 @@ module Contrast
19
19
 
20
20
  SPLIT_TRACKER = Contrast::Utils::ThreadTracker.new
21
21
  class << self
22
+ # Propagate taint from a source as it is split into composite
23
+ # sections. This method MUST return nil, otherwise it risks
24
+ # changing the result of of the propagation.
25
+ #
26
+ # @param propagation_node [Contrast::Agent::Assess::Policy::PropagationNode]
27
+ # the node that governs this propagation event.
28
+ # @param preshift [Contrast::Agent::Assess::PreShift] The capture
29
+ # of the state of the code just prior to the invocation of the
30
+ # patched method.
31
+ # @param target [Array, String] the target to which to propagate.
32
+ # @return [nil]
22
33
  def propagate propagation_node, preshift, target
23
- return Contrast::Agent::Assess::Policy::Propagator::Keep.propagate(propagation_node, preshift, target) unless target.is_a?(Array)
34
+ logger.trace('Propagation detected',
35
+ node_id: propagation_node.id,
36
+ target_id: target.__id__)
37
+ unless target.is_a?(Array)
38
+ Contrast::Agent::Assess::Policy::Propagator::Keep.propagate(propagation_node, preshift, target)
39
+ return
40
+ end
24
41
 
25
42
  source = find_source(propagation_node.sources[0], preshift)
26
43
 
@@ -49,6 +66,7 @@ module Contrast
49
66
  elem.cs__properties.add_properties(propagation_node.properties)
50
67
  current_index = current_index + elem_length + separator_length
51
68
  end
69
+ nil
52
70
  end
53
71
 
54
72
  # Marks the point in which the String#split method is called.
@@ -66,7 +84,7 @@ module Contrast
66
84
  rescue Exception => e # rubocop:disable Lint/RescueException
67
85
  # don't let our errors propagate and disable String#split for
68
86
  # this since we're in an error state
69
- logger.warn(e, 'Unable to record split context')
87
+ logger.warn('Unable to record split context', e)
70
88
  end_split
71
89
  end
72
90
 
@@ -86,7 +104,7 @@ module Contrast
86
104
  SPLIT_TRACKER.set(:split_depth, depth)
87
105
  end
88
106
  rescue StandardError => e
89
- logger.warn(e, 'Unable to remove split context')
107
+ logger.warn('Unable to remove split context', e)
90
108
  end
91
109
 
92
110
  # This method is called whenever an rb_yield is called. We need
@@ -96,6 +114,8 @@ module Contrast
96
114
  # @param target [String] the entity being passed to the yield
97
115
  # block
98
116
  def propagate_yield target
117
+ depth, index = nil
118
+
99
119
  depth = SPLIT_TRACKER.get(:split_depth)
100
120
  return unless depth
101
121
 
@@ -108,9 +128,9 @@ module Contrast
108
128
  true_source = source[index]
109
129
  target.cs__copy_from(true_source)
110
130
  rescue StandardError => e
111
- logger.warn(e, 'Unable to track within split context')
131
+ logger.warn('Unable to track within split context', e)
112
132
  ensure
113
- if defined?(depth) && defined?(index) && depth && index
133
+ if depth && index
114
134
  idx = SPLIT_TRACKER.get(:split_index)
115
135
  idx[depth] = index + 1 if defined?(idx) && idx.is_a?(Array)
116
136
  end
@@ -122,7 +142,7 @@ module Contrast
122
142
  cs__scoped_require 'cs__assess_yield_track/cs__assess_yield_track' if AGENT.patch_yield?
123
143
  true
124
144
  rescue StandardError => e
125
- logger.error(e, 'Error loading split rb_yield patch')
145
+ logger.error('Error loading split rb_yield patch', e)
126
146
  false
127
147
  end
128
148
  end
@@ -1,6 +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/components/interface'
5
+
4
6
  module Contrast
5
7
  module Agent
6
8
  module Assess
@@ -14,43 +14,26 @@ module Contrast
14
14
  # This is our interface from the Patcher to the Rewriter
15
15
  # functionality
16
16
  #
17
- # TODO: RUBY-534 remove w/ EOL of 2.5
17
+ # TODO: RUBY-714 remove w/ EOL of 2.5
18
+ # @deprecated Changes to this class are discouraged as this approach is
19
+ # being phased out with support for those language versions.
18
20
  module RewriterPatch
19
21
  include Contrast::Components::Interface
20
22
  access_component :agent, :analysis, :logging
21
23
 
22
24
  class << self
23
25
  def rewrite_interpolations
24
- return unless ASSESS.enabled?
25
- return unless AGENT.rewrite_interpolation?
26
+ return unless agent_should_rewrite?
26
27
 
27
- logger.debug_with_time("\t\tRunning Assess interpolation rewrite") do
28
+ logger.debug_with_time('Running Assess interpolation rewrite') do
28
29
  ObjectSpace.each_object(Module) do |mod|
29
30
  rewrite_interpolation(mod)
30
31
  end
31
32
  end
32
33
  end
33
34
 
34
- # Rails is being a jerk, again. It passes in a class before it is
35
- # done being defined. There is a state where the files have been
36
- # loaded, but the class definition is not complete, so the
37
- # methods of the class are not defined despite the class existing
38
- #
39
- # To get around this, we have those methods tell us the class
40
- # isn't ready
41
- def mid_defining? mod
42
- mod.instance_variable_defined?(:@cs__defining_class) &&
43
- mod.instance_variable_get(:@cs__defining_class)
44
- end
45
-
46
35
  def rewrite_interpolation mod, redo_rewrite = false
47
- return unless ASSESS.enabled?
48
- return unless AGENT.rewrite_interpolation?
49
- return unless AGENT.interpolation_enabled?
50
- return if AGENT.skip_instrumentation? mod.cs__name
51
- return if mod.cs__frozen?
52
- return if mod.singleton_class?
53
- return if mid_defining?(mod)
36
+ return unless should_rewrite?(mod)
54
37
 
55
38
  status = Contrast::Agent::Patching::Policy::PatchStatus.get_status(mod)
56
39
  return if (status&.rewritten? || status&.rewriting?) && !redo_rewrite
@@ -64,13 +47,43 @@ module Contrast
64
47
 
65
48
  end
66
49
  module_data = Contrast::Agent::ModuleData.new(mod, module_name)
67
- logger.debug_with_time("\t\t\tRewriting #{ module_name }") do
50
+ logger.trace_with_time('Rewriting module', module: module_name) do
68
51
  Contrast::Agent::Rewriter.rewrite_class(module_data, redo_rewrite)
69
52
  end
70
53
  rescue StandardError => e
71
- logger.error(
72
- e,
73
- "Unable to patch assess into the module #{ mod }")
54
+ logger.error('Unable to patch for assess', e, module: mod)
55
+ end
56
+
57
+ private
58
+
59
+ # Rails is being a jerk, again. It passes in a class before it is
60
+ # done being defined. There is a state where the files have been
61
+ # loaded, but the class definition is not complete, so the
62
+ # methods of the class are not defined despite the class existing
63
+ #
64
+ # To get around this, we have those methods tell us the class
65
+ # isn't ready
66
+ def mid_defining? mod
67
+ mod.instance_variable_defined?(:@cs__defining_class) &&
68
+ mod.instance_variable_get(:@cs__defining_class)
69
+ end
70
+
71
+ def agent_should_rewrite?
72
+ return false unless ASSESS.enabled?
73
+ return false unless AGENT.rewrite_interpolation?
74
+ return false unless AGENT.interpolation_enabled?
75
+
76
+ true
77
+ end
78
+
79
+ def should_rewrite? mod
80
+ return false unless agent_should_rewrite?
81
+ return false if AGENT.skip_instrumentation? mod.cs__name
82
+ return false if mod.cs__frozen?
83
+ return false if mod.singleton_class?
84
+ return false if mid_defining?(mod)
85
+
86
+ true
74
87
  end
75
88
  end
76
89
  end
@@ -82,8 +82,7 @@ module Contrast
82
82
  target = ret
83
83
  end
84
84
 
85
- invoked = 3 # apply_post_patch => apply_assess => source_patchers
86
- apply_source(current_context, source_node, target, object, ret, source_node.type, nil, invoked, *args)
85
+ apply_source(current_context, source_node, target, object, ret, source_node.type, nil, *args)
87
86
 
88
87
  ret.cs__freeze if restore_frozen_state
89
88
  ret
@@ -106,37 +105,31 @@ module Contrast
106
105
  # @param source_name [String, nil] the name of this source, i.e.
107
106
  # the key used to accessed if from a map or nil if a type like
108
107
  # BODY
109
- # @param invoked [Integer] the depth of this invocation from
110
- # application code; often a lie.
111
108
  # @param args [Array<Object>] the Arguments with which the method
112
109
  # was invoked
113
- def apply_source context, source_node, target, object, ret, source_type, source_name = nil, invoked = 0, *args
110
+ def apply_source context, source_node, target, object, ret, source_type, source_name = nil, *args
114
111
  return unless context && source_node && target
115
112
 
116
113
  source_name ||= determine_source_name(source_node, object, ret, *args)
117
114
  # We know we only work on certain things.
118
115
  # Skip if this isn't one of them
119
116
  if Contrast::Utils::DuckUtils.quacks_to?(target, :cs__properties)
120
- apply_tags(source_node, target, object, ret, source_type, source_name, invoked, *args)
117
+ apply_tags(source_node, target, object, ret, source_type, source_name, *args)
121
118
  # While we don't taint hashes themselves, we may taint the things
122
119
  # they hold. Let's pass their keys and values back to ourselves and
123
120
  # try again
124
121
  elsif Contrast::Utils::DuckUtils.iterable_hash?(target)
125
- source_key_type = invoked.zero? ? key_type(source_type) : source_type
126
- invoked += 1
127
122
  to_replace = []
128
123
  target.each_pair do |key, value|
129
124
  # We only do this for Strings b/c of the way Hash lookup works.
130
125
  # To replace another object would break hash lookup and,
131
126
  # therefore, the application
132
- if ASSESS.track_frozen_sources? &&
133
- key.is_a?(String) &&
134
- Contrast::Utils::DuckUtils.quacks_to?(target, :delete)
127
+ if can_track_hash_key?(key, target)
135
128
  key = Contrast::Utils::FreezeUtil.unfreeze_dup(key)
136
129
  to_replace << key
137
130
  end
138
- apply_source(context, source_node, key, object, ret, source_key_type, key, invoked, *args)
139
- apply_source(context, source_node, value, object, ret, source_type, key, invoked, *args)
131
+ apply_source(context, source_node, key, object, ret, key_type(source_type), key, *args)
132
+ apply_source(context, source_node, value, object, ret, source_type, key, *args)
140
133
  end
141
134
 
142
135
  # Hash is designed to keep one instance of the string key in it.
@@ -152,19 +145,23 @@ module Contrast
152
145
  # they hold. Let's pass their keys and values back to ourselves and
153
146
  # try again
154
147
  elsif Contrast::Utils::DuckUtils.iterable_enumerable?(target)
155
- invoked += 1
156
- target.each { |value| apply_source(context, source_node, value, object, ret, source_type, source_name, invoked, *args) }
148
+ target.each { |value| apply_source(context, source_node, value, object, ret, source_type, source_name, *args) }
157
149
  end
158
150
  rescue StandardError => e
159
- logger.warn(e, "Unable to apply source for source_node #{ source_node.id }")
151
+ logger.warn('Unable to apply source', e, node_id: source_node.id)
160
152
  end
161
153
 
162
- def apply_tags source_node, target, object, ret, source_type, source_name, invoked, *args
154
+ def can_track_hash_key? key, target
155
+ ASSESS.track_frozen_sources? &&
156
+ key.is_a?(String) &&
157
+ Contrast::Utils::DuckUtils.quacks_to?(target, :delete)
158
+ end
159
+
160
+ def apply_tags source_node, target, object, ret, source_type, source_name, *args
163
161
  # don't apply second source -- probably needs tuning later if we
164
162
  # use more than 'UNTRUSTED' in our sources
165
163
  return if target.cs__tracked? || target.cs__frozen?
166
164
 
167
- invoked += 1
168
165
  # otherwise for each tag this source_node applies, create a tag range
169
166
  # on the target object
170
167
  # I realize this looping is counter-intuitive from the above
@@ -175,11 +172,14 @@ module Contrast
175
172
  length = Contrast::Utils::StringUtils.ret_length(target)
176
173
  target.cs__properties.add_tag(tag, Contrast::Agent::Assess::AdjustedSpan.new(0, length))
177
174
  target.cs__properties.add_properties(source_node.properties)
178
- logger.debug(nil, "Source #{ source_node.id } detected: #{ target.__id__ } tagged with #{ tag }")
175
+ logger.trace('Source detected',
176
+ node_id: source_node.id,
177
+ target_id: target.__id__,
178
+ tag: tag)
179
179
  end
180
180
 
181
181
  # make a representation of this method that TeamServer can render
182
- target.cs__properties.build_event(source_node, target, object, ret, args, invoked, source_type, source_name)
182
+ target.cs__properties.build_event(source_node, target, object, ret, args, source_type, source_name)
183
183
  end
184
184
 
185
185
  # Find the name of the source
@@ -13,22 +13,7 @@ module Contrast
13
13
  class SourceNode < PolicyNode
14
14
  attr_accessor :type
15
15
 
16
- DB_SOURCE_TYPE = 'TAINTED_DATABASE'
17
- def self.build_dynamic_source _id, dynamic_source
18
- dynamic_source_hash = {
19
- JSON_CLASS_NAME => dynamic_source.class_name,
20
- JSON_METHOD_NAME => dynamic_source.method_name,
21
- JSON_INSTANCE_METHOD => dynamic_source.instance_method,
22
- JSON_TYPE => DB_SOURCE_TYPE,
23
- JSON_METHOD_VISIBILITY => 'public',
24
- JSON_TARGET => dynamic_source.target,
25
- JSON_PROPERTIES => dynamic_source.properties
26
- }
27
- Contrast::Agent::Assess::Policy::SourceNode.new(dynamic_source_hash)
28
- end
29
-
30
16
  JSON_TYPE = 'type'
31
- JSON_SOURCE_NAME = 'source_name'
32
17
  SOURCE_TAG = 'UNTRUSTED'
33
18
  def initialize source_hash = {}
34
19
  super(source_hash)
@@ -0,0 +1,90 @@
1
+ # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ module Contrast
5
+ module Agent
6
+ module Assess
7
+ module Policy
8
+ module Trigger
9
+ # This acts a trigger to handle the special cases of the Tilt
10
+ # library gem. Reflected XSS data may come into the trigger methods
11
+ # from these classes.
12
+ class ReflectedXss
13
+ class << self
14
+ NODE_HASH = {
15
+ 'class_name' => 'Tilt::Template',
16
+ 'instance_method' => true,
17
+ 'method_name' => 'render',
18
+ 'method_visibility' => 'public',
19
+ 'action' => 'CUSTOM',
20
+ 'source' => 'O,P0',
21
+ 'target' => 'R',
22
+ 'patch_class' => 'Contrast::Agent::Assess::Policy::Trigger::ReflectedXss',
23
+ 'patch_method' => 'xss_tilt_trigger'
24
+ }.cs__freeze
25
+ TEMPLATE_PROPAGATION_NODE = Contrast::Agent::Assess::Policy::PropagationNode.new(NODE_HASH)
26
+
27
+ def xss_tilt_trigger context, trigger_node, _source, object, ret, *args
28
+ scope = args[0]
29
+
30
+ erb_template_prerender = object.instance_variable_get(:@data)
31
+ interpolated_inputs = []
32
+ handle_binding_variables(scope, erb_template_prerender, ret, interpolated_inputs)
33
+
34
+ handle_local_variables(args, erb_template_prerender, ret, interpolated_inputs)
35
+
36
+ unless interpolated_inputs.empty?
37
+ interpolated_inputs.each do |input|
38
+ input.cs__properties.events.each do |event|
39
+ ret.cs__properties.events << event
40
+ end
41
+ end
42
+ ret.cs__properties.build_event(TEMPLATE_PROPAGATION_NODE, ret, erb_template_prerender, ret, interpolated_inputs)
43
+ end
44
+
45
+ if ret.cs__tracked?
46
+ Contrast::Agent::Assess::Policy::TriggerMethod.build_finding(context, trigger_node, ret, erb_template_prerender, ret, interpolated_inputs)
47
+ end
48
+
49
+ ret
50
+ end
51
+
52
+ private
53
+
54
+ def handle_binding_variables scope, erb_template_prerender, ret, interpolated_inputs
55
+ binding_variables = scope.instance_variables
56
+
57
+ binding_variables.each do |bound_variable_sym|
58
+ bound_variable_value = scope.instance_variable_get(bound_variable_sym)
59
+
60
+ next unless bound_variable_value.cs__respond_to?(:cs__tracked?) && bound_variable_value.cs__tracked?
61
+ next unless erb_template_prerender.include?(bound_variable_sym.to_s)
62
+
63
+ start_index = ret.index(bound_variable_value)
64
+ next if start_index.nil?
65
+
66
+ ret.cs__copy_from(bound_variable_value, start_index)
67
+ interpolated_inputs << bound_variable_sym
68
+ end
69
+ end
70
+
71
+ def handle_local_variables args, erb_template_prerender, ret, interpolated_inputs
72
+ locals = args[1]
73
+ locals.each do |local_name, local_value|
74
+ next unless local_value.cs__respond_to?(:cs__tracked?) && local_value.cs__tracked?
75
+ next unless erb_template_prerender.include?(local_name.to_s)
76
+
77
+ start_index = ret.index(local_value)
78
+ next if start_index.nil?
79
+
80
+ ret.cs__copy_from(local_value, start_index)
81
+ interpolated_inputs << local_name
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end