Make WordPress Core

Changeset 56815


Ignore:
Timestamp:
10/10/2023 11:20:28 AM (3 years ago)
Author:
SergeyBiryukov
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-query.php

    r56811 r56815  
    20212021
    20222022                if ( isset( $q['page'] ) ) {
    2023                         $q['page'] = trim( $q['page'], '/' );
    2024                         $q['page'] = absint( $q['page'] );
     2023                        $q['page'] = is_scalar( $q['page'] ) ? absint( trim( $q['page'], '/' ) ) : 0;
    20252024                }
    20262025
  • trunk/tests/phpunit/tests/query/invalidQueries.php

    r52577 r56815  
    160160                $this->assertCount( 1, $query->posts );
    161161        }
     162
     163        /**
     164         * Ensure a non-scalar page parameter does not throw a fatal error for trim().
     165         *
     166         * @ticket 56558
     167         * @covers WP_Query::get_posts
     168         */
     169        public function test_non_scalar_page_value() {
     170                $query = new WP_Query(
     171                        array(
     172                                'page' => array( 1, 2, 3 ),
     173                        )
     174                );
     175
     176                $this->assertSame( 0, $query->query_vars['page'] );
     177        }
    162178}
Note: See TracChangeset for help on using the changeset viewer.