Skip to content

Commit a8d8c6c

Browse files
committed
NonceVerification: bug fix - sanitization is no alternative for nonce check
Any usage of superglobals in calls to sanitization functions were up to now ignored. As the output of sanitization is normally assigned to a names variable, this meant that it was possible to bypass the nonce verification sniff that way. I don't believe that was the intended behaviour. Instead IMO, a sanitization call should be allowed prior to the nonce verification, but should not negate the nonce verification check. This fixes that. Includes unit tests.
1 parent ca3d41f commit a8d8c6c

File tree

4 files changed

+13
-4
lines changed

4 files changed

+13
-4
lines changed

WordPress/Sniff.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,6 +1456,7 @@ protected function has_nonce_check( $stackPtr ) {
14561456
|| $this->is_comparison( $stackPtr )
14571457
|| $this->is_in_array_comparison( $stackPtr )
14581458
|| $this->is_in_function_call( $stackPtr, $this->unslashingFunctions ) !== false
1459+
|| $this->is_only_sanitized( $stackPtr )
14591460
) {
14601461
$allow_nonce_after = true;
14611462
}

WordPress/Sniffs/Security/NonceVerificationSniff.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,6 @@ public function process_token( $stackPtr ) {
121121

122122
$this->mergeFunctionLists();
123123

124-
if ( $this->is_only_sanitized( $stackPtr ) ) {
125-
return;
126-
}
127-
128124
if ( $this->has_nonce_check( $stackPtr ) ) {
129125
return;
130126
}

WordPress/Tests/Security/NonceVerificationUnitTest.inc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,14 @@ function allow_for_unslash_in_sanitization() {
264264
wp_verify_nonce( $var );
265265
echo $var;
266266
}
267+
268+
function dont_allow_bypass_nonce_via_sanitization() {
269+
$var = sanitize_text_field( $_POST['foo'] ); // Bad.
270+
echo $var;
271+
}
272+
273+
function dont_allow_bypass_nonce_via_sanitization() {
274+
$var = sanitize_text_field( $_POST['foo'] ); // OK.
275+
wp_verify_nonce( $var );
276+
echo $var;
277+
}

WordPress/Tests/Security/NonceVerificationUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public function getErrorList() {
5252
198 => 1,
5353
202 => 1,
5454
252 => 1,
55+
269 => 1,
5556
);
5657
}
5758

0 commit comments

Comments
 (0)