Make WordPress Core


Ignore:
Timestamp:
10/24/2017 09:04:50 PM (9 years ago)
Author:
joehoyle
Message:

REST API: Don’t remove unregistered properties from objects in schema.

In r41727 the ability to sanitise and validate objects from JSON schema was added, with a whitelist approach. It was decided we should pass through all non-registered properties to reflect the behaviour of the root object in register_rest_route. To prevent arbitrary extra data via setting objects, we force additionalProperties to false in the settings endpoint.

See #38583.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-settings-controller.php

    r41758 r42000  
    191191                ) );
    192192
     193                // We have to re-register the route, as the args changes based off registered settings.
     194                $this->server->override_by_default = true;
     195                $this->endpoint->register_routes();
     196
    193197                // Object is cast to correct types.
    194198                update_option( 'mycustomsetting', array( 'a' => '1' ) );
     
    210214                $response = $this->server->dispatch( $request );
    211215                $data = $response->get_data();
    212                 $this->assertEquals( array( 'a' => 1 ), $data['mycustomsetting'] );
     216                $this->assertEquals( null, $data['mycustomsetting'] );
    213217
    214218                unregister_setting( 'somegroup', 'mycustomsetting' );
     
    373377        }
    374378
     379        public function test_update_item_with_nested_object() {
     380                register_setting( 'somegroup', 'mycustomsetting', array(
     381                        'show_in_rest' => array(
     382                                'schema' => array(
     383                                        'type'       => 'object',
     384                                        'properties' => array(
     385                                                'a' => array(
     386                                                        'type' => 'object',
     387                                                        'properties' => array(
     388                                                                'b' => array(
     389                                                                        'type' => 'number',
     390                                                                ),
     391                                                        ),
     392                                                ),
     393                                        ),
     394                                ),
     395                        ),
     396                        'type'         => 'object',
     397                ) );
     398
     399                // We have to re-register the route, as the args changes based off registered settings.
     400                $this->server->override_by_default = true;
     401                $this->endpoint->register_routes();
     402                wp_set_current_user( self::$administrator );
     403
     404                $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
     405                $request->set_param( 'mycustomsetting', array( 'a' => array( 'b' => 1, 'c' => 1 ) ) );
     406                $response = $this->server->dispatch( $request );
     407                $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
     408        }
     409
    375410        public function test_update_item_with_object() {
    376411                register_setting( 'somegroup', 'mycustomsetting', array(
     
    408443                $this->assertEquals( array(), get_option( 'mycustomsetting' ) );
    409444
     445                // Provide more keys.
     446                $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
     447                $request->set_param( 'mycustomsetting', array( 'a' => 1, 'b' => 2 ) );
     448                $response = $this->server->dispatch( $request );
     449
     450                $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
     451
    410452                // Setting an invalid object.
    411453                $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
Note: See TracChangeset for help on using the changeset viewer.