Module: Datadog::AppSec::RouteNormalizer Private

Defined in:
lib/datadog/appsec/route_normalizer.rb,
lib/datadog/appsec/route_normalizer/route_text.rb,
lib/datadog/appsec/route_normalizer/route_pattern.rb,
lib/datadog/appsec/route_normalizer/rails_route_pattern.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Extracts framework route data from Rack env and normalizes it into OpenAPI v3 compatible route format

Defined Under Namespace

Modules: RouteText Classes: RailsRoutePattern, RoutePattern

Constant Summary collapse

GRAPE_ROUTE_KEY =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"grape.routing_args"
SINATRA_ROUTE_KEY =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"sinatra.route"
DATADOG_ROUTE_KEY =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"datadog.action_dispatch.route"
RAILS_ROUTE_KEY =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"action_dispatch.route"
RAILS_ROUTE_URI_PATTERN_KEY =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"action_dispatch.route_uri_pattern"
RAILS_PATH_PARAMS_KEY =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"action_dispatch.request.path_parameters"

Class Method Summary collapse

Class Method Details

.extract_normalized_route(rack_env, prefix: nil, pattern: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Extracts a normalized route path for a Rack request.

Examples:

Grape:   "/api/users/:id"              => "/api/users/{id}"
Sinatra: "GET /download/*.*"           => "/download/{param1+param2}"
Rails:   "/posts/:id(.:format)" + path => "/posts/{id+format}"
Pattern: "/users/:id"                  => "/users/{id}"


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/datadog/appsec/route_normalizer.rb', line 30

def extract_normalized_route(rack_env, prefix: nil, pattern: nil)
  route_prefix = prefix || rack_env["SCRIPT_NAME"]

  if rack_env.key?(GRAPE_ROUTE_KEY)
    route_info = rack_env[GRAPE_ROUTE_KEY][:route_info]
    normalize_route_string(route_info&.pattern&.origin)
  elsif rack_env.key?(SINATRA_ROUTE_KEY)
    normalize_route_string(rack_env[SINATRA_ROUTE_KEY].split(" ", 2)[1])
  elsif (rails_route = rack_env[DATADOG_ROUTE_KEY] || rack_env[RAILS_ROUTE_KEY])
    normalize_rails_route(rails_route, rack_env, route_prefix)
  elsif rack_env.key?(RAILS_ROUTE_URI_PATTERN_KEY)
    normalize_rails_route(rack_env[RAILS_ROUTE_URI_PATTERN_KEY], rack_env, route_prefix)
  else
    normalize_route_string(pattern)
  end
rescue => e
  AppSec.telemetry&.report(e, description: "AppSec: Could not compute normalized route")

  nil
end