Skip to content

Commit 85e9028

Browse files
ro0NLfabpot
authored andcommitted
[FrameworkBundle] Detect indirect env vars in routing
1 parent 0bb32ea commit 85e9028

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

Routing/Router.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ private function resolve($value)
147147
return '%%';
148148
}
149149

150-
if (preg_match('/^env\(\w+\)$/', $match[1])) {
150+
if (preg_match('/^env\((?:\w++:)*+\w++\)$/', $match[1])) {
151151
throw new RuntimeException(sprintf('Using "%%%s%%" is not allowed in routing configuration.', $match[1]));
152152
}
153153

@@ -156,7 +156,7 @@ private function resolve($value)
156156
if (\is_string($resolved) || is_numeric($resolved)) {
157157
$this->collectedParameters[$match[1]] = $resolved;
158158

159-
return (string) $resolved;
159+
return (string) $this->resolve($resolved);
160160
}
161161

162162
throw new RuntimeException(sprintf('The container parameter "%s", used in the route configuration value "%s", must be a string or numeric, but it is of type %s.', $match[1], $value, \gettype($resolved)));

Tests/Routing/RouterTest.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Bundle\FrameworkBundle\Routing\Router;
1616
use Symfony\Component\DependencyInjection\Config\ContainerParametersResource;
17+
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
1718
use Symfony\Component\Routing\Route;
1819
use Symfony\Component\Routing\RouteCollection;
1920

@@ -122,13 +123,13 @@ public function testPatternPlaceholders()
122123
$routes->add('foo', new Route('/before/%parameter.foo%/after/%%escaped%%'));
123124

124125
$sc = $this->getServiceContainer($routes);
125-
$sc->setParameter('parameter.foo', 'foo');
126+
$sc->setParameter('parameter.foo', 'foo-%%escaped%%');
126127

127128
$router = new Router($sc, 'foo');
128129
$route = $router->getRouteCollection()->get('foo');
129130

130131
$this->assertEquals(
131-
'/before/foo/after/%escaped%',
132+
'/before/foo-%escaped%/after/%escaped%',
132133
$route->getPath()
133134
);
134135
}
@@ -147,6 +148,22 @@ public function testEnvPlaceholders()
147148
$router->getRouteCollection();
148149
}
149150

151+
public function testIndirectEnvPlaceholders()
152+
{
153+
$routes = new RouteCollection();
154+
155+
$routes->add('foo', new Route('/%foo%'));
156+
157+
$router = new Router($container = $this->getServiceContainer($routes), 'foo');
158+
$container->setParameter('foo', 'foo-%bar%');
159+
$container->setParameter('bar', '%env(string:FOO)%');
160+
161+
$this->expectException(RuntimeException::class);
162+
$this->expectExceptionMessage('Using "%env(string:FOO)%" is not allowed in routing configuration.');
163+
164+
$router->getRouteCollection();
165+
}
166+
150167
public function testHostPlaceholders()
151168
{
152169
$routes = new RouteCollection();

0 commit comments

Comments
 (0)