aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2014-10-10 16:00:03 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2014-10-10 16:00:03 -0700
commitd1123f2056eff3696ae76e5116a6ab53e6c33f57 (patch)
treeda217ab0eb56ae97895f7994d68dd1922bf35cdf /actionpack
parent51278579477eb7ee20fe2aba53b4b13203791b22 (diff)
downloadrails-d1123f2056eff3696ae76e5116a6ab53e6c33f57.tar.gz
rails-d1123f2056eff3696ae76e5116a6ab53e6c33f57.tar.bz2
rails-d1123f2056eff3696ae76e5116a6ab53e6c33f57.zip
FileHandler should not be called for files outside the root
FileHandler#matches? should return false for files that are outside the "root" path.
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_dispatch/middleware/static.rb14
-rw-r--r--actionpack/test/dispatch/static_test.rb22
2 files changed, 32 insertions, 4 deletions
diff --git a/actionpack/lib/action_dispatch/middleware/static.rb b/actionpack/lib/action_dispatch/middleware/static.rb
index e66c21ef85..002bf8b11a 100644
--- a/actionpack/lib/action_dispatch/middleware/static.rb
+++ b/actionpack/lib/action_dispatch/middleware/static.rb
@@ -24,9 +24,19 @@ module ActionDispatch
path = URI.parser.unescape(path)
return false unless path.valid_encoding?
- paths = [path, "#{path}#{ext}", "#{path}/index#{ext}"]
+ paths = [path, "#{path}#{ext}", "#{path}/index#{ext}"].map { |v|
+ Rack::Utils.clean_path_info v
+ }
- if match = paths.detect {|p| File.file?(File.join(@root, p)) }
+ if match = paths.detect { |p|
+ path = File.join(@root, p)
+ begin
+ File.file?(path) && File.readable?(path)
+ rescue SystemCallError
+ false
+ end
+
+ }
return ::Rack::Utils.escape(match)
end
end
diff --git a/actionpack/test/dispatch/static_test.rb b/actionpack/test/dispatch/static_test.rb
index 6f7373201c..7f1207eaed 100644
--- a/actionpack/test/dispatch/static_test.rb
+++ b/actionpack/test/dispatch/static_test.rb
@@ -200,7 +200,8 @@ class StaticTest < ActiveSupport::TestCase
}
def setup
- @app = ActionDispatch::Static.new(DummyApp, "#{FIXTURE_LOAD_PATH}/public", "public, max-age=60")
+ @root = "#{FIXTURE_LOAD_PATH}/public"
+ @app = ActionDispatch::Static.new(DummyApp, @root, "public, max-age=60")
end
def public_path
@@ -208,11 +209,28 @@ class StaticTest < ActiveSupport::TestCase
end
include StaticTests
+
+ def test_custom_handler_called_when_file_is_outside_root
+ filename = 'shared.html.erb'
+ assert File.exist?(File.join(@root, '..', filename))
+ env = {
+ "REQUEST_METHOD"=>"GET",
+ "REQUEST_PATH"=>"/..%2F#{filename}",
+ "PATH_INFO"=>"/..%2F#{filename}",
+ "REQUEST_URI"=>"/..%2F#{filename}",
+ "HTTP_VERSION"=>"HTTP/1.1",
+ "SERVER_NAME"=>"localhost",
+ "SERVER_PORT"=>"8080",
+ "QUERY_STRING"=>""
+ }
+ assert_equal(DummyApp.call(nil), @app.call(env))
+ end
end
class StaticEncodingTest < StaticTest
def setup
- @app = ActionDispatch::Static.new(DummyApp, "#{FIXTURE_LOAD_PATH}/公共", "public, max-age=60")
+ @root = "#{FIXTURE_LOAD_PATH}/公共"
+ @app = ActionDispatch::Static.new(DummyApp, @root, "public, max-age=60")
end
def public_path