From c94754e2f0bc488907157382ece309baf4ddb1b0 Mon Sep 17 00:00:00 2001 From: Petri Avikainen Date: Sun, 3 Feb 2019 23:34:02 +0200 Subject: Define word boundary for unanchored path regexp --- .../lib/action_dispatch/journey/path/pattern.rb | 2 +- actionpack/test/dispatch/mount_test.rb | 6 ++++++ actionpack/test/journey/path/pattern_test.rb | 22 +++++++++++----------- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/actionpack/lib/action_dispatch/journey/path/pattern.rb b/actionpack/lib/action_dispatch/journey/path/pattern.rb index 537f479ee5..6924dc92c2 100644 --- a/actionpack/lib/action_dispatch/journey/path/pattern.rb +++ b/actionpack/lib/action_dispatch/journey/path/pattern.rb @@ -119,7 +119,7 @@ module ActionDispatch class UnanchoredRegexp < AnchoredRegexp # :nodoc: def accept(node) - %r{\A#{visit node}} + %r{\A#{visit node}\b} end end diff --git a/actionpack/test/dispatch/mount_test.rb b/actionpack/test/dispatch/mount_test.rb index f6cf653980..e42ea89f6f 100644 --- a/actionpack/test/dispatch/mount_test.rb +++ b/actionpack/test/dispatch/mount_test.rb @@ -80,6 +80,12 @@ class TestRoutingMount < ActionDispatch::IntegrationTest assert_equal "/shorthand -- /omg", response.body end + def test_mounting_does_not_match_similar_paths + get "/shorthandomg" + assert_not_equal "/shorthand -- /omg", response.body + assert_equal " -- /shorthandomg", response.body + end + def test_mounting_works_with_via get "/getfake" assert_equal "OK", response.body diff --git a/actionpack/test/journey/path/pattern_test.rb b/actionpack/test/journey/path/pattern_test.rb index 3e7aea57f1..97d99064f2 100644 --- a/actionpack/test/journey/path/pattern_test.rb +++ b/actionpack/test/journey/path/pattern_test.rb @@ -34,17 +34,17 @@ module ActionDispatch end { - "/:controller(/:action)" => %r{\A/(#{x})(?:/([^/.?]+))?}, - "/:controller/foo" => %r{\A/(#{x})/foo}, - "/:controller/:action" => %r{\A/(#{x})/([^/.?]+)}, - "/:controller" => %r{\A/(#{x})}, - "/:controller(/:action(/:id))" => %r{\A/(#{x})(?:/([^/.?]+)(?:/([^/.?]+))?)?}, - "/:controller/:action.xml" => %r{\A/(#{x})/([^/.?]+)\.xml}, - "/:controller.:format" => %r{\A/(#{x})\.([^/.?]+)}, - "/:controller(.:format)" => %r{\A/(#{x})(?:\.([^/.?]+))?}, - "/:controller/*foo" => %r{\A/(#{x})/(.+)}, - "/:controller/*foo/bar" => %r{\A/(#{x})/(.+)/bar}, - "/:foo|*bar" => %r{\A/(?:([^/.?]+)|(.+))}, + "/:controller(/:action)" => %r{\A/(#{x})(?:/([^/.?]+))?\b}, + "/:controller/foo" => %r{\A/(#{x})/foo\b}, + "/:controller/:action" => %r{\A/(#{x})/([^/.?]+)\b}, + "/:controller" => %r{\A/(#{x})\b}, + "/:controller(/:action(/:id))" => %r{\A/(#{x})(?:/([^/.?]+)(?:/([^/.?]+))?)?\b}, + "/:controller/:action.xml" => %r{\A/(#{x})/([^/.?]+)\.xml\b}, + "/:controller.:format" => %r{\A/(#{x})\.([^/.?]+)\b}, + "/:controller(.:format)" => %r{\A/(#{x})(?:\.([^/.?]+))?\b}, + "/:controller/*foo" => %r{\A/(#{x})/(.+)\b}, + "/:controller/*foo/bar" => %r{\A/(#{x})/(.+)/bar\b}, + "/:foo|*bar" => %r{\A/(?:([^/.?]+)|(.+))\b}, }.each do |path, expected| define_method(:"test_to_non_anchored_regexp_#{Regexp.escape(path)}") do path = Pattern.build( -- cgit v1.2.3 From 1315582b374f33becfb325bf339674a0a5e6d141 Mon Sep 17 00:00:00 2001 From: Petri Avikainen Date: Tue, 5 Feb 2019 14:13:38 +0200 Subject: Restrict matching with word boundary or end of string --- .../lib/action_dispatch/journey/path/pattern.rb | 2 +- actionpack/test/journey/path/pattern_test.rb | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/actionpack/lib/action_dispatch/journey/path/pattern.rb b/actionpack/lib/action_dispatch/journey/path/pattern.rb index 6924dc92c2..697f5b9d8b 100644 --- a/actionpack/lib/action_dispatch/journey/path/pattern.rb +++ b/actionpack/lib/action_dispatch/journey/path/pattern.rb @@ -119,7 +119,7 @@ module ActionDispatch class UnanchoredRegexp < AnchoredRegexp # :nodoc: def accept(node) - %r{\A#{visit node}\b} + %r{\A#{visit node}(?:\b|\Z)} end end diff --git a/actionpack/test/journey/path/pattern_test.rb b/actionpack/test/journey/path/pattern_test.rb index 97d99064f2..fcfaba96b0 100644 --- a/actionpack/test/journey/path/pattern_test.rb +++ b/actionpack/test/journey/path/pattern_test.rb @@ -34,17 +34,17 @@ module ActionDispatch end { - "/:controller(/:action)" => %r{\A/(#{x})(?:/([^/.?]+))?\b}, - "/:controller/foo" => %r{\A/(#{x})/foo\b}, - "/:controller/:action" => %r{\A/(#{x})/([^/.?]+)\b}, - "/:controller" => %r{\A/(#{x})\b}, - "/:controller(/:action(/:id))" => %r{\A/(#{x})(?:/([^/.?]+)(?:/([^/.?]+))?)?\b}, - "/:controller/:action.xml" => %r{\A/(#{x})/([^/.?]+)\.xml\b}, - "/:controller.:format" => %r{\A/(#{x})\.([^/.?]+)\b}, - "/:controller(.:format)" => %r{\A/(#{x})(?:\.([^/.?]+))?\b}, - "/:controller/*foo" => %r{\A/(#{x})/(.+)\b}, - "/:controller/*foo/bar" => %r{\A/(#{x})/(.+)/bar\b}, - "/:foo|*bar" => %r{\A/(?:([^/.?]+)|(.+))\b}, + "/:controller(/:action)" => %r{\A/(#{x})(?:/([^/.?]+))?(?:\b|\Z)}, + "/:controller/foo" => %r{\A/(#{x})/foo(?:\b|\Z)}, + "/:controller/:action" => %r{\A/(#{x})/([^/.?]+)(?:\b|\Z)}, + "/:controller" => %r{\A/(#{x})(?:\b|\Z)}, + "/:controller(/:action(/:id))" => %r{\A/(#{x})(?:/([^/.?]+)(?:/([^/.?]+))?)?(?:\b|\Z)}, + "/:controller/:action.xml" => %r{\A/(#{x})/([^/.?]+)\.xml(?:\b|\Z)}, + "/:controller.:format" => %r{\A/(#{x})\.([^/.?]+)(?:\b|\Z)}, + "/:controller(.:format)" => %r{\A/(#{x})(?:\.([^/.?]+))?(?:\b|\Z)}, + "/:controller/*foo" => %r{\A/(#{x})/(.+)(?:\b|\Z)}, + "/:controller/*foo/bar" => %r{\A/(#{x})/(.+)/bar(?:\b|\Z)}, + "/:foo|*bar" => %r{\A/(?:([^/.?]+)|(.+))(?:\b|\Z)}, }.each do |path, expected| define_method(:"test_to_non_anchored_regexp_#{Regexp.escape(path)}") do path = Pattern.build( -- cgit v1.2.3