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

[Dotenv] Get env using $_SERVER to work with fastcgi_param and workaround thread safety issues #23949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions 4 src/Symfony/Component/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -438,13 +438,13 @@ protected function getEnv($name)
if (isset($this->envCache[$name]) || array_key_exists($name, $this->envCache)) {
return $this->envCache[$name];
}
if (0 !== strpos($name, 'HTTP_') && isset($_SERVER[$name])) {
if (isset($_SERVER[$name]) && 0 !== strpos($name, 'HTTP_')) {
return $this->envCache[$name] = $_SERVER[$name];
}
if (isset($_ENV[$name])) {
return $this->envCache[$name] = $_ENV[$name];
}
if (false !== $env = getenv($name)) {
if (false !== ($env = getenv($name)) && null !== $env) { // null is a possible value because of thread safety issues
return $this->envCache[$name] = $env;
}
if (!$this->hasParameter("env($name)")) {
Expand Down
18 changes: 15 additions & 3 deletions 18 src/Symfony/Component/Dotenv/Dotenv.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,17 @@ public function populate($values)
unset($loadedVars['']);

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

putenv("$name=$value");
$_ENV[$name] = $value;
$_SERVER[$name] = $value;
if ($notHttpName) {
Copy link
Member Author

@nicolas-grekas nicolas-grekas Aug 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with this, env vars that start with HTTP_* do not populate $_SERVER anymore.
this is to mitigate security issues with $_SERVER mixing several sources (env+http headers+etc).

$_SERVER[$name] = $value;
}

$loadedVars[$name] = true;
}
Expand Down Expand Up @@ -363,7 +367,15 @@ private function resolveVariables($value)
}

$name = $matches[3];
$value = isset($this->values[$name]) ? $this->values[$name] : (isset($_ENV[$name]) ? $_ENV[$name] : (string) getenv($name));
if (isset($this->values[$name])) {
$value = $this->values[$name];
} elseif (isset($_SERVER[$name]) && 0 !== strpos($name, 'HTTP_')) {
$value = $_SERVER[$name];
} elseif (isset($_ENV[$name])) {
$value = $_ENV[$name];
} else {
$value = (string) getenv($name);
}

if (!$matches[2] && isset($matches[4])) {
$value .= '}';
Expand Down
13 changes: 13 additions & 0 deletions 13 src/Symfony/Component/Dotenv/Tests/DotenvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,26 @@ public function testServerSuperglobalIsNotOverriden()
public function testEnvVarIsNotOverriden()
{
putenv('TEST_ENV_VAR=original_value');
$_SERVER['TEST_ENV_VAR'] = 'original_value';

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

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

public function testHttpVarIsPartiallyOverriden()
{
$_SERVER['HTTP_TEST_ENV_VAR'] = 'http_value';

$dotenv = new DotEnv();
$dotenv->populate(array('HTTP_TEST_ENV_VAR' => 'env_value'));

$this->assertSame('env_value', getenv('HTTP_TEST_ENV_VAR'));
$this->assertSame('env_value', $_ENV['HTTP_TEST_ENV_VAR']);
$this->assertSame('http_value', $_SERVER['HTTP_TEST_ENV_VAR']);
}

public function testMemorizingLoadedVarsNamesInSpecialVar()
{
// Special variable not exists
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.