Make WordPress Core

Changeset 57839


Ignore:
Timestamp:
03/15/2024 11:23:18 AM (2 years ago)
Author:
swissspidy
Message:

REST API: Prevent error when passing invalid type parameter to search endpoint.

In WP_REST_Search_Controller, the type parameter is accessed via the sanitization callback for the subtype parameter, which is too early for type itself to be already sanitized. This change adds a type check in the get_search_handler() method to prevent errors when the type doesn’t match.

Props swissspidy, timothyblynjacobs, dd32.
Fixes #60771.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php

    r57648 r57839  
    396396                $type = $request->get_param( self::PROP_TYPE );
    397397
    398                 if ( ! $type || ! isset( $this->search_handlers[ $type ] ) ) {
     398                if ( ! $type || ! is_string( $type ) || ! isset( $this->search_handlers[ $type ] ) ) {
    399399                        return new WP_Error(
    400400                                'rest_search_invalid_type',
  • trunk/tests/phpunit/tests/rest-api/rest-search-controller.php

    r56547 r57839  
    889889                );
    890890        }
     891
     892        /**
     893         * @ticket 60771
     894         */
     895        public function test_sanitize_subtypes_validates_type() {
     896                $response = $this->do_request_with_params(
     897                        array(
     898                                'subtype' => 'page',
     899                                'type'    => array( 'invalid' ),
     900                        )
     901                );
     902
     903                $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
     904        }
    891905}
Note: See TracChangeset for help on using the changeset viewer.