Skip to content

Commit 5765dae

Browse files
authored
Merge pull request #2572 from rodrigoprimo/nonce-verification-fix-function-name-case-false-positive
Security/NonceVerification: handle function names case correctly
2 parents ae34825 + 106aa59 commit 5765dae

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

WordPress/Sniffs/Security/NonceVerificationSniff.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,10 @@ private function has_nonce_check( $stackPtr, array $cache_keys, $allow_nonce_aft
309309
continue;
310310
}
311311

312+
$content_lc = \strtolower( $this->tokens[ $i ]['content'] );
313+
312314
// If this is one of the nonce verification functions, we can bail out.
313-
if ( isset( $this->nonceVerificationFunctions[ $this->tokens[ $i ]['content'] ] ) ) {
315+
if ( isset( $this->nonceVerificationFunctions[ $content_lc ] ) ) {
314316
/*
315317
* Now, make sure it is a call to a global function.
316318
*/
@@ -416,6 +418,8 @@ protected function mergeFunctionLists() {
416418
$this->nonceVerificationFunctions
417419
);
418420

421+
$this->nonceVerificationFunctions = array_change_key_case( $this->nonceVerificationFunctions );
422+
419423
$this->addedCustomNonceFunctions = $this->customNonceVerificationFunctions;
420424
}
421425
}

WordPress/Tests/Security/NonceVerificationUnitTest.1.inc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,3 +486,41 @@ enum MyEnum {
486486
echo $_POST['foo']; // OK.
487487
}
488488
}
489+
490+
// Good, has a nonce check. Ensure the check is case-insensitive as function names are case-insensitive in PHP.
491+
function ajax_process() {
492+
CHECK_AJAX_REFERER( 'something' );
493+
494+
update_post_meta( (int) $_POST['id'], 'a_key', $_POST['a_value'] );
495+
}
496+
497+
// phpcs:set WordPress.Security.NonceVerification customNonceVerificationFunctions[] MIXED_case_NAME
498+
function non_ascii_characters() {
499+
MIXED_case_NAME( $_POST['something'] ); // Passing $_POST to ensure the sniff bails correctly for variables inside the nonce verification function.
500+
501+
update_post_meta( (int) $_POST['id'], 'a_key', $_POST['a_value'] );
502+
}
503+
// phpcs:set WordPress.Security.NonceVerification customNonceVerificationFunctions[]
504+
505+
/*
506+
* Test case handling of non-ASCII characters in function names.
507+
*/
508+
// phpcs:set WordPress.Security.NonceVerification customNonceVerificationFunctions[] déjà_vu
509+
function same_function_same_case() {
510+
déjà_vu( 'something' ); // Ok.
511+
512+
update_post_meta( (int) $_POST['id'], 'a_key', $_POST['a_value'] );
513+
}
514+
515+
function same_function_different_case() {
516+
DéJà_VU( 'something' ); // Ok.
517+
518+
update_post_meta( (int) $_POST['id'], 'a_key', $_POST['a_value'] );
519+
}
520+
521+
function different_function_name() {
522+
dÉjÀ_vu( 'something' ); // Bad, dÉjÀ_vu() and déjà_vu() are NOT the same function.
523+
524+
update_post_meta( (int) $_POST['id'], 'a_key', $_POST['a_value'] );
525+
}
526+
// phpcs:set WordPress.Security.NonceVerification customNonceVerificationFunctions[]

WordPress/Tests/Security/NonceVerificationUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public function getErrorList( $testFile = '' ) {
7474
453 => 1,
7575
470 => 1,
7676
478 => 1,
77+
524 => 2,
7778
);
7879

7980
case 'NonceVerificationUnitTest.2.inc':

0 commit comments

Comments
 (0)