Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sniff::has_nonce_check(): ignore nonce checks in nested closed scopes #1683

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions WordPress/Sniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,16 @@ protected function has_nonce_check( $stackPtr ) {

// Loop through the tokens looking for nonce verification functions.
for ( $i = $start; $i < $end; $i++ ) {
// Skip over nested closed scope constructs.
if ( \T_FUNCTION === $tokens[ $i ]['code']
|| \T_CLOSURE === $tokens[ $i ]['code']
|| isset( Tokens::$ooScopeTokens[ $tokens[ $i ]['code'] ] )
) {
if ( isset( $tokens[ $i ]['scope_closer'] ) ) {
$i = $tokens[ $i ]['scope_closer'];
}
continue;
}

// If this isn't a function name, skip it.
if ( \T_STRING !== $tokens[ $i ]['code'] ) {
Expand Down
20 changes: 20 additions & 0 deletions WordPress/Tests/Security/NonceVerificationUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,23 @@ function fix_false_negatives_namespaced_function_same_name() {
WP_Faker\SecurityBypass\wp_verify_nonce( 'something' );
do_something( $_POST['abc'] ); // Bad.
}

function skip_over_nested_constructs_1() {
$b = function () {
check_ajax_referer( 'something' ); // Nonce check is not in the same function scope.
};

do_something( $_POST['abc'] ); // Bad.
}

function skip_over_nested_constructs_2() {
if ( $_POST['abc'] === 'test' ) { // Bad.
return;
}

$b = new class() {
public function named() {
check_ajax_referer( 'something' ); // Nonce check is not in the same function scope.
}
};
}
2 changes: 2 additions & 0 deletions WordPress/Tests/Security/NonceVerificationUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public function getErrorList() {
177 => 1,
185 => 1,
190 => 1,
198 => 1,
202 => 1,
);
}

Expand Down