Skip to content
Browse files

Do some path normalization to prevent the possible_controllers list f…

…rom containing invalid entries

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4443 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 4e3543b commit ae7029ddd1fe11890479df119d0f5996c40c80de @jamis jamis committed Jun 7, 2006
Showing with 43 additions and 6 deletions.
  1. +31 −5 actionpack/lib/action_controller/routing.rb
  2. +12 −1 actionpack/test/controller/routing_test.rb
View
36 actionpack/lib/action_controller/routing.rb
@@ -57,28 +57,54 @@ def with_controllers(names)
ensure
use_controllers! nil
end
-
+
+ def normalize_paths(paths=$LOAD_PATH)
+ # do the hokey-pokey of path normalization...
+ paths = paths.collect do |path|
+ path = path.
+ gsub("//", "/"). # replace double / chars with a single
+ gsub("\\\\", "\\"). # replace double \ chars with a single
+ gsub(%r{(.)[\\/]$}, '\1') # drop final / or \ if path ends with it
+
+ # eliminate .. paths where possible
+ re = %r{\w+[/\\]\.\.[/\\]}
+ path.gsub!(%r{\w+[/\\]\.\.[/\\]}, "") while path.match(re)
+ path
+ end
+
+ # start with longest path, first
+ paths = paths.uniq.sort_by { |path| - path.length }
+ end
+
def possible_controllers
unless @possible_controllers
@possible_controllers = []
paths = $LOAD_PATH.select { |path| File.directory? path }
- paths = paths.sort_by { |path| - path.length }
-
+
seen_paths = Hash.new {|h, k| h[k] = true; false}
- paths.each do |load_path|
+ normalize_paths(paths).each do |load_path|
Dir["#{load_path}/**/*_controller.rb"].collect do |path|
- next if seen_paths[path]
+ next if seen_paths[path.gsub(%r{^\.[/\\]}, "")]
controller_name = path[(load_path.length + 1)..-1]
+ next unless path_may_be_controller?(controller_name)
+
controller_name.gsub!(/_controller\.rb\Z/, '')
@possible_controllers << controller_name
end
end
+
+ # remove duplicates
+ @possible_controllers.uniq!
end
@possible_controllers
end
+ def path_may_be_controller?(path)
+ path !~ /(?:rails\/.*\/(?:examples|test))|(?:actionpack\/lib\/action_controller.rb$)|(?:app\/controllers)/o
+ end
+
def use_controllers!(controller_names)
@possible_controllers = controller_names
end
View
13 actionpack/test/controller/routing_test.rb
@@ -1425,5 +1425,16 @@ def test_with_controllers
assert_equal c, ActionController::Routing.possible_controllers
end
end
-
+
+ def test_normalize_unix_paths
+ load_paths = %w(. config/../app/controllers config/../app//helpers script/../config/../vendor/rails/actionpack/lib vendor/rails/railties/builtin/rails_info app/models lib script/../config/../foo/bar/../../app/models)
+ paths = ActionController::Routing.normalize_paths(load_paths)
+ assert_equal %w(vendor/rails/railties/builtin/rails_info vendor/rails/actionpack/lib app/controllers app/helpers app/models lib .), paths
+ end
+
+ def test_normalize_windows_paths
+ load_paths = %w(. config\\..\\app\\controllers config\\..\\app\\\\helpers script\\..\\config\\..\\vendor\\rails\\actionpack\\lib vendor\\rails\\railties\\builtin\\rails_info app\\models lib script\\..\\config\\..\\foo\\bar\\..\\..\\app\\models)
+ paths = ActionController::Routing.normalize_paths(load_paths)
+ assert_equal %w(vendor\\rails\\railties\\builtin\\rails_info vendor\\rails\\actionpack\\lib app\\controllers app\\helpers app\\models lib .), paths
+ end
end

0 comments on commit ae7029d

Please sign in to comment.
Something went wrong with that request. Please try again.