From 0c142bb1faba41239fac098b3c29bd92235845b7 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Fri, 9 Oct 2026 22:45:00 +0100 Subject: [PATCH] fix: match double-quoted keys in restricted array members RestrictedVariables never flagged `$_SERVER["REMOTE_ADDR"]` or `$_SERVER["HTTP_USER_AGENT"]`, only their single-quoted forms. The quote normalisation in test_patterns() used `[` and `]` as regex delimiters rather than as a character class, so it only ever matched a literal `'"` pair and never ran. Even when fixed as intended, rewriting the pattern's quotes to single quotes would not help, because the code being checked is what uses the double quotes. Quotes in a pattern now match either quote style. The ruleset fixtures already contained `$_SERVER["REMOTE_ADDR"]` under ServerVariables, which now also correctly raises the RestrictedVariables warning. --- WordPress-VIP-Go/ruleset-test.inc | 2 +- WordPress-VIP-Go/ruleset-test.php | 1 + .../Sniffs/AbstractVariableRestrictionsSniff.php | 6 ++++-- .../Tests/Variables/RestrictedVariablesUnitTest.inc | 7 +++++++ .../Tests/Variables/RestrictedVariablesUnitTest.php | 2 ++ WordPressVIPMinimum/ruleset-test.inc | 2 +- WordPressVIPMinimum/ruleset-test.php | 1 + 7 files changed, 17 insertions(+), 4 deletions(-) diff --git a/WordPress-VIP-Go/ruleset-test.inc b/WordPress-VIP-Go/ruleset-test.inc index ab99a21b..224f053f 100644 --- a/WordPress-VIP-Go/ruleset-test.inc +++ b/WordPress-VIP-Go/ruleset-test.inc @@ -568,7 +568,7 @@ foo( $_SESSION['bar'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedIn $test = $_SERVER['PHP_AUTH_PW']; // Error. bar( $_SERVER['HTTP_X_IP_TRAIL'] ); // Error. $_SERVER['HTTP_X_FORWARDED_FOR']; // Error. -$_SERVER["REMOTE_ADDR"]; // Error. +$_SERVER["REMOTE_ADDR"]; // Error + Warning (RestrictedVariables). // phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotValidated,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized // Generic.VersionControl.GitMergeConflict diff --git a/WordPress-VIP-Go/ruleset-test.php b/WordPress-VIP-Go/ruleset-test.php index fb1d792f..0da565d0 100644 --- a/WordPress-VIP-Go/ruleset-test.php +++ b/WordPress-VIP-Go/ruleset-test.php @@ -232,6 +232,7 @@ 544 => 1, 554 => 1, 560 => 1, + 571 => 1, 583 => 1, ], 'messages' => [ diff --git a/WordPressVIPMinimum/Sniffs/AbstractVariableRestrictionsSniff.php b/WordPressVIPMinimum/Sniffs/AbstractVariableRestrictionsSniff.php index b40808fe..c9bf46ec 100644 --- a/WordPressVIPMinimum/Sniffs/AbstractVariableRestrictionsSniff.php +++ b/WordPressVIPMinimum/Sniffs/AbstractVariableRestrictionsSniff.php @@ -217,14 +217,16 @@ public function process_token( $stackPtr ) { /** * Transform a wildcard pattern to a usable regex pattern. * + * Quotes in the pattern match either a single or a double quote. + * * @param string $pattern Pattern. * @return string|null */ private function test_patterns( $pattern ): ?string { $pattern = preg_quote( $pattern, '#' ); $pattern = preg_replace( - [ '#\\\\\*#', '[\'"]' ], - [ '.*', '\'' ], + [ '#\\\\\*#', '#[\'"]#' ], + [ '.*', '[\'"]' ], $pattern ); return $pattern; diff --git a/WordPressVIPMinimum/Tests/Variables/RestrictedVariablesUnitTest.inc b/WordPressVIPMinimum/Tests/Variables/RestrictedVariablesUnitTest.inc index 3ec1502f..a8082c6c 100644 --- a/WordPressVIPMinimum/Tests/Variables/RestrictedVariablesUnitTest.inc +++ b/WordPressVIPMinimum/Tests/Variables/RestrictedVariablesUnitTest.inc @@ -39,3 +39,10 @@ foo( $_SESSION['bar'] ); // Error. if ( isset( $_SESSION ) ) { // OK. $cache = false; } + +foo( $_SERVER["REMOTE_ADDR"] ); // Warning. + +// Warning. +$phrase = << 1, 17 => 1, 28 => 1, + 43 => 1, + 47 => 1, ]; } } diff --git a/WordPressVIPMinimum/ruleset-test.inc b/WordPressVIPMinimum/ruleset-test.inc index 388840e5..bfd6657f 100644 --- a/WordPressVIPMinimum/ruleset-test.inc +++ b/WordPressVIPMinimum/ruleset-test.inc @@ -598,7 +598,7 @@ foo( $_SESSION['bar'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedIn $test = $_SERVER['PHP_AUTH_PW']; // Error. bar( $_SERVER['HTTP_X_IP_TRAIL'] ); // Error. $_SERVER['HTTP_X_FORWARDED_FOR']; // Error. -$_SERVER["REMOTE_ADDR"]; // Error. +$_SERVER["REMOTE_ADDR"]; // Error + Warning (RestrictedVariables). // phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotValidated,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized class MyClass { diff --git a/WordPressVIPMinimum/ruleset-test.php b/WordPressVIPMinimum/ruleset-test.php index c97cb561..af8832b9 100644 --- a/WordPressVIPMinimum/ruleset-test.php +++ b/WordPressVIPMinimum/ruleset-test.php @@ -289,6 +289,7 @@ 563 => 1, 569 => 1, 593 => 1, + 601 => 1, 622 => 1, ], 'messages' => [