From 08291b1665a96c77a6516ac52c4a100135e7061f Mon Sep 17 00:00:00 2001 From: Thomas Calvet Date: Thu, 28 Oct 2021 12:54:11 +0200 Subject: [PATCH] [Dotenv] Duplicate $_SERVER values in $_ENV if they don't exist Co-authored-by: Nicolas Grekas --- src/Symfony/Component/Dotenv/Dotenv.php | 6 +++++- src/Symfony/Component/Dotenv/Tests/DotenvTest.php | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Dotenv/Dotenv.php b/src/Symfony/Component/Dotenv/Dotenv.php index 57128b4ec73f4..8180f4bfa10ba 100644 --- a/src/Symfony/Component/Dotenv/Dotenv.php +++ b/src/Symfony/Component/Dotenv/Dotenv.php @@ -136,8 +136,12 @@ public function populate(array $values, bool $overrideExistingVars = false): voi foreach ($values as $name => $value) { $notHttpName = 0 !== strpos($name, 'HTTP_'); + if (isset($_SERVER[$name]) && $notHttpName && !isset($_ENV[$name])) { + $_ENV[$name] = $_SERVER[$name]; + } + // don't check existence with getenv() because of thread safety issues - if (!isset($loadedVars[$name]) && (!$overrideExistingVars && (isset($_ENV[$name]) || (isset($_SERVER[$name]) && $notHttpName)))) { + if (!isset($loadedVars[$name]) && !$overrideExistingVars && isset($_ENV[$name])) { continue; } diff --git a/src/Symfony/Component/Dotenv/Tests/DotenvTest.php b/src/Symfony/Component/Dotenv/Tests/DotenvTest.php index 4428658d9ac78..62494e4c07076 100644 --- a/src/Symfony/Component/Dotenv/Tests/DotenvTest.php +++ b/src/Symfony/Component/Dotenv/Tests/DotenvTest.php @@ -493,4 +493,14 @@ public function testDoNotUsePutenv() $this->assertSame('no', $_ENV['TEST_USE_PUTENV']); $this->assertFalse(getenv('TEST_USE_PUTENV')); } + + public function testSERVERVarsDuplicationInENV() + { + unset($_ENV['SYMFONY_DOTENV_VARS'], $_SERVER['SYMFONY_DOTENV_VARS'], $_ENV['FOO']); + $_SERVER['FOO'] = 'CCC'; + + (new Dotenv(false))->populate(['FOO' => 'BAR']); + + $this->assertSame('CCC', $_ENV['FOO']); + } }