Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 420f089

Browse filesBrowse files
committed
bug #23949 [Dotenv] Get env using $_SERVER to work with fastcgi_param and workaround thread safety issues (nicolas-grekas)
This PR was merged into the 3.3 branch. Discussion ---------- [Dotenv] Get env using $_SERVER to work with fastcgi_param and workaround thread safety issues | Q | A | ------------- | --- | Branch? | 3.3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #23502 | License | MIT | Doc PR | - `getenv()` is not thread safe, and doesn't work with `fastcgi_param`, see links in linked issue. Commits ------- f76e420 [Dotenv] Get env using $_SERVER to work with fastcgi_param and workaround thread safety issues
2 parents 2204f91 + f76e420 commit 420f089
Copy full SHA for 420f089

File tree

3 files changed

+30
-5
lines changed
Filter options

3 files changed

+30
-5
lines changed

‎src/Symfony/Component/DependencyInjection/Container.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Container.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,13 +438,13 @@ protected function getEnv($name)
438438
if (isset($this->envCache[$name]) || array_key_exists($name, $this->envCache)) {
439439
return $this->envCache[$name];
440440
}
441-
if (0 !== strpos($name, 'HTTP_') && isset($_SERVER[$name])) {
441+
if (isset($_SERVER[$name]) && 0 !== strpos($name, 'HTTP_')) {
442442
return $this->envCache[$name] = $_SERVER[$name];
443443
}
444444
if (isset($_ENV[$name])) {
445445
return $this->envCache[$name] = $_ENV[$name];
446446
}
447-
if (false !== $env = getenv($name)) {
447+
if (false !== ($env = getenv($name)) && null !== $env) { // null is a possible value because of thread safety issues
448448
return $this->envCache[$name] = $env;
449449
}
450450
if (!$this->hasParameter("env($name)")) {

‎src/Symfony/Component/Dotenv/Dotenv.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Dotenv/Dotenv.php
+15-3Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,17 @@ public function populate($values)
7070
unset($loadedVars['']);
7171

7272
foreach ($values as $name => $value) {
73-
if (!isset($loadedVars[$name]) && (isset($_ENV[$name]) || isset($_SERVER[$name]) || false !== getenv($name))) {
73+
$notHttpName = 0 !== strpos($name, 'HTTP_');
74+
// don't check existence with getenv() because of thread safety issues
75+
if (!isset($loadedVars[$name]) && (isset($_ENV[$name]) || (isset($_SERVER[$name]) && $notHttpName))) {
7476
continue;
7577
}
7678

7779
putenv("$name=$value");
7880
$_ENV[$name] = $value;
79-
$_SERVER[$name] = $value;
81+
if ($notHttpName) {
82+
$_SERVER[$name] = $value;
83+
}
8084

8185
$loadedVars[$name] = true;
8286
}
@@ -363,7 +367,15 @@ private function resolveVariables($value)
363367
}
364368

365369
$name = $matches[3];
366-
$value = isset($this->values[$name]) ? $this->values[$name] : (isset($_ENV[$name]) ? $_ENV[$name] : (string) getenv($name));
370+
if (isset($this->values[$name])) {
371+
$value = $this->values[$name];
372+
} elseif (isset($_SERVER[$name]) && 0 !== strpos($name, 'HTTP_')) {
373+
$value = $_SERVER[$name];
374+
} elseif (isset($_ENV[$name])) {
375+
$value = $_ENV[$name];
376+
} else {
377+
$value = (string) getenv($name);
378+
}
367379

368380
if (!$matches[2] && isset($matches[4])) {
369381
$value .= '}';

‎src/Symfony/Component/Dotenv/Tests/DotenvTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Dotenv/Tests/DotenvTest.php
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,26 @@ public function testServerSuperglobalIsNotOverriden()
208208
public function testEnvVarIsNotOverriden()
209209
{
210210
putenv('TEST_ENV_VAR=original_value');
211+
$_SERVER['TEST_ENV_VAR'] = 'original_value';
211212

212213
$dotenv = new DotEnv();
213214
$dotenv->populate(array('TEST_ENV_VAR' => 'new_value'));
214215

215216
$this->assertSame('original_value', getenv('TEST_ENV_VAR'));
216217
}
217218

219+
public function testHttpVarIsPartiallyOverriden()
220+
{
221+
$_SERVER['HTTP_TEST_ENV_VAR'] = 'http_value';
222+
223+
$dotenv = new DotEnv();
224+
$dotenv->populate(array('HTTP_TEST_ENV_VAR' => 'env_value'));
225+
226+
$this->assertSame('env_value', getenv('HTTP_TEST_ENV_VAR'));
227+
$this->assertSame('env_value', $_ENV['HTTP_TEST_ENV_VAR']);
228+
$this->assertSame('http_value', $_SERVER['HTTP_TEST_ENV_VAR']);
229+
}
230+
218231
public function testMemorizingLoadedVarsNamesInSpecialVar()
219232
{
220233
// Special variable not exists

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.