Class: Datadog::AppSec::RouteNormalizer::RailsRoutePattern Private

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/appsec/route_normalizer/rails_route_pattern.rb

Overview

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

Normalizes Rails route patterns into route format, inspired by OpenAPI v3 path templating

NOTE: Uses the parsed Journey AST when available

Defined Under Namespace

Classes: Buffer

Constant Summary collapse

DOT_CHAR =

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.

"."
GROUP_OPEN_CHAR =

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.

"("
OPTIONAL_GROUP_PATTERN =

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.

/\(([^()]*)\)/
NAMED_PARAM_PREFIX_CHAR =

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.

":"
GLOB_PARAM_PREFIX_CHAR =

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.

"*"

Instance Method Summary collapse

Constructor Details

#initialize(pattern) ⇒ RailsRoutePattern

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.

Returns a new instance of RailsRoutePattern.



22
23
24
# File 'lib/datadog/appsec/route_normalizer/rails_route_pattern.rb', line 22

def initialize(pattern)
  @pattern = pattern
end

Instance Method Details

#normalize(path_params:, path:) ⇒ 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.



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

def normalize(path_params:, path:)
  @path_params = path_params
  @path = path

  if @pattern.is_a?(String)
    # NOTE: Journey groups without route params are never kept
    #       Example: `/foo(/bar)` with request `/foo/bar` normalizes to `/foo`
    pattern = remove_paramless_optional_groups(@pattern)
    return RoutePattern.new(pattern).normalize(path: path)
  end

  route_path = @pattern.path
  route_spec = route_path.spec

  unless route_spec.respond_to?(:type)
    return RoutePattern.new(route_spec.to_s).normalize(path: path)
  end

  if route_path.names.empty?
    route_string = route_spec.to_s
    return RouteText.escape(route_string) unless route_string.include?(GROUP_OPEN_CHAR)
  end

  buffer = Buffer.new
  visit_route_node(route_spec, buffer)
  buffer.to_path
end