Skip to content

Commit

Permalink
SanitizingFunctionsTrait: make sure function names are checked case-i…
Browse files Browse the repository at this point in the history
…nsensitively

These functions should be self-contained, so should not presume that the sniff has already lowercased the function name before passing it.

This fixes a bug as, in this case, the sniffs didn't actually lowercase the name before passing it to the Helper class methods, so the sniffs would throw false positives for non-lowercase function calls.

Tested by adjusting some pre-existing tests for the `ValidatedSanitizedInput` sniff.
  • Loading branch information
jrfnl committed Jun 29, 2023
1 parent 53309ac commit 7455b6f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions WordPress/Helpers/SanitizingFunctionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public function get_sanitizing_and_unslashing_functions() {
* @return bool
*/
public function is_sanitizing_function( $functionName ) {
return isset( $this->get_sanitizing_functions()[ $functionName ] );
return isset( $this->get_sanitizing_functions()[ strtolower( $functionName ) ] );
}

/**
Expand All @@ -233,6 +233,6 @@ public function is_sanitizing_function( $functionName ) {
* @return bool
*/
public function is_sanitizing_and_unslashing_function( $functionName ) {
return isset( $this->get_sanitizing_and_unslashing_functions()[ $functionName ] );
return isset( $this->get_sanitizing_and_unslashing_functions()[ strtolower( $functionName ) ] );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ switch ( do_something( wp_unslash( $_POST['foo'] ) ) ) {} // Bad.

// Sanitization is required even when the value is being escaped.
echo esc_html( wp_unslash( $_POST['foo'] ) ); // Bad.
echo esc_html( sanitize_text_field( wp_unslash( $_POST['foo'] ) ) ); // Ok.
echo esc_html( Sanitize_Text_Field( wp_unslash( $_POST['foo'] ) ) ); // Ok.

$current_tax_slug = isset( $_GET['a'] ) ? sanitize_key( $_GET['a'] ) : false; // Ok.
$current_tax_slug = isset( $_GET['a'] ) ? $_GET['a'] : false; // Bad x 2
Expand All @@ -105,7 +105,7 @@ echo sanitize_text_field( $_POST['foo545'] ); // Error for no validation, unslas
echo array_map( 'sanitize_text_field', $_GET['test'] ); // Bad, no unslashing.
echo Array_Map( 'sanitize_key', $_GET['test'] ); // Ok.

foo( absint( $_GET['foo'] ) ); // Ok.
foo( AbsINT( $_GET['foo'] ) ); // Ok.
$ids = array_map( 'absint', $_GET['test'] ); // Ok.

if ( is_array( $_GET['test'] ) ) {} // Ok.
Expand Down

0 comments on commit 7455b6f

Please sign in to comment.