Skip to content

Commit 6cf3d56

Browse files
committed
bug symfony#24673 [DI] Throw when a service name or an alias contains dynamic values (prevent an infinite loop) (dunglas)
This PR was squashed before being merged into the 3.3 branch (closes symfony#24673). Discussion ---------- [DI] Throw when a service name or an alias contains dynamic values (prevent an infinite loop) | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no | Deprecations? | no <!-- don't forget to update UPGRADE-*.md files --> | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a If an environment variable is used to build a service name (like in [this snippet](https://github.com/api-platform/core/blame/4b3d1abfe595d507c9064ef86fd8ef6881b7e5b5/src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php#L471)), an infinite loop occurs. It's common to build dynamic service names (in a compiler pass), if the dynamic part comes from a parameter, this bug can occurs. Commits ------- 14e3085 [DI] Throw when a service name or an alias contains dynamic values (prevent an infinite loop)
2 parents ea3a9a9 + 14e3085 commit 6cf3d56

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/Symfony/Component/DependencyInjection/Compiler/CheckDefinitionValidityPass.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\DependencyInjection\Compiler;
1313

1414
use Symfony\Component\DependencyInjection\ContainerBuilder;
15+
use Symfony\Component\DependencyInjection\Exception\EnvParameterException;
1516
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
1617

1718
/**
@@ -75,6 +76,18 @@ public function process(ContainerBuilder $container)
7576
}
7677
}
7778
}
79+
80+
$resolvedId = $container->resolveEnvPlaceholders($id, null, $usedEnvs);
81+
if (null !== $usedEnvs) {
82+
throw new EnvParameterException(array($resolvedId), null, 'A service name ("%s") cannot contain dynamic values.');
83+
}
84+
}
85+
86+
foreach ($container->getAliases() as $id => $alias) {
87+
$resolvedId = $container->resolveEnvPlaceholders($id, null, $usedEnvs);
88+
if (null !== $usedEnvs) {
89+
throw new EnvParameterException(array($resolvedId), null, 'An alias name ("%s") cannot contain dynamic values.');
90+
}
7891
}
7992
}
8093
}

src/Symfony/Component/DependencyInjection/Tests/Compiler/CheckDefinitionValidityPassTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,30 @@ public function testInvalidTags()
7676
$this->process($container);
7777
}
7878

79+
/**
80+
* @expectedException \Symfony\Component\DependencyInjection\Exception\EnvParameterException
81+
*/
82+
public function testDynamicServiceName()
83+
{
84+
$container = new ContainerBuilder();
85+
$env = $container->getParameterBag()->get('env(BAR)');
86+
$container->register("foo.$env", 'class');
87+
88+
$this->process($container);
89+
}
90+
91+
/**
92+
* @expectedException \Symfony\Component\DependencyInjection\Exception\EnvParameterException
93+
*/
94+
public function testDynamicAliasName()
95+
{
96+
$container = new ContainerBuilder();
97+
$env = $container->getParameterBag()->get('env(BAR)');
98+
$container->setAlias("foo.$env", 'class');
99+
100+
$this->process($container);
101+
}
102+
79103
protected function process(ContainerBuilder $container)
80104
{
81105
$pass = new CheckDefinitionValidityPass();

0 commit comments

Comments
 (0)