Skip to content

Commit

Permalink
[BUGFIX] Apply previous site identifier as fallback for URL resolving
Browse files Browse the repository at this point in the history
Prior to #93240 URL routes were reverse sorted using the corresponding
site identifier (which did not contain any URL information). For the
scenario of matching undefined URLs (e.g. base URL is "/website/", but
request was like "https://example.org/"), the first item of a reverse
sorted list was used.

Resolves: #99149
Related: #93240
Releases: main, 11.5
Change-Id: Ia242591cebfba7d8221494a5d214f6dca75a00fc
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/78794
Tested-by: Oliver Hader <oliver.hader@typo3.org>
Reviewed-by: Oliver Hader <oliver.hader@typo3.org>
Tested-by: core-ci <typo3@b13.com>
  • Loading branch information
ohader committed Apr 22, 2023
1 parent 1c530c5 commit 628e335
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
7 changes: 6 additions & 1 deletion typo3/sysext/core/Classes/Routing/BestUrlMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ protected function sortMatchedRoutes(MatchedRoute $a, MatchedRoute $b): int
}
// index `1` refers to the array index containing the corresponding `tail` match
// @todo not sure, whether `tail` can be defined generic, it's hard coded in `SiteMatcher`
return $b->getPathMatchScore(1) <=> $a->getPathMatchScore(1);
if ($b->getPathMatchScore(1) !== $a->getPathMatchScore(1)) {
return $b->getPathMatchScore(1) <=> $a->getPathMatchScore(1);
}
// fallback for behavior prior to issue #93240, using reverse sorted site identifier
// (side note: site identifier did not contain any URL relevant information)
return $b->getSiteIdentifier() <=> $a->getSiteIdentifier();
}
}
7 changes: 7 additions & 0 deletions typo3/sysext/core/Classes/Routing/MatchedRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace TYPO3\CMS\Core\Routing;

use Symfony\Component\Routing\Route as SymfonyRoute;
use TYPO3\CMS\Core\Site\Entity\SiteInterface;

/**
* @internal
Expand Down Expand Up @@ -84,4 +85,10 @@ public function getPathMatchScore(int $index): int
// example: complete: `/french/other`, tail: `/other` -> `strlen` of `/french`
return strpos($completeMatch, $tailMatch);
}

public function getSiteIdentifier(): string
{
$site = $this->route->getDefault('site');
return $site instanceof SiteInterface ? $site->getIdentifier() : '';
}
}
4 changes: 2 additions & 2 deletions typo3/sysext/core/Tests/Unit/Routing/SiteMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ public function fullUrlMatchesSpecificLanguageWithSubdomainsAndDomainSuffixes():
$request = new ServerRequest('http://www.example.com/');
/** @var SiteRouteResult $result */
$result = $subject->matchRequest($request);
// Nothing found, only the empty site, but finds a random site ("main") according to the algorithm
// Nothing found, only the empty site, but finds the last site ("second") according to the algorithm
self::assertNull($result->getLanguage());
self::assertEquals('main', $result->getSite()->getIdentifier());
self::assertEquals('second', $result->getSite()->getIdentifier());
}

/**
Expand Down

0 comments on commit 628e335

Please sign in to comment.