diff --git a/src/Symfony/Component/DependencyInjection/Compiler/MergeExtensionConfigurationPass.php b/src/Symfony/Component/DependencyInjection/Compiler/MergeExtensionConfigurationPass.php index a0a896458fc57..3391f15616787 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/MergeExtensionConfigurationPass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/MergeExtensionConfigurationPass.php @@ -117,9 +117,15 @@ public function freezeAfterProcessing(Extension $extension, ContainerBuilder $co // serialize config and container to catch env vars nested in object graphs $config = serialize($config).serialize($container->getDefinitions()).serialize($container->getAliases()).serialize($container->getParameterBag()->all()); + if (false === stripos($config, 'env_')) { + return; + } + + preg_match_all('/env_[a-f0-9]{16}_\w+_[a-f0-9]{32}/Ui', $config, $matches); + $usedPlaceholders = array_flip($matches[0]); foreach (parent::getEnvPlaceholders() as $env => $placeholders) { foreach ($placeholders as $placeholder) { - if (false !== stripos($config, $placeholder)) { + if (isset($usedPlaceholders[$placeholder])) { $this->processedEnvPlaceholders[$env] = $placeholders; break; } diff --git a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php index 5f9bb9d1d8de7..033492623b39d 100644 --- a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php +++ b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php @@ -1358,15 +1358,17 @@ public function resolveEnvPlaceholders(mixed $value, string|bool $format = null, return $result; } - if (!\is_string($value) || 38 > \strlen($value) || !preg_match('/env[_(]/i', $value)) { + if (!\is_string($value) || 38 > \strlen($value) || false === stripos($value, 'env_')) { return $value; } $envPlaceholders = $bag instanceof EnvPlaceholderParameterBag ? $bag->getEnvPlaceholders() : $this->envPlaceholders; $completed = false; + preg_match_all('/env_[a-f0-9]{16}_\w+_[a-f0-9]{32}/Ui', $value, $matches); + $usedPlaceholders = array_flip($matches[0]); foreach ($envPlaceholders as $env => $placeholders) { foreach ($placeholders as $placeholder) { - if (false !== stripos($value, $placeholder)) { + if (isset($usedPlaceholders[$placeholder])) { if (true === $format) { $resolved = $bag->escapeValue($this->getEnv($env)); } else { diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/ValidateEnvPlaceholdersPassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/ValidateEnvPlaceholdersPassTest.php index 50828a47b4bb3..e7bdb78615362 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/ValidateEnvPlaceholdersPassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/ValidateEnvPlaceholdersPassTest.php @@ -153,6 +153,19 @@ public function testConcatenatedEnvInConfig() $this->assertSame(['scalar_node' => $expected], $container->resolveEnvPlaceholders($ext->getConfig())); } + public function testSurroundedEnvInConfig() + { + $container = new ContainerBuilder(); + $container->registerExtension($ext = new EnvExtension()); + $container->prependExtensionConfig('env_extension', [ + 'scalar_node' => $expected = 'foo%env(BAR)%baz', + ]); + + $this->doProcess($container); + + $this->assertSame(['scalar_node' => $expected], $container->resolveEnvPlaceholders($ext->getConfig())); + } + public function testEnvIsIncompatibleWithArrayNode() { $this->expectException(InvalidConfigurationException::class);