Skip to content

Commit 67bba57

Browse files
committed
Variables/ServerVariables: bug fix - false negatives for $GLOBALS['_SERVER']
The `$GLOBALS['_SERVER']` superglobals access is equivalent to using `$_SERVER`, so should be examined too. Includes tests.
1 parent 5bbd1b9 commit 67bba57

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

WordPressVIPMinimum/Sniffs/Variables/ServerVariablesSniff.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,39 @@ public function register() {
5555
*/
5656
public function process_token( $stackPtr ) {
5757

58-
if ( $this->tokens[ $stackPtr ]['content'] !== '$_SERVER' ) {
59-
// Not the variable we are looking for.
58+
if ( $this->tokens[ $stackPtr ]['content'] !== '$_SERVER'
59+
&& $this->tokens[ $stackPtr ]['content'] !== '$GLOBALS'
60+
) {
61+
// Not a variable we are looking for.
6062
return;
6163
}
6264

65+
$searchStart = $stackPtr;
66+
if ( $this->tokens[ $stackPtr ]['content'] === '$GLOBALS' ) {
67+
$globalsIndexPtr = $this->get_array_access_key( $stackPtr );
68+
if ( $globalsIndexPtr === false ) {
69+
// Couldn't find an array index token usable for the purposes of this sniff. Bow out.
70+
return;
71+
}
72+
73+
$globalsIndexName = TextStrings::stripQuotes( $this->tokens[ $globalsIndexPtr ]['content'] );
74+
if ( $globalsIndexName !== '_SERVER' ) {
75+
// Not access to `$GLOBALS['_SERVER']`.
76+
return;
77+
}
78+
79+
// Set the start point for the next array access key search to the close bracket of this array index.
80+
// No need for defensive coding as we already know there will be a valid close bracket next.
81+
$searchStart = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $globalsIndexPtr + 1 ), null, true );
82+
}
83+
6384
$prevNonEmpty = $this->phpcsFile->findPrevious( Tokens::$emptyTokens, ( $stackPtr - 1 ), null, true );
6485
if ( $this->tokens[ $prevNonEmpty ]['code'] === T_DOUBLE_COLON ) {
6586
// Access to OO property mirroring the name of the superglobal. Not our concern.
6687
return;
6788
}
6889

69-
$indexPtr = $this->get_array_access_key( $stackPtr );
90+
$indexPtr = $this->get_array_access_key( $searchStart );
7091
if ( $indexPtr === false ) {
7192
// Couldn't find an array index token usable for the purposes of this sniff. Bow out as undetermined.
7293
return;

WordPressVIPMinimum/Tests/Variables/ServerVariablesUnitTest.inc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,15 @@ echo $_SERVER[$other_key] . $NOT_SERVER['PHP_AUTH_PW'];
6161

6262
// Safeguard the sniff doesn't get confused over partially dynamic keys.
6363
echo $_SERVER[$other_key . 'PHP_AUTH_PW'];
64+
65+
// Access of the array indices via the $GLOBALS superglobal should also be recognized.
66+
$GLOBALS['NOT_SERVER']['PHP_AUTH_USER']; // OK.
67+
$GLOBALS[$a]['PHP_AUTH_USER']; // OK.
68+
$GLOBALS['_SERVER']['SOME_OTHER_KEY']; // OK.
69+
$GLOBALS['_SERVER'][$a]; // OK.
70+
71+
$GLOBALS['_SERVER']['PHP_AUTH_USER'];
72+
$GLOBALS['_SERVER']['PHP_AUTH_PW'];
73+
$GLOBALS["_SERVER"]['HTTP_X_IP_TRAIL'];
74+
$GLOBALS['_SERVER']['HTTP_X_FORWARDED_FOR'];
75+
$GLOBALS['_SERVER']["REMOTE_ADDR"];

WordPressVIPMinimum/Tests/Variables/ServerVariablesUnitTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public function getErrorList() {
2828
33 => 1,
2929
36 => 1,
3030
37 => 1,
31+
71 => 1,
32+
72 => 1,
33+
73 => 1,
34+
74 => 1,
35+
75 => 1,
3136
];
3237
}
3338

0 commit comments

Comments
 (0)