From 8c146ecb33a3c21fe34ad5d4687da3a6235b6a5e Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 10 Oct 2023 11:20:28 +0000 Subject: [PATCH] Query: Ensure that the `page` parameter is scalar in `WP_Query::get_posts()`. The `page` query var only accepts a scalar value and passes the value through functions that assume a scalar value. Adding an extra guard condition does not affect its functionality but does avoid a PHP fatal error for `trim()` when a non-scalar value such as an array is passed. Follow-up to [2535], [53891]. Props brookedot, rlmc, mukesh27, SergeyBiryukov. Fixes #56558. git-svn-id: https://develop.svn.wordpress.org/trunk@56815 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-query.php | 3 +-- tests/phpunit/tests/query/invalidQueries.php | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php index 3fcb942aa7..e79f460fc5 100644 --- a/src/wp-includes/class-wp-query.php +++ b/src/wp-includes/class-wp-query.php @@ -2020,8 +2020,7 @@ class WP_Query { } if ( isset( $q['page'] ) ) { - $q['page'] = trim( $q['page'], '/' ); - $q['page'] = absint( $q['page'] ); + $q['page'] = is_scalar( $q['page'] ) ? absint( trim( $q['page'], '/' ) ) : 0; } // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present. diff --git a/tests/phpunit/tests/query/invalidQueries.php b/tests/phpunit/tests/query/invalidQueries.php index 4c0b631f80..0cec942245 100644 --- a/tests/phpunit/tests/query/invalidQueries.php +++ b/tests/phpunit/tests/query/invalidQueries.php @@ -159,4 +159,20 @@ class Tests_Query_InvalidQueries extends WP_UnitTestCase { // Only the published post should be returned. $this->assertCount( 1, $query->posts ); } + + /** + * Ensure a non-scalar page parameter does not throw a fatal error for trim(). + * + * @ticket 56558 + * @covers WP_Query::get_posts + */ + public function test_non_scalar_page_value() { + $query = new WP_Query( + array( + 'page' => array( 1, 2, 3 ), + ) + ); + + $this->assertSame( 0, $query->query_vars['page'] ); + } }