aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSantiago Pastorino <santiago@wyeworks.com>2010-08-13 17:34:20 -0300
committerSantiago Pastorino <santiago@wyeworks.com>2010-08-13 17:35:52 -0300
commit919888503d481c3bd21a33acd3cd1018fc48f500 (patch)
treebfea4bc408e3ab968556f5ce20a7464206e14e9d
parentb9eec677c4ca28203124a9b5b160a57ca13b95f1 (diff)
downloadrails-919888503d481c3bd21a33acd3cd1018fc48f500.tar.gz
rails-919888503d481c3bd21a33acd3cd1018fc48f500.tar.bz2
rails-919888503d481c3bd21a33acd3cd1018fc48f500.zip
Moves local_request? to require.local?
[#5361 state:committed]
-rw-r--r--actionpack/lib/action_dispatch/http/request.rb7
-rw-r--r--actionpack/lib/action_dispatch/middleware/show_exceptions.rb9
-rw-r--r--railties/lib/rails/info_controller.rb2
-rw-r--r--railties/test/rails_info_controller_test.rb10
4 files changed, 15 insertions, 13 deletions
diff --git a/actionpack/lib/action_dispatch/http/request.rb b/actionpack/lib/action_dispatch/http/request.rb
index fd23b1df79..5606d6abfe 100644
--- a/actionpack/lib/action_dispatch/http/request.rb
+++ b/actionpack/lib/action_dispatch/http/request.rb
@@ -15,6 +15,8 @@ module ActionDispatch
include ActionDispatch::Http::Upload
include ActionDispatch::Http::URL
+ LOCALHOST = [/^127\.0\.0\.\d{1,3}$/, "::1", /^0:0:0:0:0:0:0:1(%.*)?$/].freeze
+
%w[ AUTH_TYPE GATEWAY_INTERFACE
PATH_TRANSLATED REMOTE_HOST
REMOTE_IDENT REMOTE_USER REMOTE_ADDR
@@ -231,5 +233,10 @@ module ActionDispatch
@env['X_HTTP_AUTHORIZATION'] ||
@env['REDIRECT_X_HTTP_AUTHORIZATION']
end
+
+ # True if the request came from localhost, 127.0.0.1.
+ def local?
+ LOCALHOST.any? { |local_ip| local_ip === remote_addr && local_ip === remote_ip }
+ end
end
end
diff --git a/actionpack/lib/action_dispatch/middleware/show_exceptions.rb b/actionpack/lib/action_dispatch/middleware/show_exceptions.rb
index e095b51342..a7d3cb473f 100644
--- a/actionpack/lib/action_dispatch/middleware/show_exceptions.rb
+++ b/actionpack/lib/action_dispatch/middleware/show_exceptions.rb
@@ -6,8 +6,6 @@ module ActionDispatch
# This middleware rescues any exception returned by the application and renders
# nice exception pages if it's being rescued locally.
class ShowExceptions
- LOCALHOST = [/^127\.0\.0\.\d{1,3}$/, "::1", /^0:0:0:0:0:0:0:1(%.*)?$/].freeze
-
RESCUES_TEMPLATE_PATH = File.join(File.dirname(__FILE__), 'templates')
cattr_accessor :rescue_responses
@@ -66,7 +64,7 @@ module ActionDispatch
log_error(exception)
request = Request.new(env)
- if @consider_all_requests_local || local_request?(request)
+ if @consider_all_requests_local || request.local?
rescue_action_locally(request, exception)
else
rescue_action_in_public(exception)
@@ -112,11 +110,6 @@ module ActionDispatch
end
end
- # True if the request came from localhost, 127.0.0.1.
- def local_request?(request)
- LOCALHOST.any? { |local_ip| local_ip === request.remote_addr && local_ip === request.remote_ip }
- end
-
def status_code(exception)
Rack::Utils.status_code(@@rescue_responses[exception.class.name])
end
diff --git a/railties/lib/rails/info_controller.rb b/railties/lib/rails/info_controller.rb
index 196eeb4a6c..6b4bdb2921 100644
--- a/railties/lib/rails/info_controller.rb
+++ b/railties/lib/rails/info_controller.rb
@@ -1,6 +1,6 @@
class Rails::InfoController < ActionController::Base
def properties
- if consider_all_requests_local? || local_request?
+ if consider_all_requests_local? || request.local?
render :inline => Rails::Info.to_html
else
render :text => '<p>For security purposes, this information is only available to local requests.</p>', :status => :forbidden
diff --git a/railties/test/rails_info_controller_test.rb b/railties/test/rails_info_controller_test.rb
index 687c2d1568..9d194f41a6 100644
--- a/railties/test/rails_info_controller_test.rb
+++ b/railties/test/rails_info_controller_test.rb
@@ -14,26 +14,28 @@ class InfoControllerTest < ActionController::TestCase
Rails.application.routes.draw do
match '/rails/info/properties' => "rails/info#properties"
end
- @controller.stubs(:consider_all_requests_local? => false, :local_request? => true)
+ @request.stubs(:local? => true)
+ @controller.stubs(:consider_all_requests_local? => false)
@routes = Rails.application.routes
Rails::InfoController.send(:include, @routes.url_helpers)
end
test "info controller does not allow remote requests" do
- @controller.stubs(:consider_all_requests_local? => false, :local_request? => false)
+ @request.stubs(:local? => false)
get :properties
assert_response :forbidden
end
test "info controller renders an error message when request was forbidden" do
- @controller.stubs(:consider_all_requests_local? => false, :local_request? => false)
+ @request.stubs(:local? => false)
get :properties
assert_select 'p'
end
test "info controller allows requests when all requests are considered local" do
- @controller.stubs(:consider_all_requests_local? => true, :local_request? => false)
+ @request.stubs(:local? => false)
+ @controller.stubs(:consider_all_requests_local? => true)
get :properties
assert_response :success
end