Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
FileHandler should not be called for files outside the root
FileHandler#matches? should return false for files that are outside the
"root" path.

Conflicts:
	actionpack/lib/action_dispatch/middleware/static.rb
  • Loading branch information
tenderlove committed Oct 29, 2014
1 parent 5aef91e commit c05f3bd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
22 changes: 21 additions & 1 deletion actionpack/lib/action_dispatch/middleware/static.rb
Expand Up @@ -14,7 +14,8 @@ def match?(path)
path = unescape_path(path)
return false unless path.valid_encoding?

full_path = path.empty? ? @root : File.join(@root, escape_glob_chars(path))
full_path = path.empty? ? @root : File.join(@root,
clean_path_info(escape_glob_chars(path)))
paths = "#{full_path}#{ext}"

matches = Dir[paths]
Expand Down Expand Up @@ -43,6 +44,25 @@ def unescape_path(path)
def escape_glob_chars(path)
path.gsub(/[*?{}\[\]]/, "\\\\\\&")
end

private

PATH_SEPS = Regexp.union(*[::File::SEPARATOR, ::File::ALT_SEPARATOR].compact)

def clean_path_info(path_info)
parts = path_info.split PATH_SEPS

clean = []

parts.each do |part|
next if part.empty? || part == '.'
part == '..' ? clean.pop : clean << part
end

clean.unshift '/' if parts.empty? || parts.first.empty?

::File.join(*clean)
end
end

class Static
Expand Down
22 changes: 20 additions & 2 deletions actionpack/test/dispatch/static_test.rb
Expand Up @@ -147,19 +147,37 @@ 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
"public"
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
Expand Down

0 comments on commit c05f3bd

Please sign in to comment.