contrast-agent 4.3.1 → 4.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (315) hide show
  1. checksums.yaml +4 -4
  2. data/.simplecov +1 -1
  3. data/Gemfile +1 -1
  4. data/LICENSE.txt +1 -1
  5. data/Rakefile +1 -1
  6. data/exe/contrast_service +1 -1
  7. data/ext/build_funchook.rb +1 -1
  8. data/ext/cs__assess_active_record_named/cs__active_record_named.c +1 -1
  9. data/ext/cs__assess_active_record_named/extconf.rb +1 -1
  10. data/ext/cs__assess_array/cs__assess_array.c +1 -1
  11. data/ext/cs__assess_array/extconf.rb +1 -1
  12. data/ext/cs__assess_basic_object/cs__assess_basic_object.c +1 -1
  13. data/ext/cs__assess_basic_object/extconf.rb +1 -1
  14. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.c +1 -1
  15. data/ext/cs__assess_fiber_track/extconf.rb +1 -1
  16. data/ext/cs__assess_hash/cs__assess_hash.c +4 -2
  17. data/ext/cs__assess_hash/extconf.rb +1 -1
  18. data/ext/cs__assess_kernel/cs__assess_kernel.c +1 -1
  19. data/ext/cs__assess_kernel/extconf.rb +1 -1
  20. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +1 -1
  21. data/ext/cs__assess_marshal_module/extconf.rb +1 -1
  22. data/ext/cs__assess_module/cs__assess_module.c +1 -1
  23. data/ext/cs__assess_module/extconf.rb +1 -1
  24. data/ext/cs__assess_regexp/cs__assess_regexp.c +1 -1
  25. data/ext/cs__assess_regexp/extconf.rb +1 -1
  26. data/ext/cs__assess_string/cs__assess_string.c +1 -1
  27. data/ext/cs__assess_string/extconf.rb +1 -1
  28. data/ext/cs__assess_string_interpolation26/cs__assess_string_interpolation26.c +1 -1
  29. data/ext/cs__assess_string_interpolation26/extconf.rb +1 -1
  30. data/ext/cs__assess_yield_track/cs__assess_yield_track.c +1 -1
  31. data/ext/cs__assess_yield_track/extconf.rb +1 -1
  32. data/ext/cs__common/cs__common.c +5 -5
  33. data/ext/cs__common/cs__common.h +4 -4
  34. data/ext/cs__common/extconf.rb +1 -1
  35. data/ext/cs__contrast_patch/cs__contrast_patch.c +22 -25
  36. data/ext/cs__contrast_patch/extconf.rb +1 -1
  37. data/ext/cs__protect_kernel/cs__protect_kernel.c +1 -1
  38. data/ext/cs__protect_kernel/extconf.rb +1 -1
  39. data/ext/extconf_common.rb +1 -1
  40. data/lib/contrast-agent.rb +1 -1
  41. data/lib/contrast.rb +20 -1
  42. data/lib/contrast/agent.rb +6 -4
  43. data/lib/contrast/agent/assess.rb +1 -10
  44. data/lib/contrast/agent/assess/contrast_event.rb +54 -71
  45. data/lib/contrast/agent/assess/contrast_object.rb +6 -3
  46. data/lib/contrast/agent/assess/events/event_factory.rb +1 -1
  47. data/lib/contrast/agent/assess/events/source_event.rb +7 -2
  48. data/lib/contrast/agent/assess/finalizers/freeze.rb +1 -1
  49. data/lib/contrast/agent/assess/finalizers/hash.rb +33 -34
  50. data/lib/contrast/agent/assess/policy/dynamic_source_factory.rb +30 -15
  51. data/lib/contrast/agent/assess/policy/patcher.rb +1 -1
  52. data/lib/contrast/agent/assess/policy/policy.rb +1 -1
  53. data/lib/contrast/agent/assess/policy/policy_node.rb +1 -1
  54. data/lib/contrast/agent/assess/policy/policy_scanner.rb +1 -1
  55. data/lib/contrast/agent/assess/policy/preshift.rb +1 -1
  56. data/lib/contrast/agent/assess/policy/propagation_method.rb +30 -19
  57. data/lib/contrast/agent/assess/policy/propagation_node.rb +1 -1
  58. data/lib/contrast/agent/assess/policy/propagator.rb +1 -1
  59. data/lib/contrast/agent/assess/policy/propagator/append.rb +29 -14
  60. data/lib/contrast/agent/assess/policy/propagator/base.rb +1 -1
  61. data/lib/contrast/agent/assess/policy/propagator/center.rb +1 -1
  62. data/lib/contrast/agent/assess/policy/propagator/custom.rb +1 -1
  63. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +22 -17
  64. data/lib/contrast/agent/assess/policy/propagator/insert.rb +1 -1
  65. data/lib/contrast/agent/assess/policy/propagator/keep.rb +1 -1
  66. data/lib/contrast/agent/assess/policy/propagator/match_data.rb +1 -1
  67. data/lib/contrast/agent/assess/policy/propagator/next.rb +1 -1
  68. data/lib/contrast/agent/assess/policy/propagator/prepend.rb +1 -1
  69. data/lib/contrast/agent/assess/policy/propagator/remove.rb +23 -19
  70. data/lib/contrast/agent/assess/policy/propagator/replace.rb +1 -1
  71. data/lib/contrast/agent/assess/policy/propagator/reverse.rb +1 -1
  72. data/lib/contrast/agent/assess/policy/propagator/select.rb +1 -1
  73. data/lib/contrast/agent/assess/policy/propagator/splat.rb +24 -14
  74. data/lib/contrast/agent/assess/policy/propagator/split.rb +15 -8
  75. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +31 -15
  76. data/lib/contrast/agent/assess/policy/propagator/trim.rb +66 -35
  77. data/lib/contrast/agent/assess/policy/rewriter_patch.rb +1 -1
  78. data/lib/contrast/agent/assess/policy/source_method.rb +87 -76
  79. data/lib/contrast/agent/assess/policy/source_node.rb +1 -1
  80. data/lib/contrast/agent/assess/policy/source_validation/cross_site_validator.rb +8 -6
  81. data/lib/contrast/agent/assess/policy/source_validation/source_validation.rb +1 -1
  82. data/lib/contrast/agent/assess/policy/trigger/reflected_xss.rb +3 -3
  83. data/lib/contrast/agent/assess/policy/trigger/xpath.rb +7 -8
  84. data/lib/contrast/agent/assess/policy/trigger_method.rb +106 -64
  85. data/lib/contrast/agent/assess/policy/trigger_node.rb +31 -10
  86. data/lib/contrast/agent/assess/policy/trigger_validation/redos_validator.rb +59 -0
  87. data/lib/contrast/agent/assess/policy/trigger_validation/ssrf_validator.rb +2 -3
  88. data/lib/contrast/agent/assess/policy/trigger_validation/trigger_validation.rb +7 -5
  89. data/lib/contrast/agent/assess/policy/trigger_validation/xss_validator.rb +3 -5
  90. data/lib/contrast/agent/assess/properties.rb +1 -3
  91. data/lib/contrast/agent/assess/property/evented.rb +1 -1
  92. data/lib/contrast/agent/assess/property/tagged.rb +38 -20
  93. data/lib/contrast/agent/assess/property/updated.rb +1 -1
  94. data/lib/contrast/agent/assess/rule/provider.rb +1 -1
  95. data/lib/contrast/agent/assess/rule/provider/hardcoded_key.rb +1 -1
  96. data/lib/contrast/agent/assess/rule/provider/hardcoded_password.rb +1 -1
  97. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +1 -1
  98. data/lib/contrast/agent/assess/tag.rb +1 -1
  99. data/lib/contrast/agent/assess/tracker.rb +2 -2
  100. data/lib/contrast/agent/at_exit_hook.rb +1 -1
  101. data/lib/contrast/agent/class_reopener.rb +1 -1
  102. data/lib/contrast/agent/deadzone/policy/deadzone_node.rb +1 -1
  103. data/lib/contrast/agent/deadzone/policy/policy.rb +7 -3
  104. data/lib/contrast/agent/disable_reaction.rb +1 -1
  105. data/lib/contrast/agent/exclusion_matcher.rb +1 -1
  106. data/lib/contrast/agent/inventory.rb +1 -2
  107. data/lib/contrast/agent/inventory/dependencies.rb +1 -1
  108. data/lib/contrast/agent/inventory/dependency_analysis.rb +1 -1
  109. data/lib/contrast/agent/inventory/dependency_usage_analysis.rb +35 -23
  110. data/lib/contrast/agent/inventory/policy/datastores.rb +1 -1
  111. data/lib/contrast/agent/inventory/policy/policy.rb +1 -1
  112. data/lib/contrast/agent/inventory/policy/trigger_node.rb +1 -1
  113. data/lib/contrast/agent/middleware.rb +112 -109
  114. data/lib/contrast/agent/module_data.rb +1 -1
  115. data/lib/contrast/agent/patching/policy/after_load_patch.rb +1 -1
  116. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +5 -1
  117. data/lib/contrast/agent/patching/policy/method_policy.rb +1 -1
  118. data/lib/contrast/agent/patching/policy/module_policy.rb +1 -1
  119. data/lib/contrast/agent/patching/policy/patch.rb +12 -13
  120. data/lib/contrast/agent/patching/policy/patch_status.rb +2 -2
  121. data/lib/contrast/agent/patching/policy/patcher.rb +59 -54
  122. data/lib/contrast/agent/patching/policy/policy.rb +14 -21
  123. data/lib/contrast/agent/patching/policy/policy_node.rb +1 -1
  124. data/lib/contrast/agent/patching/policy/trigger_node.rb +6 -3
  125. data/lib/contrast/agent/protect/policy/applies_command_injection_rule.rb +1 -1
  126. data/lib/contrast/agent/protect/policy/applies_deserialization_rule.rb +1 -1
  127. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +1 -1
  128. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +1 -1
  129. data/lib/contrast/agent/protect/policy/applies_sqli_rule.rb +1 -1
  130. data/lib/contrast/agent/protect/policy/applies_xxe_rule.rb +1 -1
  131. data/lib/contrast/agent/protect/policy/policy.rb +1 -1
  132. data/lib/contrast/agent/protect/policy/rule_applicator.rb +1 -1
  133. data/lib/contrast/agent/protect/policy/trigger_node.rb +1 -1
  134. data/lib/contrast/agent/protect/rule.rb +1 -1
  135. data/lib/contrast/agent/protect/rule/base.rb +7 -18
  136. data/lib/contrast/agent/protect/rule/base_service.rb +2 -2
  137. data/lib/contrast/agent/protect/rule/cmd_injection.rb +2 -2
  138. data/lib/contrast/agent/protect/rule/default_scanner.rb +1 -1
  139. data/lib/contrast/agent/protect/rule/deserialization.rb +1 -1
  140. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +1 -1
  141. data/lib/contrast/agent/protect/rule/no_sqli.rb +2 -2
  142. data/lib/contrast/agent/protect/rule/no_sqli/mongo_no_sql_scanner.rb +1 -1
  143. data/lib/contrast/agent/protect/rule/path_traversal.rb +1 -1
  144. data/lib/contrast/agent/protect/rule/sqli.rb +18 -12
  145. data/lib/contrast/agent/protect/rule/sqli/default_sql_scanner.rb +1 -1
  146. data/lib/contrast/agent/protect/rule/sqli/mysql_sql_scanner.rb +1 -1
  147. data/lib/contrast/agent/protect/rule/sqli/postgres_sql_scanner.rb +2 -2
  148. data/lib/contrast/agent/protect/rule/sqli/sqlite_sql_scanner.rb +1 -1
  149. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +1 -1
  150. data/lib/contrast/agent/protect/rule/xss.rb +1 -1
  151. data/lib/contrast/agent/protect/rule/xxe.rb +1 -1
  152. data/lib/contrast/agent/protect/rule/xxe/entity_wrapper.rb +1 -1
  153. data/lib/contrast/agent/railtie.rb +1 -1
  154. data/lib/contrast/agent/reaction_processor.rb +1 -1
  155. data/lib/contrast/agent/request.rb +2 -1
  156. data/lib/contrast/agent/request_context.rb +17 -10
  157. data/lib/contrast/agent/request_handler.rb +1 -1
  158. data/lib/contrast/agent/response.rb +1 -1
  159. data/lib/contrast/agent/rewriter.rb +1 -1
  160. data/lib/contrast/agent/rule_set.rb +3 -3
  161. data/lib/contrast/agent/scope.rb +1 -1
  162. data/lib/contrast/agent/service_heartbeat.rb +1 -1
  163. data/lib/contrast/agent/static_analysis.rb +1 -1
  164. data/lib/contrast/agent/thread.rb +2 -2
  165. data/lib/contrast/agent/thread_watcher.rb +21 -6
  166. data/lib/contrast/agent/tracepoint_hook.rb +1 -1
  167. data/lib/contrast/agent/version.rb +2 -2
  168. data/lib/contrast/agent/worker_thread.rb +1 -1
  169. data/lib/contrast/api.rb +1 -1
  170. data/lib/contrast/api/communication.rb +1 -1
  171. data/lib/contrast/api/communication/connection_status.rb +1 -1
  172. data/lib/contrast/api/communication/messaging_queue.rb +19 -22
  173. data/lib/contrast/api/communication/response_processor.rb +11 -4
  174. data/lib/contrast/api/communication/service_lifecycle.rb +1 -1
  175. data/lib/contrast/api/communication/socket.rb +1 -1
  176. data/lib/contrast/api/communication/socket_client.rb +23 -15
  177. data/lib/contrast/api/communication/speedracer.rb +3 -3
  178. data/lib/contrast/api/communication/tcp_socket.rb +1 -1
  179. data/lib/contrast/api/communication/unix_socket.rb +1 -1
  180. data/lib/contrast/api/decorators.rb +3 -1
  181. data/lib/contrast/api/decorators/address.rb +1 -1
  182. data/lib/contrast/api/decorators/agent_startup.rb +58 -0
  183. data/lib/contrast/api/decorators/application_settings.rb +1 -1
  184. data/lib/contrast/api/decorators/application_startup.rb +53 -0
  185. data/lib/contrast/api/decorators/application_update.rb +1 -1
  186. data/lib/contrast/api/decorators/http_request.rb +1 -1
  187. data/lib/contrast/api/decorators/input_analysis.rb +1 -1
  188. data/lib/contrast/api/decorators/instrumentation_mode.rb +37 -0
  189. data/lib/contrast/api/decorators/library.rb +1 -1
  190. data/lib/contrast/api/decorators/library_usage_update.rb +1 -1
  191. data/lib/contrast/api/decorators/message.rb +1 -1
  192. data/lib/contrast/api/decorators/rasp_rule_sample.rb +1 -1
  193. data/lib/contrast/api/decorators/route_coverage.rb +16 -6
  194. data/lib/contrast/api/decorators/server_features.rb +1 -1
  195. data/lib/contrast/api/decorators/trace_event.rb +43 -15
  196. data/lib/contrast/api/decorators/trace_event_object.rb +1 -1
  197. data/lib/contrast/api/decorators/trace_event_signature.rb +1 -1
  198. data/lib/contrast/api/decorators/trace_taint_range.rb +1 -1
  199. data/lib/contrast/api/decorators/trace_taint_range_tags.rb +1 -1
  200. data/lib/contrast/api/decorators/user_input.rb +1 -1
  201. data/lib/contrast/components/agent.rb +7 -11
  202. data/lib/contrast/components/app_context.rb +5 -23
  203. data/lib/contrast/components/assess.rb +6 -11
  204. data/lib/contrast/components/config.rb +1 -1
  205. data/lib/contrast/components/contrast_service.rb +1 -1
  206. data/lib/contrast/components/heap_dump.rb +1 -1
  207. data/lib/contrast/components/interface.rb +1 -1
  208. data/lib/contrast/components/inventory.rb +1 -1
  209. data/lib/contrast/components/logger.rb +1 -1
  210. data/lib/contrast/components/protect.rb +11 -14
  211. data/lib/contrast/components/sampling.rb +49 -7
  212. data/lib/contrast/components/scope.rb +2 -1
  213. data/lib/contrast/components/settings.rb +27 -99
  214. data/lib/contrast/config.rb +1 -1
  215. data/lib/contrast/config/agent_configuration.rb +1 -1
  216. data/lib/contrast/config/application_configuration.rb +1 -1
  217. data/lib/contrast/config/assess_configuration.rb +1 -1
  218. data/lib/contrast/config/assess_rules_configuration.rb +1 -1
  219. data/lib/contrast/config/base_configuration.rb +1 -1
  220. data/lib/contrast/config/default_value.rb +1 -1
  221. data/lib/contrast/config/exception_configuration.rb +1 -1
  222. data/lib/contrast/config/heap_dump_configuration.rb +1 -1
  223. data/lib/contrast/config/inventory_configuration.rb +1 -1
  224. data/lib/contrast/config/logger_configuration.rb +1 -1
  225. data/lib/contrast/config/protect_configuration.rb +1 -1
  226. data/lib/contrast/config/protect_rule_configuration.rb +23 -1
  227. data/lib/contrast/config/protect_rules_configuration.rb +1 -1
  228. data/lib/contrast/config/root_configuration.rb +1 -1
  229. data/lib/contrast/config/ruby_configuration.rb +1 -1
  230. data/lib/contrast/config/sampling_configuration.rb +1 -1
  231. data/lib/contrast/config/server_configuration.rb +1 -1
  232. data/lib/contrast/config/service_configuration.rb +1 -1
  233. data/lib/contrast/configuration.rb +1 -1
  234. data/lib/contrast/delegators/input_analysis.rb +12 -0
  235. data/lib/contrast/extension/assess.rb +1 -1
  236. data/lib/contrast/extension/assess/array.rb +1 -1
  237. data/lib/contrast/extension/assess/erb.rb +1 -1
  238. data/lib/contrast/extension/assess/eval_trigger.rb +1 -5
  239. data/lib/contrast/extension/assess/exec_trigger.rb +1 -5
  240. data/lib/contrast/extension/assess/fiber.rb +1 -1
  241. data/lib/contrast/extension/assess/hash.rb +1 -1
  242. data/lib/contrast/extension/assess/kernel.rb +1 -1
  243. data/lib/contrast/extension/assess/marshal.rb +1 -5
  244. data/lib/contrast/extension/assess/regexp.rb +1 -1
  245. data/lib/contrast/extension/assess/string.rb +1 -1
  246. data/lib/contrast/extension/delegator.rb +1 -1
  247. data/lib/contrast/extension/inventory.rb +1 -1
  248. data/lib/contrast/extension/kernel.rb +1 -1
  249. data/lib/contrast/extension/module.rb +1 -1
  250. data/lib/contrast/extension/protect.rb +1 -1
  251. data/lib/contrast/extension/protect/kernel.rb +1 -1
  252. data/lib/contrast/extension/protect/psych.rb +1 -1
  253. data/lib/contrast/extension/thread.rb +1 -1
  254. data/lib/contrast/framework/base_support.rb +1 -1
  255. data/lib/contrast/framework/manager.rb +13 -15
  256. data/lib/contrast/framework/platform_version.rb +1 -1
  257. data/lib/contrast/framework/rack/patch/session_cookie.rb +1 -1
  258. data/lib/contrast/framework/rack/patch/support.rb +1 -1
  259. data/lib/contrast/framework/rack/support.rb +1 -1
  260. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +1 -1
  261. data/lib/contrast/framework/rails/patch/assess_configuration.rb +1 -1
  262. data/lib/contrast/framework/rails/patch/rails_application_configuration.rb +4 -4
  263. data/lib/contrast/framework/rails/patch/support.rb +1 -1
  264. data/lib/contrast/framework/rails/rewrite/action_controller_railties_helper_inherited.rb +1 -1
  265. data/lib/contrast/framework/rails/rewrite/active_record_attribute_methods_read.rb +1 -1
  266. data/lib/contrast/framework/rails/rewrite/active_record_named.rb +1 -1
  267. data/lib/contrast/framework/rails/rewrite/active_record_time_zone_inherited.rb +1 -1
  268. data/lib/contrast/framework/rails/support.rb +43 -44
  269. data/lib/contrast/framework/sinatra/support.rb +101 -42
  270. data/lib/contrast/funchook/funchook.rb +1 -1
  271. data/lib/contrast/logger/application.rb +1 -1
  272. data/lib/contrast/logger/format.rb +1 -1
  273. data/lib/contrast/logger/log.rb +32 -16
  274. data/lib/contrast/logger/request.rb +1 -1
  275. data/lib/contrast/logger/time.rb +1 -1
  276. data/lib/contrast/security_exception.rb +1 -1
  277. data/lib/contrast/tasks/config.rb +1 -1
  278. data/lib/contrast/tasks/service.rb +1 -1
  279. data/lib/contrast/utils/assess/sampling_util.rb +1 -1
  280. data/lib/contrast/utils/assess/tracking_util.rb +1 -1
  281. data/lib/contrast/utils/class_util.rb +18 -4
  282. data/lib/contrast/utils/duck_utils.rb +1 -1
  283. data/lib/contrast/utils/env_configuration_item.rb +1 -1
  284. data/lib/contrast/utils/hash_digest.rb +14 -19
  285. data/lib/contrast/utils/heap_dump_util.rb +104 -88
  286. data/lib/contrast/utils/invalid_configuration_util.rb +22 -13
  287. data/lib/contrast/utils/inventory_util.rb +1 -1
  288. data/lib/contrast/utils/io_util.rb +1 -1
  289. data/lib/contrast/utils/job_servers_running.rb +1 -1
  290. data/lib/contrast/utils/object_share.rb +1 -1
  291. data/lib/contrast/utils/os.rb +1 -1
  292. data/lib/contrast/utils/preflight_util.rb +1 -1
  293. data/lib/contrast/utils/resource_loader.rb +1 -1
  294. data/lib/contrast/utils/ruby_ast_rewriter.rb +1 -1
  295. data/lib/contrast/utils/sha256_builder.rb +1 -1
  296. data/lib/contrast/utils/stack_trace_utils.rb +1 -1
  297. data/lib/contrast/utils/string_utils.rb +1 -1
  298. data/lib/contrast/utils/tag_util.rb +1 -1
  299. data/lib/contrast/utils/thread_tracker.rb +1 -1
  300. data/lib/contrast/utils/timer.rb +1 -1
  301. data/resources/assess/policy.json +8 -11
  302. data/resources/deadzone/policy.json +7 -17
  303. data/ruby-agent.gemspec +59 -19
  304. data/service_executables/VERSION +1 -1
  305. data/service_executables/linux/contrast-service +0 -0
  306. data/service_executables/mac/contrast-service +0 -0
  307. metadata +134 -137
  308. data/lib/contrast/agent/assess/rule.rb +0 -18
  309. data/lib/contrast/agent/assess/rule/base.rb +0 -52
  310. data/lib/contrast/agent/assess/rule/redos.rb +0 -67
  311. data/lib/contrast/agent/inventory/gemfile_digest_cache.rb +0 -38
  312. data/lib/contrast/common_agent_configuration.rb +0 -87
  313. data/lib/contrast/framework/sinatra/patch/base.rb +0 -83
  314. data/lib/contrast/framework/sinatra/patch/support.rb +0 -27
  315. data/lib/contrast/utils/prevent_serialization.rb +0 -52
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
1
+ # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'monitor'
@@ -52,7 +52,7 @@ module Contrast
52
52
  # startup to catchup on everything we didn't see get loaded
53
53
  def patch
54
54
  catchup_after_load_patches
55
- patch_methods
55
+ catchup_loaded_methods
56
56
  Contrast::Agent::Assess::Policy::RewriterPatch.rewrite_interpolations
57
57
  end
58
58
 
@@ -62,10 +62,11 @@ module Contrast
62
62
  # where only a subset of the Assess changes are needed.
63
63
  PATCH_MONITOR = Monitor.new
64
64
 
65
- def patch_methods
65
+ # Iterate over and patch those Modules and Methods which were loaded before the Agent was enabled.
66
+ def catchup_loaded_methods
66
67
  PATCH_MONITOR.synchronize do
67
68
  t = Contrast::Agent::Thread.new do
68
- synchronized_patch_methods
69
+ synchronized_catchup_loaded_methods
69
70
  end
70
71
  # aborting on exception makes exceptions propagate.
71
72
  t.abort_on_exception = true
@@ -100,7 +101,7 @@ module Contrast
100
101
  # @param mod [Module] the module in which the patch should be
101
102
  # placed.
102
103
  # @param methods [Array(Symbol)] all the instance or singleton
103
- # methods in this clazz.
104
+ # methods in this mod.
104
105
  # @param method_policy [Contrast::Agent::Patching::Policy::MethodPolicy]
105
106
  # the policy that applies to the given method_name.
106
107
  # @return [Boolean] if patched, either by this invocation or a
@@ -149,7 +150,7 @@ module Contrast
149
150
  # other functions, like rewriting or scanning. This method should
150
151
  # only be invoked by the patch_methods method above in order to
151
152
  # ensure it is wrapped in a synchronize call
152
- def synchronized_patch_methods
153
+ def synchronized_catchup_loaded_methods
153
154
  logger.trace_with_time('Running patching') do
154
155
  patched = []
155
156
  all_module_names.each do |patchable_name|
@@ -166,53 +167,37 @@ module Contrast
166
167
  end
167
168
  end
168
169
 
169
- # Given the patchers that apply to this class that may apply, patch
170
- # Contrast method calls into the methods for which we have rules.
170
+ # Given the patchers that apply to this class that may apply, patch Contrast method calls into the methods
171
+ # for which we have rules.
171
172
  #
172
- # @param module_data [Contrast::Agent::ModuleData] the module, and
173
- # its name, that's being patched into
174
- # @param redo_patch [Boolean] a trigger to force patching
175
- # regardless of the state of the
176
- # Contrast::Agent::Patching::Policy::PatchStatus status on the
177
- # Module
173
+ # @param module_data [Contrast::Agent::ModuleData] the module, and its name, that's being patched into
174
+ # @param redo_patch [Boolean] a trigger to force patching regardless of the state of the
175
+ # Contrast::Agent::Patching::Policy::PatchStatus status on the Module
178
176
  def patch_into_module module_data, redo_patch = false
179
177
  status = status_type.get_status(module_data.mod)
180
178
  return if (status&.patched? || status&.patching?) && !redo_patch
181
179
 
182
- # Begin patching our sources into the given clazz (or module)
183
- # Any patcher that has the name of the clazz will be evaluated for
184
- # patching.
185
- # Find all the patchers that apply to this class, sorted by type.
180
+ # Begin patching our sources into the given module. Any patcher that has the name of the module will be
181
+ # evaluated for patching. Find all the patchers that apply to this class, sorted by type.
186
182
  module_policy = Contrast::Agent::Patching::Policy::ModulePolicy.create_module_policy(module_data.name)
187
-
188
- clazz = module_data.mod
183
+ # If there's nothing to match, then set that status and exit
184
+ if module_policy.empty?
185
+ status.no_patch!
186
+ return
187
+ end
189
188
 
190
189
  status.patching!
191
- patched = false
192
-
193
- counts = 0
194
- # Monkey patch any methods in this class that have matching nodes in the policy
195
- unless module_policy.empty?
196
- instance_methods = all_instance_methods(clazz, true)
197
- singleton_methods = clazz.singleton_methods(false)
198
- counts += patch_into_methods(clazz, instance_methods, module_policy, true)
199
- counts += patch_into_methods(clazz, singleton_methods, module_policy, false)
200
- counts = module_policy.num_expected_patches if adjust_for_prepend(clazz)
201
- patched = true
202
- end
190
+ num_applied_patches = patch_into_instance_methods(module_data, module_policy)
191
+ num_applied_patches += patch_into_singleton_methods(module_data, module_policy)
192
+ if adjust_for_prepend(module_data) ||
193
+ module_policy.num_expected_patches == num_applied_patches
203
194
 
204
- if patched
205
- if module_policy.num_expected_patches == counts
206
- status.patched!
207
- else
208
- status.partial_patch!
209
- end
195
+ status.patched!
210
196
  else
211
- status.no_patch!
197
+ status.partial_patch!
212
198
  end
213
199
  rescue StandardError => e
214
- status ||= status_type.get_status(module_data.mod)
215
- status.failed_patch!
200
+ status&.failed_patch!
216
201
  logger.warn('Patching failed', e, module: module_data.name)
217
202
  ensure
218
203
  logger.trace('Patching complete',
@@ -249,18 +234,39 @@ module Contrast
249
234
  instance_methods
250
235
  end
251
236
 
237
+ # Patch into the Instance Methods, including private, of the given Module that match the ModulePolicy
238
+ # provided.
239
+ #
240
+ # @param module_data [Contrast::Agent::ModuleData] the module, and its name, that's being patched into
241
+ # @param module_policy [Contrast::Agent::Patching::Policy::ModulePolicy] All the patchers that apply to
242
+ # this module, sorted by type.
243
+ def patch_into_instance_methods module_data, module_policy
244
+ mod = module_data.mod
245
+ methods = all_instance_methods(mod, true)
246
+ patch_into_methods(mod, methods, module_policy, true)
247
+ end
248
+
249
+ # Patch into the Singleton Methods of the given Module that match the ModulePolicy provided.
250
+ #
251
+ # @param module_data [Contrast::Agent::ModuleData] the module, and its name, that's being patched into
252
+ # @param module_policy [Contrast::Agent::Patching::Policy::ModulePolicy] All the patchers that apply to
253
+ # this module, sorted by type.
254
+ def patch_into_singleton_methods module_data, module_policy
255
+ mod = module_data.mod
256
+ methods = mod.singleton_methods(false)
257
+ patch_into_methods(mod, methods, module_policy, false)
258
+ end
259
+
252
260
  # We've found the patchers that apply to this class (or module). Now we'll
253
261
  # filter on the given method.
254
262
  #
255
- # @param mod [Module] The module from which to retrieve instance
256
- # methods.
257
- # @param methods [Array<Symbol>] The names of all the methods in
258
- # in this module
259
- # @param module_policy [Contrast::Agent::Patching::Policy::ModulePolicy]
260
- # All the patchers that apply to this class, sorted by type.
261
- # @param is_instance_method [Boolean] Indicates if these methods
262
- # are the instance or singleton methods for this module.
263
- # @return [Integer] number of methods patched
263
+ # @param mod [Module] The module from which to retrieve instance methods.
264
+ # @param methods [Array<Symbol>] The names of all the methods in in this module
265
+ # @param module_policy [Contrast::Agent::Patching::Policy::ModulePolicy] All the patchers that apply to
266
+ # this class, sorted by type.
267
+ # @param is_instance_method [Boolean] Indicates if these methods are the instance or singleton methods for
268
+ # this module.
269
+ # @return [Integer] number of methods patched.
264
270
  def patch_into_methods mod, methods, module_policy, is_instance_method
265
271
  count = 0
266
272
  methods.each do |method|
@@ -279,11 +285,10 @@ module Contrast
279
285
  # it has to be reapplied.
280
286
  # TODO: RUBY-620 should remove the need for this
281
287
  #
282
- # @param mod[Module] the Module for which a prepend action needs to
283
- # be accounted
288
+ # @param module_data [Contrast::Agent::ModuleData] the module, and its name, that's being patched into
284
289
  # @return [Boolean] if an adjustment was made or not
285
- def adjust_for_prepend mod
286
- return false unless mod.cs__name == 'CGI::Util'
290
+ def adjust_for_prepend module_data
291
+ return false unless module_data.mod.cs__name == 'CGI::Util'
287
292
 
288
293
  CGI.include(CGI::Util)
289
294
  CGI.extend(CGI::Util)
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
1
+ # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'json'
@@ -13,8 +13,9 @@ module Contrast
13
13
  module Agent
14
14
  module Patching
15
15
  module Policy
16
- # This is just a holder for our policy. Takes the policy JSON and
17
- # converts it into hashes that we can access nicely
16
+ # This is just a holder for our policy. Takes the policy JSON and converts it into hashes that we can access
17
+ # nicely.
18
+ #
18
19
  # @abstract
19
20
  class Policy
20
21
  include Singleton
@@ -25,8 +26,8 @@ module Contrast
25
26
  raise(NoMethodError, 'specify policy_folder for patching')
26
27
  end
27
28
 
28
- # Indicates is this feature has been disabled by the configuration,
29
- # read at startup, and therefore can never be enabled.
29
+ # Indicates is this feature has been disabled by the configuration, read at startup, and therefore can never
30
+ # be enabled.
30
31
  def disabled_globally?
31
32
  raise(NoMethodError, 'specify disabled_globally? conditions for patching')
32
33
  end
@@ -58,17 +59,15 @@ module Contrast
58
59
  from_hash_string(json)
59
60
  end
60
61
 
61
- # Our policy for patching rules is a 'dope ass' JSON file. Rather than
62
- # hard code in a bunch of things to monkey patch, we let the JSON file
63
- # define the conditions in which modifications are applied.
64
- # This let's us be flexible and extensible.
62
+ # Our policy for patching rules is a 'dope ass' JSON file. Rather than hard code in a bunch of things to
63
+ # monkey patch, we let the JSON file define the conditions in which modifications are applied. This let's us
64
+ # be flexible and extensible.
65
65
  def from_hash_string string
66
- # The default behavior of the agent is to load the policy on startup,
67
- # as at this point we do not know in which mode we'll be run.
66
+ # The default behavior of the agent is to load the policy on startup, as at this point we do not know in
67
+ # which mode we'll be run.
68
68
  #
69
- # If the configuration file explicitly disables a feature, we know
70
- # that we will not ever be able to enable it, so in that case, we
71
- # can skip policy loading.
69
+ # If the configuration file explicitly disables a feature, we know that we will not ever be able to enable
70
+ # it, so in that case, we can skip policy loading.
72
71
  return if disabled_globally?
73
72
 
74
73
  policy_data = JSON.parse(string)
@@ -110,13 +109,7 @@ module Contrast
110
109
  end
111
110
 
112
111
  def module_names
113
- @_module_names ||= begin
114
- m = Set.new
115
- sources.each { |source| m << source.class_name }
116
- propagators.each { |propagator| m << propagator.class_name }
117
- triggers.each { |trigger| m << trigger.class_name }
118
- m
119
- end
112
+ @_module_names ||= Set.new([sources, propagators, triggers].flatten.map!(&:class_name))
120
113
  end
121
114
 
122
115
  def find_triggers_by_rule rule_id
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
1
+ # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/components/interface'
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
1
+ # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/extension/module'
@@ -46,6 +46,11 @@ module Contrast
46
46
  raise(ArgumentError,
47
47
  "#{ id } did not have a proper applicator method: #{ applicator } does not respond to #{ applicator_method }. Unable to create.")
48
48
  end
49
+ validate_properties
50
+ validate_rule
51
+ end
52
+
53
+ def validate_properties
49
54
  if (required_properties & optional_properties).any?
50
55
  raise(ArgumentError, "#{ rule_id } had overlapping elements between required and optional properties. Unable to create.")
51
56
  end
@@ -53,8 +58,6 @@ module Contrast
53
58
  raise(ArgumentError, "#{ id } had an unexpected property. Unable to create.")
54
59
  end
55
60
  raise(ArgumentError, "#{ id } did not have a required property. Unable to create.") if (required_properties - properties.keys).any?
56
-
57
- validate_rule
58
61
  end
59
62
 
60
63
  def validate_rule
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
1
+ # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/agent/protect/rule/cmd_injection'
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
1
+ # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/agent/protect/rule/deserialization'
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
1
+ # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/agent/protect/rule/no_sqli'
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
1
+ # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/agent/protect/rule/path_traversal'
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
1
+ # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/agent/protect/rule/sqli'
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
1
+ # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/agent/protect/rule/xxe'
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
1
+ # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/agent/patching/policy/policy'
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
1
+ # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/components/interface'
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
1
+ # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/agent/patching/policy/trigger_node'
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
1
+ # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Contrast
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
1
+ # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/components/interface'
@@ -36,9 +36,9 @@ module Contrast
36
36
 
37
37
  attr_reader :mode
38
38
 
39
- def initialize default_mode = Contrast::Api::Settings::ProtectionRule::Mode::NO_ACTION
39
+ def initialize
40
40
  PROTECT.rules[name] = self
41
- @mode = mode_from_settings || default_mode
41
+ @mode = mode_from_settings
42
42
  end
43
43
 
44
44
  # Should return the name as it is known to Teamserver; defaults to class
@@ -46,27 +46,16 @@ module Contrast
46
46
  cs__class.name
47
47
  end
48
48
 
49
- OFF = 'off'
50
-
51
49
  def enabled?
52
50
  # 1. it is not enabled because protect is not enabled
53
51
  return false unless AGENT.enabled?
54
52
  return false unless PROTECT.enabled?
55
53
 
56
- rule_configs = PROTECT.rule_config
57
- unless rule_configs.nil?
58
- # 2. it is not enabled because it is in the list of disabled protect rules
59
- disabled_rules = rule_configs.disabled_rules
60
- return false if disabled_rules&.include?(name)
61
-
62
- # 3. it is not enabled because it has been turned "off" explicitly
63
- rule_config = rule_configs.send(name)
64
-
65
- return rule_config.mode != OFF unless rule_config.mode.nil?
66
- end
54
+ # 2. it is not enabled because it is in the list of disabled protect rules
55
+ return false if PROTECT.rule_config&.disabled_rules&.include?(name)
67
56
 
68
- # 4. it is not enabled because it's mode is :NO_ACTION
69
- @mode != :NO_ACTION
57
+ # 3. it is enabled so long as its mode is not NO_ACTION
58
+ @mode != Contrast::Api::Settings::ProtectionRule::Mode::NO_ACTION
70
59
  end
71
60
 
72
61
  def excluded? exclusions
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
1
+ # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/agent/protect/rule/base'
@@ -32,7 +32,7 @@ module Contrast
32
32
  # streamed responses will break
33
33
  def postfilter context
34
34
  return unless enabled? && POSTFILTER_MODES.include?(mode)
35
- return if mode == :NO_ACTION || mode == :PERMIT
35
+ return if mode == Contrast::Api::Settings::ProtectionRule::Mode::NO_ACTION || mode == Contrast::Api::Settings::ProtectionRule::Mode::PERMIT
36
36
 
37
37
  result = find_postfilter_attacker(context, nil)
38
38
  return unless result&.samples&.any?
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
1
+ # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/agent/protect/rule/base_service'
@@ -47,7 +47,7 @@ module Contrast
47
47
  end
48
48
 
49
49
  def build_attack_with_match context, input_analysis_result, result, candidate_string, **kwargs
50
- return result if mode == :NO_ACTION || mode == :PERMIT
50
+ return result if mode == Contrast::Api::Settings::ProtectionRule::Mode::NO_ACTION || mode == Contrast::Api::Settings::ProtectionRule::Mode::PERMIT
51
51
 
52
52
  result ||= build_attack_result(context)
53
53
  update_successful_attack_response(context, input_analysis_result, result, candidate_string)
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
1
+ # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
4
  # The base class used to determine if a user input crosses a token boundary or
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
1
+ # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/agent/protect/rule/base'