Class: Rack::Protection::PathTraversal

Inherits:
Base show all
Defined in:
lib/vendor/rack-protection-1.5.1/lib/rack/protection/path_traversal.rb

Overview

Prevented attack

Directory traversal

Supported browsers

all

More infos

en.wikipedia.org/wiki/Directory_traversal

Unescapes ‘/’ and ‘.’, expands path_info. Thus GET /foo/%2e%2e%2fbar becomes GET /bar.

Constant Summary

Constants inherited from Base

Base::DEFAULT_OPTIONS

Instance Attribute Summary

Attributes inherited from Base

#app, #options

Instance Method Summary collapse

Methods inherited from Base

#accepts?, #default_options, default_options, default_reaction, #deny, #drop_session, #encrypt, #html?, #initialize, #instrument, #origin, #random_string, #react, #referrer, #report, #safe?, #session, #session?, #warn

Constructor Details

This class inherits a constructor from Rack::Protection::Base

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/vendor/rack-protection-1.5.1/lib/rack/protection/path_traversal.rb', line 13

def call(env)
  path_was         = env["PATH_INFO"]
  env["PATH_INFO"] = cleanup path_was if path_was && !path_was.empty?
  app.call env
ensure
  env["PATH_INFO"] = path_was
end

#cleanup(path) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vendor/rack-protection-1.5.1/lib/rack/protection/path_traversal.rb', line 21

def cleanup(path)
  if path.respond_to?(:encoding)
    # Ruby 1.9+ M17N
    encoding = path.encoding
    dot   = '.'.encode(encoding)
    slash = '/'.encode(encoding)
  else
    # Ruby 1.8
    dot   = '.'
    slash = '/'
  end

  parts     = []
  unescaped = path.gsub(/%2e/i, dot).gsub(/%2f/i, slash)

  unescaped.split(slash).each do |part|
    next if part.empty? or part == dot
    part == '..' ? parts.pop : parts << part
  end

  cleaned = slash + parts.join(slash)
  cleaned << slash if parts.any? and unescaped =~ %r{/\.{0,2}$}
  cleaned
end