Skip to content

Commit 3c68f38

Browse files
committed
Restrict allowed usages of $GLOBALS
This restricts allowed usage of $GLOBALS, with the effect that plain PHP arrays can no longer contain INDIRECT elements. RFC: https://wiki.php.net/rfc/restrict_globals_usage Closes GH-6487.
1 parent 73f989a commit 3c68f38

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+869
-642
lines changed

UPGRADING

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ PHP 8.1 UPGRADE NOTES
1919
1. Backward Incompatible Changes
2020
========================================
2121

22+
- Core:
23+
. Access to the $GLOBALS array is now subject to a number of restrictions.
24+
Read and write access to individual array elements like $GLOBALS['var']
25+
continues to work as-is. Read-only access to the entire $GLOBALS array also
26+
continues to be supported. However, write access to the entire $GLOBALS
27+
array is no longer supported. For example, array_pop($GLOBALS) will result
28+
in an error.
29+
RFC: https://wiki.php.net/rfc/restrict_globals_usage
30+
2231
- Fileinfo:
2332
. The fileinfo functions now accept and return, respectively, finfo objects
2433
instead of resources.

Zend/tests/array_self_add_globals.phpt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
Add $GLOBALS to itself
33
--FILE--
44
<?php
5-
$GLOBALS += $GLOBALS;
65
$x = $GLOBALS + $GLOBALS;
76
?>
87
===DONE===

Zend/tests/bug71539_6.phpt

Lines changed: 0 additions & 15 deletions
This file was deleted.

Zend/tests/bug71695.phpt

Lines changed: 0 additions & 17 deletions
This file was deleted.

Zend/tests/gc_010.phpt

Lines changed: 0 additions & 21 deletions
This file was deleted.

Zend/tests/globals_001.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ string(%d) "%s"
2929
Warning: Undefined array key "PHP_SELF" in %s on line %d
3030
NULL
3131

32-
Warning: Undefined variable $_SERVER in %s on line %d
32+
Warning: Undefined global variable $_SERVER in %s on line %d
3333
NULL
3434
Done

Zend/tests/globals_002.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ string(%d) "%s"
3232
Warning: Undefined array key "PHP_SELF" in %s on line %d
3333
NULL
3434

35-
Warning: Undefined variable $_SERVER in %s on line %d
35+
Warning: Undefined global variable $_SERVER in %s on line %d
3636
NULL
3737
Done

Zend/tests/globals_003.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ string(%d) "%s"
3838
Warning: Undefined array key "PHP_SELF" in %s on line %d
3939
NULL
4040

41-
Warning: Undefined variable $_SERVER in %s on line %d
41+
Warning: Undefined global variable $_SERVER in %s on line %d
4242
NULL
4343
Done

Zend/tests/globals_004.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ string(%d) "%s"
2323
Warning: Undefined array key "PHP_SELF" in %s on line %d
2424
NULL
2525

26-
Warning: Undefined variable $_SERVER in %s on line %d
26+
Warning: Undefined global variable $_SERVER in %s on line %d
2727
NULL
2828
Done
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
$GLOBALS no longer contains 'GLOBALS'
3+
--FILE--
4+
<?php
5+
6+
$g = $GLOBALS;
7+
var_dump(isset($g['GLOBALS']));
8+
9+
?>
10+
--EXPECT--
11+
bool(false)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Cannot assign to $GLOBALS
3+
--FILE--
4+
<?php
5+
6+
$GLOBALS = [];
7+
8+
?>
9+
--EXPECTF--
10+
Fatal error: $GLOBALS can only be modified using the $GLOBALS[$name] = $value syntax in %s on line %d
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Cannot list-assign to $GLOBALS
3+
--FILE--
4+
<?php
5+
6+
list($GLOBALS) = [1];
7+
8+
?>
9+
--EXPECTF--
10+
Fatal error: $GLOBALS can only be modified using the $GLOBALS[$name] = $value syntax in %s on line %d
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Cannot list-assign to $GLOBALS (by-ref)
3+
--FILE--
4+
<?php
5+
6+
list(&$GLOBALS) = [1];
7+
8+
?>
9+
--EXPECTF--
10+
Fatal error: Cannot assign reference to non referencable value in %s on line %d
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Cannot compound assign to $GLOBALS
3+
--FILE--
4+
<?php
5+
6+
$GLOBALS += [];
7+
8+
?>
9+
--EXPECTF--
10+
Fatal error: $GLOBALS can only be modified using the $GLOBALS[$name] = $value syntax in %s on line %d
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
Cannot by-ref assign to $GLOBALS (LHS)
3+
--FILE--
4+
<?php
5+
6+
$var = [];
7+
$GLOBALS =& $var;
8+
9+
?>
10+
--EXPECTF--
11+
Fatal error: $GLOBALS can only be modified using the $GLOBALS[$name] = $value syntax in %s on line %d
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
Cannot by-ref assign to $GLOBALS (RHS)
3+
--FILE--
4+
<?php
5+
6+
$var = [];
7+
$var =& $GLOBALS;
8+
9+
?>
10+
--EXPECTF--
11+
Fatal error: Cannot acquire reference to $GLOBALS in %s on line %d
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Cannot use $GLOBALS as foreach result variable
3+
--FILE--
4+
<?php
5+
6+
foreach ([1] as $GLOBALS) {}
7+
8+
?>
9+
--EXPECTF--
10+
Fatal error: $GLOBALS can only be modified using the $GLOBALS[$name] = $value syntax in %s on line %d
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Cannot use $GLOBALS as foreach result variable (by-ref)
3+
--FILE--
4+
<?php
5+
6+
foreach ([1] as &$GLOBALS) {}
7+
8+
?>
9+
--EXPECTF--
10+
Fatal error: $GLOBALS can only be modified using the $GLOBALS[$name] = $value syntax in %s on line %d
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
$GLOBALS cannot be passed by reference (runtime error)
3+
--FILE--
4+
<?php
5+
6+
function by_ref(&$ref) {}
7+
try {
8+
by_ref($GLOBALS);
9+
} catch (Error $e) {
10+
echo $e->getMessage(), "\n";
11+
}
12+
13+
try {
14+
by_ref2($GLOBALS);
15+
} catch (Error $e) {
16+
echo $e->getMessage(), "\n";
17+
}
18+
function by_ref2(&$ref) {}
19+
20+
?>
21+
--EXPECT--
22+
by_ref(): Argument #1 ($ref) cannot be passed by reference
23+
by_ref2(): Argument #1 ($ref) cannot be passed by reference
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Cannot unset $GLOBALS
3+
--FILE--
4+
<?php
5+
6+
unset($GLOBALS);
7+
8+
?>
9+
--EXPECTF--
10+
Fatal error: $GLOBALS can only be modified using the $GLOBALS[$name] = $value syntax in %s on line %d
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
$GLOBALS should have canonicalized keys
3+
--FILE--
4+
<?php
5+
6+
${1} = 42;
7+
var_dump($GLOBALS[1]);
8+
9+
?>
10+
--EXPECT--
11+
int(42)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
--TEST--
2+
Supported operations on $GLOBALS
3+
--FILE--
4+
<?php
5+
6+
function test() {
7+
var_dump($GLOBALS['x']);
8+
$GLOBALS['x'] = 1;
9+
var_dump($GLOBALS['x']);
10+
$GLOBALS['x']++;
11+
var_dump($GLOBALS['x']);
12+
$GLOBALS['x'] += 2;
13+
var_dump($GLOBALS['x']);
14+
unset($GLOBALS['y']);
15+
var_dump(isset($GLOBALS['x']));
16+
var_dump(isset($GLOBALS['y']));
17+
$GLOBALS['z'][] = 1;
18+
}
19+
20+
$y = 1;
21+
test();
22+
var_dump($x, $y, $z);
23+
24+
$ref = 1;
25+
$GLOBALS['z'] =& $ref;
26+
$ref++;
27+
var_dump($z);
28+
29+
$x = 1;
30+
$ref2 =& $GLOBALS['x'];
31+
$ref2++;
32+
var_dump($x);
33+
34+
?>
35+
--EXPECTF--
36+
Warning: Undefined global variable $x in %s on line %d
37+
NULL
38+
int(1)
39+
int(2)
40+
int(4)
41+
bool(true)
42+
bool(false)
43+
44+
Warning: Undefined variable $y in %s on line %d
45+
int(4)
46+
NULL
47+
array(1) {
48+
[0]=>
49+
int(1)
50+
}
51+
int(2)
52+
int(2)

Zend/tests/undef_index_to_exception.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,5 @@ array(0) {
4242
Undefined array key "key"
4343
array(0) {
4444
}
45-
Undefined array key "test"
45+
Undefined global variable $test
4646
Undefined variable $test

Zend/tests/unset_cv09.phpt

Lines changed: 0 additions & 14 deletions
This file was deleted.

Zend/tests/unset_cv10.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
unset() CV 10 (unset() of global variable in ArrayObject::offsetUnset($GLOBALS))
33
--FILE--
44
<?php
5+
/* This is working on a copy of $GLOBALS, so nothing interesting happens here. */
56
$a = new ArrayObject($GLOBALS);
67
$x = "ok\n";
78
echo $x;
@@ -12,5 +13,6 @@ echo "ok\n";
1213
--EXPECTF--
1314
ok
1415

15-
Warning: Undefined variable $x in %s on line %d
16+
Warning: Undefined array key "x" in %s on line %d
17+
ok
1618
ok

Zend/zend.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -786,13 +786,8 @@ static void module_destructor_zval(zval *zv) /* {{{ */
786786

787787
static zend_bool php_auto_globals_create_globals(zend_string *name) /* {{{ */
788788
{
789-
zval globals;
790-
791-
/* IS_ARRAY, but with ref-counter 1 and not IS_TYPE_REFCOUNTED */
792-
ZVAL_ARR(&globals, &EG(symbol_table));
793-
Z_TYPE_FLAGS_P(&globals) = 0;
794-
ZVAL_NEW_REF(&globals, &globals);
795-
zend_hash_update(&EG(symbol_table), name, &globals);
789+
/* While we keep registering $GLOBALS as an auto-global, we do not create an
790+
* actual variable for it. Access to it handled specially by the compiler. */
796791
return 0;
797792
}
798793
/* }}} */

0 commit comments

Comments
 (0)