diff --git a/rack-protection/lib/rack/protection/path_traversal.rb b/rack-protection/lib/rack/protection/path_traversal.rb index a6ca87f065..4e8c94ca95 100644 --- a/rack-protection/lib/rack/protection/path_traversal.rb +++ b/rack-protection/lib/rack/protection/path_traversal.rb @@ -19,18 +19,10 @@ def call(env) end def cleanup(path) - if path.respond_to?(:encoding) - # Ruby 1.9+ M17N - encoding = path.encoding - dot = '.'.encode(encoding) - slash = '/'.encode(encoding) - backslash = '\\'.encode(encoding) - else - # Ruby 1.8 - dot = '.' - slash = '/' - backslash = '\\' - end + encoding = path.encoding + dot = '.'.encode(encoding) + slash = '/'.encode(encoding) + backslash = '\\'.encode(encoding) parts = [] unescaped = path.gsub(/%2e/i, dot).gsub(/%2f/i, slash).gsub(/%5c/i, backslash) diff --git a/rack-protection/spec/lib/rack/protection/path_traversal_spec.rb b/rack-protection/spec/lib/rack/protection/path_traversal_spec.rb index a4d52ff2c4..0e191cf2fc 100644 --- a/rack-protection/spec/lib/rack/protection/path_traversal_spec.rb +++ b/rack-protection/spec/lib/rack/protection/path_traversal_spec.rb @@ -24,16 +24,14 @@ end end - if "".respond_to?(:encoding) # Ruby 1.9+ M17N - context "PATH_INFO's encoding" do - before do - @app = Rack::Protection::PathTraversal.new(proc { |e| [200, {'Content-Type' => 'text/plain'}, [e['PATH_INFO'].encoding.to_s]] }) - end + context "PATH_INFO's encoding" do + before do + @app = Rack::Protection::PathTraversal.new(proc { |e| [200, {'Content-Type' => 'text/plain'}, [e['PATH_INFO'].encoding.to_s]] }) + end - it 'should remain unchanged as ASCII-8BIT' do - body = @app.call({ 'PATH_INFO' => '/'.encode('ASCII-8BIT') })[2][0] - expect(body).to eq('ASCII-8BIT') - end + it 'should remain unchanged as ASCII-8BIT' do + body = @app.call({ 'PATH_INFO' => '/'.encode('ASCII-8BIT') })[2][0] + expect(body).to eq('ASCII-8BIT') end end end