Make WordPress Core

Changeset 61765


Ignore:
Timestamp:
02/27/2026 10:58:26 PM (2 months ago)
Author:
westonruter
Message:

REST API: Prevent fatal error when non-string value is passed in endpoints for font faces and font families.

The value is expected to be a serialized JSON string, which the validation callback validates.

Developed in https://github.com/WordPress/wordpress-develop/pull/10966

Follow-up to r57548.

Props deepaklalwani, westonruter.
See #59166.
Fixes #64666.

Location:
trunk
Files:
4 edited

Legend:

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

    r58353 r61765  
    162162     */
    163163    public function validate_create_font_face_settings( $value, $request ) {
     164        // Enforce JSON Schema validity for field before applying custom validation logic.
     165        $args     = $this->get_create_params();
     166        $validity = rest_validate_value_from_schema( $value, $args['font_face_settings'], 'font_face_settings' );
     167
     168        if ( is_wp_error( $validity ) ) {
     169            return $validity;
     170        }
     171
    164172        $settings = json_decode( $value, true );
    165173
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-font-families-controller.php

    r58328 r61765  
    8888     */
    8989    public function validate_font_family_settings( $value, $request ) {
     90        // Enforce JSON Schema validity for field before applying custom validation logic.
     91        $args     = $this->get_endpoint_args_for_item_schema( $request->get_method() );
     92        $validity = rest_validate_value_from_schema( $value, $args['font_family_settings'], 'font_family_settings' );
     93
     94        if ( is_wp_error( $validity ) ) {
     95            return $validity;
     96        }
     97
    9098        $settings = json_decode( $value, true );
    9199
  • trunk/tests/phpunit/tests/fonts/font-library/wpRestFontFacesController.php

    r61622 r61765  
    766766        $this->assertErrorResponse( 'rest_invalid_param', $response, 400, 'The response should return an error for "rest_invalid_param" with 400 status.' );
    767767        $expected_message = 'font_face_settings parameter must be a valid JSON string.';
     768        $message          = $response->as_error()->get_all_error_data()[0]['params']['font_face_settings'];
     769        $this->assertSame( $expected_message, $message, 'The response error message should match.' );
     770    }
     771
     772    /**
     773     * @covers WP_REST_Font_Faces_Controller::validate_create_font_face_settings
     774     */
     775    public function test_create_item_non_string_settings() {
     776        wp_set_current_user( self::$admin_id );
     777        $request = new WP_REST_Request( 'POST', '/wp/v2/font-families/' . self::$font_family_id . '/font-faces' );
     778        $request->set_param( 'theme_json_version', WP_REST_Font_Faces_Controller::LATEST_THEME_JSON_VERSION_SUPPORTED );
     779        $request->set_param( 'font_face_settings', self::$default_settings );
     780
     781        $response = rest_get_server()->dispatch( $request );
     782
     783        $this->assertErrorResponse( 'rest_invalid_param', $response, 400, 'The response should return an error for "rest_invalid_param" with 400 status.' );
     784        $expected_message = 'font_face_settings is not of type string.';
    768785        $message          = $response->as_error()->get_all_error_data()[0]['params']['font_face_settings'];
    769786        $this->assertSame( $expected_message, $message, 'The response error message should match.' );
  • trunk/tests/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php

    r61622 r61765  
    630630
    631631    /**
     632     * @covers WP_REST_Font_Family_Controller::validate_font_family_settings
     633     */
     634    public function test_create_item_non_string_settings() {
     635        wp_set_current_user( self::$admin_id );
     636        $request = new WP_REST_Request( 'POST', '/wp/v2/font-families' );
     637        $request->set_param( 'theme_json_version', WP_REST_Font_Families_Controller::LATEST_THEME_JSON_VERSION_SUPPORTED );
     638        $request->set_param( 'font_family_settings', self::$default_settings );
     639
     640        $response = rest_get_server()->dispatch( $request );
     641
     642        $this->assertErrorResponse( 'rest_invalid_param', $response, 400, 'The response should return an error for "rest_invalid_param" with 400 status.' );
     643        $expected_message = 'font_family_settings is not of type string.';
     644        $message          = $response->as_error()->get_all_error_data()[0]['params']['font_family_settings'];
     645        $this->assertSame( $expected_message, $message, 'The response error message should match.' );
     646    }
     647
     648    /**
    632649     * @covers WP_REST_Font_Family_Controller::create_item
    633650     */
     
    831848
    832849    /**
     850     * @covers WP_REST_Font_Family_Controller::validate_font_family_settings
     851     */
     852    public function test_update_item_non_string_settings() {
     853        wp_set_current_user( self::$admin_id );
     854        $request = new WP_REST_Request( 'POST', '/wp/v2/font-families/' . self::$font_family_id1 );
     855        $request->set_param( 'font_family_settings', self::$default_settings );
     856
     857        $response = rest_get_server()->dispatch( $request );
     858
     859        $this->assertErrorResponse( 'rest_invalid_param', $response, 400, 'The response should return an error for "rest_invalid_param" with 400 status.' );
     860        $expected_message = 'font_family_settings is not of type string.';
     861        $message          = $response->as_error()->get_all_error_data()[0]['params']['font_family_settings'];
     862        $this->assertSame( $expected_message, $message, 'The response error message should match.' );
     863    }
     864
     865    /**
    833866     * @covers WP_REST_Font_Families_Controller::update_item
    834867     */
Note: See TracChangeset for help on using the changeset viewer.