Skip to content

Environment variables are not inherited when $_ENV is empty #26451

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

Closed
hongaar opened this issue Mar 7, 2018 · 5 comments
Closed

Environment variables are not inherited when $_ENV is empty #26451

hongaar opened this issue Mar 7, 2018 · 5 comments

Comments

@hongaar
Copy link

hongaar commented Mar 7, 2018

Q A
Bug report? yes
Feature request? no
BC Break report? not sure
RFC? no
Symfony version >=3.3

It looks like starting with version 3.3.x in the symfony/process component, there's an issue with inheriting environment variables when $_ENV is empty - which is the recommended setup for production systems (see http://php.net/manual/en/reserved.variables.environment.php#98113).

Could be solved by including an extra loop with getenv() in Process::getDefaultEnv(), but not sure if there's a rationale to not do this.

This bug might cause hard to find bugs - i.e. I discovered this issue after debugging an issue for hours where a headless chrome instance wouldn't listen to the specified port when ran with --remote-debugging-port=9222, turned out it was missing some - apparently - critical env vars.

Reproduce:

$_ENV = [];
$process = new PhpProcess(<<<EOF
<?php var_dump(getenv()); ?>
EOF
);
$process->setEnv(getenv()); // <-- comment out this line to see bug
$process->run();

echo "<pre>".$process->getOutput();

Edit: seems to conflict with #25559, though I think that could be solved by introducing a loop and filtering out all non-string values, like in the other loops.

@nicolas-grekas
Copy link
Member

The env is inherited using the values in both $_SERVER and $_ENV. getenv() is not used because it is not available in PHP 5.5, and it shouldn't be needed since either $_SERVER or $_ENV contain the env vars. If you modify these arrays, it's your responsibility to do it wisely I believe.

@hongaar
Copy link
Author

hongaar commented Mar 14, 2018

If I'm not mistaken, getenv() is around since PHP 4 😉

On the other hand, $_ENV is unavailable on production systems using php.ini-production. $_SERVER is though, I'm not sure why it's still not working for me. I'll do some more tests and update this issue.

@nicolas-grekas
Copy link
Member

getenv() with no arguments exists only since 7.1. $_SERVER also contains the env vars.

@hongaar
Copy link
Author

hongaar commented Mar 14, 2018

Snap, I missed that from the docs. Thanks for the clarification.

@hongaar
Copy link
Author

hongaar commented Mar 14, 2018

I think I traced it down to being an issue with PHPs built-in web server.

<?php
# test.php
var_dump($_SERVER);
var_dump($_ENV);
var_dump(getenv());

And run with php -S 0.0.0.0:88 test.php results in:

array(23) {
  ["DOCUMENT_ROOT"]
  // ... some more server vars, without env vars
}
array(0) {
}
array(62) {
  ["PATH"]
  // ... all env vars
}

A simple workaround for this would be (granted you're on PHP >= 7.1):

if (php_sapi_name() === "cli-server") {
    $_ENV = getenv();
}

Or running the server with php -S 0.0.0.0:88 -d variables_order=EGPCS test.php (source).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants