lib/contrast/framework/sinatra/support.rb in contrast-agent-4.14.1 vs lib/contrast/framework/sinatra/support.rb in contrast-agent-5.0.0

- old
+ new

@@ -1,9 +1,10 @@ -# Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details. +# Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details. # frozen_string_literal: true require 'contrast/framework/base_support' +require 'contrast/agent/reporting/report' module Contrast module Framework module Sinatra # Used when Sinatra is present to define framework specific behavior @@ -80,10 +81,31 @@ full_route ||= request.path_info Contrast::Api::Dtm::RouteCoverage.from_sinatra_route(final_controller, method, route_pattern, full_route) end + # Given the current request - return a RouteCoverage object + + # @param request [Contrast::Agent::Request] a contrast tracked request. + # @param controller [::Sinatra::Base] optionally use this controller instead of global ::Sinatra::Base. + # @return [Contrast::Agent::Reporting::RouteCoverage, nil] a Dtm describing the route + # matched to the request if a match was found. + def current_route_coverage request, controller = ::Sinatra::Base, full_route = nil + return unless sinatra_controller?(controller) + + method = request.env[::Rack::REQUEST_METHOD] # GET, PUT, POST, etc... + + # Find route match--checking superclasses if necessary. + final_controller, route_pattern = _route_recurse(controller, method, _cleaned_route(request)) + return unless final_controller + + full_route ||= request.env[::Rack::PATH_INFO] + + new_route_coverage = Contrast::Agent::Reporting::RouteCoverage.new + new_route_coverage.attach_rack_based_data final_controller, method, route_pattern, full_route + end + # Search object space for sinatra controllers--any class that subclasses ::Sinatra::Base. # # @return [Array<::Sinatra::Base>] sinatra controlelrs def sinatra_controllers [::Sinatra::Base] + ObjectSpace.each_object(Class).select { |clazz| sinatra_controller?(clazz) } @@ -98,10 +120,10 @@ # Given a controller and a route to match against, find the route_pattern and class that will serve the # route. This is recursive as Sinatra's routing is recursive from subclass to super. # # @param controller [Sinatra::Base, #routes] a Sinatra application. # @param method [::Rack::REQUEST_METHOD] GET, POST, PUT, etc... - # @param method [String] the relative route passed from Rack. + # @param route [String] the relative route passed from Rack. # @return [Array[Sinatra::Base, Mustermann::Sinatra], nil] Either the controller that # will handle the route along with the route pattern or nil if no match. def _route_recurse controller, method, route return if controller.nil? || controller.cs__class == NilClass