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 '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/inventory/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
  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
  require 'ipaddr'
@@ -16,18 +16,17 @@ require 'contrast/utils/timer'
16
16
 
17
17
  module Contrast
18
18
  module Agent
19
- # This class allows the Agent to plug into the Rack middleware stack. When the
20
- # application is first started, we initialize ourselves as a rack middleware
21
- # inside of #initialize. Afterwards, we process each http request and response
22
- # as it goes through the middleware stack inside of #call.
19
+ # This class allows the Agent to plug into the Rack middleware stack. When the application is first started, we
20
+ # initialize ourselves as a rack middleware inside of #initialize. Afterwards, we process each http request and
21
+ # response as it goes through the middleware stack inside of #call.
23
22
  class Middleware
24
23
  include Contrast::Components::Interface
25
24
  access_component :agent, :config, :logging, :scope, :settings
26
25
 
27
26
  attr_reader :app
28
27
 
29
- # Allows the Agent to function as a middleware. We perform all our one-time whole-app routines in here
30
- # since we're only going to be initialized a single time. Our initialization order is:
28
+ # Allows the Agent to function as a middleware. We perform all our one-time whole-app routines in here since
29
+ # we're only going to be initialized a single time. Our initialization order is:
31
30
  # - capture the application
32
31
  # - setup the Agent
33
32
  # - startup the Agent
@@ -39,51 +38,27 @@ module Contrast
39
38
  setup_agent # THIS MUST BE SECOND AND ALWAYS CALLED!
40
39
  unless AGENT.enabled?
41
40
  logger.error(
42
- 'The Agent was unable to initialize before the application middleware was initialized. Disabling permanently.')
41
+ 'The Agent was unable to initialize before the application middleware was initialized. ' \
42
+ 'Disabling permanently.')
43
43
  AGENT.disable! # ensure the agent is disabled (probably redundant)
44
44
  return
45
45
  end
46
46
  agent_startup_routine
47
47
  end
48
48
 
49
- # Startup the Agent as part of the initialization process:
50
- # - start the service sending thread, responsible for sending and
51
- # processing messages
52
- # - start the heartbeat thread, which triggers service startup
53
- # - start instrumenting libraries and do a 'catchup' patch for everything
54
- # we didn't see get loaded
55
- # - enable TracePoint, which handles all class loads and required
56
- # instrumentation going forward
57
- def agent_startup_routine
58
- logger.debug_with_time('middleware: starting service') do
59
- Contrast::Agent.thread_watcher.ensure_running?
60
- end
61
-
62
- logger.debug_with_time('middleware: instrument shared libraries and patch') do
63
- Contrast::Agent::Patching::Policy::Patcher.patch
64
- end
65
-
66
- AGENT.enable_tracepoint
67
- Contrast::Agent::AtExitHook.exit_hook
68
- end
69
-
70
- # This is where we're hooked into the middleware stack. If the agent is enabled, we're ready
71
- # to do some processing on a per request basis. If not, we just pass the request along to the
72
- # next middleware in the stack.
49
+ # This is where we're hooked into the middleware stack. If the agent is enabled, we're ready to do some
50
+ # processing on a per request basis. If not, we just pass the request along to the next middleware in the stack.
73
51
  #
74
- # @param env [Hash] the various variables stored by this and other Middlewares to know the state
75
- # and values of this Request
76
- # @return [Array,Rack::Response] the Response of this and subsequent Middlewares to be passed back
77
- # to the user up the Rack framework.
52
+ # @param env [Hash] the various variables stored by this and other Middlewares to know the state and values of
53
+ # this Request
54
+ # @return [Array,Rack::Response] the Response of this and subsequent Middlewares to be passed back to the user up
55
+ # the Rack framework.
78
56
  def call env
79
- Contrast::Utils::HeapDumpUtil.run
57
+ return app.call(env) unless AGENT.enabled?
80
58
 
81
- if AGENT.enabled?
82
- handle_first_request
83
- call_with_agent(env)
84
- else
85
- app.call(env)
86
- end
59
+ Contrast::Agent.heapdump_util.start_thread!
60
+ handle_first_request
61
+ call_with_agent(env)
87
62
  end
88
63
 
89
64
  private
@@ -103,73 +78,56 @@ module Contrast
103
78
  end
104
79
  end
105
80
 
106
- # Some things have to wait until first request to happen, either because
107
- # resolution is not complete or because the framework will preload
108
- # classes, which confuses some of our instrumentation.
81
+ # Startup the Agent as part of the initialization process:
82
+ # - start the service sending thread, responsible for sending and processing messages
83
+ # - start the heartbeat thread, which triggers service startup
84
+ # - start instrumenting libraries and do a 'catchup' patch for everything we didn't see get loaded
85
+ # - enable TracePoint, which handles all class loads and required instrumentation going forward
86
+ def agent_startup_routine
87
+ logger.debug_with_time('middleware: starting service') do
88
+ Contrast::Agent.thread_watcher.ensure_running?
89
+ end
90
+
91
+ logger.debug_with_time('middleware: instrument shared libraries and patch') do
92
+ Contrast::Agent::Patching::Policy::Patcher.patch
93
+ end
94
+
95
+ logger.debug_with_time('middleware: enabling tracepoint') do
96
+ AGENT.enable_tracepoint
97
+ end
98
+ Contrast::Agent::AtExitHook.exit_hook
99
+ end
100
+
101
+ # Some things have to wait until first request to happen, either because resolution is not complete or because
102
+ # the framework will preload classes, which confuses some of our instrumentation.
109
103
  def handle_first_request
110
104
  @_handle_first_request ||= begin
111
105
  Contrast::Agent::StaticAnalysis.catchup
112
- force_patching
113
106
  true
114
107
  end
115
108
  end
116
109
 
117
- # TODO: RUBY-1090 remove this method and those it calls.
118
- #
119
- # These modules are auto-loaded by Rails, meaning they are defined at
120
- # startup, but that they don't actually exist. We account for this in
121
- # most cases by using the ClassUtil.truly_defined? method, but it appears
122
- # to fail for these Modules. In the short term, we can forcing a re-patch
123
- # so that their dead zones apply.
124
- def force_patching
125
- force_patch(ActionDispatch::FileHandler) if defined?(ActionDispatch::FileHandler)
126
- force_patch(ActionDispatch::Http::MimeNegotiation) if defined?(ActionDispatch::Http::MimeNegotiation)
127
- force_patch(ActionView::Template) if defined?(ActionView::Template)
128
- end
129
-
130
- def force_patch mod
131
- data = Contrast::Agent::ModuleData.new(mod)
132
- Contrast::Agent::Patching::Policy::Patcher.send(:patch_into_module, data, true)
133
- end
134
-
135
- # This is where we process each request we intercept as a middleware. We make the request context
136
- # available globally so that it can be accessed from anywhere. A RequestHandler object is made
137
- # for each request, which handles prefilter and postfilter operations.
110
+ # This is where we process each request we intercept as a middleware. We make the request context available
111
+ # globally so that it can be accessed from anywhere. A RequestHandler object is made for each request, which
112
+ # handles prefilter and postfilter operations.
113
+ # @param env [Hash] the various variables stored by this and other Middlewares to know the state and values of
114
+ # this Request
115
+ # @return [Array,Rack::Response] the Response of this and subsequent Middlewares to be passed back to the user
116
+ # up the Rack framework.
138
117
  def call_with_agent env
139
118
  Contrast::Agent.thread_watcher.ensure_running?
140
- return unless AGENT.enabled?
141
-
142
119
  framework_request = Contrast::Agent.framework_manager.retrieve_request(env)
143
120
  context = Contrast::Agent::RequestContext.new(framework_request)
144
121
  response = nil
145
122
 
146
- # make the context available for the lifecycle of this request
123
+ # Make the context available for the lifecycle of this request.
147
124
  Contrast::Agent::REQUEST_TRACKER.lifespan(context) do
148
125
  logger.request_start
149
126
  request_handler = Contrast::Agent::RequestHandler.new(context)
150
127
 
151
- # prefilter sequence
152
- with_contrast_scope do
153
- context.service_extract_request
154
- request_handler.ruleset.prefilter
155
- end
156
-
157
- response = application_code(env) # pass request down the Rack chain with original env
158
-
159
- # postfilter sequence
160
- with_contrast_scope do
161
- context.extract_after(response) # update context with final response information
162
-
163
- if Contrast::Agent.framework_manager.streaming?(env)
164
- context.reset_activity
165
- request_handler.stream_safe_postfilter
166
- else
167
- request_handler.ruleset.postfilter
168
- # return response stored in the context in case any postfilter rules updated the response data
169
- response = context.response&.rack_response || response
170
- request_handler.send_activity_messages
171
- end
172
- end
128
+ pre_call_with_agent(context, request_handler)
129
+ response = application_code(env)
130
+ post_call_with_agent(context, env, request_handler, response)
173
131
  ensure
174
132
  logger.request_end
175
133
  end
@@ -179,6 +137,47 @@ module Contrast
179
137
  handle_exception(e)
180
138
  end
181
139
 
140
+ # Handle the operations the Agent needs to accomplish prior to the Application code executing during this
141
+ # request.
142
+ #
143
+ # @param context [Contrast::Agent::RequestContext]
144
+ # @param request_handler [Contrast::Agent::RequestHandler]
145
+ def pre_call_with_agent context, request_handler
146
+ with_contrast_scope do
147
+ context.service_extract_request
148
+ request_handler.ruleset.prefilter
149
+ end
150
+ rescue StandardError => e
151
+ raise e if security_exception?(e)
152
+
153
+ logger.error('Unable to execute agent pre_call', e)
154
+ end
155
+
156
+ # Handle the operations the Agent needs to accomplish after the Application code executes during this request.
157
+ #
158
+ # @param context [Contrast::Agent::RequestContext]
159
+ # @param env [Hash] the various variables stored by this and other Middlewares to know the state and values of
160
+ # this Request
161
+ # @param request_handler [Contrast::Agent::RequestHandler]
162
+ # @param response [Array,Rack::Response]
163
+ def post_call_with_agent context, env, request_handler, response
164
+ with_contrast_scope do
165
+ context.extract_after(response) # update context with final response information
166
+
167
+ if Contrast::Agent.framework_manager.streaming?(env)
168
+ context.reset_activity
169
+ request_handler.stream_safe_postfilter
170
+ else
171
+ request_handler.ruleset.postfilter
172
+ request_handler.send_activity_messages
173
+ end
174
+ end
175
+ rescue StandardError => e
176
+ raise e if security_exception?(e)
177
+
178
+ logger.error('Unable to execute agent post_call', e)
179
+ end
180
+
182
181
  def application_code env
183
182
  logger.trace_with_time('application') do
184
183
  app.call(env)
@@ -189,12 +188,10 @@ module Contrast
189
188
  end
190
189
 
191
190
  SECURITY_EXCEPTION_MARKER = 'Contrast::SecurityException'
192
- # We're only going to suppress SecurityExceptions indicating a blocked attack.
193
- # And, only if the config.agent.ruby.exceptions.capture? is set
191
+ # We're only going to suppress SecurityExceptions indicating a blocked attack. And, only if the
192
+ # config.agent.ruby.exceptions.capture? is set
194
193
  def handle_exception exception
195
- if exception.is_a?(Contrast::SecurityException) ||
196
- exception.message&.include?(SECURITY_EXCEPTION_MARKER)
197
-
194
+ if security_exception?(exception)
198
195
  exception_control = AGENT.exception_control
199
196
  raise exception unless exception_control[:enable]
200
197
 
@@ -205,18 +202,24 @@ module Contrast
205
202
  end
206
203
  end
207
204
 
208
- # As we deprecate support to prepare to remove dead code, we need to
209
- # inform our users still relying on the now deprecated and soon to be
210
- # removed functionality. This method handles doing that by leveraging the
211
- # standard Kernel#warn approach
205
+ # Is the given exception one raised by our Protect code?
206
+ #
207
+ # @param exception [Exception]
208
+ # @return [Boolean]
209
+ def security_exception? exception
210
+ exception.is_a?(Contrast::SecurityException) ||
211
+ exception.message&.include?(SECURITY_EXCEPTION_MARKER)
212
+ end
213
+
214
+ # As we deprecate support to prepare to remove dead code, we need to inform our users still relying on the now
215
+ # deprecated and soon to be removed functionality. This method handles doing that by leveraging the standard
216
+ # Kernel#warn approach
212
217
  def inform_deprecations
213
- # Ruby 2.5 is currently in security maintenance, meaning int is only
214
- # receiving updates for security issues. It will move to eol on 31
215
- # March 2021. As such, we can remove support for it in Q2. We'll begin
216
- # the deprecation warnings now so that customers have time to reach out
217
- # if they'll be impacted.
218
- # TODO: RUBY-715 remove this part of the method, leaving it empty if
219
- # there are no other deprecations, when we drop 2.5 support.
218
+ # Ruby 2.5 is currently in security maintenance, meaning int is only receiving updates for security issues. It
219
+ # will move to eol on 31 March 2021. As such, we can remove support for it in Q3. We'll begin the deprecation
220
+ # warnings now so that customers have time to reach out if they'll be impacted.
221
+ # TODO: RUBY-715 remove this part of the method, leaving it empty if there are no other deprecations, when we
222
+ # drop 2.5 support.
220
223
  return unless RUBY_VERSION < '2.6.0'
221
224
 
222
225
  Kernel.warn('[Contrast Security] [DEPRECATION] Support for Ruby 2.5 will be removed in April 2021. '\
@@ -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'
@@ -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/after_load_patch'
@@ -55,6 +55,10 @@ module Contrast
55
55
  'Catching up on already loaded afterload patch - applying instrumentation',
56
56
  module: after_load_patch.module_name)
57
57
  after_load_patch.instrument!
58
+ rescue NameError => e
59
+ logger.error('Method undefined in afterload patch', e, module: after_load_patch.module_name, method: after_load_patch.method_to_instrument)
60
+ rescue StandardError => e
61
+ logger.error('Afterload patch failed to apply', e, module: after_load_patch.module_name, method: after_load_patch.method_to_instrument)
58
62
  end
59
63
  after_load_patches.delete_if(&:applied?)
60
64
  end
@@ -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/agent/patching/policy/method_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 'monitor'
@@ -178,7 +178,7 @@ module Contrast
178
178
  return ret unless method_policy && ASSESS.enabled?
179
179
 
180
180
  current_context = Contrast::Agent::REQUEST_TRACKER.current
181
- return ret unless current_context.analyze_request?
181
+ return ret if current_context && !current_context.analyze_request?
182
182
 
183
183
  trigger_node = method_policy.trigger_node
184
184
  Contrast::Agent::Assess::Policy::TriggerMethod.apply_trigger_rule(trigger_node, object, ret, args) if trigger_node
@@ -362,29 +362,27 @@ module Contrast
362
362
 
363
363
  case impl
364
364
  when :alias_instance, :alias_singleton
365
+ # Core to patching. Ignore define method usage cop.
366
+ # rubocop:disable Performance/Kernel/DefineMethod
365
367
  unless target_module.instance_methods(false).include? underlying_method_name
366
368
  # alias_method may be private
367
369
  target_module.send(:alias_method, underlying_method_name, method_name)
368
- # TODO: RUBY-1052
369
- # rubocop:disable Performance/Kernel/DefineMethod
370
370
  target_module.send(:define_method, method_name, unbound_method.bind(target_module))
371
- # rubocop:enable Performance/Kernel/DefineMethod
372
371
  end
373
372
  target_module.send(visibility, method_name) # e.g., module.private(:my_method)
374
373
  when :prepend
375
374
  prepending_module = Module.new
376
- # TODO: RUBY-1052
377
- # rubocop:disable Performance/Kernel/DefineMethod
378
375
  prepending_module.send(:define_method, method_name, unbound_method.bind(target_module))
379
- # rubocop:enable Performance/Kernel/DefineMethod
380
376
  prepending_module.send(visibility, method_name)
381
377
  # This prepends to the singleton class (it patches a class method)
382
378
  target_module.prepend prepending_module
379
+ # rubocop:enable Performance/Kernel/DefineMethod
383
380
  end
384
381
  # Ougai::Logger.create_item_with_2args calls Hash#[]=, so we
385
382
  # can't invoke this logging method or we'll seg fault as we'd
386
383
  # change the method definition mid-call
387
- # if method_name != :[]=
384
+ # if method_name != :[]= &&
385
+ # Contrast::Agent::Logger.defined!
388
386
  # logger.trace(
389
387
  # 'Registered C-defined patch',
390
388
  # implementation: impl,
@@ -392,14 +390,15 @@ module Contrast
392
390
  # method: method_name,
393
391
  # visibility: visibility)
394
392
  # end
395
- underlying_method_name.to_sym
393
+ underlying_method_name
396
394
  end
397
395
 
398
396
  # @return [Boolean]
399
397
  def skip_contrast_analysis?
400
398
  return true if in_contrast_scope?
401
- return true unless defined?(Contrast::Agent::REQUEST_TRACKER)
402
- return true unless Contrast::Agent::REQUEST_TRACKER.current&.analyze_request?
399
+ return false unless defined?(Contrast::Agent::REQUEST_TRACKER)
400
+ return false unless Contrast::Agent::REQUEST_TRACKER.current
401
+ return true unless Contrast::Agent::REQUEST_TRACKER.current.analyze_request?
403
402
 
404
403
  false
405
404
  end
@@ -411,7 +410,7 @@ module Contrast
411
410
  def skip_assess_analysis?
412
411
  return true if skip_contrast_analysis?
413
412
 
414
- !ASSESS.enabled?
413
+ !ASSESS&.enabled?
415
414
  end
416
415
  end
417
416
  end
@@ -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
@@ -13,7 +13,7 @@ module Contrast
13
13
  # one does not exist.
14
14
  #
15
15
  # @param mod [Module] the Module for which the status is asked
16
- # @return [Contrast::Agent::Patching::Policy::PolicyStatus]
16
+ # @return [Contrast::Agent::Patching::Policy::PatchStatus]
17
17
  def get_status mod
18
18
  if mod.cs__const_defined?(status_key, false)
19
19
  mod.cs__const_get(status_key, false)