File: path_normalizer.rb

package info (click to toggle)
ruby-rack-attack 6.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 380 kB
  • sloc: ruby: 2,626; makefile: 4
file content (28 lines) | stat: -rw-r--r-- 987 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# frozen_string_literal: true

module Rack
  class Attack
    # When using Rack::Attack with a Rails app, developers expect the request path
    # to be normalized. In particular, trailing slashes are stripped.
    # (See
    # https://github.com/rails/rails/blob/f8edd20/actionpack/lib/action_dispatch/journey/router/utils.rb#L5-L22
    # for implementation.)
    #
    # Look for an ActionDispatch utility class that Rails folks would expect
    # to normalize request paths. If unavailable, use a fallback class that
    # doesn't normalize the path (as a non-Rails rack app developer expects).

    module FallbackPathNormalizer
      def self.normalize_path(path)
        path
      end
    end

    PathNormalizer = if defined?(::ActionDispatch::Journey::Router::Utils)
                       # For Rails apps
                       ::ActionDispatch::Journey::Router::Utils
                     else
                       FallbackPathNormalizer
                     end
  end
end