提交 95ab4fd6 编写于 作者: R Rafael Mendonça França

Merge pull request #11166 from xavier/callable_constraint_verification

Callable route constraint verification

Conflicts:
	actionpack/CHANGELOG.md
* Added verification of route constraints given as a Proc or an object responding
to `:matches?`. Previously, when given an non-complying object, it would just
silently fail to enforce the constraint. It will now raise an ArgumentError
when setting up the routes.
*Xavier Defrang*
* Properly treat the entire IPv6 User Local Address space as private for * Properly treat the entire IPv6 User Local Address space as private for
purposes of remote IP detection. Also handle uppercase private IPv6 purposes of remote IP detection. Also handle uppercase private IPv6
addresses. addresses.
......
...@@ -159,6 +159,8 @@ def normalize_defaults! ...@@ -159,6 +159,8 @@ def normalize_defaults!
@defaults[key] ||= default @defaults[key] ||= default
end end
end end
elsif options[:constraints]
verify_callable_constraint(options[:constraints])
end end
if Regexp === options[:format] if Regexp === options[:format]
...@@ -168,6 +170,11 @@ def normalize_defaults! ...@@ -168,6 +170,11 @@ def normalize_defaults!
end end
end end
def verify_callable_constraint(callable_constraint)
return if callable_constraint.respond_to?(:call) || callable_constraint.respond_to?(:matches?)
raise ArgumentError, "Invalid constraint: #{callable_constraint.inspect} must respond to :call or :matches?"
end
def normalize_conditions! def normalize_conditions!
@conditions[:path_info] = path @conditions[:path_info] = path
......
...@@ -4111,6 +4111,21 @@ def test_enforce_with_string ...@@ -4111,6 +4111,21 @@ def test_enforce_with_string
end end
end end
class TestCallableConstraintValidation < ActionDispatch::IntegrationTest
def test_constraint_with_object_not_callable
assert_raise(ArgumentError) do
ActionDispatch::Routing::RouteSet.new.tap do |app|
app.draw do
ok = lambda { |env| [200, { 'Content-Type' => 'text/plain' }, []] }
get '/test', to: ok, constraints: Object.new
end
end
end
end
end
class TestRouteDefaults < ActionDispatch::IntegrationTest class TestRouteDefaults < ActionDispatch::IntegrationTest
stub_controllers do |routes| stub_controllers do |routes|
Routes = routes Routes = routes
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册