# Copyright (c) 2015 Sqreen. All Rights Reserved. # Please refer to our terms for more information: https://www.sqreen.io/terms.html module Sqreen module Dependency module Sinatra module_function def required? Sqreen::Dependency.const_exist?('Sinatra::Base') end def insert_sqreen_middlewares(builder, *args, &block) Sqreen.log.debug { 'Inserting Sqreen middlewares for Sinatra' } middleware = Sqreen::ErrorHandlingMiddleware use = builder.instance_variable_get('@use') p = proc { |app| middleware.new(app, *args, &block) } return if middlewares(builder).include?(middleware) if middlewares(builder).include?(::Sinatra::ShowExceptions) Sqreen.log.warn('Sinatra :show_exceptions detected: Sinatra exception handling may prevent the Sqreen error page to display on attacks.') end if (i = middlewares(builder).index(::Rack::Head)) use.insert(i, p) elsif (i = middlewares(builder).index(::Rack::MethodOverride)) use.insert(i + 1, p) elsif (i = middlewares(builder).index(::Sinatra::ExtendedRack)) use.insert(i + 1, p) else use.insert(0, p) end end def inspect_middlewares(builder) Sqreen.log.debug do "Middlewares: " << middlewares(builder).map(&:inspect).inspect end end def middlewares(builder) builder.instance_variable_get(:@use).map do |p| unless p.respond_to?(:binding) && p.binding.local_variable_defined?(:middleware) next :unknown end p.binding.local_variable_get(:middleware) end end end end end