contrast-agent 3.13.1 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (251) hide show
  1. checksums.yaml +4 -4
  2. data/exe/contrast_service +1 -7
  3. data/ext/cs__assess_active_record_named/cs__active_record_named.c +8 -7
  4. data/ext/cs__assess_array/cs__assess_array.c +6 -5
  5. data/ext/cs__assess_basic_object/cs__assess_basic_object.c +5 -5
  6. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.c +2 -1
  7. data/ext/cs__assess_hash/cs__assess_hash.c +18 -17
  8. data/ext/cs__assess_hash/cs__assess_hash.h +2 -1
  9. data/ext/cs__assess_kernel/cs__assess_kernel.c +7 -8
  10. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +18 -16
  11. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.h +1 -0
  12. data/ext/cs__assess_module/cs__assess_module.c +6 -6
  13. data/ext/cs__assess_regexp/cs__assess_regexp.c +4 -4
  14. data/ext/cs__assess_string/cs__assess_string.c +31 -16
  15. data/ext/cs__assess_string/cs__assess_string.h +6 -1
  16. data/ext/cs__assess_string_interpolation26/cs__assess_string_interpolation26.c +4 -2
  17. data/ext/cs__assess_yield_track/cs__assess_yield_track.c +2 -2
  18. data/ext/cs__common/cs__common.c +48 -39
  19. data/ext/cs__common/cs__common.h +16 -21
  20. data/ext/cs__contrast_patch/cs__contrast_patch.c +27 -25
  21. data/ext/cs__contrast_patch/cs__contrast_patch.h +5 -7
  22. data/ext/cs__protect_kernel/cs__protect_kernel.c +11 -12
  23. data/ext/cs__protect_kernel/cs__protect_kernel.h +2 -2
  24. data/lib/contrast-agent.rb +1 -1
  25. data/lib/contrast.rb +13 -23
  26. data/lib/contrast/agent.rb +39 -47
  27. data/lib/contrast/agent/assess.rb +12 -12
  28. data/lib/contrast/agent/assess/contrast_event.rb +151 -85
  29. data/lib/contrast/agent/assess/events/event_factory.rb +2 -2
  30. data/lib/contrast/agent/assess/events/source_event.rb +3 -3
  31. data/lib/contrast/agent/assess/finalizers/freeze.rb +15 -0
  32. data/lib/contrast/agent/assess/finalizers/hash.rb +97 -0
  33. data/lib/contrast/agent/assess/policy/dynamic_source_factory.rb +11 -4
  34. data/lib/contrast/agent/assess/policy/patcher.rb +6 -6
  35. data/lib/contrast/agent/assess/policy/policy.rb +9 -11
  36. data/lib/contrast/agent/assess/policy/policy_node.rb +17 -12
  37. data/lib/contrast/agent/assess/policy/policy_scanner.rb +21 -5
  38. data/lib/contrast/agent/assess/policy/preshift.rb +13 -7
  39. data/lib/contrast/agent/assess/policy/propagation_method.rb +64 -44
  40. data/lib/contrast/agent/assess/policy/propagation_node.rb +2 -2
  41. data/lib/contrast/agent/assess/policy/propagator.rb +18 -18
  42. data/lib/contrast/agent/assess/policy/propagator/append.rb +8 -5
  43. data/lib/contrast/agent/assess/policy/propagator/base.rb +1 -1
  44. data/lib/contrast/agent/assess/policy/propagator/center.rb +9 -5
  45. data/lib/contrast/agent/assess/policy/propagator/custom.rb +1 -1
  46. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +6 -4
  47. data/lib/contrast/agent/assess/policy/propagator/insert.rb +7 -4
  48. data/lib/contrast/agent/assess/policy/propagator/keep.rb +4 -1
  49. data/lib/contrast/agent/assess/policy/propagator/match_data.rb +7 -9
  50. data/lib/contrast/agent/assess/policy/propagator/next.rb +7 -5
  51. data/lib/contrast/agent/assess/policy/propagator/prepend.rb +13 -5
  52. data/lib/contrast/agent/assess/policy/propagator/remove.rb +8 -4
  53. data/lib/contrast/agent/assess/policy/propagator/replace.rb +5 -2
  54. data/lib/contrast/agent/assess/policy/propagator/reverse.rb +7 -5
  55. data/lib/contrast/agent/assess/policy/propagator/select.rb +13 -7
  56. data/lib/contrast/agent/assess/policy/propagator/splat.rb +10 -9
  57. data/lib/contrast/agent/assess/policy/propagator/split.rb +27 -22
  58. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +52 -35
  59. data/lib/contrast/agent/assess/policy/propagator/trim.rb +11 -5
  60. data/lib/contrast/agent/assess/policy/rewriter_patch.rb +5 -5
  61. data/lib/contrast/agent/assess/policy/source_method.rb +90 -72
  62. data/lib/contrast/agent/assess/policy/source_validation/cross_site_validator.rb +1 -1
  63. data/lib/contrast/agent/assess/policy/source_validation/source_validation.rb +1 -1
  64. data/lib/contrast/agent/assess/policy/trigger/reflected_xss.rb +16 -12
  65. data/lib/contrast/agent/assess/policy/trigger/xpath.rb +2 -2
  66. data/lib/contrast/agent/assess/policy/trigger_method.rb +81 -33
  67. data/lib/contrast/agent/assess/policy/trigger_node.rb +41 -46
  68. data/lib/contrast/agent/assess/policy/trigger_validation/ssrf_validator.rb +2 -1
  69. data/lib/contrast/agent/assess/policy/trigger_validation/trigger_validation.rb +2 -2
  70. data/lib/contrast/agent/assess/properties.rb +15 -5
  71. data/lib/contrast/agent/assess/property/evented.rb +7 -20
  72. data/lib/contrast/agent/assess/property/tagged.rb +13 -7
  73. data/lib/contrast/agent/assess/property/updated.rb +131 -0
  74. data/lib/contrast/agent/assess/rule.rb +2 -2
  75. data/lib/contrast/agent/assess/rule/base.rb +3 -4
  76. data/lib/contrast/agent/assess/rule/provider.rb +3 -3
  77. data/lib/contrast/agent/assess/rule/provider/hardcoded_key.rb +58 -5
  78. data/lib/contrast/agent/assess/rule/provider/hardcoded_password.rb +24 -9
  79. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +85 -16
  80. data/lib/contrast/agent/assess/tag.rb +1 -1
  81. data/lib/contrast/agent/assess/tracker.rb +66 -0
  82. data/lib/contrast/agent/at_exit_hook.rb +6 -6
  83. data/lib/contrast/agent/class_reopener.rb +14 -11
  84. data/lib/contrast/agent/deadzone/policy/deadzone_node.rb +1 -1
  85. data/lib/contrast/agent/deadzone/policy/policy.rb +2 -2
  86. data/lib/contrast/agent/disable_reaction.rb +1 -1
  87. data/lib/contrast/agent/exclusion_matcher.rb +1 -1
  88. data/lib/contrast/agent/inventory.rb +15 -0
  89. data/lib/contrast/agent/inventory/dependencies.rb +50 -0
  90. data/lib/contrast/agent/inventory/dependency_analysis.rb +37 -0
  91. data/lib/contrast/agent/inventory/dependency_usage_analysis.rb +104 -0
  92. data/lib/contrast/agent/inventory/gemfile_digest_cache.rb +38 -0
  93. data/lib/contrast/agent/inventory/policy/datastores.rb +2 -2
  94. data/lib/contrast/agent/inventory/policy/policy.rb +3 -3
  95. data/lib/contrast/agent/inventory/policy/trigger_node.rb +1 -1
  96. data/lib/contrast/agent/middleware.rb +33 -34
  97. data/lib/contrast/agent/patching/policy/after_load_patch.rb +9 -9
  98. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +23 -22
  99. data/lib/contrast/agent/patching/policy/module_policy.rb +11 -11
  100. data/lib/contrast/agent/patching/policy/patch.rb +15 -15
  101. data/lib/contrast/agent/patching/policy/patcher.rb +43 -44
  102. data/lib/contrast/agent/patching/policy/policy.rb +23 -12
  103. data/lib/contrast/agent/patching/policy/policy_node.rb +1 -1
  104. data/lib/contrast/agent/patching/policy/trigger_node.rb +2 -2
  105. data/lib/contrast/agent/protect/policy/applies_command_injection_rule.rb +6 -8
  106. data/lib/contrast/agent/protect/policy/applies_deserialization_rule.rb +2 -2
  107. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +2 -2
  108. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +7 -6
  109. data/lib/contrast/agent/protect/policy/applies_sqli_rule.rb +2 -2
  110. data/lib/contrast/agent/protect/policy/applies_xxe_rule.rb +4 -4
  111. data/lib/contrast/agent/protect/policy/policy.rb +8 -8
  112. data/lib/contrast/agent/protect/policy/rule_applicator.rb +1 -1
  113. data/lib/contrast/agent/protect/policy/trigger_node.rb +1 -1
  114. data/lib/contrast/agent/protect/rule.rb +18 -18
  115. data/lib/contrast/agent/protect/rule/base.rb +4 -3
  116. data/lib/contrast/agent/protect/rule/base_service.rb +1 -1
  117. data/lib/contrast/agent/protect/rule/cmd_injection.rb +5 -5
  118. data/lib/contrast/agent/protect/rule/deserialization.rb +1 -1
  119. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +1 -1
  120. data/lib/contrast/agent/protect/rule/no_sqli.rb +1 -1
  121. data/lib/contrast/agent/protect/rule/no_sqli/mongo_no_sql_scanner.rb +1 -0
  122. data/lib/contrast/agent/protect/rule/path_traversal.rb +4 -5
  123. data/lib/contrast/agent/protect/rule/sqli.rb +2 -2
  124. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +1 -1
  125. data/lib/contrast/agent/protect/rule/xss.rb +1 -1
  126. data/lib/contrast/agent/protect/rule/xxe.rb +3 -5
  127. data/lib/contrast/agent/protect/rule/xxe/entity_wrapper.rb +2 -2
  128. data/lib/contrast/agent/railtie.rb +1 -1
  129. data/lib/contrast/agent/reaction_processor.rb +2 -2
  130. data/lib/contrast/agent/request.rb +45 -43
  131. data/lib/contrast/agent/request_context.rb +10 -6
  132. data/lib/contrast/agent/request_handler.rb +1 -1
  133. data/lib/contrast/agent/response.rb +23 -12
  134. data/lib/contrast/agent/rewriter.rb +6 -9
  135. data/lib/contrast/agent/service_heartbeat.rb +2 -2
  136. data/lib/contrast/agent/static_analysis.rb +9 -9
  137. data/lib/contrast/agent/thread.rb +1 -1
  138. data/lib/contrast/agent/thread_watcher.rb +2 -2
  139. data/lib/contrast/agent/tracepoint_hook.rb +2 -2
  140. data/lib/contrast/agent/version.rb +1 -1
  141. data/lib/contrast/api.rb +4 -4
  142. data/lib/contrast/api/communication.rb +9 -9
  143. data/lib/contrast/api/communication/messaging_queue.rb +3 -6
  144. data/lib/contrast/api/communication/response_processor.rb +1 -1
  145. data/lib/contrast/api/communication/socket_client.rb +41 -6
  146. data/lib/contrast/api/communication/speedracer.rb +1 -1
  147. data/lib/contrast/api/communication/tcp_socket.rb +1 -1
  148. data/lib/contrast/api/communication/unix_socket.rb +1 -1
  149. data/lib/contrast/api/decorators.rb +17 -14
  150. data/lib/contrast/api/decorators/address.rb +20 -20
  151. data/lib/contrast/api/decorators/application_settings.rb +3 -2
  152. data/lib/contrast/api/decorators/application_update.rb +7 -8
  153. data/lib/contrast/api/decorators/http_request.rb +13 -12
  154. data/lib/contrast/api/decorators/input_analysis.rb +3 -2
  155. data/lib/contrast/api/decorators/library.rb +53 -0
  156. data/lib/contrast/api/decorators/library_usage_update.rb +30 -0
  157. data/lib/contrast/api/decorators/message.rb +4 -2
  158. data/lib/contrast/api/decorators/rasp_rule_sample.rb +2 -1
  159. data/lib/contrast/api/decorators/route_coverage.rb +3 -2
  160. data/lib/contrast/api/decorators/server_features.rb +3 -2
  161. data/lib/contrast/api/decorators/trace_event.rb +28 -25
  162. data/lib/contrast/api/decorators/trace_event_object.rb +6 -5
  163. data/lib/contrast/api/decorators/trace_event_signature.rb +5 -4
  164. data/lib/contrast/api/decorators/trace_taint_range.rb +4 -3
  165. data/lib/contrast/api/decorators/user_input.rb +4 -4
  166. data/lib/contrast/common_agent_configuration.rb +2 -2
  167. data/lib/contrast/components/agent.rb +8 -7
  168. data/lib/contrast/components/app_context.rb +50 -39
  169. data/lib/contrast/components/config.rb +32 -50
  170. data/lib/contrast/components/contrast_service.rb +10 -10
  171. data/lib/contrast/components/interface.rb +39 -17
  172. data/lib/contrast/components/inventory.rb +6 -1
  173. data/lib/contrast/components/logger.rb +1 -1
  174. data/lib/contrast/components/scope.rb +3 -3
  175. data/lib/contrast/components/settings.rb +20 -23
  176. data/lib/contrast/config.rb +18 -18
  177. data/lib/contrast/config/application_configuration.rb +5 -2
  178. data/lib/contrast/config/base_configuration.rb +2 -2
  179. data/lib/contrast/config/inventory_configuration.rb +2 -2
  180. data/lib/contrast/config/protect_rule_configuration.rb +1 -1
  181. data/lib/contrast/config/service_configuration.rb +8 -0
  182. data/lib/contrast/configuration.rb +93 -52
  183. data/lib/contrast/extension/assess.rb +21 -22
  184. data/lib/contrast/extension/assess/array.rb +18 -11
  185. data/lib/contrast/extension/assess/erb.rb +11 -3
  186. data/lib/contrast/extension/assess/eval_trigger.rb +7 -7
  187. data/lib/contrast/extension/assess/exec_trigger.rb +2 -2
  188. data/lib/contrast/extension/assess/fiber.rb +14 -14
  189. data/lib/contrast/extension/assess/hash.rb +7 -6
  190. data/lib/contrast/extension/assess/kernel.rb +34 -28
  191. data/lib/contrast/extension/assess/marshal.rb +67 -0
  192. data/lib/contrast/extension/assess/regexp.rb +10 -9
  193. data/lib/contrast/extension/assess/string.rb +23 -23
  194. data/lib/contrast/extension/inventory.rb +4 -4
  195. data/lib/contrast/extension/kernel.rb +1 -1
  196. data/lib/contrast/extension/module.rb +1 -1
  197. data/lib/contrast/extension/protect.rb +3 -3
  198. data/lib/contrast/extension/protect/kernel.rb +4 -4
  199. data/lib/contrast/extension/protect/psych.rb +2 -2
  200. data/lib/contrast/framework/base_support.rb +1 -1
  201. data/lib/contrast/framework/manager.rb +10 -11
  202. data/lib/contrast/framework/rack/patch/session_cookie.rb +22 -28
  203. data/lib/contrast/framework/rack/patch/support.rb +1 -1
  204. data/lib/contrast/framework/rack/support.rb +2 -2
  205. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +13 -13
  206. data/lib/contrast/framework/rails/patch/assess_configuration.rb +6 -12
  207. data/lib/contrast/framework/rails/patch/rails_application_configuration.rb +11 -11
  208. data/lib/contrast/framework/rails/patch/support.rb +4 -4
  209. data/lib/contrast/framework/rails/rewrite/action_controller_railties_helper_inherited.rb +11 -11
  210. data/lib/contrast/framework/rails/rewrite/active_record_attribute_methods_read.rb +12 -12
  211. data/lib/contrast/framework/rails/rewrite/active_record_named.rb +4 -4
  212. data/lib/contrast/framework/rails/rewrite/active_record_time_zone_inherited.rb +12 -12
  213. data/lib/contrast/framework/rails/support.rb +67 -14
  214. data/lib/contrast/framework/sinatra/patch/base.rb +12 -12
  215. data/lib/contrast/framework/sinatra/patch/support.rb +1 -1
  216. data/lib/contrast/framework/sinatra/support.rb +6 -6
  217. data/lib/contrast/funchook/funchook.rb +1 -1
  218. data/lib/contrast/logger/application.rb +13 -5
  219. data/lib/contrast/logger/format.rb +22 -9
  220. data/lib/contrast/logger/log.rb +17 -10
  221. data/lib/contrast/logger/request.rb +30 -0
  222. data/lib/contrast/tasks/config.rb +1 -1
  223. data/lib/contrast/tasks/service.rb +2 -2
  224. data/lib/contrast/utils/assess/sampling_util.rb +2 -2
  225. data/lib/contrast/utils/assess/tracking_util.rb +49 -4
  226. data/lib/contrast/utils/class_util.rb +2 -2
  227. data/lib/contrast/utils/duck_utils.rb +0 -10
  228. data/lib/contrast/utils/env_configuration_item.rb +2 -1
  229. data/lib/contrast/utils/hash_digest.rb +2 -1
  230. data/lib/contrast/utils/heap_dump_util.rb +2 -2
  231. data/lib/contrast/utils/invalid_configuration_util.rb +21 -22
  232. data/lib/contrast/utils/inventory_util.rb +3 -10
  233. data/lib/contrast/utils/io_util.rb +1 -1
  234. data/lib/contrast/utils/os.rb +1 -1
  235. data/lib/contrast/utils/ruby_ast_rewriter.rb +1 -1
  236. data/lib/contrast/utils/sha256_builder.rb +2 -14
  237. data/lib/contrast/utils/stack_trace_utils.rb +2 -2
  238. data/lib/contrast/utils/string_utils.rb +11 -6
  239. data/resources/assess/policy.json +31 -22
  240. data/resources/deadzone/policy.json +5 -0
  241. data/ruby-agent.gemspec +21 -19
  242. data/service_executables/VERSION +1 -1
  243. data/service_executables/linux/contrast-service +0 -0
  244. data/service_executables/mac/contrast-service +0 -0
  245. metadata +73 -30
  246. data/lib/contrast/agent/assess/insulator.rb +0 -49
  247. data/lib/contrast/agent/require_state.rb +0 -61
  248. data/lib/contrast/extension/assess/assess_extension.rb +0 -147
  249. data/lib/contrast/utils/boolean_util.rb +0 -30
  250. data/lib/contrast/utils/freeze_util.rb +0 -32
  251. data/lib/contrast/utils/gemfile_reader.rb +0 -193
@@ -1,10 +1,10 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- cs__scoped_require 'contrast/api/dtm.pb'
5
- cs__scoped_require 'contrast/framework/base_support'
6
- cs__scoped_require 'contrast/framework/rails/patch/support'
7
- cs__scoped_require 'contrast/utils/string_utils'
4
+ require 'contrast/api/dtm.pb'
5
+ require 'contrast/framework/base_support'
6
+ require 'contrast/framework/rails/patch/support'
7
+ require 'contrast/utils/string_utils'
8
8
 
9
9
  module Contrast
10
10
  module Framework
@@ -42,12 +42,12 @@ module Contrast
42
42
  end
43
43
 
44
44
  def collect_routes
45
- routes = ::Rails.application.routes.routes.map do |route|
46
- Contrast::Api::Dtm::RouteCoverage.from_action_dispatch_journey(route)
47
- end
48
- routes
45
+ find_all_routes(::Rails.application, [])
49
46
  end
50
47
 
48
+ # Find the current route, based on the provided Request wrapper
49
+ # @param request[Contrast::Agent::Request]
50
+ # @return [Contrast::Api::Dtm::RouteCoverage]
51
51
  def current_route request
52
52
  return unless ::Rails.cs__respond_to?(:application)
53
53
 
@@ -59,13 +59,17 @@ module Contrast
59
59
  full_routes = ::Rails.application.routes.router.send(:find_routes, request.rack_request)
60
60
  return if full_routes.empty?
61
61
 
62
- full_route = full_routes[0] # [match_data, path_parameters, route]
63
- return unless full_route
64
-
65
- route = full_route[2] # route w/ highest precedence
66
- return unless route
62
+ full_route = full_routes[0]
67
63
 
68
- Contrast::Api::Dtm::RouteCoverage.from_action_dispatch_journey(route)
64
+ # the route is directly implemented within the application
65
+ if direct_route?(full_route)
66
+ route = full_route[2] # route w/ highest precedence
67
+ Contrast::Api::Dtm::RouteCoverage.from_action_dispatch_journey(route)
68
+ else
69
+ engine_route(full_route, request)
70
+ end
71
+ rescue StandardError => _e
72
+ nil
69
73
  end
70
74
 
71
75
  def retrieve_request env
@@ -79,6 +83,55 @@ module Contrast
79
83
 
80
84
  env[AC_INSTANCE].cs__class.included_modules.include?(::ActionController::Live)
81
85
  end
86
+
87
+ private
88
+
89
+ # route is not mounted within an engine
90
+ def direct_route? full_route
91
+ full_route[2]&.app&.cs__class == ActionDispatch::Routing::RouteSet::Dispatcher ||
92
+ (full_route[2].cs__class == ActionDispatch::Journey::Route && full_route[2]&.app&.cs__class == ActionDispatch::Routing::Mapper::Constraints)
93
+ end
94
+
95
+ def engine_route full_route, request
96
+ engine_route = full_route[2] # supposed route - but actually an Engine mount point
97
+ return unless engine_route
98
+
99
+ engine_mount_name = engine_route.name
100
+ return unless engine_mount_name
101
+
102
+ engine_path_segments = request.rack_request.path_info.split(engine_mount_name)
103
+ return if engine_path_segments.empty?
104
+
105
+ path_within_engine = engine_path_segments[-1]
106
+ return unless path_within_engine
107
+
108
+ engine_router = engine_route.app&.app&.routes&.router
109
+ return unless engine_router
110
+
111
+ # Get all routes regardless of http method
112
+ matching_routes = engine_router.send(:filter_routes, path_within_engine)
113
+ return unless matching_routes
114
+
115
+ # filter for current http method
116
+ reportable_routes = engine_router.send(:match_routes, matching_routes, request.rack_request)
117
+ return if reportable_routes.empty?
118
+
119
+ Contrast::Api::Dtm::RouteCoverage.from_action_dispatch_journey(reportable_routes[0])
120
+ end
121
+
122
+ # Rails engine routes need to be detected by inspecting Engine class route set
123
+ def find_all_routes app, route_list
124
+ return route_list unless app.cs__respond_to?(:routes) && app.routes.cs__respond_to?(:routes)
125
+
126
+ app.routes.routes.each do |route|
127
+ if route.cs__respond_to?(:app) && route.app.cs__class == ActionDispatch::Routing::RouteSet::Dispatcher
128
+ route_list << Contrast::Api::Dtm::RouteCoverage.from_action_dispatch_journey(route)
129
+ elsif route.app.app.cs__respond_to?(:routes)
130
+ route_list += find_all_routes(route.app.app, [])
131
+ end
132
+ end
133
+ route_list
134
+ end
82
135
  end
83
136
  end
84
137
  end
@@ -1,7 +1,7 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- cs__scoped_require 'contrast/framework/sinatra/support'
4
+ require 'contrast/framework/sinatra/support'
5
5
 
6
6
  module Contrast
7
7
  module Framework
@@ -34,17 +34,17 @@ module Contrast
34
34
 
35
35
  def instrument
36
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
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
48
  end
49
49
 
50
50
  private
@@ -1,7 +1,7 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- cs__scoped_require 'contrast/agent/patching/policy/after_load_patch'
4
+ require 'contrast/agent/patching/policy/after_load_patch'
5
5
 
6
6
  module Contrast
7
7
  module Framework
@@ -1,8 +1,8 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- cs__scoped_require 'contrast/framework/base_support'
5
- cs__scoped_require 'contrast/framework/sinatra/patch/support'
4
+ require 'contrast/framework/base_support'
5
+ require 'contrast/framework/sinatra/patch/support'
6
6
 
7
7
  module Contrast
8
8
  module Framework
@@ -70,10 +70,10 @@ module Contrast
70
70
  return nil unless defined?(::Sinatra) && defined?(::Sinatra::Base)
71
71
 
72
72
  @_app_class ||= begin
73
- sinatra_layers = ObjectSpace.each_object(::Sinatra::Base).to_a
74
- result_layer = sinatra_layers.find { |layer| layer.app.nil? }
75
- result_layer
76
- end
73
+ sinatra_layers = ObjectSpace.each_object(::Sinatra::Base).to_a
74
+ result_layer = sinatra_layers.find { |layer| layer.app.nil? }
75
+ result_layer
76
+ end
77
77
  end
78
78
 
79
79
  # Iterate over every class that extends Sinatra::Base, pull out its routes
@@ -1,7 +1,7 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- cs__scoped_require 'contrast/components/interface'
4
+ require 'contrast/components/interface'
5
5
  # This module is used to find funchook library and determine availability
6
6
  module Funchook
7
7
  include Contrast::Components::Interface
@@ -1,11 +1,11 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- cs__scoped_require 'contrast/components/interface'
4
+ require 'contrast/components/interface'
5
5
 
6
6
  module Contrast
7
7
  module Logger
8
- # # Our decorator for the Ougai logger allowing for the logging of the
8
+ # Our decorator for the Ougai logger allowing for the logging of the
9
9
  # application environment, used to provide context during troubleshooting.
10
10
  module Application
11
11
  include Contrast::Components::Interface
@@ -33,9 +33,17 @@ module Contrast
33
33
  def application_configuration
34
34
  return unless info?
35
35
 
36
- loggable = CONFIG.raw.send(:load_config)
37
- loggable.delete('api')
38
- info('Current configuration', configuration: JSON.pretty_generate(loggable))
36
+ loggable = CONFIG.loggable
37
+ info('Current configuration', configuration: loggable)
38
+ env_keys = ENV.keys.select { |env_key| env_key&.to_s&.start_with?(Contrast::Components::Config::CONTRAST_ENV_MARKER) }
39
+ env_items = env_keys.map { |env_key| Contrast::Utils::EnvConfigurationItem.new(env_key, nil) }
40
+ env_translations = env_items.each_with_object({}) do |conversion, hash|
41
+ hash[conversion.key] = conversion.dot_path_array.join('.')
42
+ end
43
+ info('Set by environment', overrides: env_translations)
44
+ rescue StandardError => e
45
+ puts e
46
+ sleep(5)
39
47
  end
40
48
 
41
49
  def application_libraries
@@ -1,8 +1,9 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- cs__scoped_require 'ougai'
5
- cs__scoped_require 'contrast/utils/thread_tracker'
4
+ require 'ougai'
5
+ require 'contrast/utils/object_share'
6
+ require 'contrast/utils/thread_tracker'
6
7
 
7
8
  module Contrast
8
9
  module Logger
@@ -31,21 +32,33 @@ module Contrast
31
32
 
32
33
  private
33
34
 
34
- # The extra values to add to the Bunyan hash. Because this is unique per
35
- # thread, we'll create it once and then store it, rather than create a
36
- # new hash on every log call.
35
+ # The extra values to add to the Bunyan hash.
37
36
  # @return [Hash{Symbol => Object}] the extra data to log
38
- # tid: current thread id
39
37
  def format_hash
40
- hash = LOG_TRACKER.get(:thread_id_hash)
38
+ thread_hash.merge(request_hash)
39
+ end
40
+
41
+ # Because this is unique per thread, we'll create it once and then store
42
+ # it, rather than create a new hash on every log call during the request.
43
+ def thread_hash
44
+ hash = LOG_TRACKER.get(:logging_hash)
41
45
  unless hash
42
46
  hash = {
43
- tid: Thread.current.object_id
47
+ thread_id: Thread.current.object_id
44
48
  }
45
- LOG_TRACKER.set(:thread_id_hash, hash)
49
+ LOG_TRACKER.set(:logging_hash, hash)
46
50
  end
47
51
  hash
48
52
  end
53
+
54
+ NO_REQUEST_HASH = { request_id: -1 }.cs__freeze
55
+ def request_hash
56
+ @request_tracker_defined ||= defined?(Contrast::Agent) &&
57
+ defined?(Contrast::Agent::REQUEST_TRACKER)
58
+ return NO_REQUEST_HASH unless @request_tracker_defined
59
+
60
+ Contrast::Agent::REQUEST_TRACKER&.current&.logging_hash || NO_REQUEST_HASH
61
+ end
49
62
  end
50
63
  end
51
64
  end
@@ -1,15 +1,16 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- cs__scoped_require 'logger'
5
- cs__scoped_require 'ougai'
6
- cs__scoped_require 'singleton'
4
+ require 'logger'
5
+ require 'ougai'
6
+ require 'singleton'
7
7
 
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/format'
12
- cs__scoped_require 'contrast/logger/time'
8
+ require 'contrast/components/interface'
9
+ require 'contrast/extension/module'
10
+ require 'contrast/logger/application'
11
+ require 'contrast/logger/format'
12
+ require 'contrast/logger/request'
13
+ require 'contrast/logger/time'
13
14
 
14
15
  module Contrast
15
16
  module Logger
@@ -48,14 +49,19 @@ module Contrast
48
49
  path = valid_path(config_path || log_file)
49
50
  level_const = valid_level(config_level || log_level)
50
51
 
52
+ path_change = path != previous_path
53
+ level_change = level_const != previous_level
54
+
51
55
  # don't needlessly recreate logger
52
- return if @_logger && (path == previous_path) && (level_const == previous_level)
56
+ return if @_logger && !(path_change || level_change)
53
57
 
54
58
  @previous_path = path
55
59
  @previous_level = level_const
56
60
 
57
61
  @_logger = build(path: path, level_const: level_const)
58
- log_update
62
+ # If we're logging to a new path, then let's start it w/ our helpful
63
+ # data gathering messages
64
+ log_update if path_change
59
65
  rescue StandardError => e
60
66
  if logger
61
67
  logger.error('Unable to process update to LoggerManager.', e)
@@ -98,6 +104,7 @@ module Contrast
98
104
 
99
105
  def add_contrast_loggers logger
100
106
  logger.extend(Contrast::Logger::Application)
107
+ logger.extend(Contrast::Logger::Request)
101
108
  logger.extend(Contrast::Logger::Time)
102
109
  end
103
110
 
@@ -0,0 +1,30 @@
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
+ require 'contrast/components/interface'
5
+ require 'contrast/utils/timer'
6
+
7
+ module Contrast
8
+ module Logger
9
+ # Our decorator for the Ougai logger allowing for the logging of the
10
+ # request lifecycle, used to provide context during troubleshooting.
11
+ module Request
12
+ include Contrast::Components::Interface
13
+ access_component :config
14
+
15
+ # Utility method to log the start of a request
16
+ def request_start
17
+ debug('Beginning request analysis')
18
+ end
19
+
20
+ # Utility method to log the end of a request, including the time it took
21
+ # for that request to be processed.
22
+ def request_end
23
+ context = Contrast::Agent::REQUEST_TRACKER.current
24
+ elapsed_time = context ? (Contrast::Utils::Timer.now_ms - context.timer.start_ms) : -1
25
+ debug('Ending request analysis',
26
+ elapsed_time_ms: elapsed_time)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,7 +1,7 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- cs__scoped_require 'yaml'
4
+ require 'yaml'
5
5
 
6
6
  module Contrast
7
7
  # A Rake task to generate a contrast_security.yaml file with some basic settings
@@ -1,8 +1,8 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- cs__scoped_require 'contrast/components/interface'
5
- cs__scoped_require 'contrast/utils/os'
4
+ require 'contrast/components/interface'
5
+ require 'contrast/utils/os'
6
6
 
7
7
  module Contrast
8
8
  # A Rake task designed to allow control of the Contrast Service as a stand
@@ -1,8 +1,8 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- cs__scoped_require 'singleton'
5
- cs__scoped_require 'contrast/components/interface'
4
+ require 'singleton'
5
+ require 'contrast/components/interface'
6
6
 
7
7
  module Contrast
8
8
  module Utils
@@ -1,7 +1,9 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- cs__scoped_require 'contrast/components/interface'
4
+ require 'contrast/agent/assess/tracker'
5
+ require 'contrast/components/interface'
6
+ require 'contrast/utils/duck_utils'
5
7
 
6
8
  module Contrast
7
9
  module Utils
@@ -23,6 +25,16 @@ module Contrast
23
25
  _tracked?(obj, 0)
24
26
  end
25
27
 
28
+ # Public interface to our tracking check, isolating the internals
29
+ # required for recursion.
30
+ #
31
+ # @param obj [Object] the thing to check if tracked
32
+ # @return [Boolean] if the obj, or something in it if a collection, is
33
+ # tracked.
34
+ def trackable? obj
35
+ _trackable?(obj, 0)
36
+ end
37
+
26
38
  private
27
39
 
28
40
  # Sometimes things are nested inside of each other, such as an Array
@@ -49,10 +61,8 @@ module Contrast
49
61
  obj.any? do |ele|
50
62
  _tracked?(ele, idx) unless obj == ele
51
63
  end
52
- elsif Contrast::Utils::DuckUtils.quacks_to?(obj, :cs__tracked?)
53
- obj.cs__tracked?
54
64
  else
55
- false
65
+ Contrast::Agent::Assess::Tracker.tracked?(obj)
56
66
  end
57
67
  rescue StandardError => e
58
68
  # This is used to ask if a ton of objects are tracked. They may not
@@ -61,6 +71,41 @@ module Contrast
61
71
  logger.warn('Failed to determine tracking', e, module: obj.cs__class)
62
72
  false
63
73
  end
74
+
75
+ # Sometimes things are nested inside of each other, such as an Array
76
+ # holding a Hash, holding that Array. In those cases, rather than
77
+ # entering an infinite loop, we'll break out.
78
+ # Right now, that level of nesting has been arbitrarily set to 10.
79
+ #
80
+ # @param obj [Object] the thing to check if trackable
81
+ # @param idx [Integer] the number of levels nested we've gone
82
+ # @return [Boolean] if the obj, or something in it if a collection, is
83
+ # trackable.
84
+ def _trackable? obj, idx
85
+ return false if obj.nil?
86
+ return false if idx > 10
87
+
88
+ idx += 1
89
+ if Contrast::Utils::DuckUtils.iterable_hash?(obj)
90
+ obj.each_pair do |k, v|
91
+ return true if _trackable?(k, idx)
92
+ return true if _trackable?(v, idx)
93
+ end
94
+ false
95
+ elsif Contrast::Utils::DuckUtils.iterable_enumerable?(obj)
96
+ obj.any? do |ele|
97
+ _trackable?(ele, idx) unless obj == ele
98
+ end
99
+ else
100
+ Contrast::Agent::Assess::Tracker.trackable?(obj)
101
+ end
102
+ rescue StandardError => e
103
+ # This is used to ask if a ton of objects are tracked. They may not
104
+ # all be iterable. Bad things could happen in some cases, like when
105
+ # checking a closed statement for SQL injection trigger events
106
+ logger.warn('Failed to determine trackable', e, module: obj.cs__class)
107
+ false
108
+ end
64
109
  end
65
110
  end
66
111
  end