Skip to content

Fix url matcher edge cases with trailing slash #31240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
use Symfony\Component\Routing\Exception\NoConfigurationException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Matcher\RedirectableUrlMatcherInterface;
use Symfony\Component\Routing\RequestContext;

/**
* @author Nicolas Grekas <p@tchwork.com>
*
* @internal
*
* @property RequestContext $context
*/
trait PhpMatcherTrait
{
Expand Down Expand Up @@ -89,13 +92,6 @@ private function doMatch(string $pathinfo, array &$allow = [], array &$allowSche
continue;
}

if ('/' !== $pathinfo && $hasTrailingSlash === ($trimmedPathinfo === $pathinfo)) {
if ($supportsRedirections && (!$requiredMethods || isset($requiredMethods['GET']))) {
return $allow = $allowSchemes = [];
}
continue;
}

if ($requiredHost) {
if ('#' !== $requiredHost[0] ? $requiredHost !== $host : !preg_match($requiredHost, $host, $hostMatches)) {
continue;
Expand All @@ -106,13 +102,21 @@ private function doMatch(string $pathinfo, array &$allow = [], array &$allowSche
}
}

if ('/' !== $pathinfo && $hasTrailingSlash === ($trimmedPathinfo === $pathinfo)) {
if ($supportsRedirections && (!$requiredMethods || isset($requiredMethods['GET']))) {
return $allow = $allowSchemes = [];
}
continue;
}

$hasRequiredScheme = !$requiredSchemes || isset($requiredSchemes[$context->getScheme()]);
if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) {
if ($hasRequiredScheme) {
$allow += $requiredMethods;
}
continue;
}

if (!$hasRequiredScheme) {
$allowSchemes += $requiredSchemes;
continue;
Expand Down
17 changes: 9 additions & 8 deletions src/Symfony/Component/Routing/Matcher/UrlMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class UrlMatcher implements UrlMatcherInterface, RequestMatcherInterface
const REQUIREMENT_MISMATCH = 1;
const ROUTE_MATCH = 2;

/** @var RequestContext */
protected $context;

/**
Expand Down Expand Up @@ -166,14 +167,6 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
}
}

if ('/' !== $pathinfo && !$hasTrailingVar && $hasTrailingSlash === ($trimmedPathinfo === $pathinfo)) {
if ($supportsTrailingSlash && (!$requiredMethods || \in_array('GET', $requiredMethods))) {
return $this->allow = $this->allowSchemes = [];
}

continue;
}

$hostMatches = [];
if ($compiledRoute->getHostRegex() && !preg_match($compiledRoute->getHostRegex(), $this->context->getHost(), $hostMatches)) {
continue;
Expand All @@ -185,6 +178,14 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
continue;
}

if ('/' !== $pathinfo && !$hasTrailingVar && $hasTrailingSlash === ($trimmedPathinfo === $pathinfo)) {
if ($supportsTrailingSlash && (!$requiredMethods || \in_array('GET', $requiredMethods))) {
return $this->allow = $this->allowSchemes = [];
}

continue;
}

$hasRequiredScheme = !$route->getSchemes() || $route->hasScheme($this->context->getScheme());
if ($requiredMethods) {
if (!\in_array($method, $requiredMethods)) {
Expand Down
153 changes: 149 additions & 4 deletions src/Symfony/Component/Routing/Tests/Matcher/UrlMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ public function testMethodNotAllowedAggregatesAllowedMethods()
}
}

public function testMatch()
public function testPatternMatchAndParameterReturn()
{
// test the patterns are matched and parameters are returned
$collection = new RouteCollection();
$collection->add('foo', new Route('/foo/{bar}'));
$matcher = $this->getUrlMatcher($collection);
Expand All @@ -96,14 +95,21 @@ public function testMatch()
$this->fail();
} catch (ResourceNotFoundException $e) {
}

$this->assertEquals(['_route' => 'foo', 'bar' => 'baz'], $matcher->match('/foo/baz'));
}

public function testDefaultsAreMerged()
{
// test that defaults are merged
$collection = new RouteCollection();
$collection->add('foo', new Route('/foo/{bar}', ['def' => 'test']));
$matcher = $this->getUrlMatcher($collection);
$this->assertEquals(['_route' => 'foo', 'bar' => 'baz', 'def' => 'test'], $matcher->match('/foo/baz'));
}

public function testMethodIsIgnoredIfNoMethodGiven()
{
// test that route "method" is ignored if no method is given in the context
$collection = new RouteCollection();
$collection->add('foo', new Route('/foo', [], [], [], '', [], ['get', 'head']));
Expand All @@ -123,8 +129,10 @@ public function testMatch()
$this->assertInternalType('array', $matcher->match('/foo'));
$matcher = $this->getUrlMatcher($collection, new RequestContext('', 'head'));
$this->assertInternalType('array', $matcher->match('/foo'));
}

// route with an optional variable as the first segment
public function testRouteWithOptionalVariableAsFirstSegment()
{
$collection = new RouteCollection();
$collection->add('bar', new Route('/{bar}/foo', ['bar' => 'bar'], ['bar' => 'foo|bar']));
$matcher = $this->getUrlMatcher($collection);
Expand All @@ -136,8 +144,10 @@ public function testMatch()
$matcher = $this->getUrlMatcher($collection);
$this->assertEquals(['_route' => 'bar', 'bar' => 'foo'], $matcher->match('/foo'));
$this->assertEquals(['_route' => 'bar', 'bar' => 'bar'], $matcher->match('/'));
}

// route with only optional variables
public function testRouteWithOnlyOptionalVariables()
{
$collection = new RouteCollection();
$collection->add('bar', new Route('/{foo}/{bar}', ['foo' => 'foo', 'bar' => 'bar'], []));
$matcher = $this->getUrlMatcher($collection);
Expand Down Expand Up @@ -512,6 +522,141 @@ public function testWithHostOnRouteCollection()
$this->assertEquals(['foo' => 'bar', '_route' => 'bar', 'locale' => 'en'], $matcher->match('/bar/bar'));
}

public function testVariationInTrailingSlashWithHosts()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/foo/', [], [], [], 'foo.example.com'));
$coll->add('bar', new Route('/foo', [], [], [], 'bar.example.com'));

$matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'foo.example.com'));
$this->assertEquals(['_route' => 'foo'], $matcher->match('/foo/'));

$matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'bar.example.com'));
$this->assertEquals(['_route' => 'bar'], $matcher->match('/foo'));
}

public function testVariationInTrailingSlashWithHostsInReverse()
{
// The order should not matter
$coll = new RouteCollection();
$coll->add('bar', new Route('/foo', [], [], [], 'bar.example.com'));
$coll->add('foo', new Route('/foo/', [], [], [], 'foo.example.com'));

$matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'foo.example.com'));
$this->assertEquals(['_route' => 'foo'], $matcher->match('/foo/'));

$matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'bar.example.com'));
$this->assertEquals(['_route' => 'bar'], $matcher->match('/foo'));
}

public function testVariationInTrailingSlashWithHostsAndVariable()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/{foo}/', [], [], [], 'foo.example.com'));
$coll->add('bar', new Route('/{foo}', [], [], [], 'bar.example.com'));

$matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'foo.example.com'));
$this->assertEquals(['foo' => 'bar', '_route' => 'foo'], $matcher->match('/bar/'));

$matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'bar.example.com'));
$this->assertEquals(['foo' => 'bar', '_route' => 'bar'], $matcher->match('/bar'));
}

public function testVariationInTrailingSlashWithHostsAndVariableInReverse()
{
// The order should not matter
$coll = new RouteCollection();
$coll->add('bar', new Route('/{foo}', [], [], [], 'bar.example.com'));
$coll->add('foo', new Route('/{foo}/', [], [], [], 'foo.example.com'));

$matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'foo.example.com'));
$this->assertEquals(['foo' => 'bar', '_route' => 'foo'], $matcher->match('/bar/'));

$matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'bar.example.com'));
$this->assertEquals(['foo' => 'bar', '_route' => 'bar'], $matcher->match('/bar'));
}

public function testVariationInTrailingSlashWithMethods()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/foo/', [], [], [], '', [], ['POST']));
$coll->add('bar', new Route('/foo', [], [], [], '', [], ['GET']));

$matcher = $this->getUrlMatcher($coll, new RequestContext('', 'POST'));
$this->assertEquals(['_route' => 'foo'], $matcher->match('/foo/'));

$matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET'));
$this->assertEquals(['_route' => 'bar'], $matcher->match('/foo'));
}

public function testVariationInTrailingSlashWithMethodsInReverse()
{
// The order should not matter
$coll = new RouteCollection();
$coll->add('bar', new Route('/foo', [], [], [], '', [], ['GET']));
$coll->add('foo', new Route('/foo/', [], [], [], '', [], ['POST']));

$matcher = $this->getUrlMatcher($coll, new RequestContext('', 'POST'));
$this->assertEquals(['_route' => 'foo'], $matcher->match('/foo/'));

$matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET'));
$this->assertEquals(['_route' => 'bar'], $matcher->match('/foo'));
}

public function testVariableVariationInTrailingSlashWithMethods()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/{foo}/', [], [], [], '', [], ['POST']));
$coll->add('bar', new Route('/{foo}', [], [], [], '', [], ['GET']));

$matcher = $this->getUrlMatcher($coll, new RequestContext('', 'POST'));
$this->assertEquals(['foo' => 'bar', '_route' => 'foo'], $matcher->match('/bar/'));

$matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET'));
$this->assertEquals(['foo' => 'bar', '_route' => 'bar'], $matcher->match('/bar'));
}

public function testVariableVariationInTrailingSlashWithMethodsInReverse()
{
// The order should not matter
$coll = new RouteCollection();
$coll->add('bar', new Route('/{foo}', [], [], [], '', [], ['GET']));
$coll->add('foo', new Route('/{foo}/', [], [], [], '', [], ['POST']));

$matcher = $this->getUrlMatcher($coll, new RequestContext('', 'POST'));
$this->assertEquals(['foo' => 'bar', '_route' => 'foo'], $matcher->match('/bar/'));

$matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET'));
$this->assertEquals(['foo' => 'bar', '_route' => 'bar'], $matcher->match('/bar'));
}

public function testMixOfStaticAndVariableVariationInTrailingSlashWithHosts()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/foo/', [], [], [], 'foo.example.com'));
$coll->add('bar', new Route('/{foo}', [], [], [], 'bar.example.com'));

$matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'foo.example.com'));
$this->assertEquals(['_route' => 'foo'], $matcher->match('/foo/'));

$matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'bar.example.com'));
$this->assertEquals(['foo' => 'bar', '_route' => 'bar'], $matcher->match('/bar'));
}

public function testMixOfStaticAndVariableVariationInTrailingSlashWithMethods()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/foo/', [], [], [], '', [], ['POST']));
$coll->add('bar', new Route('/{foo}', [], [], [], '', [], ['GET']));

$matcher = $this->getUrlMatcher($coll, new RequestContext('', 'POST'));
$this->assertEquals(['_route' => 'foo'], $matcher->match('/foo/'));

$matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET'));
$this->assertEquals(['foo' => 'bar', '_route' => 'bar'], $matcher->match('/bar'));
$this->assertEquals(['foo' => 'foo', '_route' => 'bar'], $matcher->match('/foo'));
}

/**
* @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
*/
Expand Down