Skip to content

Commit 24fb53f

Browse files
bug #62459 [Routing] Fix case sensitivity for static host matching in compiled routes (yoeunes)
This PR was merged into the 6.4 branch. Discussion ---------- [Routing] Fix case sensitivity for static host matching in compiled routes | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | - | License | MIT `CompiledUrlMatcher` uses string comparison for static hosts, but checks lowercased request host against the original route host. This fails if the route host has uppercase letters (e.g., `'api.example.com' !== 'API.example.com'`), throwing `ResourceNotFoundException`. `UrlMatcher` avoids this via case-insensitive regex. This PR lowercases static hosts in `CompiledUrlMatcherDumper` for consistency. Commits ------- 9c33b1fdb6b [Routing] Fix case sensitivity for static host matching in compiled routes
2 parents 16519ed + 6c11ba9 commit 24fb53f

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

Matcher/Dumper/CompiledUrlMatcherDumper.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,12 @@ private function compileStaticRoutes(array $staticRoutes, array &$conditions): a
222222
foreach ($staticRoutes as $url => $routes) {
223223
$compiledRoutes[$url] = [];
224224
foreach ($routes as $name => [$route, $hasTrailingSlash]) {
225-
$compiledRoutes[$url][] = $this->compileRoute($route, $name, (!$route->compile()->getHostVariables() ? $route->getHost() : $route->compile()->getHostRegex()) ?: null, $hasTrailingSlash, false, $conditions);
225+
if ($route->compile()->getHostVariables()) {
226+
$host = $route->compile()->getHostRegex();
227+
} elseif ($host = $route->getHost()) {
228+
$host = strtolower($host);
229+
}
230+
$compiledRoutes[$url][] = $this->compileRoute($route, $name, $host ?: null, $hasTrailingSlash, false, $conditions);
226231
}
227232
}
228233

Tests/Matcher/CompiledUrlMatcherTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,33 @@
1313

1414
use Symfony\Component\Routing\Matcher\CompiledUrlMatcher;
1515
use Symfony\Component\Routing\Matcher\Dumper\CompiledUrlMatcherDumper;
16+
use Symfony\Component\Routing\Matcher\UrlMatcher;
1617
use Symfony\Component\Routing\RequestContext;
18+
use Symfony\Component\Routing\Route;
1719
use Symfony\Component\Routing\RouteCollection;
1820

1921
class CompiledUrlMatcherTest extends UrlMatcherTest
2022
{
23+
public function testStaticHostIsCaseInsensitive()
24+
{
25+
$collection = new RouteCollection();
26+
$collection->add('static_host_route', new Route('/test', [], [], [], 'API.example.com'));
27+
28+
$context = new RequestContext('/test', 'GET', 'api.example.com');
29+
$matcher = new UrlMatcher($collection, $context);
30+
31+
$result = $matcher->match('/test');
32+
$this->assertEquals('static_host_route', $result['_route'], 'UrlMatcher should match case-insensitive host');
33+
34+
$dumper = new CompiledUrlMatcherDumper($collection);
35+
$compiledRoutes = $dumper->getCompiledRoutes();
36+
37+
$compiledMatcher = new CompiledUrlMatcher($compiledRoutes, $context);
38+
39+
$result = $compiledMatcher->match('/test');
40+
$this->assertEquals('static_host_route', $result['_route'], 'CompiledUrlMatcher should match case-insensitive host');
41+
}
42+
2143
protected function getUrlMatcher(RouteCollection $routes, ?RequestContext $context = null)
2244
{
2345
$dumper = new CompiledUrlMatcherDumper($routes);

0 commit comments

Comments
 (0)