contrast-agent 4.4.1 → 4.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (314) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +6 -1
  3. data/.gitmodules +1 -1
  4. data/.simplecov +2 -1
  5. data/Gemfile +1 -1
  6. data/LICENSE.txt +1 -1
  7. data/Rakefile +2 -3
  8. data/exe/contrast_service +1 -1
  9. data/ext/build_funchook.rb +4 -4
  10. data/ext/cs__assess_active_record_named/cs__active_record_named.c +1 -1
  11. data/ext/cs__assess_active_record_named/extconf.rb +1 -1
  12. data/ext/cs__assess_array/cs__assess_array.c +1 -1
  13. data/ext/cs__assess_array/extconf.rb +1 -1
  14. data/ext/cs__assess_basic_object/cs__assess_basic_object.c +1 -1
  15. data/ext/cs__assess_basic_object/extconf.rb +1 -1
  16. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.c +1 -1
  17. data/ext/cs__assess_fiber_track/extconf.rb +1 -1
  18. data/ext/cs__assess_hash/cs__assess_hash.c +4 -2
  19. data/ext/cs__assess_hash/extconf.rb +1 -1
  20. data/ext/cs__assess_kernel/cs__assess_kernel.c +1 -1
  21. data/ext/cs__assess_kernel/extconf.rb +1 -1
  22. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +1 -1
  23. data/ext/cs__assess_marshal_module/extconf.rb +1 -1
  24. data/ext/cs__assess_module/cs__assess_module.c +1 -1
  25. data/ext/cs__assess_module/extconf.rb +1 -1
  26. data/ext/cs__assess_regexp/cs__assess_regexp.c +1 -1
  27. data/ext/cs__assess_regexp/extconf.rb +1 -1
  28. data/ext/cs__assess_string/cs__assess_string.c +1 -1
  29. data/ext/cs__assess_string/extconf.rb +1 -1
  30. data/ext/cs__assess_string_interpolation26/cs__assess_string_interpolation26.c +1 -1
  31. data/ext/cs__assess_string_interpolation26/extconf.rb +1 -1
  32. data/ext/cs__assess_yield_track/cs__assess_yield_track.c +1 -1
  33. data/ext/cs__assess_yield_track/extconf.rb +1 -1
  34. data/ext/cs__common/cs__common.c +5 -5
  35. data/ext/cs__common/cs__common.h +4 -4
  36. data/ext/cs__common/extconf.rb +1 -1
  37. data/ext/cs__contrast_patch/cs__contrast_patch.c +22 -25
  38. data/ext/cs__contrast_patch/extconf.rb +1 -1
  39. data/ext/cs__protect_kernel/cs__protect_kernel.c +1 -1
  40. data/ext/cs__protect_kernel/extconf.rb +1 -1
  41. data/ext/extconf_common.rb +2 -6
  42. data/lib/contrast-agent.rb +1 -1
  43. data/lib/contrast.rb +44 -15
  44. data/lib/contrast/agent.rb +1 -3
  45. data/lib/contrast/agent/assess.rb +2 -2
  46. data/lib/contrast/agent/assess/contrast_event.rb +54 -72
  47. data/lib/contrast/agent/assess/contrast_object.rb +3 -3
  48. data/lib/contrast/agent/assess/events/event_factory.rb +3 -2
  49. data/lib/contrast/agent/assess/events/source_event.rb +7 -2
  50. data/lib/contrast/agent/assess/finalizers/freeze.rb +1 -1
  51. data/lib/contrast/agent/assess/finalizers/hash.rb +28 -38
  52. data/lib/contrast/agent/assess/policy/dynamic_source_factory.rb +24 -20
  53. data/lib/contrast/agent/assess/policy/patcher.rb +17 -22
  54. data/lib/contrast/agent/assess/policy/policy.rb +2 -2
  55. data/lib/contrast/agent/assess/policy/policy_node.rb +26 -34
  56. data/lib/contrast/agent/assess/policy/policy_scanner.rb +4 -6
  57. data/lib/contrast/agent/assess/policy/preshift.rb +8 -6
  58. data/lib/contrast/agent/assess/policy/propagation_method.rb +12 -25
  59. data/lib/contrast/agent/assess/policy/propagation_node.rb +20 -9
  60. data/lib/contrast/agent/assess/policy/propagator.rb +2 -1
  61. data/lib/contrast/agent/assess/policy/propagator/append.rb +1 -1
  62. data/lib/contrast/agent/assess/policy/propagator/base.rb +1 -1
  63. data/lib/contrast/agent/assess/policy/propagator/center.rb +3 -2
  64. data/lib/contrast/agent/assess/policy/propagator/custom.rb +1 -1
  65. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +4 -7
  66. data/lib/contrast/agent/assess/policy/propagator/insert.rb +4 -2
  67. data/lib/contrast/agent/assess/policy/propagator/keep.rb +1 -1
  68. data/lib/contrast/agent/assess/policy/propagator/match_data.rb +3 -2
  69. data/lib/contrast/agent/assess/policy/propagator/next.rb +1 -1
  70. data/lib/contrast/agent/assess/policy/propagator/prepend.rb +1 -1
  71. data/lib/contrast/agent/assess/policy/propagator/rack_protection.rb +73 -0
  72. data/lib/contrast/agent/assess/policy/propagator/remove.rb +23 -19
  73. data/lib/contrast/agent/assess/policy/propagator/replace.rb +1 -1
  74. data/lib/contrast/agent/assess/policy/propagator/reverse.rb +1 -1
  75. data/lib/contrast/agent/assess/policy/propagator/select.rb +3 -13
  76. data/lib/contrast/agent/assess/policy/propagator/splat.rb +1 -1
  77. data/lib/contrast/agent/assess/policy/propagator/split.rb +13 -14
  78. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +4 -11
  79. data/lib/contrast/agent/assess/policy/propagator/trim.rb +64 -45
  80. data/lib/contrast/agent/assess/policy/rewriter_patch.rb +14 -11
  81. data/lib/contrast/agent/assess/policy/source_method.rb +97 -86
  82. data/lib/contrast/agent/assess/policy/source_node.rb +1 -1
  83. data/lib/contrast/agent/assess/policy/source_validation/cross_site_validator.rb +8 -6
  84. data/lib/contrast/agent/assess/policy/source_validation/source_validation.rb +2 -4
  85. data/lib/contrast/agent/assess/policy/trigger/reflected_xss.rb +7 -3
  86. data/lib/contrast/agent/assess/policy/trigger/xpath.rb +7 -11
  87. data/lib/contrast/agent/assess/policy/trigger_method.rb +104 -77
  88. data/lib/contrast/agent/assess/policy/trigger_node.rb +6 -5
  89. data/lib/contrast/agent/assess/policy/trigger_validation/redos_validator.rb +5 -4
  90. data/lib/contrast/agent/assess/policy/trigger_validation/ssrf_validator.rb +2 -3
  91. data/lib/contrast/agent/assess/policy/trigger_validation/trigger_validation.rb +1 -1
  92. data/lib/contrast/agent/assess/policy/trigger_validation/xss_validator.rb +2 -9
  93. data/lib/contrast/agent/assess/properties.rb +1 -1
  94. data/lib/contrast/agent/assess/property/evented.rb +9 -6
  95. data/lib/contrast/agent/assess/property/tagged.rb +1 -1
  96. data/lib/contrast/agent/assess/property/updated.rb +1 -1
  97. data/lib/contrast/agent/assess/rule/provider.rb +1 -1
  98. data/lib/contrast/agent/assess/rule/provider/hardcoded_key.rb +12 -6
  99. data/lib/contrast/agent/assess/rule/provider/hardcoded_password.rb +5 -2
  100. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +8 -10
  101. data/lib/contrast/agent/assess/tag.rb +1 -1
  102. data/lib/contrast/agent/assess/tracker.rb +1 -1
  103. data/lib/contrast/agent/at_exit_hook.rb +4 -4
  104. data/lib/contrast/agent/class_reopener.rb +10 -7
  105. data/lib/contrast/agent/deadzone/policy/deadzone_node.rb +1 -1
  106. data/lib/contrast/agent/deadzone/policy/policy.rb +7 -3
  107. data/lib/contrast/agent/disable_reaction.rb +5 -8
  108. data/lib/contrast/agent/exclusion_matcher.rb +8 -15
  109. data/lib/contrast/agent/inventory.rb +1 -2
  110. data/lib/contrast/agent/inventory/dependencies.rb +3 -1
  111. data/lib/contrast/agent/inventory/dependency_analysis.rb +3 -7
  112. data/lib/contrast/agent/inventory/dependency_usage_analysis.rb +38 -28
  113. data/lib/contrast/agent/inventory/policy/datastores.rb +4 -5
  114. data/lib/contrast/agent/inventory/policy/policy.rb +2 -2
  115. data/lib/contrast/agent/inventory/policy/trigger_node.rb +1 -1
  116. data/lib/contrast/agent/middleware.rb +52 -80
  117. data/lib/contrast/agent/module_data.rb +4 -4
  118. data/lib/contrast/agent/patching/policy/after_load_patch.rb +4 -4
  119. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +10 -10
  120. data/lib/contrast/agent/patching/policy/method_policy.rb +7 -3
  121. data/lib/contrast/agent/patching/policy/module_policy.rb +15 -8
  122. data/lib/contrast/agent/patching/policy/patch.rb +32 -38
  123. data/lib/contrast/agent/patching/policy/patch_status.rb +7 -8
  124. data/lib/contrast/agent/patching/policy/patcher.rb +29 -28
  125. data/lib/contrast/agent/patching/policy/policy.rb +16 -25
  126. data/lib/contrast/agent/patching/policy/policy_node.rb +17 -8
  127. data/lib/contrast/agent/patching/policy/trigger_node.rb +22 -9
  128. data/lib/contrast/agent/protect/policy/applies_command_injection_rule.rb +2 -2
  129. data/lib/contrast/agent/protect/policy/applies_deserialization_rule.rb +2 -2
  130. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +2 -2
  131. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +3 -4
  132. data/lib/contrast/agent/protect/policy/applies_sqli_rule.rb +2 -2
  133. data/lib/contrast/agent/protect/policy/applies_xxe_rule.rb +6 -10
  134. data/lib/contrast/agent/protect/policy/policy.rb +2 -2
  135. data/lib/contrast/agent/protect/policy/rule_applicator.rb +8 -10
  136. data/lib/contrast/agent/protect/policy/trigger_node.rb +1 -1
  137. data/lib/contrast/agent/protect/rule.rb +1 -1
  138. data/lib/contrast/agent/protect/rule/base.rb +26 -40
  139. data/lib/contrast/agent/protect/rule/base_service.rb +10 -6
  140. data/lib/contrast/agent/protect/rule/cmd_injection.rb +19 -24
  141. data/lib/contrast/agent/protect/rule/default_scanner.rb +1 -1
  142. data/lib/contrast/agent/protect/rule/deserialization.rb +7 -14
  143. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +4 -15
  144. data/lib/contrast/agent/protect/rule/no_sqli.rb +7 -3
  145. data/lib/contrast/agent/protect/rule/no_sqli/mongo_no_sql_scanner.rb +2 -4
  146. data/lib/contrast/agent/protect/rule/path_traversal.rb +7 -11
  147. data/lib/contrast/agent/protect/rule/sqli.rb +3 -3
  148. data/lib/contrast/agent/protect/rule/sqli/default_sql_scanner.rb +1 -1
  149. data/lib/contrast/agent/protect/rule/sqli/mysql_sql_scanner.rb +1 -1
  150. data/lib/contrast/agent/protect/rule/sqli/postgres_sql_scanner.rb +2 -2
  151. data/lib/contrast/agent/protect/rule/sqli/sqlite_sql_scanner.rb +1 -1
  152. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +2 -2
  153. data/lib/contrast/agent/protect/rule/xss.rb +2 -2
  154. data/lib/contrast/agent/protect/rule/xxe.rb +6 -13
  155. data/lib/contrast/agent/protect/rule/xxe/entity_wrapper.rb +2 -3
  156. data/lib/contrast/agent/reaction_processor.rb +14 -14
  157. data/lib/contrast/agent/request.rb +29 -27
  158. data/lib/contrast/agent/request_context.rb +20 -30
  159. data/lib/contrast/agent/request_handler.rb +6 -4
  160. data/lib/contrast/agent/response.rb +3 -4
  161. data/lib/contrast/agent/rewriter.rb +10 -7
  162. data/lib/contrast/agent/rule_set.rb +6 -5
  163. data/lib/contrast/agent/scope.rb +1 -1
  164. data/lib/contrast/agent/service_heartbeat.rb +5 -7
  165. data/lib/contrast/agent/static_analysis.rb +7 -6
  166. data/lib/contrast/agent/thread.rb +3 -5
  167. data/lib/contrast/agent/thread_watcher.rb +4 -5
  168. data/lib/contrast/agent/tracepoint_hook.rb +6 -6
  169. data/lib/contrast/agent/version.rb +2 -2
  170. data/lib/contrast/agent/worker_thread.rb +1 -1
  171. data/lib/contrast/api.rb +1 -1
  172. data/lib/contrast/api/communication.rb +1 -1
  173. data/lib/contrast/api/communication/connection_status.rb +1 -1
  174. data/lib/contrast/api/communication/messaging_queue.rb +5 -6
  175. data/lib/contrast/api/communication/response_processor.rb +13 -15
  176. data/lib/contrast/api/communication/service_lifecycle.rb +10 -7
  177. data/lib/contrast/api/communication/socket.rb +1 -1
  178. data/lib/contrast/api/communication/socket_client.rb +23 -32
  179. data/lib/contrast/api/communication/speedracer.rb +10 -15
  180. data/lib/contrast/api/communication/tcp_socket.rb +1 -1
  181. data/lib/contrast/api/communication/unix_socket.rb +1 -1
  182. data/lib/contrast/api/decorators.rb +1 -1
  183. data/lib/contrast/api/decorators/address.rb +3 -4
  184. data/lib/contrast/api/decorators/agent_startup.rb +8 -10
  185. data/lib/contrast/api/decorators/application_settings.rb +1 -1
  186. data/lib/contrast/api/decorators/application_startup.rb +14 -10
  187. data/lib/contrast/api/decorators/application_update.rb +1 -5
  188. data/lib/contrast/api/decorators/http_request.rb +4 -8
  189. data/lib/contrast/api/decorators/input_analysis.rb +1 -1
  190. data/lib/contrast/api/decorators/instrumentation_mode.rb +35 -0
  191. data/lib/contrast/api/decorators/library.rb +9 -7
  192. data/lib/contrast/api/decorators/library_usage_update.rb +1 -1
  193. data/lib/contrast/api/decorators/message.rb +10 -10
  194. data/lib/contrast/api/decorators/rasp_rule_sample.rb +1 -1
  195. data/lib/contrast/api/decorators/route_coverage.rb +1 -1
  196. data/lib/contrast/api/decorators/server_features.rb +1 -1
  197. data/lib/contrast/api/decorators/trace_event.rb +4 -2
  198. data/lib/contrast/api/decorators/trace_event_object.rb +4 -7
  199. data/lib/contrast/api/decorators/trace_event_signature.rb +1 -1
  200. data/lib/contrast/api/decorators/trace_taint_range.rb +1 -1
  201. data/lib/contrast/api/decorators/trace_taint_range_tags.rb +2 -7
  202. data/lib/contrast/api/decorators/user_input.rb +1 -1
  203. data/lib/contrast/components/agent.rb +20 -26
  204. data/lib/contrast/components/app_context.rb +12 -16
  205. data/lib/contrast/components/assess.rb +20 -25
  206. data/lib/contrast/components/base.rb +40 -0
  207. data/lib/contrast/components/config.rb +3 -4
  208. data/lib/contrast/components/contrast_service.rb +13 -19
  209. data/lib/contrast/components/heap_dump.rb +6 -5
  210. data/lib/contrast/components/inventory.rb +3 -8
  211. data/lib/contrast/components/logger.rb +2 -3
  212. data/lib/contrast/components/protect.rb +14 -20
  213. data/lib/contrast/components/sampling.rb +14 -8
  214. data/lib/contrast/components/scope.rb +2 -5
  215. data/lib/contrast/components/settings.rb +28 -103
  216. data/lib/contrast/config.rb +1 -1
  217. data/lib/contrast/config/agent_configuration.rb +1 -1
  218. data/lib/contrast/config/application_configuration.rb +1 -1
  219. data/lib/contrast/config/assess_configuration.rb +1 -1
  220. data/lib/contrast/config/assess_rules_configuration.rb +2 -4
  221. data/lib/contrast/config/base_configuration.rb +5 -6
  222. data/lib/contrast/config/default_value.rb +1 -1
  223. data/lib/contrast/config/exception_configuration.rb +2 -6
  224. data/lib/contrast/config/heap_dump_configuration.rb +13 -7
  225. data/lib/contrast/config/inventory_configuration.rb +1 -1
  226. data/lib/contrast/config/logger_configuration.rb +2 -6
  227. data/lib/contrast/config/protect_configuration.rb +1 -1
  228. data/lib/contrast/config/protect_rule_configuration.rb +23 -1
  229. data/lib/contrast/config/protect_rules_configuration.rb +1 -1
  230. data/lib/contrast/config/root_configuration.rb +1 -1
  231. data/lib/contrast/config/ruby_configuration.rb +1 -1
  232. data/lib/contrast/config/sampling_configuration.rb +1 -1
  233. data/lib/contrast/config/server_configuration.rb +1 -1
  234. data/lib/contrast/config/service_configuration.rb +1 -1
  235. data/lib/contrast/configuration.rb +7 -19
  236. data/lib/contrast/extension/assess.rb +1 -1
  237. data/lib/contrast/extension/assess/array.rb +4 -11
  238. data/lib/contrast/extension/assess/erb.rb +2 -8
  239. data/lib/contrast/extension/assess/eval_trigger.rb +5 -14
  240. data/lib/contrast/extension/assess/exec_trigger.rb +4 -14
  241. data/lib/contrast/extension/assess/fiber.rb +9 -18
  242. data/lib/contrast/extension/assess/hash.rb +4 -4
  243. data/lib/contrast/extension/assess/kernel.rb +5 -14
  244. data/lib/contrast/extension/assess/marshal.rb +7 -15
  245. data/lib/contrast/extension/assess/regexp.rb +7 -11
  246. data/lib/contrast/extension/assess/string.rb +9 -7
  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 +3 -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 -6
  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 +7 -12
  257. data/lib/contrast/framework/platform_version.rb +1 -1
  258. data/lib/contrast/framework/rack/patch/session_cookie.rb +12 -25
  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 +13 -10
  263. data/lib/contrast/framework/rails/patch/rails_application_configuration.rb +4 -4
  264. data/lib/contrast/framework/rails/patch/support.rb +42 -36
  265. data/lib/contrast/framework/rails/railtie.rb +34 -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 +6 -5
  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 +6 -9
  273. data/lib/contrast/logger/application.rb +14 -16
  274. data/lib/contrast/logger/format.rb +3 -6
  275. data/lib/contrast/logger/log.rb +27 -10
  276. data/lib/contrast/logger/request.rb +2 -7
  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 +7 -8
  281. data/lib/contrast/utils/assess/sampling_util.rb +3 -4
  282. data/lib/contrast/utils/assess/tracking_util.rb +4 -7
  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 +6 -4
  288. data/lib/contrast/utils/invalid_configuration_util.rb +5 -4
  289. data/lib/contrast/utils/inventory_util.rb +3 -4
  290. data/lib/contrast/utils/io_util.rb +4 -6
  291. data/lib/contrast/utils/job_servers_running.rb +14 -8
  292. data/lib/contrast/utils/object_share.rb +1 -1
  293. data/lib/contrast/utils/os.rb +5 -5
  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 +3 -4
  300. data/lib/contrast/utils/tag_util.rb +26 -20
  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 -17
  305. data/ruby-agent.gemspec +25 -21
  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 +108 -51
  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
  314. data/lib/contrast/components/interface.rb +0 -195
@@ -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
@@ -10,6 +10,5 @@ module Contrast
10
10
  end
11
11
 
12
12
  require 'contrast/agent/inventory/dependencies'
13
- require 'contrast/agent/inventory/gemfile_digest_cache'
14
13
  require 'contrast/agent/inventory/dependency_usage_analysis'
15
14
  require 'contrast/agent/inventory/dependency_analysis'
@@ -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
@@ -33,6 +33,7 @@ module Contrast
33
33
  # then contrast depends on it. If its array of dependents is 1, then contrast is the
34
34
  # only dependency in that list. Since only contrast depends on it, we should ignore it.
35
35
  def find_contrast_gems
36
+ # rubocop:disable Security/Module/Name -- here name is part of Ruby Gems.
36
37
  ignore = Set.new([CONTRAST_AGENT])
37
38
  contrast_specs = Gem::DependencyList.from_specs.specs.find do |dependency|
38
39
  dependency.name == CONTRAST_AGENT
@@ -43,6 +44,7 @@ module Contrast
43
44
  ignore.add(dependency.name) if contrast_dep_set.include?(dependency.name) && dependents.length == 1
44
45
  end
45
46
  ignore
47
+ # rubocop:enable Security/Module/Name
46
48
  end
47
49
  end
48
50
  end
@@ -1,8 +1,7 @@
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/dependencies'
5
- require 'contrast/components/interface'
6
5
  require 'contrast/utils/object_share'
7
6
 
8
7
  module Contrast
@@ -12,17 +11,14 @@ module Contrast
12
11
  class DependencyAnalysis
13
12
  include Singleton
14
13
  include Contrast::Agent::Inventory::Dependencies
15
- include Contrast::Components::Interface
16
-
17
- access_component :analysis
18
14
 
19
15
  # Report the dependencies of this application
20
16
  #
21
17
  # @return [Array<Contrast::Api::Dtm::Library>] protobuf form of the
22
18
  # Gem::Specification that have been loaded for this application.
23
19
  def library_pb_list
24
- return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless INVENTORY.enabled?
25
- return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless INVENTORY.analyze_libraries?
20
+ return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless ::Contrast::INVENTORY.enabled?
21
+ return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless ::Contrast::INVENTORY.analyze_libraries?
26
22
 
27
23
  loaded_specs.each_with_object([]) do |(_name, spec), reported_lib_list|
28
24
  next unless spec
@@ -1,10 +1,10 @@
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
- require 'contrast/agent/inventory/gemfile_digest_cache'
5
4
  require 'contrast/agent/inventory/dependencies'
6
- require 'contrast/components/interface'
5
+ require 'contrast/components/logger'
7
6
  require 'contrast/utils/object_share'
7
+ require 'set'
8
8
 
9
9
  module Contrast
10
10
  module Agent
@@ -12,41 +12,41 @@ module Contrast
12
12
  # Used to analyze class usage for reporting
13
13
  class DependencyUsageAnalysis
14
14
  include Singleton
15
- include Contrast::Components::Interface
15
+ include Contrast::Components::Logger::InstanceMethods
16
16
  include Contrast::Agent::Inventory::Dependencies
17
17
 
18
- access_component :analysis, :config, :logging
19
-
20
18
  def initialize
21
19
  return unless enabled?
22
20
 
23
- @gemdigest_cache = Contrast::Agent::Inventory::GemfileDigestCache.new
21
+ @lock = Mutex.new
22
+ @lock.synchronize { @gemdigest_cache = Hash.new { |hash, key| hash[key] = Set.new } }
24
23
  end
25
24
 
26
- # This method is invoked once, along with the rest of our catchup code
27
- # to report libraries and their associated files that have already been loaded pre-contrast
25
+ # This method is invoked once, along with the rest of our catchup code to report libraries and their associated
26
+ # files that have already been loaded pre-contrast.
28
27
  def catchup
29
28
  return unless enabled?
30
29
 
31
30
  loaded_specs.each do |_name, spec|
32
- # Get a digest of the Gem file itself
31
+ # Get a digest of the Gem file itself.
33
32
  next unless (digest = Contrast::Utils::Sha256Builder.instance.build_from_spec(spec))
34
33
 
35
- @gemdigest_cache.use_cache(digest) do |existing_files|
36
- loaded_files_from_gem = $LOADED_FEATURES.select { |f| f.start_with?(spec.full_gem_path) }
37
- loaded_files_from_gem.each do |file_path|
38
- logger.trace('Recording loaded file for inventory analysis', line: file_path)
39
- existing_files << adjust_path_for_reporting(file_path, spec)
40
- end
34
+ loaded_files_from_gem = $LOADED_FEATURES.select { |f| f.start_with?(spec.full_gem_path) }
35
+
36
+ new_files = loaded_files_from_gem.each_with_object(Set.new) do |file_path, set|
37
+ set << adjust_path_for_reporting(file_path, spec)
38
+ logger.trace('Recording loaded file for inventory analysis', line: file_path)
41
39
  end
40
+
41
+ # Even if new_files is empty, still need to add digest key for library discovery.
42
+ @lock.synchronize { @gemdigest_cache[digest].merge(new_files) }
42
43
  end
43
44
  end
44
45
 
45
- # This method is invoked once per TracePoint :end - to map a specific
46
- # file being required to the gem it belongs to
46
+ # This method is invoked once per TracePoint :end - to map a specific file being required to the gem to which
47
+ # it belongs.
47
48
  #
48
- # @param path [String] the result of TracePoint#path from the :end
49
- # event in which the Module was defined
49
+ # @param path [String] the result of TracePoint#path from the :end event in which the Module was defined.
50
50
  def associate_file path
51
51
  return unless enabled?
52
52
 
@@ -64,21 +64,31 @@ module Contrast
64
64
  return
65
65
  end
66
66
  report_path = adjust_path_for_reporting(path, spec)
67
- @gemdigest_cache.get(digest) << report_path
67
+ @lock.synchronize { @gemdigest_cache[digest] << report_path }
68
68
  rescue StandardError => e
69
69
  logger.error('Unable to inventory file path', e, path: path)
70
70
  end
71
71
 
72
- # Populate the library_usages filed of the Activity message using the
73
- # data stored in the @gemdigest_cache
72
+ # Populate the library_usages field of the Activity message using the data stored in the @gemdigest_cache.
74
73
  #
75
- # @param activity [Contrast::Api::Dtm::Activity] the message to which
76
- # to append the usage data
74
+ # @param activity [Contrast::Api::Dtm::Activity] the message to which to append the usage data
77
75
  def generate_library_usage activity
78
76
  return unless enabled?
79
- return if @gemdigest_cache.empty?
77
+ return unless activity
80
78
 
81
- @gemdigest_cache.generate_usage_data(activity)
79
+ # Copy gemdigest_cache and clear it in sync.
80
+ gem_spec_digest_to_files = @lock.synchronize do
81
+ copy = @gemdigest_cache.dup
82
+ @gemdigest_cache.clear
83
+ copy
84
+ end
85
+
86
+ gem_spec_digest_to_files.each_pair do |digest, files|
87
+ usage = Contrast::Api::Dtm::LibraryUsageUpdate.build(digest, files)
88
+ activity.library_usages[usage.hash_code] = usage
89
+ end
90
+ rescue StandardError => e
91
+ logger.error('Unable to generate library usage.', e)
82
92
  end
83
93
 
84
94
  private
@@ -95,7 +105,7 @@ module Contrast
95
105
 
96
106
  # We only use this if inventory and library analysis are enabled
97
107
  def enabled?
98
- @_enabled = INVENTORY.enabled? && INVENTORY.analyze_libraries? if @_enabled.nil?
108
+ @_enabled = ::Contrast::INVENTORY.enabled? && ::Contrast::INVENTORY.analyze_libraries? if @_enabled.nil?
99
109
  @_enabled
100
110
  end
101
111
  end
@@ -1,7 +1,7 @@
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
- require 'contrast/components/interface'
4
+ require 'contrast/components/logger'
5
5
  require 'contrast/utils/inventory_util'
6
6
 
7
7
  module Contrast
@@ -14,15 +14,14 @@ module Contrast
14
14
  # the given invocation is worth reporting or not.
15
15
  module DataStores
16
16
  class << self
17
- include Contrast::Components::Interface
17
+ extend Contrast::Components::Logger::InstanceMethods
18
18
 
19
- access_component :analysis, :logging
20
19
  # The key used in policy.json to indicate the database type to
21
20
  # report.
22
21
  DATA_STORE_MARKER = 'data_store'
23
22
 
24
23
  def report_data_store _method, _exception, properties, object, _args
25
- return unless INVENTORY.enabled?
24
+ return unless ::Contrast::INVENTORY.enabled?
26
25
 
27
26
  marker = properties[DATA_STORE_MARKER]
28
27
  return unless marker
@@ -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'
@@ -19,7 +19,7 @@ module Contrast
19
19
  end
20
20
 
21
21
  def disabled_globally?
22
- !INVENTORY.enabled?
22
+ !::Contrast::INVENTORY.enabled?
23
23
  end
24
24
 
25
25
  def node_type
@@ -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'
@@ -7,7 +7,8 @@ require 'rack'
7
7
 
8
8
  require 'contrast/security_exception'
9
9
  require 'contrast/utils/object_share'
10
- require 'contrast/components/interface'
10
+ require 'contrast/components/logger'
11
+ require 'contrast/components/scope'
11
12
  require 'contrast/utils/heap_dump_util'
12
13
  require 'contrast/agent/request_handler'
13
14
  require 'contrast/agent/static_analysis'
@@ -16,18 +17,17 @@ require 'contrast/utils/timer'
16
17
 
17
18
  module Contrast
18
19
  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.
20
+ # This class allows the Agent to plug into the Rack middleware stack. When the application is first started, we
21
+ # initialize ourselves as a rack middleware inside of #initialize. Afterwards, we process each http request and
22
+ # response as it goes through the middleware stack inside of #call.
23
23
  class Middleware
24
- include Contrast::Components::Interface
25
- access_component :agent, :config, :logging, :scope, :settings
24
+ include Contrast::Components::Logger::InstanceMethods
25
+ include Contrast::Components::Scope::InstanceMethods
26
26
 
27
27
  attr_reader :app
28
28
 
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:
29
+ # Allows the Agent to function as a middleware. We perform all our one-time whole-app routines in here since
30
+ # we're only going to be initialized a single time. Our initialization order is:
31
31
  # - capture the application
32
32
  # - setup the Agent
33
33
  # - startup the Agent
@@ -37,25 +37,24 @@ module Contrast
37
37
  def initialize app, _legacy_param = nil
38
38
  @app = app # THIS MUST BE FIRST AND ALWAYS SET!
39
39
  setup_agent # THIS MUST BE SECOND AND ALWAYS CALLED!
40
- unless AGENT.enabled?
41
- logger.error(
42
- 'The Agent was unable to initialize before the application middleware was initialized. Disabling permanently.')
43
- AGENT.disable! # ensure the agent is disabled (probably redundant)
40
+ unless ::Contrast::AGENT.enabled?
41
+ logger.error('The Agent was unable to initialize before the application middleware was initialized. '\
42
+ 'Disabling permanently.')
43
+ ::Contrast::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
- # 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.
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.
52
51
  #
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.
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.
57
56
  def call env
58
- return app.call(env) unless AGENT.enabled?
57
+ return app.call(env) unless ::Contrast::AGENT.enabled?
59
58
 
60
59
  Contrast::Agent.heapdump_util.start_thread!
61
60
  handle_first_request
@@ -65,28 +64,25 @@ module Contrast
65
64
  private
66
65
 
67
66
  def setup_agent
68
- SETTINGS.reset_state
67
+ ::Contrast::SETTINGS.reset_state
69
68
 
70
69
  inform_deprecations
71
70
 
72
- if CONFIG.invalid?
73
- AGENT.disable!
71
+ if ::Contrast::CONFIG.invalid?
72
+ ::Contrast::AGENT.disable!
74
73
  logger.error('!!! CONFIG FILE IS INVALID - DISABLING CONTRAST AGENT !!!')
75
- elsif AGENT.disabled?
74
+ elsif ::Contrast::AGENT.disabled?
76
75
  logger.warn('Contrast disabled by configuration. Continuing without instrumentation.')
77
76
  else
78
- AGENT.enable!
77
+ ::Contrast::AGENT.enable!
79
78
  end
80
79
  end
81
80
 
82
81
  # Startup the Agent as part of the initialization process:
83
- # - start the service sending thread, responsible for sending and
84
- # processing messages
82
+ # - start the service sending thread, responsible for sending and processing messages
85
83
  # - 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
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
90
86
  def agent_startup_routine
91
87
  logger.debug_with_time('middleware: starting service') do
92
88
  Contrast::Agent.thread_watcher.ensure_running?
@@ -97,54 +93,34 @@ module Contrast
97
93
  end
98
94
 
99
95
  logger.debug_with_time('middleware: enabling tracepoint') do
100
- AGENT.enable_tracepoint
96
+ ::Contrast::AGENT.enable_tracepoint
101
97
  end
102
98
  Contrast::Agent::AtExitHook.exit_hook
103
99
  end
104
100
 
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.
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.
108
103
  def handle_first_request
109
104
  @_handle_first_request ||= begin
110
105
  Contrast::Agent::StaticAnalysis.catchup
111
- force_patching
112
106
  true
113
107
  end
114
108
  end
115
109
 
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.
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.
141
117
  def call_with_agent env
142
118
  Contrast::Agent.thread_watcher.ensure_running?
143
119
  framework_request = Contrast::Agent.framework_manager.retrieve_request(env)
144
120
  context = Contrast::Agent::RequestContext.new(framework_request)
145
121
  response = nil
146
122
 
147
- # make the context available for the lifecycle of this request
123
+ # Make the context available for the lifecycle of this request.
148
124
  Contrast::Agent::REQUEST_TRACKER.lifespan(context) do
149
125
  logger.request_start
150
126
  request_handler = Contrast::Agent::RequestHandler.new(context)
@@ -212,11 +188,11 @@ module Contrast
212
188
  end
213
189
 
214
190
  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
191
+ # We're only going to suppress SecurityExceptions indicating a blocked attack. And, only if the
192
+ # config.agent.ruby.exceptions.capture? is set
217
193
  def handle_exception exception
218
194
  if security_exception?(exception)
219
- exception_control = AGENT.exception_control
195
+ exception_control = ::Contrast::AGENT.exception_control
220
196
  raise exception unless exception_control[:enable]
221
197
 
222
198
  [exception_control[:status], {}, [exception_control[:message]]]
@@ -231,22 +207,18 @@ module Contrast
231
207
  # @param exception [Exception]
232
208
  # @return [Boolean]
233
209
  def security_exception? exception
234
- exception.is_a?(Contrast::SecurityException) ||
235
- exception.message&.include?(SECURITY_EXCEPTION_MARKER)
210
+ exception.is_a?(Contrast::SecurityException) || exception.message&.include?(SECURITY_EXCEPTION_MARKER)
236
211
  end
237
212
 
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
213
+ # As we deprecate support to prepare to remove dead code, we need to inform our users still relying on the now
214
+ # deprecated and soon to be removed functionality. This method handles doing that by leveraging the standard
215
+ # Kernel#warn approach
242
216
  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.
217
+ # Ruby 2.5 is currently in security maintenance, meaning int is only receiving updates for security issues. It
218
+ # will move to eol on 31 March 2021. As such, we can remove support for it in Q3. We'll begin the deprecation
219
+ # warnings now so that customers have time to reach out if they'll be impacted.
220
+ # TODO: RUBY-715 remove this part of the method, leaving it empty if there are no other deprecations, when we
221
+ # drop 2.5 support.
250
222
  return unless RUBY_VERSION < '2.6.0'
251
223
 
252
224
  Kernel.warn('[Contrast Security] [DEPRECATION] Support for Ruby 2.5 will be removed in April 2021. '\