Skip to content

Fix bug #75712: getenv in php-fpm should not read $_ENV, $_SERVER #13195

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
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ PHP_FUNCTION(getenv)

if (!str) {
array_init(return_value);
php_import_environment_variables(return_value);
php_load_environment_variables(return_value);
return;
}

Expand Down
7 changes: 7 additions & 0 deletions main/php_variables.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@

/* for systems that need to override reading of environment variables */
void _php_import_environment_variables(zval *array_ptr);
void _php_load_environment_variables(zval *array_ptr);
PHPAPI void (*php_import_environment_variables)(zval *array_ptr) = _php_import_environment_variables;
PHPAPI void (*php_load_environment_variables)(zval *array_ptr) = _php_load_environment_variables;

PHPAPI void php_register_variable(const char *var, const char *strval, zval *track_vars_array)
{
Expand Down Expand Up @@ -632,6 +634,11 @@ void _php_import_environment_variables(zval *array_ptr)
tsrm_env_unlock();
}

void _php_load_environment_variables(zval *array_ptr)
{
php_import_environment_variables(array_ptr);
}

bool php_std_auto_global_callback(char *name, uint32_t name_len)
{
zend_printf("%s\n", name);
Expand Down
1 change: 1 addition & 0 deletions main/php_variables.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
BEGIN_EXTERN_C()
void php_startup_auto_globals(void);
extern PHPAPI void (*php_import_environment_variables)(zval *array_ptr);
extern PHPAPI void (*php_load_environment_variables)(zval *array_ptr);
PHPAPI void php_register_variable(const char *var, const char *val, zval *track_vars_array);
/* binary-safe version */
PHPAPI void php_register_variable_safe(const char *var, const char *val, size_t val_len, zval *track_vars_array);
Expand Down
18 changes: 16 additions & 2 deletions sapi/fpm/fpm/fpm_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,21 @@ static void cgi_php_load_env_var(const char *var, unsigned int var_len, char *va
}
/* }}} */

void cgi_php_import_environment_variables(zval *array_ptr) /* {{{ */
static void cgi_php_load_env_var_unfilterd(const char *var, unsigned int var_len, char *val, unsigned int val_len, void *arg)
{
zval *array_ptr = (zval *) arg;
php_register_variable_safe(var, val, val_len, array_ptr);
}

static void cgi_php_load_environment_variables(zval *array_ptr)
{
php_php_import_environment_variables(array_ptr);

fcgi_request *request = (fcgi_request*) SG(server_context);
fcgi_loadenv(request, cgi_php_load_env_var_unfilterd, array_ptr);
}

static void cgi_php_import_environment_variables(zval *array_ptr)
{
fcgi_request *request = NULL;

Expand All @@ -542,7 +556,6 @@ void cgi_php_import_environment_variables(zval *array_ptr) /* {{{ */
request = (fcgi_request*) SG(server_context);
fcgi_loadenv(request, cgi_php_load_env_var, array_ptr);
}
/* }}} */

static void sapi_cgi_register_variables(zval *track_vars_array) /* {{{ */
{
Expand Down Expand Up @@ -1840,6 +1853,7 @@ consult the installation file that came with this distribution, or visit \n\
/* make php call us to get _ENV vars */
php_php_import_environment_variables = php_import_environment_variables;
php_import_environment_variables = cgi_php_import_environment_variables;
php_load_environment_variables = cgi_php_load_environment_variables;

/* library is already initialized, now init our request */
request = fpm_init_request(fcgi_fd);
Expand Down
62 changes: 62 additions & 0 deletions sapi/fpm/tests/bug75712-getenv-server-vars.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
--TEST--
FPM: bug75712 - getenv should not read from $_ENV and $_SERVER
--SKIPIF--
<?php include "skipif.inc"; ?>
--FILE--
<?php

require_once "tester.inc";

$cfg = <<<EOT
[global]
error_log = {{FILE:LOG}}
[unconfined]
listen = {{ADDR}}
pm = static
pm.max_children = 1
env[TEST] = test
php_value[register_argc_argv] = on
EOT;

$code = <<<EOT
<?php

var_dump(isset(getenv()['argv']));
var_dump(isset(getenv()['SERVER_NAME']));
var_dump(getenv()['TEST']);
var_dump(isset(getenv()['DTEST']));
var_dump(getenv('DTEST'));
putenv('DTEST=dt');
var_dump(getenv()['DTEST']);
var_dump(getenv('DTEST'));

function notcalled()
{
\$_SERVER['argv'];
}
EOT;

$tester = new FPM\Tester($cfg, $code);
$tester->start();
$tester->expectLogStartNotices();
$tester->request()->expectBody([
'bool(false)',
'bool(true)',
'string(4) "test"',
'bool(false)',
'bool(false)',
'string(2) "dt"',
'string(2) "dt"',
]);
$tester->terminate();
$tester->close();

?>
Done
--EXPECT--
Done
--CLEAN--
<?php
require_once "tester.inc";
FPM\Tester::clean();
?>