Skip to content

[Routing] fix greediness of trailing slash #29380

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
Nov 30, 2018
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
59 changes: 41 additions & 18 deletions src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -550,16 +550,23 @@ private function compileStaticPrefixCollection(StaticPrefixCollection $tree, \st
private function compileSwitchDefault(bool $hasVars, bool $matchHost): string
{
$code = sprintf("
if ('/' !== \$pathinfo) {
if (!\$hasTrailingSlash && '/' === \$pathinfo[-1]%s) {
%s;
}
if (\$hasTrailingSlash && '/' !== \$pathinfo[-1]) {
%2\$s;
if ('/' !== \$pathinfo) {%s
if (\$hasTrailingSlash !== ('/' === \$pathinfo[-1])) {%s
break;
}
}\n",
$hasVars ? ' && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n[\'MARK\']' : '',
$this->supportsRedirections ? 'return null' : 'break'
$hasVars ? "
if ('/' === \$pathinfo[-1]) {
if (preg_match(\$regex, substr(\$pathinfo, 0, -1), \$n) && \$m === (int) \$n['MARK']) {
\$matches = \$n;
} else {
\$hasTrailingSlash = true;
}
}\n" : '',
$this->supportsRedirections ? "
if (!\$requiredMethods || isset(\$requiredMethods['GET'])) {
return null;
}" : ''
);

if ($hasVars) {
Expand Down Expand Up @@ -616,26 +623,42 @@ private function compileSwitchDefault(bool $hasVars, bool $matchHost): string
*/
private function compileRoute(Route $route, string $name, bool $checkHost, bool $hasTrailingSlash): string
{
$compiledRoute = $route->compile();
$conditions = array();
$matches = (bool) $compiledRoute->getPathVariables();
$hostMatches = (bool) $compiledRoute->getHostVariables();
$methods = array_flip($route->getMethods());
$code = " // $name";

if ('/' !== $route->getPath()) {
if ('/' === $route->getPath()) {
$code .= "\n";
} elseif (!$matches) {
$code .= sprintf("
if ('/' !== \$pathinfo && '/' %s \$pathinfo[-1]) {
%s;
}\n",
}\n\n",
$hasTrailingSlash ? '!==' : '===',
$this->supportsRedirections ? 'return null' : 'break'
$this->supportsRedirections && (!$methods || isset($methods['GET'])) ? 'return null' : 'break'
);
} elseif ($hasTrailingSlash) {
$code .= sprintf("
if ('/' !== \$pathinfo[-1]) {
%s;
}
if ('/' !== \$pathinfo && preg_match(\$regex, substr(\$pathinfo, 0, -1), \$n) && \$m === (int) \$n['MARK']) {
\$matches = \$n;
}\n\n",
$this->supportsRedirections && (!$methods || isset($methods['GET'])) ? 'return null' : 'break'
);
} else {
$code .= "\n";
$code .= sprintf("
if ('/' !== \$pathinfo && '/' === \$pathinfo[-1] && preg_match(\$regex, substr(\$pathinfo, 0, -1), \$n) && \$m === (int) \$n['MARK']) {
%s;
}\n\n",
$this->supportsRedirections && (!$methods || isset($methods['GET'])) ? 'return null' : 'break'
);
}

$compiledRoute = $route->compile();
$conditions = array();
$matches = (bool) $compiledRoute->getPathVariables();
$hostMatches = (bool) $compiledRoute->getHostVariables();
$methods = array_flip($route->getMethods());

if ($route->getCondition()) {
$expression = $this->getExpressionLanguage()->compile($route->getCondition(), array('context', 'request'));

Expand Down
20 changes: 14 additions & 6 deletions src/Symfony/Component/Routing/Matcher/UrlMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,12 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
foreach ($routes as $name => $route) {
$compiledRoute = $route->compile();
$staticPrefix = $compiledRoute->getStaticPrefix();
$requiredMethods = $route->getMethods();

// check the static prefix of the URL first. Only use the more expensive preg_match when it matches
if ('' === $staticPrefix || 0 === strpos($pathinfo, $staticPrefix)) {
// no-op
} elseif (!$supportsTrailingSlash) {
} elseif (!$supportsTrailingSlash || ($requiredMethods && !\in_array('GET', $requiredMethods))) {
continue;
} elseif ('/' === $staticPrefix[-1] && substr($staticPrefix, 0, -1) === $pathinfo) {
return;
Expand All @@ -161,11 +162,18 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
}

if ($supportsTrailingSlash) {
if (!$hasTrailingSlash && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1))) {
return;
if ('/' === $pathinfo[-1]) {
if (preg_match($regex, substr($pathinfo, 0, -1), $m)) {
$matches = $m;
} else {
$hasTrailingSlash = true;
}
}
if ($hasTrailingSlash && '/' !== $pathinfo[-1]) {
return;
if ($hasTrailingSlash !== ('/' === $pathinfo[-1])) {
if (!$requiredMethods || \in_array('GET', $requiredMethods)) {
return;
}
continue;
}
}

Expand All @@ -181,7 +189,7 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
}

$hasRequiredScheme = !$route->getSchemes() || $route->hasScheme($this->context->getScheme());
if ($requiredMethods = $route->getMethods()) {
if ($requiredMethods) {
// HEAD and GET are equivalent as per RFC
if ('HEAD' === $method = $this->context->getMethod()) {
$method = 'GET';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ public function match($rawPathinfo)
list($ret, $requiredHost, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$trimmedPathinfo];

if ('/' !== $pathinfo) {
if (!$hasTrailingSlash && '/' === $pathinfo[-1]) {
break;
}
if ($hasTrailingSlash && '/' !== $pathinfo[-1]) {
if ($hasTrailingSlash !== ('/' === $pathinfo[-1])) {
break;
}
}
Expand Down Expand Up @@ -145,15 +142,23 @@ public function match($rawPathinfo)
$matches = array('foo' => $matches[1] ?? null);

// baz4
if ('/' !== $pathinfo && '/' !== $pathinfo[-1]) {
if ('/' !== $pathinfo[-1]) {
break;
}
if ('/' !== $pathinfo && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
$matches = $n;
}

return $this->mergeDefaults(array('_route' => 'baz4') + $matches, array());

// baz5
if ('/' !== $pathinfo && '/' !== $pathinfo[-1]) {
if ('/' !== $pathinfo[-1]) {
break;
}
if ('/' !== $pathinfo && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
$matches = $n;
}

$ret = $this->mergeDefaults(array('_route' => 'baz5') + $matches, array());
if (!isset(($a = array('POST' => 0))[$requestMethod])) {
$allow += $a;
Expand All @@ -164,9 +169,13 @@ public function match($rawPathinfo)
not_baz5:

// baz.baz6
if ('/' !== $pathinfo && '/' !== $pathinfo[-1]) {
if ('/' !== $pathinfo[-1]) {
break;
}
if ('/' !== $pathinfo && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
$matches = $n;
}

$ret = $this->mergeDefaults(array('_route' => 'baz.baz6') + $matches, array());
if (!isset(($a = array('PUT' => 0))[$requestMethod])) {
$allow += $a;
Expand All @@ -181,9 +190,10 @@ public function match($rawPathinfo)
$matches = array('foo' => $matches[1] ?? null);

// foo1
if ('/' !== $pathinfo && '/' === $pathinfo[-1]) {
if ('/' !== $pathinfo && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
break;
}

$ret = $this->mergeDefaults(array('_route' => 'foo1') + $matches, array());
if (!isset(($a = array('PUT' => 0))[$requestMethod])) {
$allow += $a;
Expand All @@ -198,19 +208,21 @@ public function match($rawPathinfo)
$matches = array('foo1' => $matches[1] ?? null);

// foo2
if ('/' !== $pathinfo && '/' === $pathinfo[-1]) {
if ('/' !== $pathinfo && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
break;
}

return $this->mergeDefaults(array('_route' => 'foo2') + $matches, array());

break;
case 279:
$matches = array('_locale' => $matches[1] ?? null, 'foo' => $matches[2] ?? null);

// foo3
if ('/' !== $pathinfo && '/' === $pathinfo[-1]) {
if ('/' !== $pathinfo && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
break;
}

return $this->mergeDefaults(array('_route' => 'foo3') + $matches, array());

break;
Expand Down Expand Up @@ -238,10 +250,15 @@ public function match($rawPathinfo)
list($ret, $vars, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$m];

if ('/' !== $pathinfo) {
if (!$hasTrailingSlash && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
break;
if ('/' === $pathinfo[-1]) {
if (preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
$matches = $n;
} else {
$hasTrailingSlash = true;
}
}
if ($hasTrailingSlash && '/' !== $pathinfo[-1]) {

if ($hasTrailingSlash !== ('/' === $pathinfo[-1])) {
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2794,10 +2794,15 @@ public function match($rawPathinfo)
list($ret, $vars, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$m];

if ('/' !== $pathinfo) {
if (!$hasTrailingSlash && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
break;
if ('/' === $pathinfo[-1]) {
if (preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
$matches = $n;
} else {
$hasTrailingSlash = true;
}
}
if ($hasTrailingSlash && '/' !== $pathinfo[-1]) {

if ($hasTrailingSlash !== ('/' === $pathinfo[-1])) {
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,19 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a
list($ret, $vars, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$m];

if ('/' !== $pathinfo) {
if (!$hasTrailingSlash && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
return null;
if ('/' === $pathinfo[-1]) {
if (preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
$matches = $n;
} else {
$hasTrailingSlash = true;
}
}
if ($hasTrailingSlash && '/' !== $pathinfo[-1]) {
return null;

if ($hasTrailingSlash !== ('/' === $pathinfo[-1])) {
if (!$requiredMethods || isset($requiredMethods['GET'])) {
return null;
}
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,15 @@ public function match($rawPathinfo)
list($ret, $vars, $requiredMethods, $requiredSchemes, $hasTrailingSlash) = $routes[$m];

if ('/' !== $pathinfo) {
if (!$hasTrailingSlash && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
break;
if ('/' === $pathinfo[-1]) {
if (preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
$matches = $n;
} else {
$hasTrailingSlash = true;
}
}
if ($hasTrailingSlash && '/' !== $pathinfo[-1]) {

if ($hasTrailingSlash !== ('/' === $pathinfo[-1])) {
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,17 @@ public function match($rawPathinfo)
$matches = array('foo' => $matches[1] ?? null, 'foo' => $matches[2] ?? null);

// r1
if ('/' !== $pathinfo && '/' === $pathinfo[-1]) {
if ('/' !== $pathinfo && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
break;
}

return $this->mergeDefaults(array('_route' => 'r1') + $matches, array());

// r2
if ('/' !== $pathinfo && '/' === $pathinfo[-1]) {
if ('/' !== $pathinfo && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
break;
}

return $this->mergeDefaults(array('_route' => 'r2') + $matches, array());

break;
Expand Down
Loading