Make WordPress Core


Ignore:
Timestamp:
10/24/2017 09:04:50 PM (8 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/src/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php

    r41758 r42000  
    249249            }
    250250
     251            $rest_args['schema'] = $this->set_additional_properties_to_false( $rest_args['schema'] );
     252
    251253            $rest_options[ $rest_args['name'] ] = $rest_args;
    252254        }
     
    302304        return rest_parse_request_arg( $value, $request, $param );
    303305    }
     306
     307    /**
     308     * Recursively add additionalProperties = false to all objects in a schema.
     309     *
     310     * This is need to restrict properties of objects in settings values to only
     311     * registered items, as the REST API will allow additional properties by
     312     * default.
     313     *
     314     * @since 4.9.0
     315     *
     316     * @param array $schema The schema array.
     317     * @return array
     318     */
     319    protected function set_additional_properties_to_false( $schema ) {
     320        switch ( $schema['type'] ) {
     321            case 'object':
     322                foreach ( $schema['properties'] as $key => $child_schema ) {
     323                    $schema['properties'][ $key ] = $this->set_additional_properties_to_false( $child_schema );
     324                }
     325                $schema['additionalProperties'] = false;
     326                break;
     327            case 'array':
     328                $schema['items'] = $this->set_additional_properties_to_false( $schema['items'] );
     329                break;
     330        }
     331
     332        return $schema;
     333    }
    304334}
Note: See TracChangeset for help on using the changeset viewer.