Skip to content

Commit 5f56dbb

Browse files
authored
Merge pull request #1683 from WordPress-Coding-Standards/feature/nonceverification-ignore-nested-closed-scopes
Sniff::has_nonce_check(): ignore nonce checks in nested closed scopes
2 parents 14c77a8 + 5f2d6b5 commit 5f56dbb

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

WordPress/Sniff.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,16 @@ protected function has_nonce_check( $stackPtr ) {
14681468

14691469
// Loop through the tokens looking for nonce verification functions.
14701470
for ( $i = $start; $i < $end; $i++ ) {
1471+
// Skip over nested closed scope constructs.
1472+
if ( \T_FUNCTION === $tokens[ $i ]['code']
1473+
|| \T_CLOSURE === $tokens[ $i ]['code']
1474+
|| isset( Tokens::$ooScopeTokens[ $tokens[ $i ]['code'] ] )
1475+
) {
1476+
if ( isset( $tokens[ $i ]['scope_closer'] ) ) {
1477+
$i = $tokens[ $i ]['scope_closer'];
1478+
}
1479+
continue;
1480+
}
14711481

14721482
// If this isn't a function name, skip it.
14731483
if ( \T_STRING !== $tokens[ $i ]['code'] ) {

WordPress/Tests/Security/NonceVerificationUnitTest.inc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,23 @@ function fix_false_negatives_namespaced_function_same_name() {
189189
WP_Faker\SecurityBypass\wp_verify_nonce( 'something' );
190190
do_something( $_POST['abc'] ); // Bad.
191191
}
192+
193+
function skip_over_nested_constructs_1() {
194+
$b = function () {
195+
check_ajax_referer( 'something' ); // Nonce check is not in the same function scope.
196+
};
197+
198+
do_something( $_POST['abc'] ); // Bad.
199+
}
200+
201+
function skip_over_nested_constructs_2() {
202+
if ( $_POST['abc'] === 'test' ) { // Bad.
203+
return;
204+
}
205+
206+
$b = new class() {
207+
public function named() {
208+
check_ajax_referer( 'something' ); // Nonce check is not in the same function scope.
209+
}
210+
};
211+
}

WordPress/Tests/Security/NonceVerificationUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public function getErrorList() {
4949
177 => 1,
5050
185 => 1,
5151
190 => 1,
52+
198 => 1,
53+
202 => 1,
5254
);
5355
}
5456

0 commit comments

Comments
 (0)