Make WordPress Core


Ignore:
Timestamp:
10/27/2016 04:07:06 PM (10 years ago)
Author:
rachelbaker
Message:

REST API: Return WP_Error when a client is attempting to update an option with a non-scalar value to null.

A null value is returned in the response for any option that has a non-scalar value.

To protect clients from accidentally including the null values from a response object in a request, we do not allow options with non-scalar values to be updated to null. Without this added protection a client could mistakenly delete all options that have non-scalar values from the database.

Props joehoyle, rachelbaker.
Fixes #38527.

File:
1 edited

Legend:

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

    r38954 r38982  
    9696         */
    9797        protected function prepare_value( $value, $schema ) {
     98                // If the value is not a scalar, it's not possible to cast it to
     99                // anything.
     100                if ( ! is_scalar( $value ) ) {
     101                        return null;
     102                }
     103
    98104                switch ( $schema['type'] ) {
    99105                        case 'string':
     
    142148                        }
    143149
    144                         // A null value means reset the option, which is essentially deleting it
    145                         // from the database and then relying on the default value.
     150                        /**
     151                        * A `null` value for an option would have the same effect as
     152                        * deleting the option from the database, and relying on the
     153                        * default value.
     154                        */
    146155                        if ( is_null( $request[ $name ] ) ) {
     156                                /**
     157                                 * A `null` value is returned in the response for any option
     158                                 * that has a non-scalar value.
     159                                 *
     160                                 * To protect clients from accidentally including the `null`
     161                                 * values from a response object in a request, we do not allow
     162                                 * options with non-scalar values to be updated to `null`.
     163                                 * Without this added protection a client could mistakenly
     164                                 * delete all options that have non-scalar values from the
     165                                 * database.
     166                                 */
     167                                if ( ! is_scalar( get_option( $args['option_name'], false ) ) ) {
     168                                        return new WP_Error(
     169                                                'rest_invalid_stored_value', sprintf( __( 'The %s property has an invalid stored value, and cannot be updated to null.' ), $name ), array( 'status' => 500 )
     170                                        );
     171                                }
     172
    147173                                delete_option( $args['option_name'] );
    148174                        } else {
Note: See TracChangeset for help on using the changeset viewer.