WordPress.org

Make WordPress Core


Ignore:
Timestamp:
04/29/2020 04:06:55 PM (15 months ago)
Author:
whyisjake
Message:

Customize: Add additional filters to Customizer to prevent JSON corruption.
User: Invalidate user_activation_key on password update.
Query: Ensure that only a single post can be returned on date/time based queries.
Block Editor: Coding standards, properly escape class names.
Cache API: Ensure proper escaping around the stats method in the cache API.
Formatting: Expand sanitize_file_name to have better support for utf8 characters.

Brings the changes in [47633], [47634], [47635], [47636], [47637], and [47638] to the 5.1 branch.

Props: aduth, batmoo, ehti, ellatrix, jorgefilipecosta, nickdaugherty, noisysocks, pento, peterwilsoncc, sergeybiryukov, sstoqnov, talldanwp, westi, westonruter, whyisjake, whyisjake, xknown.

Location:
branches/5.1
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/5.1

  • branches/5.1/tests/phpunit/tests/customize/manager.php

    r44582 r47646  
    11621162
    11631163    /**
     1164     * Test saving changeset post without Kses or other content_save_pre filters mutating content.
     1165     *
     1166     * @covers WP_Customize_Manager::save_changeset_post()
     1167     */
     1168    public function test_save_changeset_post_without_kses_corrupting_json() {
     1169        global $wp_customize;
     1170        $lesser_admin_user_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
     1171
     1172        $uuid         = wp_generate_uuid4();
     1173        $wp_customize = new WP_Customize_Manager(
     1174            array(
     1175                'changeset_uuid' => $uuid,
     1176            )
     1177        );
     1178
     1179        add_filter( 'map_meta_cap', array( $this, 'filter_map_meta_cap_to_disallow_unfiltered_html' ), 10, 2 );
     1180        kses_init();
     1181        add_filter( 'content_save_pre', 'capital_P_dangit' );
     1182        add_post_type_support( 'customize_changeset', 'revisions' );
     1183
     1184        $options = array(
     1185            'custom_html_1' => '<script>document.write(" Wordpress 1")</script>',
     1186            'custom_html_2' => '<script>document.write(" Wordpress 2")</script>',
     1187            'custom_html_3' => '<script>document.write(" Wordpress 3")</script>',
     1188        );
     1189
     1190        // Populate setting as user who can bypass content_save_pre filter.
     1191        wp_set_current_user( self::$admin_user_id );
     1192        $wp_customize = $this->get_manager_for_testing_json_corruption_protection( $uuid );
     1193        $wp_customize->set_post_value( 'custom_html_1', $options['custom_html_1'] );
     1194        $wp_customize->save_changeset_post(
     1195            array(
     1196                'status' => 'draft',
     1197            )
     1198        );
     1199
     1200        // Populate setting as user who cannot bypass content_save_pre filter.
     1201        wp_set_current_user( $lesser_admin_user_id );
     1202        $wp_customize = $this->get_manager_for_testing_json_corruption_protection( $uuid );
     1203        $wp_customize->set_post_value( 'custom_html_2', $options['custom_html_2'] );
     1204        $wp_customize->save_changeset_post(
     1205            array(
     1206                'autosave' => true,
     1207            )
     1208        );
     1209
     1210        /*
     1211         * Ensure that the unsanitized value (the "POST data") is preserved in the autosave revision.
     1212         * The value is sent through the sanitize function when it is read from the changeset.
     1213         */
     1214        $autosave_revision = wp_get_post_autosave( $wp_customize->changeset_post_id(), get_current_user_id() );
     1215        $saved_data        = json_decode( $autosave_revision->post_content, true );
     1216        $this->assertEquals( $options['custom_html_1'], $saved_data['custom_html_1']['value'] );
     1217        $this->assertEquals( $options['custom_html_2'], $saved_data['custom_html_2']['value'] );
     1218
     1219        // Update post to discard autosave.
     1220        $wp_customize->save_changeset_post(
     1221            array(
     1222                'status' => 'draft',
     1223            )
     1224        );
     1225
     1226        /*
     1227         * Ensure that the unsanitized value (the "POST data") is preserved in the post content.
     1228         * The value is sent through the sanitize function when it is read from the changeset.
     1229         */
     1230        $wp_customize = $this->get_manager_for_testing_json_corruption_protection( $uuid );
     1231        $saved_data   = json_decode( get_post( $wp_customize->changeset_post_id() )->post_content, true );
     1232        $this->assertEquals( $options['custom_html_1'], $saved_data['custom_html_1']['value'] );
     1233        $this->assertEquals( $options['custom_html_2'], $saved_data['custom_html_2']['value'] );
     1234
     1235        /*
     1236         * Ensure that the unsanitized value (the "POST data") is preserved in the revisions' content.
     1237         * The value is sent through the sanitize function when it is read from the changeset.
     1238         */
     1239        $revisions  = wp_get_post_revisions( $wp_customize->changeset_post_id() );
     1240        $revision   = array_shift( $revisions );
     1241        $saved_data = json_decode( $revision->post_content, true );
     1242        $this->assertEquals( $options['custom_html_1'], $saved_data['custom_html_1']['value'] );
     1243        $this->assertEquals( $options['custom_html_2'], $saved_data['custom_html_2']['value'] );
     1244
     1245        /*
     1246         * Now when publishing the changeset, the unsanitized values will be read from the changeset
     1247         * and sanitized according to the capabilities of the users who originally updated each
     1248         * setting in the changeset to begin with.
     1249         */
     1250        wp_set_current_user( $lesser_admin_user_id );
     1251        $wp_customize = $this->get_manager_for_testing_json_corruption_protection( $uuid );
     1252        $wp_customize->set_post_value( 'custom_html_3', $options['custom_html_3'] );
     1253        $wp_customize->save_changeset_post(
     1254            array(
     1255                'status' => 'publish',
     1256            )
     1257        );
     1258
     1259        // User saved as one who can bypass content_save_pre filter.
     1260        $this->assertContains( '<script>', get_option( 'custom_html_1' ) );
     1261        $this->assertContains( 'Wordpress', get_option( 'custom_html_1' ) ); // phpcs:ignore WordPress.WP.CapitalPDangit.Misspelled
     1262
     1263        // User saved as one who cannot bypass content_save_pre filter.
     1264        $this->assertNotContains( '<script>', get_option( 'custom_html_2' ) );
     1265        $this->assertContains( 'WordPress', get_option( 'custom_html_2' ) );
     1266
     1267        // User saved as one who also cannot bypass content_save_pre filter.
     1268        $this->assertNotContains( '<script>', get_option( 'custom_html_3' ) );
     1269        $this->assertContains( 'WordPress', get_option( 'custom_html_3' ) );
     1270    }
     1271
     1272    /**
     1273     * Get a manager for testing JSON corruption protection.
     1274     *
     1275     * @param string $uuid UUID.
     1276     * @return WP_Customize_Manager Manager.
     1277     */
     1278    private function get_manager_for_testing_json_corruption_protection( $uuid ) {
     1279        global $wp_customize;
     1280        $wp_customize = new WP_Customize_Manager(
     1281            array(
     1282                'changeset_uuid' => $uuid,
     1283            )
     1284        );
     1285        for ( $i = 0; $i < 5; $i++ ) {
     1286            $wp_customize->add_setting(
     1287                sprintf( 'custom_html_%d', $i ),
     1288                array(
     1289                    'type'              => 'option',
     1290                    'sanitize_callback' => array( $this, 'apply_content_save_pre_filters_if_not_main_admin_user' ),
     1291                )
     1292            );
     1293        }
     1294        return $wp_customize;
     1295    }
     1296
     1297    /**
     1298     * Sanitize content with Kses if the current user is not the main admin.
     1299     *
     1300     * @since 5.4.1
     1301     *
     1302     * @param string $content Content to sanitize.
     1303     * @return string Sanitized content.
     1304     */
     1305    public function apply_content_save_pre_filters_if_not_main_admin_user( $content ) {
     1306        if ( get_current_user_id() !== self::$admin_user_id ) {
     1307            $content = apply_filters( 'content_save_pre', $content );
     1308        }
     1309        return $content;
     1310    }
     1311
     1312    /**
     1313     * Filter map_meta_cap to disallow unfiltered_html.
     1314     *
     1315     * @since 5.4.1
     1316     *
     1317     * @param array  $caps User's capabilities.
     1318     * @param string $cap  Requested cap.
     1319     * @return array Caps.
     1320     */
     1321    public function filter_map_meta_cap_to_disallow_unfiltered_html( $caps, $cap ) {
     1322        if ( 'unfiltered_html' === $cap ) {
     1323            $caps = array( 'do_not_allow' );
     1324        }
     1325        return $caps;
     1326    }
     1327
     1328    /**
    11641329     * Call count for customize_changeset_save_data filter.
    11651330     *
Note: See TracChangeset for help on using the changeset viewer.