Skip to content

[Routing] Fix subdomain routing with host parameter defaults #60976

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

Open
wants to merge 5 commits into
base: 6.4
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,19 @@ private function compileStaticRoutes(array $staticRoutes, array &$conditions): a
foreach ($staticRoutes as $url => $routes) {
$compiledRoutes[$url] = [];
foreach ($routes as $name => [$route, $hasTrailingSlash]) {
$compiledRoutes[$url][] = $this->compileRoute($route, $name, (!$route->compile()->getHostVariables() ? $route->getHost() : $route->compile()->getHostRegex()) ?: null, $hasTrailingSlash, false, $conditions);
$compiledRoute = $route->compile();
$hostVars = $compiledRoute->getHostVariables();
$hostArg = null;

if ($hostVars) {
// Pass both host regex and host variables for routes with host variables
$hostArg = [$compiledRoute->getHostRegex(), $hostVars];
} elseif ($host = $route->getHost()) {
// Pass just the host string for static hosts
$hostArg = $host;
}

$compiledRoutes[$url][] = $this->compileRoute($route, $name, $hostArg, $hasTrailingSlash, false, $conditions);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,45 @@ private function doMatch(string $pathinfo, array &$allow = [], array &$allowSche

foreach ($this->staticRoutes[$trimmedPathinfo] ?? [] as [$ret, $requiredHost, $requiredMethods, $requiredSchemes, $hasTrailingSlash, , $condition]) {
if ($requiredHost) {
if ('{' !== $requiredHost[0] ? $requiredHost !== $host : !preg_match($requiredHost, $host, $hostMatches)) {
continue;
}
if ('{' === $requiredHost[0] && $hostMatches) {
$hostMatches['_route'] = $ret['_route'];
$ret = $this->mergeDefaults($hostMatches, $ret);
$hostMatches = [];

// Check if host requirement is an array [regex, hostVars] or a string
if (\is_array($requiredHost)) {
[$hostRegex, $hostVars] = $requiredHost;

if (!preg_match($hostRegex, $host, $hostMatches)) {
// Check if all host variables have defaults
$hasAllDefaults = true;
foreach ($hostVars as $var) {
if (!array_key_exists($var, $ret)) {
$hasAllDefaults = false;
break;
}
}

if (!$hasAllDefaults) {
continue;
}

// Use defaults for host variables
foreach ($hostVars as $var) {
$hostMatches[$var] = $ret[$var];
}
}

if ($hostMatches) {
$hostMatches['_route'] = $ret['_route'];
$ret = $this->mergeDefaults($hostMatches, $ret);
}
} else {
// Simple string host match (backwards compatibility)
if ('{' !== $requiredHost[0] ? $requiredHost !== $host : !preg_match($requiredHost, $host, $hostMatches)) {
continue;
}
if ('{' === $requiredHost[0] && $hostMatches) {
$hostMatches['_route'] = $ret['_route'];
$ret = $this->mergeDefaults($hostMatches, $ret);
}
}
}

Expand Down
29 changes: 27 additions & 2 deletions src/Symfony/Component/Routing/Matcher/TraceableUrlMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,33 @@ protected function matchCollection(string $pathinfo, RouteCollection $routes): a

$hostMatches = [];
if ($compiledRoute->getHostRegex() && !preg_match($compiledRoute->getHostRegex(), $this->context->getHost(), $hostMatches)) {
$this->addTrace(sprintf('Host "%s" does not match the requirement ("%s")', $this->context->getHost(), $route->getHost()), self::ROUTE_ALMOST_MATCHES, $name, $route);
continue;
// Check if route has defaults for all host variables before skipping
$hostVariables = $compiledRoute->getHostVariables();
if ($hostVariables) {
$routeDefaults = $route->getDefaults();
$hasAllDefaults = true;

foreach ($hostVariables as $variable) {
if (!\array_key_exists($variable, $routeDefaults)) {
$hasAllDefaults = false;
break;
}
}

if ($hasAllDefaults) {
// Use defaults for host variables
foreach ($hostVariables as $variable) {
$hostMatches[$variable] = $routeDefaults[$variable];
}
$this->addTrace('Host does not match, but using default values', self::ROUTE_ALMOST_MATCHES, $name, $route);
} else {
$this->addTrace(sprintf('Host "%s" does not match the requirement ("%s")', $this->context->getHost(), $route->getHost()), self::ROUTE_ALMOST_MATCHES, $name, $route);
continue;
}
} else {
$this->addTrace(sprintf('Host "%s" does not match the requirement ("%s")', $this->context->getHost(), $route->getHost()), self::ROUTE_ALMOST_MATCHES, $name, $route);
continue;
}
}

$attributes = $this->getAttributes($route, $name, array_replace($matches, $hostMatches));
Expand Down
25 changes: 24 additions & 1 deletion src/Symfony/Component/Routing/Matcher/UrlMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,30 @@ protected function matchCollection(string $pathinfo, RouteCollection $routes): a

$hostMatches = [];
if ($compiledRoute->getHostRegex() && !preg_match($compiledRoute->getHostRegex(), $this->context->getHost(), $hostMatches)) {
continue;
// Check if route has defaults for all host variables before skipping
$hostVariables = $compiledRoute->getHostVariables();
if ($hostVariables) {
$routeDefaults = $route->getDefaults();
$hasAllDefaults = true;

foreach ($hostVariables as $variable) {
if (!\array_key_exists($variable, $routeDefaults)) {
$hasAllDefaults = false;
break;
}
}

if ($hasAllDefaults) {
// Use defaults for host variables
foreach ($hostVariables as $variable) {
$hostMatches[$variable] = $routeDefaults[$variable];
}
} else {
continue;
}
} else {
continue;
}
}

$attributes = $this->getAttributes($route, $name, array_replace($matches, $hostMatches));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
'/c2/route3' => [[['_route' => 'route3'], 'b.example.com', null, null, false, false, null]],
'/route5' => [[['_route' => 'route5'], 'c.example.com', null, null, false, false, null]],
'/route6' => [[['_route' => 'route6'], null, null, null, false, false, null]],
'/route11' => [[['_route' => 'route11'], '{^(?P<var1>[^\\.]++)\\.example\\.com$}sDi', null, null, false, false, null]],
'/route12' => [[['_route' => 'route12', 'var1' => 'val'], '{^(?P<var1>[^\\.]++)\\.example\\.com$}sDi', null, null, false, false, null]],
'/route11' => [[['_route' => 'route11'], ['{^(?P<var1>[^\\.]++)\\.example\\.com$}sDi', ['var1']], null, null, false, false, null]],
'/route12' => [[['_route' => 'route12', 'var1' => 'val'], ['{^(?P<var1>[^\\.]++)\\.example\\.com$}sDi', ['var1']], null, null, false, false, null]],
'/route17' => [[['_route' => 'route17'], null, null, null, false, false, null]],
],
[ // $regexpList
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
'/c2/route3' => [[['_route' => 'route3'], 'b.example.com', null, null, false, false, null]],
'/route5' => [[['_route' => 'route5'], 'c.example.com', null, null, false, false, null]],
'/route6' => [[['_route' => 'route6'], null, null, null, false, false, null]],
'/route11' => [[['_route' => 'route11'], '{^(?P<var1>[^\\.]++)\\.example\\.com$}sDi', null, null, false, false, null]],
'/route12' => [[['_route' => 'route12', 'var1' => 'val'], '{^(?P<var1>[^\\.]++)\\.example\\.com$}sDi', null, null, false, false, null]],
'/route11' => [[['_route' => 'route11'], ['{^(?P<var1>[^\\.]++)\\.example\\.com$}sDi', ['var1']], null, null, false, false, null]],
'/route12' => [[['_route' => 'route12', 'var1' => 'val'], ['{^(?P<var1>[^\\.]++)\\.example\\.com$}sDi', ['var1']], null, null, false, false, null]],
'/route17' => [[['_route' => 'route17'], null, null, null, false, false, null]],
'/secure' => [[['_route' => 'secure'], null, null, ['https' => 0], false, false, null]],
'/nonsecure' => [[['_route' => 'nonsecure'], null, null, ['http' => 0], false, false, null]],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
true, // $matchHost
[ // $staticRoutes
'/' => [
[['_route' => 'a'], '{^(?P<d>[^\\.]++)\\.e\\.c\\.b\\.a$}sDi', null, null, false, false, null],
[['_route' => 'c'], '{^(?P<e>[^\\.]++)\\.e\\.c\\.b\\.a$}sDi', null, null, false, false, null],
[['_route' => 'a'], ['{^(?P<d>[^\\.]++)\\.e\\.c\\.b\\.a$}sDi', ['d']], null, null, false, false, null],
[['_route' => 'c'], ['{^(?P<e>[^\\.]++)\\.e\\.c\\.b\\.a$}sDi', ['e']], null, null, false, false, null],
[['_route' => 'b'], 'd.c.b.a', null, null, false, false, null],
],
],
Expand Down
24 changes: 24 additions & 0 deletions src/Symfony/Component/Routing/Tests/Matcher/UrlMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,30 @@ public function testUtf8VarName()
$this->assertEquals(['_route' => 'foo', 'bär' => 'baz', 'bäz' => 'foo'], $matcher->match('/foo/baz'));
}

public function testHostVariablesWithDefaults()
{
$collection = new RouteCollection();

// Route with host defaults - should match when subdomain is missing
$routeWithDefaults = new Route('/test', ['subdomain' => 'en'], ['subdomain' => '[a-zA-Z]+'], [], '{subdomain}.example.com');
$collection->add('with_defaults', $routeWithDefaults);

// Route without host defaults - should NOT match when subdomain is missing
$routeWithoutDefaults = new Route('/test2', [], ['subdomain' => '[a-zA-Z]+'], [], '{subdomain}.example.com');
$collection->add('without_defaults', $routeWithoutDefaults);

// Test 1: Host without subdomain - should match route with defaults
$matcher = $this->getUrlMatcher($collection, new RequestContext('', 'GET', 'example.com'));

$result = $matcher->match('/test');
$this->assertSame('with_defaults', $result['_route']);
$this->assertSame('en', $result['subdomain']);

// Test 2: Same host should NOT match route without defaults
$this->expectException(ResourceNotFoundException::class);
$matcher->match('/test2');
}

protected function getUrlMatcher(RouteCollection $routes, ?RequestContext $context = null)
{
return new UrlMatcher($routes, $context ?? new RequestContext());
Expand Down