aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorJamis Buck <jamis@37signals.com>2006-06-07 16:16:37 +0000
committerJamis Buck <jamis@37signals.com>2006-06-07 16:16:37 +0000
commitae7029ddd1fe11890479df119d0f5996c40c80de (patch)
tree4ca4b96ee306b8c116edff2472938904c1845baf /actionpack
parent4e3543b46749a83e4ccf1e9346b4dfe6d1a03233 (diff)
downloadrails-ae7029ddd1fe11890479df119d0f5996c40c80de.tar.gz
rails-ae7029ddd1fe11890479df119d0f5996c40c80de.tar.bz2
rails-ae7029ddd1fe11890479df119d0f5996c40c80de.zip
Do some path normalization to prevent the possible_controllers list from containing invalid entries
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4443 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_controller/routing.rb36
-rw-r--r--actionpack/test/controller/routing_test.rb13
2 files changed, 43 insertions, 6 deletions
diff --git a/actionpack/lib/action_controller/routing.rb b/actionpack/lib/action_controller/routing.rb
index 70c864646d..14921c3ac0 100644
--- a/actionpack/lib/action_controller/routing.rb
+++ b/actionpack/lib/action_controller/routing.rb
@@ -57,28 +57,54 @@ module ActionController
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
diff --git a/actionpack/test/controller/routing_test.rb b/actionpack/test/controller/routing_test.rb
index 970bdc4a1a..44e986e3d5 100644
--- a/actionpack/test/controller/routing_test.rb
+++ b/actionpack/test/controller/routing_test.rb
@@ -1425,5 +1425,16 @@ class RoutingTest < Test::Unit::TestCase
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