Make WordPress Core

Changeset 43740 for branches/5.0


Ignore:
Timestamp:
10/17/2018 08:09:33 PM (6 years ago)
Author:
kadamwhite
Message:

REST API: Slash existing meta values when comparing with incoming meta upates.

When comparing the old and new values for a meta key being set, ensure both values are sanitized using the same logic so that equal values match.

props boonebgorges, dcavins, MattGeri, pilou69, TimothyBlynJacobs.
Fixes #42069.

Location:
branches/5.0
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/5.0/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php

    r43510 r43740  
    296296        }
    297297
    298         $meta_key   = wp_slash( $meta_key );
    299         $meta_value = wp_slash( $value );
    300 
    301298        // Do the exact same check for a duplicate value as in update_metadata() to avoid update_metadata() returning false.
    302299        $old_value = get_metadata( $meta_type, $object_id, $meta_key );
     300        $subtype   = get_object_subtype( $meta_type, $object_id );
    303301
    304302        if ( 1 === count( $old_value ) ) {
    305             if ( $old_value[0] === $meta_value ) {
     303            if ( (string) sanitize_meta( $meta_key, $value, $meta_type, $subtype ) === $old_value[0] ) {
    306304                return true;
    307305            }
    308306        }
    309307
    310         if ( ! update_metadata( $meta_type, $object_id, $meta_key, $meta_value ) ) {
     308        if ( ! update_metadata( $meta_type, $object_id, wp_slash( $meta_key ), wp_slash( $value ) ) ) {
    311309            return new WP_Error(
    312310                'rest_meta_database_error',
  • branches/5.0/tests/phpunit/tests/rest-api/rest-post-meta-fields.php

    r43510 r43740  
    2121        ) );
    2222
    23         self::$wp_meta_keys_saved = $GLOBALS['wp_meta_keys'];
     23        self::$wp_meta_keys_saved = isset( $GLOBALS['wp_meta_keys'] ) ? $GLOBALS['wp_meta_keys'] : array();
    2424        self::$post_id = $factory->post->create();
    2525        self::$cpt_post_id        = $factory->post->create( array( 'post_type' => 'cpt' ) );
     
    130130            'auth_callback'  => '__return_false',
    131131        ) );
     132
     133        register_meta(
     134            'post',
     135            'test_boolean_update',
     136            array(
     137                'single'            => true,
     138                'type'              => 'boolean',
     139                'sanitize_callback' => 'absint',
     140                'show_in_rest'      => true,
     141            )
     142        );
     143
     144        register_meta(
     145            'post',
     146            'test_textured_text_update',
     147            array(
     148                'single'            => true,
     149                'type'              => 'string',
     150                'sanitize_callback' => 'sanitize_text_field',
     151                'show_in_rest'      => true,
     152            )
     153        );
     154
     155        register_meta(
     156            'post',
     157            'test_json_encoded',
     158            array(
     159                'single'       => true,
     160                'type'         => 'string',
     161                'show_in_rest' => true,
     162            )
     163        );
     164
     165        register_meta(
     166            'post',
     167            'test\'slashed\'key',
     168            array(
     169                'single'       => true,
     170                'type'         => 'string',
     171                'show_in_rest' => true,
     172            )
     173        );
    132174
    133175        /** @var WP_REST_Server $wp_rest_server */
     
    11631205
    11641206    /**
     1207     * @ticket 42069
     1208     * @dataProvider data_update_value_return_success_with_same_value
     1209     */
     1210    public function test_update_value_return_success_with_same_value( $meta_key, $meta_value ) {
     1211        add_post_meta( self::$post_id, $meta_key, $meta_value );
     1212
     1213        $this->grant_write_permission();
     1214
     1215        $data = array(
     1216            'meta' => array(
     1217                $meta_key => $meta_value,
     1218            ),
     1219        );
     1220
     1221        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     1222        $request->set_body_params( $data );
     1223
     1224        $response = rest_get_server()->dispatch( $request );
     1225
     1226        $this->assertEquals( 200, $response->get_status() );
     1227    }
     1228
     1229    public function data_update_value_return_success_with_same_value() {
     1230        return array(
     1231            array( 'test_boolean_update', false ),
     1232            array( 'test_boolean_update', true ),
     1233            array( 'test_textured_text_update', 'She said, "What about the > 10,000 penguins in the kitchen?"' ),
     1234            array( 'test_textured_text_update', "He's about to do something rash..." ),
     1235            array( 'test_json_encoded', json_encode( array( 'foo' => 'bar' ) ) ),
     1236            array( 'test\'slashed\'key', 'Hello' ),
     1237        );
     1238    }
     1239
     1240    /**
     1241     * @ticket 42069
     1242     */
     1243    public function test_slashed_meta_key() {
     1244
     1245        add_post_meta( self::$post_id, 'test\'slashed\'key', 'Hello' );
     1246
     1247        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     1248
     1249        $response = rest_get_server()->dispatch( $request );
     1250        $data     = $response->get_data();
     1251
     1252        $this->assertArrayHasKey( 'test\'slashed\'key', $data['meta'] );
     1253        $this->assertEquals( 'Hello', $data['meta']['test\'slashed\'key'] );
     1254    }
     1255
     1256    /**
    11651257     * Internal function used to disable an insert query which
    11661258     * will trigger a wpdb error for testing purposes.
Note: See TracChangeset for help on using the changeset viewer.