Make WordPress Core

Changeset 47644


Ignore:
Timestamp:
04/29/2020 04:04:20 PM (6 years 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.4 branch.

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

Location:
branches/5.3
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • branches/5.3

  • branches/5.3/src/wp-includes/blocks/rss.php

    r46189 r47644  
    9797        }
    9898
    99         $list_items_markup = "<ul class='{$class}'>{$list_items}</ul>";
     99        $list_items_markup = sprintf( "<ul class='%s'>%s</ul>", esc_attr( $class ), $list_items );
    100100
    101101        // PHP 5.2 compatibility. See: http://simplepie.org/wiki/faq/i_m_getting_memory_leaks.
  • branches/5.3/src/wp-includes/blocks/search.php

    r46189 r47644  
    5353        return sprintf(
    5454                '<form class="%s" role="search" method="get" action="%s">%s</form>',
    55                 $class,
     55                esc_attr( $class ),
    5656                esc_url( home_url( '/' ) ),
    5757                $label_markup . $input_markup . $button_markup
  • branches/5.3/src/wp-includes/cache.php

    r45161 r47644  
    696696                echo '<ul>';
    697697                foreach ( $this->cache as $group => $cache ) {
    698                         echo "<li><strong>Group:</strong> $group - ( " . number_format( strlen( serialize( $cache ) ) / KB_IN_BYTES, 2 ) . 'k )</li>';
     698                        echo '<li><strong>Group:</strong> ' . esc_html( $group ) . ' - ( ' . number_format( strlen( serialize( $cache ) ) / KB_IN_BYTES, 2 ) . 'k )</li>';
    699699                }
    700700                echo '</ul>';
  • branches/5.3/src/wp-includes/class-wp-customize-manager.php

    r46548 r47644  
    29242924
    29252925                /*
    2926                  * Update the changeset post. The publish_customize_changeset action
    2927                  * will cause the settings in the changeset to be saved via
    2928                  * WP_Customize_Setting::save().
     2926                 * Update the changeset post. The publish_customize_changeset action will cause the settings in the
     2927                 * changeset to be saved via WP_Customize_Setting::save(). Updating a post with publish status will
     2928                 * trigger WP_Customize_Manager::publish_changeset_values().
    29292929                 */
    2930 
    2931                 // Prevent content filters from corrupting JSON in post_content.
    2932                 $has_kses = ( false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' ) );
    2933                 if ( $has_kses ) {
    2934                         kses_remove_filters();
    2935                 }
    2936                 $has_targeted_link_rel_filters = ( false !== has_filter( 'content_save_pre', 'wp_targeted_link_rel' ) );
    2937                 if ( $has_targeted_link_rel_filters ) {
    2938                         wp_remove_targeted_link_rel_filters();
    2939                 }
    2940 
    2941                 // Note that updating a post with publish status will trigger WP_Customize_Manager::publish_changeset_values().
     2930                add_filter( 'wp_insert_post_data', array( $this, 'preserve_insert_changeset_post_content' ), 5, 3 );
    29422931                if ( $changeset_post_id ) {
    29432932                        if ( $args['autosave'] && 'auto-draft' !== get_post_status( $changeset_post_id ) ) {
     
    29662955                        }
    29672956                }
    2968 
    2969                 // Restore removed content filters.
    2970                 if ( $has_kses ) {
    2971                         kses_init_filters();
    2972                 }
    2973                 if ( $has_targeted_link_rel_filters ) {
    2974                         wp_init_targeted_link_rel_filters();
    2975                 }
     2957                remove_filter( 'wp_insert_post_data', array( $this, 'preserve_insert_changeset_post_content' ), 5 );
    29762958
    29772959                $this->_changeset_data = null; // Reset so WP_Customize_Manager::changeset_data() will re-populate with updated contents.
     
    29892971
    29902972                return $response;
     2973        }
     2974
     2975        /**
     2976         * Preserve the initial JSON post_content passed to save into the post.
     2977         *
     2978         * This is needed to prevent KSES and other {@see 'content_save_pre'} filters
     2979         * from corrupting JSON data.
     2980         *
     2981         * Note that WP_Customize_Manager::validate_setting_values() have already
     2982         * run on the setting values being serialized as JSON into the post content
     2983         * so it is pre-sanitized.
     2984         *
     2985         * Also, the sanitization logic is re-run through the respective
     2986         * WP_Customize_Setting::sanitize() method when being read out of the
     2987         * changeset, via WP_Customize_Manager::post_value(), and this sanitized
     2988         * value will also be sent into WP_Customize_Setting::update() for
     2989         * persisting to the DB.
     2990         *
     2991         * Multiple users can collaborate on a single changeset, where one user may
     2992         * have the unfiltered_html capability but another may not. A user with
     2993         * unfiltered_html may add a script tag to some field which needs to be kept
     2994         * intact even when another user updates the changeset to modify another field
     2995         * when they do not have unfiltered_html.
     2996         *
     2997         * @since 5.4.1
     2998         *
     2999         * @param array $data                An array of slashed and processed post data.
     3000         * @param array $postarr             An array of sanitized (and slashed) but otherwise unmodified post data.
     3001         * @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed post data as originally passed to wp_insert_post().
     3002         * @return array Filtered post data.
     3003         */
     3004        public function preserve_insert_changeset_post_content( $data, $postarr, $unsanitized_postarr ) {
     3005                if (
     3006                        isset( $data['post_type'] ) &&
     3007                        isset( $unsanitized_postarr['post_content'] ) &&
     3008                        'customize_changeset' === $data['post_type'] ||
     3009                        (
     3010                                'revision' === $data['post_type'] &&
     3011                                ! empty( $data['post_parent'] ) &&
     3012                                'customize_changeset' === get_post_type( $data['post_parent'] )
     3013                        )
     3014                ) {
     3015                        $data['post_content'] = $unsanitized_postarr['post_content'];
     3016                }
     3017                return $data;
    29913018        }
    29923019
  • branches/5.3/src/wp-includes/class-wp-query.php

    r46474 r47644  
    801801                } elseif ( $qv['p'] ) {
    802802                        $this->is_single = true;
    803                 } elseif ( ( '' !== $qv['hour'] ) && ( '' !== $qv['minute'] ) && ( '' !== $qv['second'] ) && ( '' != $qv['year'] ) && ( '' != $qv['monthnum'] ) && ( '' != $qv['day'] ) ) {
    804                         // If year, month, day, hour, minute, and second are set, a single
    805                         // post is being queried.
    806                         $this->is_single = true;
    807803                } elseif ( '' != $qv['pagename'] || ! empty( $qv['page_id'] ) ) {
    808804                        $this->is_page   = true;
  • branches/5.3/src/wp-includes/formatting.php

    r46900 r47644  
    20042004        $filename_raw  = $filename;
    20052005        $special_chars = array( '?', '[', ']', '/', '\\', '=', '<', '>', ':', ';', ',', "'", '"', '&', '$', '#', '*', '(', ')', '|', '~', '`', '!', '{', '}', '%', '+', chr( 0 ) );
     2006
     2007        // Check for support for utf8 in the installed PCRE library once and store the result in a static.
     2008        static $utf8_pcre = null;
     2009        if ( ! isset( $utf8_pcre ) ) {
     2010                // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
     2011                $utf8_pcre = @preg_match( '/^./u', 'a' );
     2012        }
     2013
     2014        if ( ! seems_utf8( $filename ) ) {
     2015                $_ext     = pathinfo( $filename, PATHINFO_EXTENSION );
     2016                $_name    = pathinfo( $filename, PATHINFO_FILENAME );
     2017                $filename = sanitize_title_with_dashes( $_name ) . '.' . $_ext;
     2018        }
     2019
     2020        if ( $utf8_pcre ) {
     2021                $filename = preg_replace( "#\x{00a0}#siu", ' ', $filename );
     2022        }
     2023
    20062024        /**
    20072025         * Filters the list of characters to remove from a filename.
     
    20132031         */
    20142032        $special_chars = apply_filters( 'sanitize_file_name_chars', $special_chars, $filename_raw );
    2015         $filename      = preg_replace( "#\x{00a0}#siu", ' ', $filename );
    20162033        $filename      = str_replace( $special_chars, '', $filename );
    20172034        $filename      = str_replace( array( '%20', '+' ), '-', $filename );
  • branches/5.3/src/wp-includes/post.php

    r46977 r47644  
    35553555        global $wpdb;
    35563556
     3557        // Capture original pre-sanitized array for passing into filters.
     3558        $unsanitized_postarr = $postarr;
     3559
    35573560        $user_id = get_current_user_id();
    35583561
     
    38663869                 *
    38673870                 * @since 3.9.0
     3871                 * @since 5.4.1 `$unsanitized_postarr` argument added.
    38683872                 *
    3869                  * @param array $data    An array of sanitized attachment post data.
    3870                  * @param array $postarr An array of unsanitized attachment post data.
     3873                 * @param array $data                An array of slashed, sanitized, and processed attachment post data.
     3874                 * @param array $postarr             An array of slashed and sanitized attachment post data, but not processed.
     3875                 * @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed attachment post data
     3876                 *                                   as originally passed to wp_insert_post().
    38713877                 */
    3872                 $data = apply_filters( 'wp_insert_attachment_data', $data, $postarr );
     3878                $data = apply_filters( 'wp_insert_attachment_data', $data, $postarr, $unsanitized_postarr );
    38733879        } else {
    38743880                /**
     
    38763882                 *
    38773883                 * @since 2.7.0
     3884                 * @since 5.4.1 `$unsanitized_postarr` argument added.
    38783885                 *
    3879                  * @param array $data    An array of slashed post data.
    3880                  * @param array $postarr An array of sanitized, but otherwise unmodified post data.
     3886                 * @param array $data                An array of slashed, sanitized, and processed post data.
     3887                 * @param array $postarr             An array of sanitized (and slashed) but otherwise unmodified post data.
     3888                 * @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed post data as
     3889                 *                                   originally passed to wp_insert_post().
    38813890                 */
    3882                 $data = apply_filters( 'wp_insert_post_data', $data, $postarr );
     3891                $data = apply_filters( 'wp_insert_post_data', $data, $postarr, $unsanitized_postarr );
    38833892        }
    38843893        $data  = wp_unslash( $data );
  • branches/5.3/src/wp-includes/user.php

    r47417 r47644  
    17941794
    17951795        if ( $update ) {
    1796                 if ( $user_email !== $old_user_data->user_email ) {
     1796                if ( $user_email !== $old_user_data->user_email || $user_pass !== $old_user_data->user_pass ) {
    17971797                        $data['user_activation_key'] = '';
    17981798                }
  • branches/5.3/tests/phpunit/tests/customize/manager.php

    r46548 r47644  
    12421242
    12431243        /**
     1244         * Test saving changeset post without Kses or other content_save_pre filters mutating content.
     1245         *
     1246         * @covers WP_Customize_Manager::save_changeset_post()
     1247         */
     1248        public function test_save_changeset_post_without_kses_corrupting_json() {
     1249                global $wp_customize;
     1250                $lesser_admin_user_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
     1251
     1252                $uuid         = wp_generate_uuid4();
     1253                $wp_customize = new WP_Customize_Manager(
     1254                        array(
     1255                                'changeset_uuid' => $uuid,
     1256                        )
     1257                );
     1258
     1259                add_filter( 'map_meta_cap', array( $this, 'filter_map_meta_cap_to_disallow_unfiltered_html' ), 10, 2 );
     1260                kses_init();
     1261                add_filter( 'content_save_pre', 'capital_P_dangit' );
     1262                add_post_type_support( 'customize_changeset', 'revisions' );
     1263
     1264                $options = array(
     1265                        'custom_html_1' => '<script>document.write(" Wordpress 1")</script>',
     1266                        'custom_html_2' => '<script>document.write(" Wordpress 2")</script>',
     1267                        'custom_html_3' => '<script>document.write(" Wordpress 3")</script>',
     1268                );
     1269
     1270                // Populate setting as user who can bypass content_save_pre filter.
     1271                wp_set_current_user( self::$admin_user_id );
     1272                $wp_customize = $this->get_manager_for_testing_json_corruption_protection( $uuid );
     1273                $wp_customize->set_post_value( 'custom_html_1', $options['custom_html_1'] );
     1274                $wp_customize->save_changeset_post(
     1275                        array(
     1276                                'status' => 'draft',
     1277                        )
     1278                );
     1279
     1280                // Populate setting as user who cannot bypass content_save_pre filter.
     1281                wp_set_current_user( $lesser_admin_user_id );
     1282                $wp_customize = $this->get_manager_for_testing_json_corruption_protection( $uuid );
     1283                $wp_customize->set_post_value( 'custom_html_2', $options['custom_html_2'] );
     1284                $wp_customize->save_changeset_post(
     1285                        array(
     1286                                'autosave' => true,
     1287                        )
     1288                );
     1289
     1290                /*
     1291                 * Ensure that the unsanitized value (the "POST data") is preserved in the autosave revision.
     1292                 * The value is sent through the sanitize function when it is read from the changeset.
     1293                 */
     1294                $autosave_revision = wp_get_post_autosave( $wp_customize->changeset_post_id(), get_current_user_id() );
     1295                $saved_data        = json_decode( $autosave_revision->post_content, true );
     1296                $this->assertEquals( $options['custom_html_1'], $saved_data['custom_html_1']['value'] );
     1297                $this->assertEquals( $options['custom_html_2'], $saved_data['custom_html_2']['value'] );
     1298
     1299                // Update post to discard autosave.
     1300                $wp_customize->save_changeset_post(
     1301                        array(
     1302                                'status' => 'draft',
     1303                        )
     1304                );
     1305
     1306                /*
     1307                 * Ensure that the unsanitized value (the "POST data") is preserved in the post content.
     1308                 * The value is sent through the sanitize function when it is read from the changeset.
     1309                 */
     1310                $wp_customize = $this->get_manager_for_testing_json_corruption_protection( $uuid );
     1311                $saved_data   = json_decode( get_post( $wp_customize->changeset_post_id() )->post_content, true );
     1312                $this->assertEquals( $options['custom_html_1'], $saved_data['custom_html_1']['value'] );
     1313                $this->assertEquals( $options['custom_html_2'], $saved_data['custom_html_2']['value'] );
     1314
     1315                /*
     1316                 * Ensure that the unsanitized value (the "POST data") is preserved in the revisions' content.
     1317                 * The value is sent through the sanitize function when it is read from the changeset.
     1318                 */
     1319                $revisions  = wp_get_post_revisions( $wp_customize->changeset_post_id() );
     1320                $revision   = array_shift( $revisions );
     1321                $saved_data = json_decode( $revision->post_content, true );
     1322                $this->assertEquals( $options['custom_html_1'], $saved_data['custom_html_1']['value'] );
     1323                $this->assertEquals( $options['custom_html_2'], $saved_data['custom_html_2']['value'] );
     1324
     1325                /*
     1326                 * Now when publishing the changeset, the unsanitized values will be read from the changeset
     1327                 * and sanitized according to the capabilities of the users who originally updated each
     1328                 * setting in the changeset to begin with.
     1329                 */
     1330                wp_set_current_user( $lesser_admin_user_id );
     1331                $wp_customize = $this->get_manager_for_testing_json_corruption_protection( $uuid );
     1332                $wp_customize->set_post_value( 'custom_html_3', $options['custom_html_3'] );
     1333                $wp_customize->save_changeset_post(
     1334                        array(
     1335                                'status' => 'publish',
     1336                        )
     1337                );
     1338
     1339                // User saved as one who can bypass content_save_pre filter.
     1340                $this->assertContains( '<script>', get_option( 'custom_html_1' ) );
     1341                $this->assertContains( 'Wordpress', get_option( 'custom_html_1' ) ); // phpcs:ignore WordPress.WP.CapitalPDangit.Misspelled
     1342
     1343                // User saved as one who cannot bypass content_save_pre filter.
     1344                $this->assertNotContains( '<script>', get_option( 'custom_html_2' ) );
     1345                $this->assertContains( 'WordPress', get_option( 'custom_html_2' ) );
     1346
     1347                // User saved as one who also cannot bypass content_save_pre filter.
     1348                $this->assertNotContains( '<script>', get_option( 'custom_html_3' ) );
     1349                $this->assertContains( 'WordPress', get_option( 'custom_html_3' ) );
     1350        }
     1351
     1352        /**
     1353         * Get a manager for testing JSON corruption protection.
     1354         *
     1355         * @param string $uuid UUID.
     1356         * @return WP_Customize_Manager Manager.
     1357         */
     1358        private function get_manager_for_testing_json_corruption_protection( $uuid ) {
     1359                global $wp_customize;
     1360                $wp_customize = new WP_Customize_Manager(
     1361                        array(
     1362                                'changeset_uuid' => $uuid,
     1363                        )
     1364                );
     1365                for ( $i = 0; $i < 5; $i++ ) {
     1366                        $wp_customize->add_setting(
     1367                                sprintf( 'custom_html_%d', $i ),
     1368                                array(
     1369                                        'type'              => 'option',
     1370                                        'sanitize_callback' => array( $this, 'apply_content_save_pre_filters_if_not_main_admin_user' ),
     1371                                )
     1372                        );
     1373                }
     1374                return $wp_customize;
     1375        }
     1376
     1377        /**
     1378         * Sanitize content with Kses if the current user is not the main admin.
     1379         *
     1380         * @since 5.4.1
     1381         *
     1382         * @param string $content Content to sanitize.
     1383         * @return string Sanitized content.
     1384         */
     1385        public function apply_content_save_pre_filters_if_not_main_admin_user( $content ) {
     1386                if ( get_current_user_id() !== self::$admin_user_id ) {
     1387                        $content = apply_filters( 'content_save_pre', $content );
     1388                }
     1389                return $content;
     1390        }
     1391
     1392        /**
     1393         * Filter map_meta_cap to disallow unfiltered_html.
     1394         *
     1395         * @since 5.4.1
     1396         *
     1397         * @param array  $caps User's capabilities.
     1398         * @param string $cap  Requested cap.
     1399         * @return array Caps.
     1400         */
     1401        public function filter_map_meta_cap_to_disallow_unfiltered_html( $caps, $cap ) {
     1402                if ( 'unfiltered_html' === $cap ) {
     1403                        $caps = array( 'do_not_allow' );
     1404                }
     1405                return $caps;
     1406        }
     1407
     1408        /**
    12441409         * Call count for customize_changeset_save_data filter.
    12451410         *
  • branches/5.3/tests/phpunit/tests/formatting/SanitizeFileName.php

    r42343 r47644  
    6969                $this->assertEquals( 'no-extension', sanitize_file_name( '_.no-extension' ) );
    7070        }
     71
     72        /**
     73         * @dataProvider data_wp_filenames
     74         */
     75        function test_replaces_invalid_utf8_characters( $input, $expected ) {
     76                $this->assertEquals( $expected, sanitize_file_name( $input ) );
     77        }
     78
     79        function data_wp_filenames() {
     80                return array(
     81                        array( urldecode( '%B1myfile.png' ), 'myfile.png' ),
     82                        array( urldecode( '%B1myfile' ), 'myfile' ),
     83                        array( 'demo bar.png', 'demo-bar.png' ),
     84                        array( 'demo' . json_decode( '"\u00a0"' ) . 'bar.png', 'demo-bar.png' ),
     85                );
     86        }
    7187}
  • branches/5.3/tests/phpunit/tests/user.php

    r45874 r47644  
    10261026        }
    10271027
    1028         function test_changing_email_invalidates_password_reset_key() {
     1028        public function test_changing_email_invalidates_password_reset_key() {
    10291029                global $wpdb;
    10301030
     
    10511051                        'user_nicename' => 'cat',
    10521052                        'user_email'    => 'foo@bar.dev',
     1053                );
     1054                wp_update_user( $userdata );
     1055
     1056                $user = get_userdata( $user->ID );
     1057                $this->assertEmpty( $user->user_activation_key );
     1058        }
     1059
     1060        public function test_changing_password_invalidates_password_reset_key() {
     1061                global $wpdb;
     1062
     1063                $user = $this->author;
     1064                $wpdb->update( $wpdb->users, array( 'user_activation_key' => 'key' ), array( 'ID' => $user->ID ) );
     1065                clean_user_cache( $user );
     1066
     1067                $user = get_userdata( $user->ID );
     1068                $this->assertEquals( 'key', $user->user_activation_key );
     1069
     1070                $userdata = array(
     1071                        'ID'        => $user->ID,
     1072                        'user_pass' => 'password',
    10531073                );
    10541074                wp_update_user( $userdata );
Note: See TracChangeset for help on using the changeset viewer.