contrast-agent 3.11.0 → 3.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (217) hide show
  1. checksums.yaml +4 -4
  2. data/.flayignore +1 -0
  3. data/ext/cs__assess_active_record_named/cs__active_record_named.c +7 -2
  4. data/ext/cs__assess_active_record_named/cs__active_record_named.h +1 -0
  5. data/ext/cs__assess_array/cs__assess_array.c +2 -1
  6. data/ext/cs__assess_array/cs__assess_array.h +1 -0
  7. data/ext/cs__assess_basic_object/cs__assess_basic_object.c +3 -7
  8. data/ext/cs__assess_basic_object/cs__assess_basic_object.h +2 -1
  9. data/ext/cs__assess_kernel/cs__assess_kernel.c +1 -1
  10. data/ext/cs__assess_module/cs__assess_module.c +5 -7
  11. data/ext/cs__assess_module/cs__assess_module.h +3 -0
  12. data/ext/cs__common/cs__common.c +1 -1
  13. data/ext/cs__protect_kernel/cs__protect_kernel.c +4 -2
  14. data/ext/cs__protect_kernel/cs__protect_kernel.h +1 -0
  15. data/funchook/autom4te.cache/output.0 +13 -1
  16. data/funchook/autom4te.cache/requests +49 -48
  17. data/funchook/autom4te.cache/traces.0 +3 -0
  18. data/funchook/config.log +217 -378
  19. data/funchook/config.status +24 -23
  20. data/funchook/configure +13 -1
  21. data/funchook/src/Makefile +7 -7
  22. data/funchook/src/config.h +2 -2
  23. data/funchook/src/decoder.o +0 -0
  24. data/funchook/src/distorm.o +0 -0
  25. data/funchook/src/funchook.o +0 -0
  26. data/funchook/src/funchook_io.o +0 -0
  27. data/funchook/src/funchook_syscall.o +0 -0
  28. data/funchook/src/funchook_unix.o +0 -0
  29. data/funchook/src/funchook_x86.o +0 -0
  30. data/funchook/src/instructions.o +0 -0
  31. data/funchook/src/insts.o +0 -0
  32. data/funchook/src/libfunchook.so +0 -0
  33. data/funchook/src/mnemonics.o +0 -0
  34. data/funchook/src/operands.o +0 -0
  35. data/funchook/src/os_func.o +0 -0
  36. data/funchook/src/os_func_unix.o +0 -0
  37. data/funchook/src/prefix.o +0 -0
  38. data/funchook/src/printf_base.o +0 -0
  39. data/funchook/src/textdefs.o +0 -0
  40. data/funchook/src/wstring.o +0 -0
  41. data/funchook/test/Makefile +2 -2
  42. data/funchook/test/funchook_test +0 -0
  43. data/funchook/test/libfunchook_test.so +0 -0
  44. data/funchook/test/test_main.o +0 -0
  45. data/funchook/test/x86_64_test.o +0 -0
  46. data/lib/contrast.rb +0 -1
  47. data/lib/contrast/agent.rb +19 -22
  48. data/lib/contrast/agent/assess.rb +0 -9
  49. data/lib/contrast/agent/assess/policy/patcher.rb +1 -0
  50. data/lib/contrast/agent/assess/policy/policy_node.rb +1 -1
  51. data/lib/contrast/agent/assess/policy/policy_scanner.rb +1 -1
  52. data/lib/contrast/agent/assess/policy/propagation_method.rb +3 -0
  53. data/lib/contrast/agent/assess/policy/propagator/custom.rb +1 -1
  54. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +1 -3
  55. data/lib/contrast/agent/assess/policy/trigger/reflected_xss.rb +90 -0
  56. data/lib/contrast/agent/assess/policy/trigger/xpath.rb +57 -0
  57. data/lib/contrast/agent/assess/policy/trigger_method.rb +3 -7
  58. data/lib/contrast/agent/assess/policy/trigger_node.rb +4 -1
  59. data/lib/contrast/agent/assess/rule/base.rb +0 -15
  60. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +22 -5
  61. data/lib/contrast/agent/assess/rule/redos.rb +0 -1
  62. data/lib/contrast/agent/at_exit_hook.rb +2 -2
  63. data/lib/contrast/agent/class_reopener.rb +9 -4
  64. data/lib/contrast/agent/exclusion_matcher.rb +0 -1
  65. data/lib/contrast/agent/inventory/policy/datastores.rb +54 -0
  66. data/lib/contrast/agent/inventory/policy/policy.rb +1 -1
  67. data/lib/contrast/agent/middleware.rb +38 -34
  68. data/lib/contrast/agent/patching/policy/after_load_patch.rb +11 -2
  69. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +51 -56
  70. data/lib/contrast/agent/patching/policy/patch.rb +2 -1
  71. data/lib/contrast/agent/patching/policy/patcher.rb +10 -12
  72. data/lib/contrast/agent/patching/policy/policy_node.rb +1 -1
  73. data/lib/contrast/agent/patching/policy/trigger_node.rb +1 -1
  74. data/lib/contrast/agent/protect/policy/applies_command_injection_rule.rb +63 -0
  75. data/lib/contrast/agent/protect/policy/applies_deserialization_rule.rb +52 -0
  76. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +68 -0
  77. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +117 -0
  78. data/lib/contrast/agent/protect/policy/applies_sqli_rule.rb +54 -0
  79. data/lib/contrast/agent/protect/policy/applies_xxe_rule.rb +129 -0
  80. data/lib/contrast/agent/protect/policy/policy.rb +6 -6
  81. data/lib/contrast/agent/protect/policy/rule_applicator.rb +51 -0
  82. data/lib/contrast/agent/protect/rule.rb +0 -5
  83. data/lib/contrast/agent/protect/rule/base.rb +6 -5
  84. data/lib/contrast/agent/protect/rule/cmd_injection.rb +3 -3
  85. data/lib/contrast/agent/protect/rule/path_traversal.rb +2 -7
  86. data/lib/contrast/agent/protect/rule/sqli.rb +4 -4
  87. data/lib/contrast/agent/railtie.rb +1 -0
  88. data/lib/contrast/agent/request.rb +2 -6
  89. data/lib/contrast/agent/request_context.rb +5 -6
  90. data/lib/contrast/agent/request_handler.rb +2 -2
  91. data/lib/contrast/agent/response.rb +0 -69
  92. data/lib/contrast/agent/service_heartbeat.rb +2 -2
  93. data/lib/contrast/agent/socket_client.rb +8 -8
  94. data/lib/contrast/agent/static_analysis.rb +2 -3
  95. data/lib/contrast/agent/version.rb +1 -1
  96. data/lib/contrast/api/decorators/application_settings.rb +1 -1
  97. data/lib/contrast/api/speedracer.rb +1 -1
  98. data/lib/contrast/components/agent.rb +17 -12
  99. data/lib/contrast/components/app_context.rb +33 -1
  100. data/lib/contrast/components/assess.rb +25 -15
  101. data/lib/contrast/components/contrast_service.rb +23 -67
  102. data/lib/contrast/components/interface.rb +4 -12
  103. data/lib/contrast/components/inventory.rb +5 -1
  104. data/lib/contrast/components/logger.rb +2 -2
  105. data/lib/contrast/components/protect.rb +40 -4
  106. data/lib/contrast/components/scope.rb +2 -52
  107. data/lib/contrast/components/settings.rb +24 -18
  108. data/lib/contrast/config/protect_rules_configuration.rb +0 -1
  109. data/lib/contrast/{extensions/ruby_core → extension}/assess.rb +12 -14
  110. data/lib/contrast/extension/assess/array.rb +77 -0
  111. data/lib/contrast/{extensions/ruby_core → extension}/assess/assess_extension.rb +2 -2
  112. data/lib/contrast/{extensions/ruby_core → extension}/assess/erb.rb +0 -0
  113. data/lib/contrast/extension/assess/eval_trigger.rb +78 -0
  114. data/lib/contrast/{extensions/ruby_core → extension}/assess/exec_trigger.rb +1 -1
  115. data/lib/contrast/{extensions/ruby_core → extension}/assess/fiber.rb +6 -5
  116. data/lib/contrast/{extensions/ruby_core → extension}/assess/hash.rb +2 -2
  117. data/lib/contrast/extension/assess/kernel.rb +110 -0
  118. data/lib/contrast/{extensions/ruby_core → extension}/assess/regexp.rb +4 -4
  119. data/lib/contrast/{extensions/ruby_core → extension}/assess/string.rb +5 -5
  120. data/lib/contrast/{extensions/ruby_core → extension}/delegator.rb +0 -0
  121. data/lib/contrast/{extensions/ruby_core → extension}/inventory.rb +2 -2
  122. data/lib/contrast/extension/kernel.rb +54 -0
  123. data/lib/contrast/{extensions/ruby_core → extension}/module.rb +0 -0
  124. data/lib/contrast/{extensions/ruby_core → extension}/protect.rb +2 -2
  125. data/lib/contrast/extension/protect/kernel.rb +44 -0
  126. data/lib/contrast/{extensions/ruby_core → extension}/protect/psych.rb +1 -1
  127. data/lib/contrast/{extensions/ruby_core → extension}/thread.rb +0 -0
  128. data/lib/contrast/framework/base_support.rb +22 -0
  129. data/lib/contrast/framework/manager.rb +33 -8
  130. data/lib/contrast/framework/rack/patch/session_cookie.rb +126 -0
  131. data/lib/contrast/framework/rack/patch/support.rb +24 -0
  132. data/lib/contrast/framework/rack/support.rb +22 -0
  133. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +43 -0
  134. data/lib/contrast/framework/rails/patch/assess_configuration.rb +103 -0
  135. data/lib/contrast/framework/rails/patch/rails_application_configuration.rb +31 -0
  136. data/lib/contrast/framework/rails/patch/support.rb +67 -0
  137. data/lib/contrast/framework/rails/rewrite/action_controller_railties_helper_inherited.rb +34 -0
  138. data/lib/contrast/framework/rails/rewrite/active_record_attribute_methods_read.rb +39 -0
  139. data/lib/contrast/framework/rails/rewrite/active_record_named.rb +73 -0
  140. data/lib/contrast/framework/rails/rewrite/active_record_time_zone_inherited.rb +33 -0
  141. data/lib/contrast/framework/rails/support.rb +115 -0
  142. data/lib/contrast/framework/sinatra/application_helper.rb +51 -0
  143. data/lib/contrast/framework/sinatra/patch/base.rb +83 -0
  144. data/lib/contrast/framework/sinatra/patch/support.rb +27 -0
  145. data/lib/contrast/framework/sinatra/support.rb +109 -0
  146. data/lib/contrast/logger/application.rb +80 -0
  147. data/lib/contrast/{agent/logger.rb → logger/log.rb} +23 -54
  148. data/lib/contrast/logger/time.rb +50 -0
  149. data/lib/contrast/tasks/config.rb +54 -0
  150. data/lib/contrast/tasks/service.rb +1 -5
  151. data/lib/contrast/utils/class_util.rb +1 -1
  152. data/lib/contrast/utils/gemfile_reader.rb +2 -2
  153. data/lib/contrast/utils/hash_digest.rb +2 -7
  154. data/lib/contrast/utils/invalid_configuration_util.rb +3 -3
  155. data/lib/contrast/utils/job_servers_running.rb +4 -2
  156. data/lib/contrast/utils/object_share.rb +0 -1
  157. data/lib/contrast/utils/service_response_util.rb +14 -12
  158. data/lib/contrast/utils/service_sender_util.rb +78 -21
  159. data/resources/assess/policy.json +9 -50
  160. data/resources/inventory/policy.json +2 -2
  161. data/resources/protect/policy.json +6 -6
  162. data/ruby-agent.gemspec +5 -1
  163. data/service_executables/VERSION +1 -1
  164. data/service_executables/linux/contrast-service +0 -0
  165. data/service_executables/mac/contrast-service +0 -0
  166. metadata +69 -83
  167. data/funchook/src/libfunchook.dylib +0 -0
  168. data/funchook/test/libfunchook_test.so.dSYM/Contents/Info.plist +0 -20
  169. data/funchook/test/libfunchook_test.so.dSYM/Contents/Resources/DWARF/libfunchook_test.so +0 -0
  170. data/lib/contrast/agent/assess/rule/csrf.rb +0 -66
  171. data/lib/contrast/agent/assess/rule/csrf/csrf_action.rb +0 -28
  172. data/lib/contrast/agent/assess/rule/csrf/csrf_applicator.rb +0 -53
  173. data/lib/contrast/agent/assess/rule/csrf/csrf_watcher.rb +0 -136
  174. data/lib/contrast/agent/assess/rule/response_scanning_rule.rb +0 -47
  175. data/lib/contrast/agent/assess/rule/response_watcher.rb +0 -36
  176. data/lib/contrast/agent/assess/rule/watcher.rb +0 -36
  177. data/lib/contrast/agent/feature_state.rb +0 -346
  178. data/lib/contrast/agent/protect/rule/csrf.rb +0 -119
  179. data/lib/contrast/agent/protect/rule/csrf/csrf_evaluator.rb +0 -100
  180. data/lib/contrast/agent/protect/rule/csrf/csrf_token_injector.rb +0 -85
  181. data/lib/contrast/agent/settings_state.rb +0 -88
  182. data/lib/contrast/api/decorators/exclusion.rb +0 -20
  183. data/lib/contrast/extensions/framework/rack/cookie.rb +0 -24
  184. data/lib/contrast/extensions/framework/rack/request.rb +0 -24
  185. data/lib/contrast/extensions/framework/rack/response.rb +0 -23
  186. data/lib/contrast/extensions/framework/rails/action_controller_inheritance.rb +0 -39
  187. data/lib/contrast/extensions/framework/rails/action_controller_railties_helper_inherited.rb +0 -20
  188. data/lib/contrast/extensions/framework/rails/active_record.rb +0 -26
  189. data/lib/contrast/extensions/framework/rails/active_record_named.rb +0 -58
  190. data/lib/contrast/extensions/framework/rails/active_record_time_zone_inherited.rb +0 -21
  191. data/lib/contrast/extensions/framework/rails/buffer.rb +0 -28
  192. data/lib/contrast/extensions/framework/rails/configuration.rb +0 -27
  193. data/lib/contrast/extensions/framework/sinatra/base.rb +0 -59
  194. data/lib/contrast/extensions/ruby_core/assess/array.rb +0 -59
  195. data/lib/contrast/extensions/ruby_core/assess/basic_object.rb +0 -15
  196. data/lib/contrast/extensions/ruby_core/assess/kernel.rb +0 -96
  197. data/lib/contrast/extensions/ruby_core/assess/module.rb +0 -14
  198. data/lib/contrast/extensions/ruby_core/assess/tilt_template_trigger.rb +0 -78
  199. data/lib/contrast/extensions/ruby_core/assess/xpath_library_trigger.rb +0 -40
  200. data/lib/contrast/extensions/ruby_core/eval_trigger.rb +0 -51
  201. data/lib/contrast/extensions/ruby_core/inventory/datastores.rb +0 -37
  202. data/lib/contrast/extensions/ruby_core/protect/applies_command_injection_rule.rb +0 -61
  203. data/lib/contrast/extensions/ruby_core/protect/applies_deserialization_rule.rb +0 -50
  204. data/lib/contrast/extensions/ruby_core/protect/applies_no_sqli_rule.rb +0 -66
  205. data/lib/contrast/extensions/ruby_core/protect/applies_path_traversal_rule.rb +0 -115
  206. data/lib/contrast/extensions/ruby_core/protect/applies_sqli_rule.rb +0 -53
  207. data/lib/contrast/extensions/ruby_core/protect/applies_xxe_rule.rb +0 -127
  208. data/lib/contrast/extensions/ruby_core/protect/kernel.rb +0 -30
  209. data/lib/contrast/extensions/ruby_core/protect/rule_applicator.rb +0 -50
  210. data/lib/contrast/framework/rails_support.rb +0 -104
  211. data/lib/contrast/framework/sinatra_application_helper.rb +0 -49
  212. data/lib/contrast/framework/sinatra_support.rb +0 -104
  213. data/lib/contrast/utils/data_store_util.rb +0 -23
  214. data/lib/contrast/utils/rack_assess_session_cookie.rb +0 -104
  215. data/lib/contrast/utils/rails_assess_configuration.rb +0 -95
  216. data/lib/contrast/utils/random_util.rb +0 -22
  217. data/resources/csrf/inject.js +0 -44
@@ -0,0 +1,83 @@
1
+ # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ cs__scoped_require 'contrast/framework/sinatra/support'
5
+
6
+ module Contrast
7
+ module Framework
8
+ module Sinatra
9
+ module Patch
10
+ # Our patch into the Sinatra::Base Class, allowing for the inventory of the
11
+ # routes called by the application
12
+ module Base
13
+ class << self
14
+ # Use logic copied from Sinatra::Base#process_route to determine which
15
+ # pattern matches the request being invoked by Sinatra::Base#call!
16
+ #
17
+ # @param clazz [Class] the Class that extends Sinatra::Base in
18
+ # which this route is defined.
19
+ # @param settings [Object] the Sinatra::Base @settings object.
20
+ # @param args [Array<Object>] we rely on the @settings object in
21
+ # Sinatra::Base and the env variable passed in as args[0].
22
+ def map_route clazz, settings, *args
23
+ context = Contrast::Agent::REQUEST_TRACKER.current
24
+ return unless context
25
+
26
+ env = args[0]
27
+ return unless env
28
+ return unless settings
29
+
30
+ convert_routes(context, clazz, settings, env)
31
+ rescue StandardError
32
+ # Being careful here since we're directly patching something
33
+ end
34
+
35
+ def instrument
36
+ @_instrument ||= begin
37
+ ::Sinatra::Base.class_eval do
38
+ alias_method :cs__patched_sinatra_base_call!, :call!
39
+ # publicly available method for Sinatra::Base things -- unfortunately,
40
+ # getting the routes appear to require a lookup every time
41
+ def call! *args
42
+ Contrast::Framework::Sinatra::Patch::Base.map_route(cs__class, settings, *args)
43
+ cs__patched_sinatra_base_call!(*args)
44
+ end
45
+ end
46
+ true
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ # Find the route that matches this request. Since we're using
53
+ # settings, we should resolve in the same precedence as Sinatra
54
+ def convert_routes context, clazz, settings, env
55
+ # There isn't a Request object in the Sinatra::Base yet - it's made
56
+ # during the #call! method, which we're patching - so we need to
57
+ # access the env
58
+ method = env[::Rack::REQUEST_METHOD]
59
+ # get all potential routes for this request type
60
+ routes = settings.routes[method]
61
+ return unless routes
62
+
63
+ route = env[::Rack::PATH_INFO]
64
+ route = route.to_s
65
+
66
+ # Do some cleanup that matches Sinatra::Base#process_route
67
+ route = '/' if route.empty? && !settings.empty_path_info?
68
+ route = route[0..-2] if !settings.strict_paths? && route != '/' && route.end_with?('/')
69
+
70
+ routes.each do |pattern, _, _| # Mustermann::Sinatra
71
+ next unless pattern.params(route)
72
+
73
+ dtm = Contrast::Framework::Sinatra::Support.send(:route_to_coverage, clazz, method, pattern)
74
+ context.append_route_coverage(dtm)
75
+ break
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,27 @@
1
+ # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ cs__scoped_require 'contrast/agent/patching/policy/after_load_patch'
5
+
6
+ module Contrast
7
+ module Framework
8
+ module Sinatra
9
+ module Patch
10
+ # Extension point allowing for the registration of Patches required to
11
+ # support the Sinatra framework.
12
+ module Support
13
+ # (See BaseSupport#after_load_patches)
14
+ def after_load_patches
15
+ Set.new([
16
+ Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
17
+ 'Sinatra::Base',
18
+ 'contrast/framework/sinatra/patch/base',
19
+ method_to_instrument: nil,
20
+ instrumenting_module: 'Contrast::Framework::Sinatra::Patch::Base')
21
+ ])
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,109 @@
1
+ # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ cs__scoped_require 'contrast/framework/base_support'
5
+ cs__scoped_require 'contrast/framework/sinatra/application_helper'
6
+ cs__scoped_require 'contrast/framework/sinatra/patch/support'
7
+
8
+ module Contrast
9
+ module Framework
10
+ module Sinatra
11
+ # Used when Sinatra is present to define framework specific behavior
12
+ class Support < BaseSupport
13
+ extend Contrast::Framework::Sinatra::ApplicationHelper
14
+ extend Contrast::Framework::Sinatra::Patch::Support
15
+ class << self
16
+ def detection_class
17
+ 'Sinatra'
18
+ end
19
+
20
+ def version
21
+ ::Sinatra::VERSION
22
+ end
23
+
24
+ def application_name
25
+ return unless app_class
26
+
27
+ app_class.cs__class.cs__name
28
+ end
29
+
30
+ def application_root
31
+ return unless app_class
32
+
33
+ app_class.root
34
+ end
35
+
36
+ def server_type
37
+ 'sinatra'
38
+ end
39
+
40
+ def scan_views
41
+ return unless app_class
42
+
43
+ scan_view_directories(scannable_view_dirs)
44
+ end
45
+
46
+ # Iterate over every class that extends Sinatra::Base, pull out its routes
47
+ # (array of arrays with Mustermann::Sinatra as [][0]) and convert them into
48
+ # Contrast::Api::Dtm::RouteCoverage
49
+ def collect_routes
50
+ routes = []
51
+ controllers = sinatra_controllers
52
+ controllers.each do |clazz|
53
+ class_routes = sinatra_class_routes(clazz)
54
+ next unless class_routes
55
+
56
+ class_routes.each_pair do |method, list|
57
+ # item: [ Mustermann::Sinatra, [], Proc]
58
+ list.each do |item|
59
+ routes << route_to_coverage(clazz, method, item[0])
60
+ end
61
+ end
62
+ end
63
+ routes
64
+ end
65
+
66
+ # TODO: RUBY-763
67
+ def current_route _request
68
+ nil
69
+ end
70
+
71
+ def retrieve_request env
72
+ ::Sinatra::Request.new(env)
73
+ end
74
+
75
+ private
76
+
77
+ # Iterate over every class that extends Sinatra::Base, pull out its routes
78
+ # (array of arrays with Mustermann::Sinatra as [][0]) and convert them into
79
+ # Contrast::Api::Dtm::RouteCoverage
80
+ def sinatra_controllers
81
+ return [] unless defined?(::Sinatra) && defined?(::Sinatra::Base)
82
+
83
+ Contrast::Utils::ClassUtil.descendants(::Sinatra::Base)
84
+ end
85
+
86
+ def sinatra_class_routes controller
87
+ controller.instance_variable_get(:@routes) if controller.instance_variable_defined?(:@routes)
88
+ rescue StandardError
89
+ logger.trace('Sinatra controller found with no route instances', module: controller)
90
+ nil
91
+ end
92
+
93
+ # TODO: RUBY-763
94
+ # given clazz, method, and Mustermann::Sinatra, build a
95
+ # Contrast::Api::Dtm::RouteCoverage
96
+ def route_to_coverage clazz, method, pattern
97
+ safe_pattern = source_or_string(pattern)
98
+
99
+ route_coverage = Contrast::Api::Dtm::RouteCoverage.new
100
+ route_coverage.route = "#{ clazz }##{ method } #{ safe_pattern }"
101
+ route_coverage.verb = Contrast::Utils::StringUtils.force_utf8(method)
102
+ route_coverage.url = Contrast::Utils::StringUtils.force_utf8(safe_pattern)
103
+ route_coverage
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,80 @@
1
+ # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ cs__scoped_require 'contrast/components/interface'
5
+
6
+ module Contrast
7
+ module Logger
8
+ # # Our decorator for the Ougai logger allowing for the logging of the
9
+ # application environment, used to provide context during troubleshooting.
10
+ module Application
11
+ include Contrast::Components::Interface
12
+ access_component :config
13
+
14
+ ENV_KEYS = %w[HOME PWD RACK_ENV RAILS_ENV RUBY_VERSION GEM_HOME GEM_PATH].cs__freeze
15
+ # Utility method to log some current ruby and rails information from environment
16
+ def application_environment
17
+ return unless info?
18
+
19
+ info('Process environment information',
20
+ p_id: Process.pid,
21
+ pp_id: Process.ppid,
22
+ agent_version: Contrast::Agent::VERSION)
23
+ ENV.each do |env_key, env_value|
24
+ env_key = env_key.to_s
25
+ next unless ENV_KEYS.include?(env_key) ||
26
+ (env_key.start_with?(Contrast::Components::Config::CONTRAST_ENV_MARKER) &&
27
+ !env_key.start_with?(Contrast::Components::Config::CONTRAST_ENV_MARKER + 'API'))
28
+
29
+ info('Environment settings', key: env_key, value: env_value)
30
+ end
31
+ end
32
+
33
+ def application_configuration
34
+ return unless info?
35
+
36
+ loggable = CONFIG.raw.send(:load_config)
37
+ loggable.delete('api')
38
+ info('Current configuration', configruation: JSON.pretty_generate(loggable))
39
+ end
40
+
41
+ def application_libraries
42
+ if debug?
43
+ log_all_libraries
44
+ elsif info?
45
+ log_specific_libraries
46
+ end
47
+ end
48
+
49
+ FRAMEWORKS = %w[rails sinatra grape].cs__freeze
50
+ WEB_SERVERS = %w[agoo falcon hoof iodine mongrel mongrel2 passenger puma rack skinny thin trinidad unicorn webrick yarn].cs__freeze
51
+ LIBRARIES = %w[excon json mongo moped mysql nokogiri oga ox pg psych sqlite3 typhoeus yaml].cs__freeze
52
+ def log_specific_libraries
53
+ FRAMEWORKS.each(&cs__method(:log_gem_data))
54
+ WEB_SERVERS.each(&cs__method(:log_gem_data))
55
+ LIBRARIES.each(&cs__method(:log_gem_data))
56
+ end
57
+
58
+ private
59
+
60
+ def log_all_libraries
61
+ return unless debug?
62
+
63
+ Gem.loaded_specs.each_pair do |_name, gem_spec|
64
+ debug('Gem loaded',
65
+ gem_name: gem_spec.name,
66
+ gem_version: gem_spec.version.to_s)
67
+ end
68
+ end
69
+
70
+ def log_gem_data gem_name
71
+ gem_spec = Gem.loaded_specs[gem_name]
72
+ return unless gem_spec
73
+
74
+ info('Gem loaded',
75
+ gem_name: gem_spec.name,
76
+ gem_version: gem_spec.version.to_s)
77
+ end
78
+ end
79
+ end
80
+ end
@@ -5,62 +5,16 @@ cs__scoped_require 'logger'
5
5
  cs__scoped_require 'ougai'
6
6
  cs__scoped_require 'singleton'
7
7
 
8
- cs__scoped_require 'contrast/extensions/ruby_core/module'
9
8
  cs__scoped_require 'contrast/components/interface'
9
+ cs__scoped_require 'contrast/extension/module'
10
+ cs__scoped_require 'contrast/logger/application'
11
+ cs__scoped_require 'contrast/logger/time'
10
12
 
11
13
  module Contrast
12
- module Ougai
13
- # Our decorator for the Ougai logger allowing for timing and with_level
14
- # methods.
15
- module Logger
16
- # Log the message at the given level.
17
- #
18
- # @param level [String] the name of the method to use. Should be one of
19
- # trace, debug, info, warn, error
20
- # @param message [String] the message to log
21
- def with_level level, message
22
- send(level.to_sym, message)
23
- end
24
-
25
- # Log, at the debug level, the action with a message including the time
26
- # it took for the wrapped function to complete.
27
- #
28
- # @param msgs [Array<Object>] the arguments to pass to the logger.
29
- # msgs[0] will be modified to include the elapsed time.
30
- # @param block [Block, Proc] the block to execute
31
- def debug_with_time *msgs, &block
32
- log_with_time(:debug, *msgs, &block)
33
- end
34
-
35
- # Log, at the trace level, the action with a message including the time
36
- # it took for the wrapped function to complete.
37
- #
38
- # @param msgs [Array<Object>] the arguments to pass to the logger.
39
- # msgs[0] will be modified to include the elapsed time.
40
- # @param block [Block, Proc] the block to execute
41
- def trace_with_time *msgs, &block
42
- log_with_time(:trace, *msgs, &block)
43
- end
44
-
45
- private
46
-
47
- def log_with_time level, *msgs
48
- a = Contrast::Utils::Timer.now_ms
49
- ret = yield if block_given?
50
- z = Contrast::Utils::Timer.now_ms
51
- msgs[0] = "#{ msgs[0] }: pid=#{ Process.pid }, elapsed=#{ z - a }ms"
52
- send(level, *msgs)
53
- ret
54
- end
55
- end
56
- end
57
- end
58
-
59
- module Contrast
60
- module Agent
14
+ module Logger
61
15
  # This class functions to serve as a wrapper around our logging, as we need
62
16
  # to be able to dynamically update level based on updates to TeamServer.
63
- class Logger
17
+ class Log
64
18
  include Singleton
65
19
  include Contrast::Components::Interface
66
20
  access_component :config
@@ -102,8 +56,7 @@ module Contrast
102
56
  @previous_level = level_const
103
57
 
104
58
  @_logger = build(path: path, level_const: level_const)
105
-
106
- logger.debug('Initialized new contrast agent logger')
59
+ log_update
107
60
  rescue StandardError => e
108
61
  logger.error('Unable to process update to LoggerManager.', e)
109
62
  end
@@ -131,13 +84,18 @@ module Contrast
131
84
  else
132
85
  ::Ougai::Logger.new(path)
133
86
  end
134
- logger.extend(Contrast::Ougai::Logger)
87
+ add_contrast_loggers(logger)
135
88
  logger.progname = 'Contrast Agent'
136
89
  logger.level = level_const
137
90
  logger.formatter.datetime_format = '%Y-%m-%dT%H:%M:%S.%L%z'
138
91
  logger
139
92
  end
140
93
 
94
+ def add_contrast_loggers logger
95
+ logger.extend(Contrast::Logger::Application)
96
+ logger.extend(Contrast::Logger::Time)
97
+ end
98
+
141
99
  def valid_path path
142
100
  path = path.nil? ? Contrast::Utils::ObjectShare::EMPTY_STRING : path
143
101
  return path if path == STDOUT_STR
@@ -168,6 +126,17 @@ module Contrast
168
126
  rescue StandardError
169
127
  DEFAULT_LEVEL
170
128
  end
129
+
130
+ # Log that the Agent log has changed and include some default information
131
+ # at the start of the log.
132
+ def log_update
133
+ logger.debug('Initialized new contrast agent logger')
134
+ logger.debug_with_time('middleware: log environment') do
135
+ logger.application_environment
136
+ logger.application_configuration
137
+ logger.application_libraries
138
+ end
139
+ end
171
140
  end
172
141
  end
173
142
  end
@@ -0,0 +1,50 @@
1
+ # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ module Contrast
5
+ module Logger
6
+ # Our decorator for the Ougai logger allowing for timing and with_level
7
+ # methods.
8
+ module Time
9
+ # Log the message at the given level.
10
+ #
11
+ # @param level [String] the name of the method to use. Should be one of
12
+ # trace, debug, info, warn, error
13
+ # @param message [String] the message to log
14
+ def with_level level, message
15
+ send(level.to_sym, message)
16
+ end
17
+
18
+ # Log, at the debug level, the action with a message including the time
19
+ # it took for the wrapped function to complete.
20
+ #
21
+ # @param msgs [Array<Object>] the arguments to pass to the logger.
22
+ # msgs[0] will be modified to include the elapsed time.
23
+ # @param block [Block, Proc] the block to execute
24
+ def debug_with_time *msgs, &block
25
+ log_with_time(:debug, *msgs, &block)
26
+ end
27
+
28
+ # Log, at the trace level, the action with a message including the time
29
+ # it took for the wrapped function to complete.
30
+ #
31
+ # @param msgs [Array<Object>] the arguments to pass to the logger.
32
+ # msgs[0] will be modified to include the elapsed time.
33
+ # @param block [Block, Proc] the block to execute
34
+ def trace_with_time *msgs, &block
35
+ log_with_time(:trace, *msgs, &block)
36
+ end
37
+
38
+ private
39
+
40
+ def log_with_time level, *msgs
41
+ a = Contrast::Utils::Timer.now_ms
42
+ ret = yield if block_given?
43
+ z = Contrast::Utils::Timer.now_ms
44
+ msgs[0] = "#{ msgs[0] }: pid=#{ Process.pid }, elapsed=#{ z - a }ms"
45
+ send(level, *msgs)
46
+ ret
47
+ end
48
+ end
49
+ end
50
+ end