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

Add defensive coding in posts per page sniff #2392

Merged
merged 1 commit into from
Sep 18, 2023
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
4 changes: 4 additions & 0 deletions WordPress/Sniffs/WP/PostsPerPageSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public function getGroups() {
public function callback( $key, $val, $line, $group ) {
$stripped_val = TextStrings::stripQuotes( $val );

if ( '' === $stripped_val ) {
return false;
}

if ( $val !== $stripped_val ) {
// The value was a text string. For text strings, we only accept purely numeric values.
if ( preg_match( '`^[0-9]+$`', $stripped_val ) !== 1 ) {
Expand Down
4 changes: 4 additions & 0 deletions WordPress/Tests/DB/SlowDBQueryUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ $query = 'foo=bar&meta_key=foo&meta_value=bar';
if ( ! isset( $widget['params'][0] ) ) {
$widget['params'][0] = array();
}

$query = 'foo=bar&meta_key=&meta_value=bar';
$query = 'foo=bar&meta_key=foo&meta_value=';
$query = 'foo=bar&meta_key=&meta_value=';
3 changes: 3 additions & 0 deletions WordPress/Tests/DB/SlowDBQueryUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public function getWarningList() {
15 => 1,
16 => 1,
19 => 2,
25 => 2,
26 => 2,
27 => 2,
);
}
}
14 changes: 14 additions & 0 deletions WordPress/Tests/WP/PostsPerPageUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,17 @@ $args = array(
'posts_per_page' => 75.0, // OK (75).
'posts_per_page' => 150.000, // Bad (150).
);

$query = 'posts_per_page=' . (int) $_POST['limit']; // OK.

$args = array(
'posts_per_page' => '', // OK.
);

_query_posts( 'nopaging=true&posts_per_page=' ); // OK.

$query_args['posts_per_page'] = ''; // OK.

$query_args[
'posts_per_page'
] = ''; // OK.