Skip to content

Commit 85f7cb1

Browse files
committed
main: Deprecate deriving $_SERVER['argc'] and $_SERVER['argv'] from the query string
RFC: https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_the_register_argc_argv_ini_directive
1 parent 026d470 commit 85f7cb1

File tree

7 files changed

+123
-7
lines changed

7 files changed

+123
-7
lines changed

main/php_variables.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -785,10 +785,13 @@ static void php_autoglobal_merge(HashTable *dest, HashTable *src)
785785
PHPAPI zend_result php_hash_environment(void)
786786
{
787787
memset(PG(http_globals), 0, sizeof(PG(http_globals)));
788+
/* Register $argc and $argv for CLI SAPIs. $_SERVER['argc'] and $_SERVER['argv']
789+
* will be registered in php_auto_globals_create_server() which clears
790+
* PG(http_globals)[TRACK_VARS_SERVER] anyways, making registration at this point
791+
* useless.
792+
*/
793+
php_build_argv(NULL, NULL);
788794
zend_activate_auto_globals();
789-
if (PG(register_argc_argv) || SG(request_info).argc) {
790-
php_build_argv(SG(request_info).query_string, &PG(http_globals)[TRACK_VARS_SERVER]);
791-
}
792795
return SUCCESS;
793796
}
794797
/* }}} */
@@ -885,6 +888,7 @@ static bool php_auto_globals_create_server(zend_string *name)
885888
zend_hash_update(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]), ZSTR_KNOWN(ZEND_STR_ARGC), argc);
886889
}
887890
} else if (PG(register_argc_argv)) {
891+
zend_error(E_DEPRECATED, "Deriving $_SERVER['argc'] and $_SERVER['argv'] from $_SERVER['QUERY_STRING'] is deprecated, configure register_argc_argv=0 to suppress this message and access the query parameters via $_SERVER['QUERY_STRING'] or $_GET");
888892
php_build_argv(SG(request_info).query_string, &PG(http_globals)[TRACK_VARS_SERVER]);
889893
}
890894

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
--TEST--
2+
FPM: bug75712 - getenv should not read from $_ENV and $_SERVER
3+
--SKIPIF--
4+
<?php include "skipif.inc"; ?>
5+
--FILE--
6+
<?php
7+
8+
require_once "tester.inc";
9+
10+
$cfg = <<<EOT
11+
[global]
12+
error_log = {{FILE:LOG}}
13+
[unconfined]
14+
listen = {{ADDR}}
15+
pm = static
16+
pm.max_children = 1
17+
env[TEST] = test
18+
php_value[register_argc_argv] = on
19+
php_value[html_errors] = off
20+
EOT;
21+
22+
$code = <<<EOT
23+
<?php
24+
25+
var_dump(isset(getenv()['argv']));
26+
var_dump(isset(getenv()['SERVER_NAME']));
27+
var_dump(getenv()['TEST']);
28+
var_dump(isset(getenv()['DTEST']));
29+
var_dump(getenv('DTEST'));
30+
putenv('DTEST=dt');
31+
var_dump(getenv()['DTEST']);
32+
var_dump(getenv('DTEST'));
33+
34+
function notcalled()
35+
{
36+
\$_SERVER['argv'];
37+
}
38+
EOT;
39+
40+
$tester = new FPM\Tester($cfg, $code);
41+
$tester->start();
42+
$tester->expectLogStartNotices();
43+
$response = $tester->request();
44+
echo "=====", PHP_EOL;
45+
$response->printBody();
46+
echo "=====", PHP_EOL;
47+
$tester->terminate();
48+
$tester->close();
49+
50+
?>
51+
Done
52+
--EXPECTF--
53+
=====
54+
Deprecated: Deriving $_SERVER['argc'] and $_SERVER['argv'] from $_SERVER['QUERY_STRING'] is deprecated, configure register_argc_argv=0 to suppress this message and access the query parameters via $_SERVER['QUERY_STRING'] or $_GET in %s on line %d
55+
bool(false)
56+
bool(true)
57+
string(4) "test"
58+
bool(false)
59+
bool(false)
60+
string(2) "dt"
61+
string(2) "dt"
62+
=====
63+
Done
64+
--CLEAN--
65+
<?php
66+
require_once "tester.inc";
67+
FPM\Tester::clean();
68+
?>

sapi/fpm/tests/bug75712-getenv-server-vars.phpt renamed to sapi/fpm/tests/bug75712-getenv-server-vars_002.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
FPM: bug75712 - getenv should not read from $_ENV and $_SERVER
2+
FPM: bug75712 - getenv should not read from $_ENV and $_SERVER (register_argc_argv=off)
33
--SKIPIF--
44
<?php include "skipif.inc"; ?>
55
--FILE--
@@ -15,7 +15,7 @@ listen = {{ADDR}}
1515
pm = static
1616
pm.max_children = 1
1717
env[TEST] = test
18-
php_value[register_argc_argv] = on
18+
php_value[register_argc_argv] = off
1919
EOT;
2020

2121
$code = <<<EOT

tests/basic/011.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ for ($i=0; $i<$argc; $i++) {
1818
}
1919

2020
?>
21-
--EXPECT--
21+
--EXPECTF--
22+
Deprecated: Deriving $_SERVER['argc'] and $_SERVER['argv'] from $_SERVER['QUERY_STRING'] is deprecated, configure register_argc_argv=0 to suppress this message and access the query parameters via $_SERVER['QUERY_STRING'] or $_GET in %s on line %d
2223
0: ab
2324
1: cd
2425
2: ef

tests/basic/011_empty_query.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Testing $argc and $argv handling (GET empty)
3+
--SKIPIF--
4+
<?php
5+
if(substr(PHP_OS, 0, 3) == 'WIN') die("skip on windows: --INI-- is ignored due to 4b9cd27ff5c0177dcb160caeae1ea79e761ada58");
6+
?>
7+
--INI--
8+
register_argc_argv=1
9+
--CGI--
10+
--FILE--
11+
<?php
12+
13+
var_dump($_SERVER['argc'], $_SERVER['argv']);
14+
15+
?>
16+
--EXPECTF--
17+
Deprecated: Deriving $_SERVER['argc'] and $_SERVER['argv'] from $_SERVER['QUERY_STRING'] is deprecated, configure register_argc_argv=0 to suppress this message and access the query parameters via $_SERVER['QUERY_STRING'] or $_GET in %s on line %d
18+
int(0)
19+
array(0) {
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
Testing $argc and $argv handling (GET, register_argc_argv=0)
3+
--SKIPIF--
4+
<?php
5+
if(substr(PHP_OS, 0, 3) == 'WIN') die("skip on windows: --INI-- is ignored due to 4b9cd27ff5c0177dcb160caeae1ea79e761ada58");
6+
?>
7+
--INI--
8+
register_argc_argv=0
9+
--GET--
10+
ab+cd+ef+123+test
11+
--FILE--
12+
<?php
13+
14+
var_dump($_SERVER['argc'], $_SERVER['argv']);
15+
16+
?>
17+
--EXPECTF--
18+
Warning: Undefined array key "argc" in %s on line %d
19+
20+
Warning: Undefined array key "argv" in %s on line %d
21+
NULL
22+
NULL

tests/basic/011_windows.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ for ($i=0; $i<$argc; $i++) {
1414
}
1515

1616
?>
17-
--EXPECT--
17+
--EXPECTF--
18+
Deprecated: Deriving $_SERVER['argc'] and $_SERVER['argv'] from $_SERVER['QUERY_STRING'] is deprecated, configure register_argc_argv=0 to suppress this message and access the query parameters via $_SERVER['QUERY_STRING'] or $_GET in %s on line %d
1819
0: foo=ab
1920
1: cd
2021
2: ef

0 commit comments

Comments
 (0)