contrast-agent 4.4.0 → 4.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (313) hide show
  1. checksums.yaml +4 -4
  2. data/.gitmodules +1 -1
  3. data/.simplecov +1 -1
  4. data/Gemfile +1 -1
  5. data/LICENSE.txt +1 -1
  6. data/Rakefile +2 -3
  7. data/exe/contrast_service +1 -1
  8. data/ext/build_funchook.rb +4 -4
  9. data/ext/cs__assess_active_record_named/cs__active_record_named.c +1 -1
  10. data/ext/cs__assess_active_record_named/extconf.rb +1 -1
  11. data/ext/cs__assess_array/cs__assess_array.c +1 -1
  12. data/ext/cs__assess_array/extconf.rb +1 -1
  13. data/ext/cs__assess_basic_object/cs__assess_basic_object.c +1 -1
  14. data/ext/cs__assess_basic_object/extconf.rb +1 -1
  15. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.c +1 -1
  16. data/ext/cs__assess_fiber_track/extconf.rb +1 -1
  17. data/ext/cs__assess_hash/cs__assess_hash.c +4 -2
  18. data/ext/cs__assess_hash/extconf.rb +1 -1
  19. data/ext/cs__assess_kernel/cs__assess_kernel.c +1 -1
  20. data/ext/cs__assess_kernel/extconf.rb +1 -1
  21. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +1 -1
  22. data/ext/cs__assess_marshal_module/extconf.rb +1 -1
  23. data/ext/cs__assess_module/cs__assess_module.c +1 -1
  24. data/ext/cs__assess_module/extconf.rb +1 -1
  25. data/ext/cs__assess_regexp/cs__assess_regexp.c +1 -1
  26. data/ext/cs__assess_regexp/extconf.rb +1 -1
  27. data/ext/cs__assess_string/cs__assess_string.c +1 -1
  28. data/ext/cs__assess_string/extconf.rb +1 -1
  29. data/ext/cs__assess_string_interpolation26/cs__assess_string_interpolation26.c +1 -1
  30. data/ext/cs__assess_string_interpolation26/extconf.rb +1 -1
  31. data/ext/cs__assess_yield_track/cs__assess_yield_track.c +1 -1
  32. data/ext/cs__assess_yield_track/extconf.rb +1 -1
  33. data/ext/cs__common/cs__common.c +5 -5
  34. data/ext/cs__common/cs__common.h +4 -4
  35. data/ext/cs__common/extconf.rb +1 -1
  36. data/ext/cs__contrast_patch/cs__contrast_patch.c +22 -25
  37. data/ext/cs__contrast_patch/extconf.rb +1 -1
  38. data/ext/cs__protect_kernel/cs__protect_kernel.c +1 -1
  39. data/ext/cs__protect_kernel/extconf.rb +1 -1
  40. data/ext/extconf_common.rb +2 -6
  41. data/lib/contrast-agent.rb +1 -1
  42. data/lib/contrast.rb +20 -1
  43. data/lib/contrast/agent.rb +1 -3
  44. data/lib/contrast/agent/assess.rb +2 -2
  45. data/lib/contrast/agent/assess/contrast_event.rb +54 -69
  46. data/lib/contrast/agent/assess/contrast_object.rb +3 -3
  47. data/lib/contrast/agent/assess/events/event_factory.rb +3 -2
  48. data/lib/contrast/agent/assess/events/source_event.rb +7 -2
  49. data/lib/contrast/agent/assess/finalizers/freeze.rb +1 -1
  50. data/lib/contrast/agent/assess/finalizers/hash.rb +26 -34
  51. data/lib/contrast/agent/assess/policy/dynamic_source_factory.rb +24 -20
  52. data/lib/contrast/agent/assess/policy/patcher.rb +11 -18
  53. data/lib/contrast/agent/assess/policy/policy.rb +1 -1
  54. data/lib/contrast/agent/assess/policy/policy_node.rb +26 -34
  55. data/lib/contrast/agent/assess/policy/policy_scanner.rb +1 -1
  56. data/lib/contrast/agent/assess/policy/preshift.rb +4 -2
  57. data/lib/contrast/agent/assess/policy/propagation_method.rb +8 -21
  58. data/lib/contrast/agent/assess/policy/propagation_node.rb +20 -9
  59. data/lib/contrast/agent/assess/policy/propagator.rb +2 -1
  60. data/lib/contrast/agent/assess/policy/propagator/append.rb +1 -1
  61. data/lib/contrast/agent/assess/policy/propagator/base.rb +1 -1
  62. data/lib/contrast/agent/assess/policy/propagator/center.rb +3 -2
  63. data/lib/contrast/agent/assess/policy/propagator/custom.rb +1 -1
  64. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +1 -1
  65. data/lib/contrast/agent/assess/policy/propagator/insert.rb +4 -2
  66. data/lib/contrast/agent/assess/policy/propagator/keep.rb +1 -1
  67. data/lib/contrast/agent/assess/policy/propagator/match_data.rb +3 -2
  68. data/lib/contrast/agent/assess/policy/propagator/next.rb +1 -1
  69. data/lib/contrast/agent/assess/policy/propagator/prepend.rb +1 -1
  70. data/lib/contrast/agent/assess/policy/propagator/rack_protection.rb +73 -0
  71. data/lib/contrast/agent/assess/policy/propagator/remove.rb +23 -19
  72. data/lib/contrast/agent/assess/policy/propagator/replace.rb +1 -1
  73. data/lib/contrast/agent/assess/policy/propagator/reverse.rb +1 -1
  74. data/lib/contrast/agent/assess/policy/propagator/select.rb +3 -13
  75. data/lib/contrast/agent/assess/policy/propagator/splat.rb +1 -1
  76. data/lib/contrast/agent/assess/policy/propagator/split.rb +4 -8
  77. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +2 -8
  78. data/lib/contrast/agent/assess/policy/propagator/trim.rb +64 -45
  79. data/lib/contrast/agent/assess/policy/rewriter_patch.rb +7 -4
  80. data/lib/contrast/agent/assess/policy/source_method.rb +92 -81
  81. data/lib/contrast/agent/assess/policy/source_node.rb +1 -1
  82. data/lib/contrast/agent/assess/policy/source_validation/cross_site_validator.rb +8 -6
  83. data/lib/contrast/agent/assess/policy/source_validation/source_validation.rb +2 -4
  84. data/lib/contrast/agent/assess/policy/trigger/reflected_xss.rb +7 -3
  85. data/lib/contrast/agent/assess/policy/trigger/xpath.rb +7 -8
  86. data/lib/contrast/agent/assess/policy/trigger_method.rb +102 -74
  87. data/lib/contrast/agent/assess/policy/trigger_node.rb +5 -4
  88. data/lib/contrast/agent/assess/policy/trigger_validation/redos_validator.rb +5 -4
  89. data/lib/contrast/agent/assess/policy/trigger_validation/ssrf_validator.rb +2 -3
  90. data/lib/contrast/agent/assess/policy/trigger_validation/trigger_validation.rb +1 -1
  91. data/lib/contrast/agent/assess/policy/trigger_validation/xss_validator.rb +2 -9
  92. data/lib/contrast/agent/assess/properties.rb +1 -1
  93. data/lib/contrast/agent/assess/property/evented.rb +9 -6
  94. data/lib/contrast/agent/assess/property/tagged.rb +1 -1
  95. data/lib/contrast/agent/assess/property/updated.rb +1 -1
  96. data/lib/contrast/agent/assess/rule/provider.rb +1 -1
  97. data/lib/contrast/agent/assess/rule/provider/hardcoded_key.rb +12 -6
  98. data/lib/contrast/agent/assess/rule/provider/hardcoded_password.rb +5 -2
  99. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +4 -6
  100. data/lib/contrast/agent/assess/tag.rb +1 -1
  101. data/lib/contrast/agent/assess/tracker.rb +1 -1
  102. data/lib/contrast/agent/at_exit_hook.rb +1 -1
  103. data/lib/contrast/agent/class_reopener.rb +4 -2
  104. data/lib/contrast/agent/deadzone/policy/deadzone_node.rb +1 -1
  105. data/lib/contrast/agent/deadzone/policy/policy.rb +7 -3
  106. data/lib/contrast/agent/disable_reaction.rb +2 -4
  107. data/lib/contrast/agent/exclusion_matcher.rb +6 -12
  108. data/lib/contrast/agent/inventory.rb +1 -2
  109. data/lib/contrast/agent/inventory/dependencies.rb +3 -1
  110. data/lib/contrast/agent/inventory/dependency_analysis.rb +1 -1
  111. data/lib/contrast/agent/inventory/dependency_usage_analysis.rb +35 -23
  112. data/lib/contrast/agent/inventory/policy/datastores.rb +1 -1
  113. data/lib/contrast/agent/inventory/policy/policy.rb +1 -1
  114. data/lib/contrast/agent/inventory/policy/trigger_node.rb +1 -1
  115. data/lib/contrast/agent/middleware.rb +38 -67
  116. data/lib/contrast/agent/module_data.rb +4 -4
  117. data/lib/contrast/agent/patching/policy/after_load_patch.rb +1 -1
  118. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +9 -4
  119. data/lib/contrast/agent/patching/policy/method_policy.rb +7 -3
  120. data/lib/contrast/agent/patching/policy/module_policy.rb +15 -8
  121. data/lib/contrast/agent/patching/policy/patch.rb +23 -29
  122. data/lib/contrast/agent/patching/policy/patch_status.rb +7 -8
  123. data/lib/contrast/agent/patching/policy/patcher.rb +23 -22
  124. data/lib/contrast/agent/patching/policy/policy.rb +14 -21
  125. data/lib/contrast/agent/patching/policy/policy_node.rb +15 -5
  126. data/lib/contrast/agent/patching/policy/trigger_node.rb +22 -9
  127. data/lib/contrast/agent/protect/policy/applies_command_injection_rule.rb +2 -2
  128. data/lib/contrast/agent/protect/policy/applies_deserialization_rule.rb +2 -2
  129. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +2 -2
  130. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +3 -4
  131. data/lib/contrast/agent/protect/policy/applies_sqli_rule.rb +2 -2
  132. data/lib/contrast/agent/protect/policy/applies_xxe_rule.rb +6 -10
  133. data/lib/contrast/agent/protect/policy/policy.rb +1 -1
  134. data/lib/contrast/agent/protect/policy/rule_applicator.rb +6 -6
  135. data/lib/contrast/agent/protect/policy/trigger_node.rb +1 -1
  136. data/lib/contrast/agent/protect/rule.rb +1 -1
  137. data/lib/contrast/agent/protect/rule/base.rb +19 -33
  138. data/lib/contrast/agent/protect/rule/base_service.rb +10 -6
  139. data/lib/contrast/agent/protect/rule/cmd_injection.rb +15 -19
  140. data/lib/contrast/agent/protect/rule/default_scanner.rb +1 -1
  141. data/lib/contrast/agent/protect/rule/deserialization.rb +7 -14
  142. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +4 -15
  143. data/lib/contrast/agent/protect/rule/no_sqli.rb +7 -3
  144. data/lib/contrast/agent/protect/rule/no_sqli/mongo_no_sql_scanner.rb +2 -4
  145. data/lib/contrast/agent/protect/rule/path_traversal.rb +6 -6
  146. data/lib/contrast/agent/protect/rule/sqli.rb +3 -3
  147. data/lib/contrast/agent/protect/rule/sqli/default_sql_scanner.rb +1 -1
  148. data/lib/contrast/agent/protect/rule/sqli/mysql_sql_scanner.rb +1 -1
  149. data/lib/contrast/agent/protect/rule/sqli/postgres_sql_scanner.rb +2 -2
  150. data/lib/contrast/agent/protect/rule/sqli/sqlite_sql_scanner.rb +1 -1
  151. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +2 -2
  152. data/lib/contrast/agent/protect/rule/xss.rb +2 -2
  153. data/lib/contrast/agent/protect/rule/xxe.rb +6 -13
  154. data/lib/contrast/agent/protect/rule/xxe/entity_wrapper.rb +2 -3
  155. data/lib/contrast/agent/reaction_processor.rb +12 -11
  156. data/lib/contrast/agent/request.rb +25 -24
  157. data/lib/contrast/agent/request_context.rb +13 -23
  158. data/lib/contrast/agent/request_handler.rb +1 -1
  159. data/lib/contrast/agent/response.rb +1 -1
  160. data/lib/contrast/agent/rewriter.rb +6 -4
  161. data/lib/contrast/agent/rule_set.rb +3 -3
  162. data/lib/contrast/agent/scope.rb +1 -1
  163. data/lib/contrast/agent/service_heartbeat.rb +3 -4
  164. data/lib/contrast/agent/static_analysis.rb +1 -1
  165. data/lib/contrast/agent/thread.rb +1 -1
  166. data/lib/contrast/agent/thread_watcher.rb +1 -1
  167. data/lib/contrast/agent/tracepoint_hook.rb +2 -2
  168. data/lib/contrast/agent/version.rb +2 -2
  169. data/lib/contrast/agent/worker_thread.rb +1 -1
  170. data/lib/contrast/api.rb +1 -1
  171. data/lib/contrast/api/communication.rb +1 -1
  172. data/lib/contrast/api/communication/connection_status.rb +1 -1
  173. data/lib/contrast/api/communication/messaging_queue.rb +1 -1
  174. data/lib/contrast/api/communication/response_processor.rb +5 -7
  175. data/lib/contrast/api/communication/service_lifecycle.rb +5 -3
  176. data/lib/contrast/api/communication/socket.rb +1 -1
  177. data/lib/contrast/api/communication/socket_client.rb +9 -22
  178. data/lib/contrast/api/communication/speedracer.rb +6 -10
  179. data/lib/contrast/api/communication/tcp_socket.rb +1 -1
  180. data/lib/contrast/api/communication/unix_socket.rb +1 -1
  181. data/lib/contrast/api/decorators.rb +1 -1
  182. data/lib/contrast/api/decorators/address.rb +1 -1
  183. data/lib/contrast/api/decorators/agent_startup.rb +1 -1
  184. data/lib/contrast/api/decorators/application_settings.rb +1 -1
  185. data/lib/contrast/api/decorators/application_startup.rb +10 -4
  186. data/lib/contrast/api/decorators/application_update.rb +1 -1
  187. data/lib/contrast/api/decorators/http_request.rb +1 -1
  188. data/lib/contrast/api/decorators/input_analysis.rb +1 -1
  189. data/lib/contrast/api/decorators/instrumentation_mode.rb +37 -0
  190. data/lib/contrast/api/decorators/library.rb +9 -7
  191. data/lib/contrast/api/decorators/library_usage_update.rb +1 -1
  192. data/lib/contrast/api/decorators/message.rb +4 -4
  193. data/lib/contrast/api/decorators/rasp_rule_sample.rb +1 -1
  194. data/lib/contrast/api/decorators/route_coverage.rb +1 -1
  195. data/lib/contrast/api/decorators/server_features.rb +1 -1
  196. data/lib/contrast/api/decorators/trace_event.rb +4 -2
  197. data/lib/contrast/api/decorators/trace_event_object.rb +2 -4
  198. data/lib/contrast/api/decorators/trace_event_signature.rb +1 -1
  199. data/lib/contrast/api/decorators/trace_taint_range.rb +1 -1
  200. data/lib/contrast/api/decorators/trace_taint_range_tags.rb +2 -7
  201. data/lib/contrast/api/decorators/user_input.rb +1 -1
  202. data/lib/contrast/components/agent.rb +14 -15
  203. data/lib/contrast/components/app_context.rb +7 -7
  204. data/lib/contrast/components/assess.rb +6 -11
  205. data/lib/contrast/components/config.rb +3 -2
  206. data/lib/contrast/components/contrast_service.rb +8 -9
  207. data/lib/contrast/components/heap_dump.rb +1 -1
  208. data/lib/contrast/components/interface.rb +4 -3
  209. data/lib/contrast/components/inventory.rb +1 -1
  210. data/lib/contrast/components/logger.rb +1 -1
  211. data/lib/contrast/components/protect.rb +11 -14
  212. data/lib/contrast/components/sampling.rb +9 -3
  213. data/lib/contrast/components/scope.rb +2 -1
  214. data/lib/contrast/components/settings.rb +27 -98
  215. data/lib/contrast/config.rb +1 -1
  216. data/lib/contrast/config/agent_configuration.rb +1 -1
  217. data/lib/contrast/config/application_configuration.rb +1 -1
  218. data/lib/contrast/config/assess_configuration.rb +1 -1
  219. data/lib/contrast/config/assess_rules_configuration.rb +2 -4
  220. data/lib/contrast/config/base_configuration.rb +5 -6
  221. data/lib/contrast/config/default_value.rb +1 -1
  222. data/lib/contrast/config/exception_configuration.rb +2 -6
  223. data/lib/contrast/config/heap_dump_configuration.rb +13 -7
  224. data/lib/contrast/config/inventory_configuration.rb +1 -1
  225. data/lib/contrast/config/logger_configuration.rb +2 -6
  226. data/lib/contrast/config/protect_configuration.rb +1 -1
  227. data/lib/contrast/config/protect_rule_configuration.rb +23 -1
  228. data/lib/contrast/config/protect_rules_configuration.rb +1 -1
  229. data/lib/contrast/config/root_configuration.rb +1 -1
  230. data/lib/contrast/config/ruby_configuration.rb +1 -1
  231. data/lib/contrast/config/sampling_configuration.rb +1 -1
  232. data/lib/contrast/config/server_configuration.rb +1 -1
  233. data/lib/contrast/config/service_configuration.rb +1 -1
  234. data/lib/contrast/configuration.rb +4 -15
  235. data/lib/contrast/delegators/input_analysis.rb +12 -0
  236. data/lib/contrast/extension/assess.rb +1 -1
  237. data/lib/contrast/extension/assess/array.rb +2 -7
  238. data/lib/contrast/extension/assess/erb.rb +2 -8
  239. data/lib/contrast/extension/assess/eval_trigger.rb +3 -11
  240. data/lib/contrast/extension/assess/exec_trigger.rb +4 -14
  241. data/lib/contrast/extension/assess/fiber.rb +3 -13
  242. data/lib/contrast/extension/assess/hash.rb +1 -1
  243. data/lib/contrast/extension/assess/kernel.rb +3 -10
  244. data/lib/contrast/extension/assess/marshal.rb +3 -11
  245. data/lib/contrast/extension/assess/regexp.rb +2 -7
  246. data/lib/contrast/extension/assess/string.rb +4 -2
  247. data/lib/contrast/extension/delegator.rb +1 -1
  248. data/lib/contrast/extension/inventory.rb +1 -1
  249. data/lib/contrast/extension/kernel.rb +5 -3
  250. data/lib/contrast/extension/module.rb +1 -1
  251. data/lib/contrast/extension/protect.rb +1 -1
  252. data/lib/contrast/extension/protect/kernel.rb +1 -1
  253. data/lib/contrast/extension/protect/psych.rb +1 -1
  254. data/lib/contrast/extension/thread.rb +1 -1
  255. data/lib/contrast/framework/base_support.rb +1 -1
  256. data/lib/contrast/framework/manager.rb +5 -9
  257. data/lib/contrast/framework/platform_version.rb +1 -1
  258. data/lib/contrast/framework/rack/patch/session_cookie.rb +6 -19
  259. data/lib/contrast/framework/rack/patch/support.rb +7 -5
  260. data/lib/contrast/framework/rack/support.rb +1 -1
  261. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +1 -1
  262. data/lib/contrast/framework/rails/patch/assess_configuration.rb +8 -3
  263. data/lib/contrast/framework/rails/patch/rails_application_configuration.rb +4 -4
  264. data/lib/contrast/framework/rails/patch/support.rb +6 -4
  265. data/lib/contrast/framework/rails/railtie.rb +32 -0
  266. data/lib/contrast/framework/rails/rewrite/action_controller_railties_helper_inherited.rb +5 -2
  267. data/lib/contrast/framework/rails/rewrite/active_record_attribute_methods_read.rb +3 -1
  268. data/lib/contrast/framework/rails/rewrite/active_record_named.rb +3 -1
  269. data/lib/contrast/framework/rails/rewrite/active_record_time_zone_inherited.rb +3 -1
  270. data/lib/contrast/framework/rails/support.rb +3 -3
  271. data/lib/contrast/framework/sinatra/support.rb +4 -2
  272. data/lib/contrast/funchook/funchook.rb +2 -6
  273. data/lib/contrast/logger/application.rb +13 -10
  274. data/lib/contrast/logger/format.rb +3 -6
  275. data/lib/contrast/logger/log.rb +5 -4
  276. data/lib/contrast/logger/request.rb +2 -3
  277. data/lib/contrast/logger/time.rb +1 -1
  278. data/lib/contrast/security_exception.rb +2 -2
  279. data/lib/contrast/tasks/config.rb +1 -1
  280. data/lib/contrast/tasks/service.rb +6 -2
  281. data/lib/contrast/utils/assess/sampling_util.rb +1 -1
  282. data/lib/contrast/utils/assess/tracking_util.rb +2 -3
  283. data/lib/contrast/utils/class_util.rb +15 -11
  284. data/lib/contrast/utils/duck_utils.rb +1 -1
  285. data/lib/contrast/utils/env_configuration_item.rb +1 -1
  286. data/lib/contrast/utils/hash_digest.rb +16 -24
  287. data/lib/contrast/utils/heap_dump_util.rb +1 -1
  288. data/lib/contrast/utils/invalid_configuration_util.rb +1 -1
  289. data/lib/contrast/utils/inventory_util.rb +1 -1
  290. data/lib/contrast/utils/io_util.rb +2 -2
  291. data/lib/contrast/utils/job_servers_running.rb +10 -5
  292. data/lib/contrast/utils/object_share.rb +1 -1
  293. data/lib/contrast/utils/os.rb +3 -2
  294. data/lib/contrast/utils/preflight_util.rb +1 -1
  295. data/lib/contrast/utils/resource_loader.rb +1 -1
  296. data/lib/contrast/utils/ruby_ast_rewriter.rb +3 -2
  297. data/lib/contrast/utils/sha256_builder.rb +1 -1
  298. data/lib/contrast/utils/stack_trace_utils.rb +1 -1
  299. data/lib/contrast/utils/string_utils.rb +1 -1
  300. data/lib/contrast/utils/tag_util.rb +1 -1
  301. data/lib/contrast/utils/thread_tracker.rb +1 -1
  302. data/lib/contrast/utils/timer.rb +1 -1
  303. data/resources/assess/policy.json +60 -2
  304. data/resources/deadzone/policy.json +7 -23
  305. data/ruby-agent.gemspec +22 -20
  306. data/service_executables/VERSION +1 -1
  307. data/service_executables/linux/contrast-service +0 -0
  308. data/service_executables/mac/contrast-service +0 -0
  309. data/sonar-project.properties +9 -0
  310. metadata +94 -50
  311. data/lib/contrast/agent/inventory/gemfile_digest_cache.rb +0 -38
  312. data/lib/contrast/agent/railtie.rb +0 -31
  313. data/lib/contrast/common_agent_configuration.rb +0 -87
@@ -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
@@ -38,22 +37,21 @@ module Contrast
38
37
  @app = app # THIS MUST BE FIRST AND ALWAYS SET!
39
38
  setup_agent # THIS MUST BE SECOND AND ALWAYS CALLED!
40
39
  unless AGENT.enabled?
41
- logger.error(
42
- 'The Agent was unable to initialize before the application middleware was initialized. Disabling permanently.')
40
+ logger.error('The Agent was unable to initialize before the application middleware was initialized. '\
41
+ 'Disabling permanently.')
43
42
  AGENT.disable! # ensure the agent is disabled (probably redundant)
44
43
  return
45
44
  end
46
45
  agent_startup_routine
47
46
  end
48
47
 
49
- # This is where we're hooked into the middleware stack. If the agent is enabled, we're ready
50
- # to do some processing on a per request basis. If not, we just pass the request along to the
51
- # next middleware in the stack.
48
+ # This is where we're hooked into the middleware stack. If the agent is enabled, we're ready to do some
49
+ # processing on a per request basis. If not, we just pass the request along to the next middleware in the stack.
52
50
  #
53
- # @param env [Hash] the various variables stored by this and other Middlewares to know the state
54
- # and values of this Request
55
- # @return [Array,Rack::Response] the Response of this and subsequent Middlewares to be passed back
56
- # to the user up the Rack framework.
51
+ # @param env [Hash] the various variables stored by this and other Middlewares to know the state and values of
52
+ # this Request
53
+ # @return [Array,Rack::Response] the Response of this and subsequent Middlewares to be passed back to the user up
54
+ # the Rack framework.
57
55
  def call env
58
56
  return app.call(env) unless AGENT.enabled?
59
57
 
@@ -80,13 +78,10 @@ module Contrast
80
78
  end
81
79
 
82
80
  # Startup the Agent as part of the initialization process:
83
- # - start the service sending thread, responsible for sending and
84
- # processing messages
81
+ # - start the service sending thread, responsible for sending and processing messages
85
82
  # - start the heartbeat thread, which triggers service startup
86
- # - start instrumenting libraries and do a 'catchup' patch for everything
87
- # we didn't see get loaded
88
- # - enable TracePoint, which handles all class loads and required
89
- # instrumentation going forward
83
+ # - start instrumenting libraries and do a 'catchup' patch for everything we didn't see get loaded
84
+ # - enable TracePoint, which handles all class loads and required instrumentation going forward
90
85
  def agent_startup_routine
91
86
  logger.debug_with_time('middleware: starting service') do
92
87
  Contrast::Agent.thread_watcher.ensure_running?
@@ -102,49 +97,29 @@ module Contrast
102
97
  Contrast::Agent::AtExitHook.exit_hook
103
98
  end
104
99
 
105
- # Some things have to wait until first request to happen, either because
106
- # resolution is not complete or because the framework will preload
107
- # classes, which confuses some of our instrumentation.
100
+ # Some things have to wait until first request to happen, either because resolution is not complete or because
101
+ # the framework will preload classes, which confuses some of our instrumentation.
108
102
  def handle_first_request
109
103
  @_handle_first_request ||= begin
110
104
  Contrast::Agent::StaticAnalysis.catchup
111
- force_patching
112
105
  true
113
106
  end
114
107
  end
115
108
 
116
- # TODO: RUBY-1090 remove this method and those it calls.
117
- #
118
- # These modules are auto-loaded by Rails, meaning they are defined at
119
- # startup, but that they don't actually exist. We account for this in
120
- # most cases by using the ClassUtil.truly_defined? method, but it appears
121
- # to fail for these Modules. In the short term, we can forcing a re-patch
122
- # so that their dead zones apply.
123
- def force_patching
124
- force_patch(ActionDispatch::FileHandler) if defined?(ActionDispatch::FileHandler)
125
- force_patch(ActionDispatch::Http::MimeNegotiation) if defined?(ActionDispatch::Http::MimeNegotiation)
126
- force_patch(ActionView::Template) if defined?(ActionView::Template)
127
- end
128
-
129
- def force_patch mod
130
- data = Contrast::Agent::ModuleData.new(mod)
131
- Contrast::Agent::Patching::Policy::Patcher.send(:patch_into_module, data, true)
132
- end
133
-
134
- # This is where we process each request we intercept as a middleware. We make the request context
135
- # available globally so that it can be accessed from anywhere. A RequestHandler object is made
136
- # for each request, which handles prefilter and postfilter operations.
137
- # @param env [Hash] the various variables stored by this and other Middlewares to know the state
138
- # and values of this Request
139
- # @return [Array,Rack::Response] the Response of this and subsequent Middlewares to be passed back
140
- # to the user up the Rack framework.
109
+ # This is where we process each request we intercept as a middleware. We make the request context available
110
+ # globally so that it can be accessed from anywhere. A RequestHandler object is made for each request, which
111
+ # handles prefilter and postfilter operations.
112
+ # @param env [Hash] the various variables stored by this and other Middlewares to know the state and values of
113
+ # this Request
114
+ # @return [Array,Rack::Response] the Response of this and subsequent Middlewares to be passed back to the user
115
+ # up the Rack framework.
141
116
  def call_with_agent env
142
117
  Contrast::Agent.thread_watcher.ensure_running?
143
118
  framework_request = Contrast::Agent.framework_manager.retrieve_request(env)
144
119
  context = Contrast::Agent::RequestContext.new(framework_request)
145
120
  response = nil
146
121
 
147
- # make the context available for the lifecycle of this request
122
+ # Make the context available for the lifecycle of this request.
148
123
  Contrast::Agent::REQUEST_TRACKER.lifespan(context) do
149
124
  logger.request_start
150
125
  request_handler = Contrast::Agent::RequestHandler.new(context)
@@ -212,8 +187,8 @@ module Contrast
212
187
  end
213
188
 
214
189
  SECURITY_EXCEPTION_MARKER = 'Contrast::SecurityException'
215
- # We're only going to suppress SecurityExceptions indicating a blocked attack.
216
- # And, only if the config.agent.ruby.exceptions.capture? is set
190
+ # We're only going to suppress SecurityExceptions indicating a blocked attack. And, only if the
191
+ # config.agent.ruby.exceptions.capture? is set
217
192
  def handle_exception exception
218
193
  if security_exception?(exception)
219
194
  exception_control = AGENT.exception_control
@@ -231,22 +206,18 @@ module Contrast
231
206
  # @param exception [Exception]
232
207
  # @return [Boolean]
233
208
  def security_exception? exception
234
- exception.is_a?(Contrast::SecurityException) ||
235
- exception.message&.include?(SECURITY_EXCEPTION_MARKER)
209
+ exception.is_a?(Contrast::SecurityException) || exception.message&.include?(SECURITY_EXCEPTION_MARKER)
236
210
  end
237
211
 
238
- # As we deprecate support to prepare to remove dead code, we need to
239
- # inform our users still relying on the now deprecated and soon to be
240
- # removed functionality. This method handles doing that by leveraging the
241
- # standard Kernel#warn approach
212
+ # As we deprecate support to prepare to remove dead code, we need to inform our users still relying on the now
213
+ # deprecated and soon to be removed functionality. This method handles doing that by leveraging the standard
214
+ # Kernel#warn approach
242
215
  def inform_deprecations
243
- # Ruby 2.5 is currently in security maintenance, meaning int is only
244
- # receiving updates for security issues. It will move to eol on 31
245
- # March 2021. As such, we can remove support for it in Q2. We'll begin
246
- # the deprecation warnings now so that customers have time to reach out
247
- # if they'll be impacted.
248
- # TODO: RUBY-715 remove this part of the method, leaving it empty if
249
- # there are no other deprecations, when we drop 2.5 support.
216
+ # Ruby 2.5 is currently in security maintenance, meaning int is only receiving updates for security issues. It
217
+ # will move to eol on 31 March 2021. As such, we can remove support for it in Q3. We'll begin the deprecation
218
+ # warnings now so that customers have time to reach out if they'll be impacted.
219
+ # TODO: RUBY-715 remove this part of the method, leaving it empty if there are no other deprecations, when we
220
+ # drop 2.5 support.
250
221
  return unless RUBY_VERSION < '2.6.0'
251
222
 
252
223
  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
@@ -6,11 +6,11 @@ module Contrast
6
6
  # A simple wrapper around a Module and a call to its name, used to avoid
7
7
  # calling the Module#name method and generating extra Strings
8
8
  class ModuleData
9
- attr_reader :mod, :name
9
+ attr_reader :mod, :mod_name
10
10
 
11
- def initialize mod, name = nil
11
+ def initialize mod, mod_name = nil
12
12
  @mod = mod
13
- @name = name || mod.cs__name
13
+ @mod_name = mod_name || mod.cs__name
14
14
  end
15
15
  end
16
16
  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
  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'
@@ -51,10 +51,15 @@ module Contrast
51
51
  next unless after_load_patch.target_defined?
52
52
  next if AGENT.skip_instrumentation?(after_load_patch.module_name)
53
53
 
54
- logger.trace(
55
- 'Catching up on already loaded afterload patch - applying instrumentation',
56
- module: after_load_patch.module_name)
54
+ logger.trace('Catching up on already loaded afterload patch - applying instrumentation',
55
+ module: after_load_patch.module_name)
57
56
  after_load_patch.instrument!
57
+ rescue NameError => e
58
+ logger.error('Method undefined in afterload patch', e, module: after_load_patch.module_name,
59
+ 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,
62
+ method: after_load_patch.method_to_instrument)
58
63
  end
59
64
  after_load_patches.delete_if(&:applied?)
60
65
  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
@@ -49,7 +49,10 @@ module Contrast
49
49
  private
50
50
 
51
51
  def nodes
52
- @_nodes ||= [source_node, propagation_node, trigger_node, inventory_node, protect_node, deadzone_node].compact
52
+ @_nodes ||= [
53
+ source_node, propagation_node, trigger_node, inventory_node, protect_node,
54
+ deadzone_node
55
+ ].compact
53
56
  end
54
57
 
55
58
  def method_scopes
@@ -76,7 +79,8 @@ module Contrast
76
79
  protect_node = find_method_node(module_policy.protect_nodes, method_name, instance_method)
77
80
  inventory_node = find_method_node(module_policy.inventory_nodes, method_name, instance_method)
78
81
  deadzone_node = find_method_node(module_policy.deadzone_nodes, method_name, instance_method)
79
- method_visibility = find_visibility(source_node, propagation_node, trigger_node, protect_node, inventory_node, deadzone_node)
82
+ method_visibility = find_visibility(source_node, propagation_node, trigger_node, protect_node,
83
+ inventory_node, deadzone_node)
80
84
  MethodPolicy.new(method_name: method_name,
81
85
  method_visibility: method_visibility,
82
86
  instance_method: instance_method,
@@ -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'
@@ -20,12 +20,18 @@ module Contrast
20
20
  # @return [Contrast::Agent::Patching::Policy::ModulePolicy]
21
21
  def create_module_policy module_name
22
22
  module_policy = Contrast::Agent::Patching::Policy::ModulePolicy.new
23
- module_policy.source_nodes = nodes_for_module(Contrast::Agent::Assess::Policy::Policy.instance.sources, module_name)
24
- module_policy.propagator_nodes = nodes_for_module(Contrast::Agent::Assess::Policy::Policy.instance.propagators, module_name)
25
- module_policy.trigger_nodes = nodes_for_module(Contrast::Agent::Assess::Policy::Policy.instance.triggers, module_name)
26
- module_policy.protect_nodes = nodes_for_module(Contrast::Agent::Protect::Policy::Policy.instance.triggers, module_name)
27
- module_policy.inventory_nodes = nodes_for_module(Contrast::Agent::Inventory::Policy::Policy.instance.triggers, module_name)
28
- module_policy.deadzone_nodes = nodes_for_module(Contrast::Agent::Deadzone::Policy::Policy.instance.deadzones, module_name)
23
+ module_policy.source_nodes = nodes_for_module(
24
+ Contrast::Agent::Assess::Policy::Policy.instance.sources, module_name)
25
+ module_policy.propagator_nodes = nodes_for_module(
26
+ Contrast::Agent::Assess::Policy::Policy.instance.propagators, module_name)
27
+ module_policy.trigger_nodes = nodes_for_module(
28
+ Contrast::Agent::Assess::Policy::Policy.instance.triggers, module_name)
29
+ module_policy.protect_nodes = nodes_for_module(
30
+ Contrast::Agent::Protect::Policy::Policy.instance.triggers, module_name)
31
+ module_policy.inventory_nodes = nodes_for_module(
32
+ Contrast::Agent::Inventory::Policy::Policy.instance.triggers, module_name)
33
+ module_policy.deadzone_nodes = nodes_for_module(
34
+ Contrast::Agent::Deadzone::Policy::Policy.instance.deadzones, module_name)
29
35
  module_policy
30
36
  end
31
37
 
@@ -42,7 +48,8 @@ module Contrast
42
48
  end
43
49
  end
44
50
 
45
- attr_accessor :source_nodes, :propagator_nodes, :trigger_nodes, :inventory_nodes, :protect_nodes, :deadzone_nodes
51
+ attr_accessor :source_nodes, :propagator_nodes, :trigger_nodes, :inventory_nodes, :protect_nodes,
52
+ :deadzone_nodes
46
53
 
47
54
  def empty?
48
55
  return false if source_nodes.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 'monitor'
@@ -130,11 +130,7 @@ module Contrast
130
130
  return unless AGENT.enabled?
131
131
  return unless PROTECT.enabled?
132
132
 
133
- apply_trigger_only(method_policy&.protect_node,
134
- method,
135
- exception,
136
- object,
137
- args)
133
+ apply_trigger_only(method_policy&.protect_node, method, exception, object, args)
138
134
  end
139
135
 
140
136
  # Apply the Inventory patch which applies to the given method.
@@ -151,11 +147,7 @@ module Contrast
151
147
  def apply_inventory method_policy, method, exception, object, args
152
148
  return unless INVENTORY.enabled?
153
149
 
154
- apply_trigger_only(method_policy&.inventory_node,
155
- method,
156
- exception,
157
- object,
158
- args)
150
+ apply_trigger_only(method_policy&.inventory_node, method, exception, object, args)
159
151
  end
160
152
 
161
153
  # Apply the Assess patches which apply to the given method.
@@ -178,15 +170,18 @@ module Contrast
178
170
  return ret unless method_policy && ASSESS.enabled?
179
171
 
180
172
  current_context = Contrast::Agent::REQUEST_TRACKER.current
181
- return ret unless current_context.analyze_request?
173
+ return ret if current_context && !current_context.analyze_request?
182
174
 
183
175
  trigger_node = method_policy.trigger_node
184
- Contrast::Agent::Assess::Policy::TriggerMethod.apply_trigger_rule(trigger_node, object, ret, args) if trigger_node
176
+ if trigger_node
177
+ Contrast::Agent::Assess::Policy::TriggerMethod.apply_trigger_rule(trigger_node, object, ret, args)
178
+ end
185
179
  if method_policy.source_node
186
180
  # If we were given a frozen return, and it was the target of a
187
181
  # source, and we have frozen sources enabled, we'll need to
188
182
  # replace the return. Note, this is not the default case.
189
- source_ret = Contrast::Agent::Assess::Policy::SourceMethod.source_patchers(method_policy, object, ret, args)
183
+ source_ret = Contrast::Agent::Assess::Policy::SourceMethod.source_patchers(method_policy, object, ret,
184
+ args)
190
185
  end
191
186
  if method_policy.propagation_node
192
187
  propagated_ret = Contrast::Agent::Assess::Policy::PropagationMethod.apply_propagation(
@@ -277,9 +272,9 @@ module Contrast
277
272
  # <method_start>_unbound_<method_name>
278
273
  def build_unbound_method_name patcher_method
279
274
  (Contrast::Utils::ObjectShare::CONTRAST_PATCHED_METHOD_START +
280
- 'unbound' +
281
- Contrast::Utils::ObjectShare::UNDERSCORE +
282
- patcher_method.to_s).to_sym
275
+ 'unbound' +
276
+ Contrast::Utils::ObjectShare::UNDERSCORE +
277
+ patcher_method.to_s).to_sym
283
278
  end
284
279
 
285
280
  # @param mod [Module] the module in which the patch should be
@@ -339,7 +334,7 @@ module Contrast
339
334
  # @return [Symbol] new alias for the underlying method (presumably, so the patched method can call it)
340
335
  def register_c_patch target_module_name, unbound_method, impl = :alias_instance
341
336
  # These could be set as AfterLoadPatches.
342
- method_name = unbound_method.name.to_sym
337
+ method_name = unbound_method.name.to_sym # rubocop:disable Security/Module/Name -- ruby built in attribute.
343
338
  underlying_method_name = build_unbound_method_name(method_name).to_sym
344
339
 
345
340
  target_module = Module.cs__const_get(target_module_name)
@@ -362,29 +357,27 @@ module Contrast
362
357
 
363
358
  case impl
364
359
  when :alias_instance, :alias_singleton
360
+ # Core to patching. Ignore define method usage cop.
361
+ # rubocop:disable Performance/Kernel/DefineMethod
365
362
  unless target_module.instance_methods(false).include? underlying_method_name
366
363
  # alias_method may be private
367
364
  target_module.send(:alias_method, underlying_method_name, method_name)
368
- # TODO: RUBY-1052
369
- # rubocop:disable Performance/Kernel/DefineMethod
370
365
  target_module.send(:define_method, method_name, unbound_method.bind(target_module))
371
- # rubocop:enable Performance/Kernel/DefineMethod
372
366
  end
373
367
  target_module.send(visibility, method_name) # e.g., module.private(:my_method)
374
368
  when :prepend
375
369
  prepending_module = Module.new
376
- # TODO: RUBY-1052
377
- # rubocop:disable Performance/Kernel/DefineMethod
378
370
  prepending_module.send(:define_method, method_name, unbound_method.bind(target_module))
379
- # rubocop:enable Performance/Kernel/DefineMethod
380
371
  prepending_module.send(visibility, method_name)
381
372
  # This prepends to the singleton class (it patches a class method)
382
373
  target_module.prepend prepending_module
374
+ # rubocop:enable Performance/Kernel/DefineMethod
383
375
  end
384
376
  # Ougai::Logger.create_item_with_2args calls Hash#[]=, so we
385
377
  # can't invoke this logging method or we'll seg fault as we'd
386
378
  # change the method definition mid-call
387
- # if method_name != :[]=
379
+ # if method_name != :[]= &&
380
+ # Contrast::Agent::Logger.defined!
388
381
  # logger.trace(
389
382
  # 'Registered C-defined patch',
390
383
  # implementation: impl,
@@ -392,14 +385,15 @@ module Contrast
392
385
  # method: method_name,
393
386
  # visibility: visibility)
394
387
  # end
395
- underlying_method_name.to_sym
388
+ underlying_method_name
396
389
  end
397
390
 
398
391
  # @return [Boolean]
399
392
  def skip_contrast_analysis?
400
393
  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?
394
+ return false unless defined?(Contrast::Agent::REQUEST_TRACKER)
395
+ return false unless Contrast::Agent::REQUEST_TRACKER.current
396
+ return true unless Contrast::Agent::REQUEST_TRACKER.current.analyze_request?
403
397
 
404
398
  false
405
399
  end
@@ -411,7 +405,7 @@ module Contrast
411
405
  def skip_assess_analysis?
412
406
  return true if skip_contrast_analysis?
413
407
 
414
- !ASSESS.enabled?
408
+ !ASSESS&.enabled?
415
409
  end
416
410
  end
417
411
  end