From 2c0815d04e0234a4a10b2232fa5911451b256ddd Mon Sep 17 00:00:00 2001 From: Thomas Calvet Date: Fri, 30 Jun 2023 18:17:14 +0200 Subject: [PATCH] [DependencyInjection] Fix autocasting null env values to empty string --- .../Component/DependencyInjection/Container.php | 10 +++++++++- .../DependencyInjection/EnvVarProcessor.php | 10 ++++++++++ .../Tests/EnvVarProcessorTest.php | 16 +++++++++++++++- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Container.php b/src/Symfony/Component/DependencyInjection/Container.php index 838756d47edba..235b76ef44fd1 100644 --- a/src/Symfony/Component/DependencyInjection/Container.php +++ b/src/Symfony/Component/DependencyInjection/Container.php @@ -390,7 +390,15 @@ protected function getEnv(string $name) $prefix = 'string'; $localName = $name; } - $processor = $processors->has($prefix) ? $processors->get($prefix) : new EnvVarProcessor($this); + + if ($processors->has($prefix)) { + $processor = $processors->get($prefix); + } else { + $processor = new EnvVarProcessor($this); + if (false === $i) { + $prefix = ''; + } + } $this->resolving[$envName] = true; try { diff --git a/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php b/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php index aec6a79b228cc..818174b3970c7 100644 --- a/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php +++ b/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php @@ -126,6 +126,12 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv) } } + $returnNull = false; + if ('' === $prefix) { + $returnNull = true; + $prefix = 'string'; + } + if (false !== $i || 'string' !== $prefix) { $env = $getEnv($name); } elseif (isset($_ENV[$name])) { @@ -177,6 +183,10 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv) } if (null === $env) { + if ($returnNull) { + return null; + } + if (!isset($this->getProvidedTypes()[$prefix])) { throw new RuntimeException(sprintf('Unsupported env var prefix "%s".', $prefix)); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php b/src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php index 72cf270a632b6..7ff161c06cee3 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php @@ -763,12 +763,13 @@ public static function provideGetEnvUrlPath() /** * @testWith ["", "string"] + * [null, ""] * [false, "bool"] * [true, "not"] * [0, "int"] * [0.0, "float"] */ - public function testGetEnvCastsNull($expected, string $prefix) + public function testGetEnvCastsNullBehavior($expected, string $prefix) { $processor = new EnvVarProcessor(new Container()); @@ -778,4 +779,17 @@ public function testGetEnvCastsNull($expected, string $prefix) }); })); } + + public function testGetEnvWithEmptyStringPrefixCastsToString() + { + $processor = new EnvVarProcessor(new Container()); + unset($_ENV['FOO']); + $_ENV['FOO'] = 4; + + try { + $this->assertSame('4', $processor->getEnv('', 'FOO', function () { $this->fail('Should not be called'); })); + } finally { + unset($_ENV['FOO']); + } + } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy