Skip to content

Commit 5d90e3b

Browse files
committed
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 #10966 Follow-up to r57548. Props deepaklalwani, westonruter. See #59166. Fixes #64666. git-svn-id: https://develop.svn.wordpress.org/trunk@61765 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 588a02f commit 5d90e3b

4 files changed

Lines changed: 66 additions & 0 deletions

File tree

src/wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ public function get_item_permissions_check( $request ) {
161161
* @return true|WP_Error True if the settings are valid, otherwise a WP_Error object.
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

166174
// Check settings string is valid JSON.

src/wp-includes/rest-api/endpoints/class-wp-rest-font-families-controller.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ public function get_item_permissions_check( $request ) {
8787
* @return true|WP_Error True if the settings are valid, otherwise a WP_Error object.
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

92100
// Check settings string is valid JSON.

tests/phpunit/tests/fonts/font-library/wpRestFontFacesController.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,23 @@ public function test_create_item_invalid_settings_json() {
769769
$this->assertSame( $expected_message, $message, 'The response error message should match.' );
770770
}
771771

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.';
785+
$message = $response->as_error()->get_all_error_data()[0]['params']['font_face_settings'];
786+
$this->assertSame( $expected_message, $message, 'The response error message should match.' );
787+
}
788+
772789
/**
773790
* @covers WP_REST_Font_Faces_Controller::validate_create_font_face_settings
774791
*/

tests/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,23 @@ public function test_create_item_invalid_settings_json() {
628628
$this->assertSame( $expected_message, $message, 'The response error message should match.' );
629629
}
630630

631+
/**
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+
631648
/**
632649
* @covers WP_REST_Font_Family_Controller::create_item
633650
*/
@@ -829,6 +846,22 @@ public function test_update_item_update_slug_not_allowed() {
829846
$this->assertSame( $expected_message, $message, 'The response error message should match.' );
830847
}
831848

849+
/**
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+
832865
/**
833866
* @covers WP_REST_Font_Families_Controller::update_item
834867
*/

0 commit comments

Comments
 (0)