Module: Datadog::AppSec::Contrib::Sinatra::RoutePatch

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

Overview

Hook into Base#route_eval, which path params are returned by pattern.params in process_route, then merged with normal params, so we get both

Instance Method Summary collapse

Instance Method Details

#process_routeObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/datadog/appsec/contrib/sinatra/patcher.rb', line 75

def process_route(*)
  env = @request.env

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

  return super unless context

  # process_route is called repeatedly until a route is found.
  # Until then, params has no route params.
  # Capture normal params.
  base_params = params

  super do |*args|
    # This block is called only once the route is found.
    # At this point params has both route params and normal params.
    route_params = params.each.with_object({}) { |(k, v), h| h[k] = v unless base_params.key?(k) }

    gateway_request = Gateway::Request.new(env)
    gateway_route_params = Gateway::RouteParams.new(route_params)

    Instrumentation.gateway.push('sinatra.request.routed', [gateway_request, gateway_route_params])

    yield(*args)
  end
end