Module: Datadog::AppSec::Contrib::Sinatra::DispatchPatch

Defined in:
lib/datadog/appsec/contrib/sinatra/patcher.rb

Overview

Hook into Base#dispatch!, which encompasses route filters

Instance Method Summary collapse

Instance Method Details

#dispatch!Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/datadog/appsec/contrib/sinatra/patcher.rb', line 52

def dispatch!
  env = @request.env

  context = env[Datadog::AppSec::Ext::SCOPE_KEY]

  return super unless context

  # TODO: handle exceptions, except for super

  gateway_request = Gateway::Request.new(env)

  request_return, request_response = Instrumentation.gateway.push('sinatra.request.dispatch', gateway_request) do
    # handle process_route interruption
    catch(Datadog::AppSec::Contrib::Sinatra::Ext::ROUTE_INTERRUPT) { super }
  end

  if request_response
    blocked_event = request_response.find { |action, _options| action == :block }
    if blocked_event
      self.response = AppSec::Response.negotiate(env, blocked_event.last[:actions]).to_sinatra_response
      request_return = nil
    end
  end

  request_return
end