From a8837fbac026ddd6bc8b200530c430fc0bc8ee07 Mon Sep 17 00:00:00 2001 From: Christopher Owen Date: Fri, 25 Sep 2009 10:24:48 +1000 Subject: [PATCH] Fix local_request? for ipv6 connections --- .../action_dispatch/middleware/show_exceptions.rb | 12 +++++-- actionpack/test/dispatch/show_exceptions_test.rb | 37 ++++++++++++-------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/actionpack/lib/action_dispatch/middleware/show_exceptions.rb b/actionpack/lib/action_dispatch/middleware/show_exceptions.rb index 036deec..d0906d6 100644 --- a/actionpack/lib/action_dispatch/middleware/show_exceptions.rb +++ b/actionpack/lib/action_dispatch/middleware/show_exceptions.rb @@ -1,10 +1,14 @@ require "active_support/core_ext/exception" +require "ipaddr" module ActionDispatch class ShowExceptions include StatusCodes - LOCALHOST = '127.0.0.1'.freeze + LOCALHOSTS = [ + IPAddr.new("127.0.0.0/8"), # IPv4 loopback range + IPAddr.new("::1") # IPv6 loopback address + ].freeze RESCUES_TEMPLATE_PATH = File.join(File.dirname(__FILE__), 'templates') @@ -98,9 +102,11 @@ module ActionDispatch end end - # True if the request came from localhost, 127.0.0.1. + # True if the request came from localhost def local_request?(request) - request.remote_addr == LOCALHOST && request.remote_ip == LOCALHOST + [request.remote_addr, request.remote_ip].all? do |addr| + LOCALHOSTS.any? { |lo| lo === addr } + end end def status_code(exception) diff --git a/actionpack/test/dispatch/show_exceptions_test.rb b/actionpack/test/dispatch/show_exceptions_test.rb index ce19738..2168b7e 100644 --- a/actionpack/test/dispatch/show_exceptions_test.rb +++ b/actionpack/test/dispatch/show_exceptions_test.rb @@ -51,21 +51,28 @@ class ShowExceptionsTest < ActionController::IntegrationTest assert_equal "", body end - test "rescue locally from a local request" do - @integration_session = open_session(ProductionApp) - self.remote_addr = '127.0.0.1' - - get "/" - assert_response 500 - assert_match /puke/, body - - get "/not_found" - assert_response 404 - assert_match /#{ActionController::UnknownAction.name}/, body - - get "/method_not_allowed" - assert_response 405 - assert_match /ActionController::MethodNotAllowed/, body + { + :IPv4 => ['127.0.0.1', '127.0.0.34', '127.0.0.254'], + :IPv6 => ['::1', '0000::1', '0:0:0:0:0:0:0:1'] + }.each do |addr_family, addrs| + addrs.to_a.each do |addr| + test "rescue locally from a local #{addr_family} request on #{addr}" do + @integration_session = open_session(ProductionApp) + self.remote_addr = addr + + get "/" + assert_response 500 + assert_match /puke/, body + + get "/not_found" + assert_response 404 + assert_match /#{ActionController::UnknownAction.name}/, body + + get "/method_not_allowed" + assert_response 405 + assert_match /ActionController::MethodNotAllowed/, body + end + end end test "localize public rescue message" do -- 1.6.3.1