Make WordPress Core

Changeset 47649


Ignore:
Timestamp:
04/29/2020 04:18:07 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.
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], [47637], and [47638] to the 4.8 branch.

Props: batmoo, ehti, nickdaugherty, peterwilsoncc, sergeybiryukov, sstoqnov, westi, westonruter, whyisjake, whyisjake, xknown.

Location:
branches/4.8
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • branches/4.8

  • branches/4.8/src/wp-includes/cache.php

    r39051 r47649  
    692692                echo '<ul>';
    693693                foreach ($this->cache as $group => $cache) {
    694                         echo "<li><strong>Group:</strong> $group - ( " . number_format( strlen( serialize( $cache ) ) / KB_IN_BYTES, 2 ) . 'k )</li>';
     694                        echo '<li><strong>Group:</strong> ' . esc_html( $group ) . ' - ( ' . number_format( strlen( serialize( $cache ) ) / KB_IN_BYTES, 2 ) . 'k )</li>';
    695695                }
    696696                echo '</ul>';
  • branches/4.8/src/wp-includes/class-wp-customize-manager.php

    r41429 r47649  
    25282528                add_filter( 'wp_save_post_revision_post_has_changed', array( $this, '_filter_revision_post_has_changed' ), 5, 3 );
    25292529
    2530                 // Update the changeset post. The publish_customize_changeset action will cause the settings in the changeset to be saved via WP_Customize_Setting::save().
    2531                 $has_kses = ( false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' ) );
    2532                 if ( $has_kses ) {
    2533                         kses_remove_filters(); // Prevent KSES from corrupting JSON in post_content.
    2534                 }
    2535 
    2536                 // Note that updating a post with publish status will trigger WP_Customize_Manager::publish_changeset_values().
     2530                /*
     2531                 * Update the changeset post. The publish_customize_changeset action will cause the settings in the
     2532                 * changeset to be saved via WP_Customize_Setting::save(). Updating a post with publish status will
     2533                 * trigger WP_Customize_Manager::publish_changeset_values().
     2534                 */
     2535                add_filter( 'wp_insert_post_data', array( $this, 'preserve_insert_changeset_post_content' ), 5, 3 );
    25372536                if ( $changeset_post_id ) {
    25382537                        $post_array['edit_date'] = true; // Prevent date clearing.
     
    25442543                        }
    25452544                }
    2546                 if ( $has_kses ) {
    2547                         kses_init_filters();
    2548                 }
     2545
     2546                remove_filter( 'wp_insert_post_data', array( $this, 'preserve_insert_changeset_post_content' ), 5 );
     2547
    25492548                $this->_changeset_data = null; // Reset so WP_Customize_Manager::changeset_data() will re-populate with updated contents.
    25502549
     
    25612560
    25622561                return $response;
     2562        }
     2563
     2564        /**
     2565         * Preserve the initial JSON post_content passed to save into the post.
     2566         *
     2567         * This is needed to prevent KSES and other {@see 'content_save_pre'} filters
     2568         * from corrupting JSON data.
     2569         *
     2570         * Note that WP_Customize_Manager::validate_setting_values() have already
     2571         * run on the setting values being serialized as JSON into the post content
     2572         * so it is pre-sanitized.
     2573         *
     2574         * Also, the sanitization logic is re-run through the respective
     2575         * WP_Customize_Setting::sanitize() method when being read out of the
     2576         * changeset, via WP_Customize_Manager::post_value(), and this sanitized
     2577         * value will also be sent into WP_Customize_Setting::update() for
     2578         * persisting to the DB.
     2579         *
     2580         * Multiple users can collaborate on a single changeset, where one user may
     2581         * have the unfiltered_html capability but another may not. A user with
     2582         * unfiltered_html may add a script tag to some field which needs to be kept
     2583         * intact even when another user updates the changeset to modify another field
     2584         * when they do not have unfiltered_html.
     2585         *
     2586         * @since 5.4.1
     2587         *
     2588         * @param array $data                An array of slashed and processed post data.
     2589         * @param array $postarr             An array of sanitized (and slashed) but otherwise unmodified post data.
     2590         * @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed post data as originally passed to wp_insert_post().
     2591         * @return array Filtered post data.
     2592         */
     2593        public function preserve_insert_changeset_post_content( $data, $postarr, $unsanitized_postarr ) {
     2594                if (
     2595                        isset( $data['post_type'] ) &&
     2596                        isset( $unsanitized_postarr['post_content'] ) &&
     2597                        'customize_changeset' === $data['post_type'] ||
     2598                        (
     2599                                'revision' === $data['post_type'] &&
     2600                                ! empty( $data['post_parent'] ) &&
     2601                                'customize_changeset' === get_post_type( $data['post_parent'] )
     2602                        )
     2603                ) {
     2604                        $data['post_content'] = $unsanitized_postarr['post_content'];
     2605                }
     2606                return $data;
    25632607        }
    25642608
  • branches/4.8/src/wp-includes/class-wp-query.php

    r46494 r47649  
    809809                } elseif ( $qv['p'] ) {
    810810                        $this->is_single = true;
    811                 } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
    812                         // If year, month, day, hour, minute, and second are set, a single
    813                         // post is being queried.
    814                         $this->is_single = true;
    815811                } elseif ( '' != $qv['pagename'] || !empty($qv['page_id']) ) {
    816812                        $this->is_page = true;
  • branches/4.8/src/wp-includes/formatting.php

    r45995 r47649  
    17631763        $filename_raw = $filename;
    17641764        $special_chars = array("?", "[", "]", "/", "\\", "=", "<", ">", ":", ";", ",", "'", "\"", "&", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}", "%", "+", chr(0));
     1765
     1766        // Check for support for utf8 in the installed PCRE library once and store the result in a static.
     1767        static $utf8_pcre = null;
     1768        if ( ! isset( $utf8_pcre ) ) {
     1769                // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
     1770                $utf8_pcre = @preg_match( '/^./u', 'a' );
     1771        }
     1772
     1773        if ( ! seems_utf8( $filename ) ) {
     1774                $_ext     = pathinfo( $filename, PATHINFO_EXTENSION );
     1775                $_name    = pathinfo( $filename, PATHINFO_FILENAME );
     1776                $filename = sanitize_title_with_dashes( $_name ) . '.' . $_ext;
     1777        }
     1778
     1779        if ( $utf8_pcre ) {
     1780                $filename = preg_replace( "#\x{00a0}#siu", ' ', $filename );
     1781        }
     1782
    17651783        /**
    17661784         * Filters the list of characters to remove from a filename.
     
    17721790         */
    17731791        $special_chars = apply_filters( 'sanitize_file_name_chars', $special_chars, $filename_raw );
    1774         $filename = preg_replace( "#\x{00a0}#siu", ' ', $filename );
    17751792        $filename = str_replace( $special_chars, '', $filename );
    17761793        $filename = str_replace( array( '%20', '+' ), '-', $filename );
  • branches/4.8/src/wp-includes/post.php

    r43394 r47649  
    29742974        global $wpdb;
    29752975
     2976        // Capture original pre-sanitized array for passing into filters.
     2977        $unsanitized_postarr = $postarr;
     2978
    29762979        $user_id = get_current_user_id();
    29772980
     
    32703273                 *
    32713274                 * @since 3.9.0
     3275                 * @since 5.4.1 `$unsanitized_postarr` argument added.
    32723276                 *
    3273                  * @param array $data    An array of sanitized attachment post data.
    3274                  * @param array $postarr An array of unsanitized attachment post data.
     3277                 * @param array $data                An array of slashed, sanitized, and processed attachment post data.
     3278                 * @param array $postarr             An array of slashed and sanitized attachment post data, but not processed.
     3279                 * @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed attachment post data
     3280                 *                                   as originally passed to wp_insert_post().
    32753281                 */
    3276                 $data = apply_filters( 'wp_insert_attachment_data', $data, $postarr );
     3282                $data = apply_filters( 'wp_insert_attachment_data', $data, $postarr, $unsanitized_postarr );
    32773283        } else {
    32783284                /**
     
    32803286                 *
    32813287                 * @since 2.7.0
     3288                 * @since 5.4.1 `$unsanitized_postarr` argument added.
    32823289                 *
    3283                  * @param array $data    An array of slashed post data.
    3284                  * @param array $postarr An array of sanitized, but otherwise unmodified post data.
     3290                 * @param array $data                An array of slashed, sanitized, and processed post data.
     3291                 * @param array $postarr             An array of sanitized (and slashed) but otherwise unmodified post data.
     3292                 * @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed post data as
     3293                 *                                   originally passed to wp_insert_post().
    32853294                 */
    3286                 $data = apply_filters( 'wp_insert_post_data', $data, $postarr );
     3295                $data = apply_filters( 'wp_insert_post_data', $data, $postarr, $unsanitized_postarr );
    32873296        }
    32883297        $data = wp_unslash( $data );
  • branches/4.8/src/wp-includes/user.php

    r40560 r47649  
    16391639
    16401640        if ( $update ) {
    1641                 if ( $user_email !== $old_user_data->user_email ) {
     1641                if ( $user_email !== $old_user_data->user_email || $user_pass !== $old_user_data->user_pass ) {
    16421642                        $data['user_activation_key'] = '';
    16431643                }
  • branches/4.8/tests/phpunit/tests/customize/manager.php

    r40521 r47649  
    893893
    894894        /**
     895         * Test saving changeset post without Kses or other content_save_pre filters mutating content.
     896         *
     897         * @covers WP_Customize_Manager::save_changeset_post()
     898         */
     899        public function test_save_changeset_post_without_kses_corrupting_json() {
     900                global $wp_customize;
     901                $lesser_admin_user_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
     902
     903                $uuid         = wp_generate_uuid4();
     904                $wp_customize = new WP_Customize_Manager(
     905                        array(
     906                                'changeset_uuid' => $uuid,
     907                        )
     908                );
     909
     910                add_filter( 'map_meta_cap', array( $this, 'filter_map_meta_cap_to_disallow_unfiltered_html' ), 10, 2 );
     911                kses_init();
     912                add_filter( 'content_save_pre', 'capital_P_dangit' );
     913                add_post_type_support( 'customize_changeset', 'revisions' );
     914
     915                $options = array(
     916                        'custom_html_1' => '<script>document.write(" Wordpress 1")</script>',
     917                        'custom_html_2' => '<script>document.write(" Wordpress 2")</script>',
     918                        'custom_html_3' => '<script>document.write(" Wordpress 3")</script>',
     919                );
     920
     921                // Populate setting as user who can bypass content_save_pre filter.
     922                wp_set_current_user( self::$admin_user_id );
     923                $wp_customize = $this->get_manager_for_testing_json_corruption_protection( $uuid );
     924                $wp_customize->set_post_value( 'custom_html_1', $options['custom_html_1'] );
     925                $wp_customize->save_changeset_post(
     926                        array(
     927                                'status' => 'draft',
     928                        )
     929                );
     930
     931                // Populate setting as user who cannot bypass content_save_pre filter.
     932                wp_set_current_user( $lesser_admin_user_id );
     933                $wp_customize = $this->get_manager_for_testing_json_corruption_protection( $uuid );
     934                $wp_customize->set_post_value( 'custom_html_2', $options['custom_html_2'] );
     935
     936                $wp_customize->save_changeset_post(
     937                        array(
     938                                'status' => 'draft',
     939                        )
     940                );
     941
     942                /*
     943                 * Ensure that the unsanitized value (the "POST data") is preserved in the post content.
     944                 * The value is sent through the sanitize function when it is read from the changeset.
     945                 */
     946                $wp_customize = $this->get_manager_for_testing_json_corruption_protection( $uuid );
     947                $saved_data   = json_decode( get_post( $wp_customize->changeset_post_id() )->post_content, true );
     948                $this->assertEquals( $options['custom_html_1'], $saved_data['custom_html_1']['value'] );
     949                $this->assertEquals( $options['custom_html_2'], $saved_data['custom_html_2']['value'] );
     950
     951                /*
     952                 * Ensure that the unsanitized value (the "POST data") is preserved in the revisions' content.
     953                 * The value is sent through the sanitize function when it is read from the changeset.
     954                 */
     955                $revisions  = wp_get_post_revisions( $wp_customize->changeset_post_id() );
     956                $revision   = array_shift( $revisions );
     957                $saved_data = json_decode( $revision->post_content, true );
     958                $this->assertEquals( $options['custom_html_1'], $saved_data['custom_html_1']['value'] );
     959                $this->assertEquals( $options['custom_html_2'], $saved_data['custom_html_2']['value'] );
     960
     961                /*
     962                 * Now when publishing the changeset, the unsanitized values will be read from the changeset
     963                 * and sanitized according to the capabilities of the users who originally updated each
     964                 * setting in the changeset to begin with.
     965                 */
     966                wp_set_current_user( $lesser_admin_user_id );
     967                $wp_customize = $this->get_manager_for_testing_json_corruption_protection( $uuid );
     968                $wp_customize->set_post_value( 'custom_html_3', $options['custom_html_3'] );
     969                $wp_customize->save_changeset_post(
     970                        array(
     971                                'status' => 'publish',
     972                        )
     973                );
     974
     975                // User saved as one who can bypass content_save_pre filter.
     976                $this->assertContains( '<script>', get_option( 'custom_html_1' ) );
     977                $this->assertContains( 'Wordpress', get_option( 'custom_html_1' ) ); // phpcs:ignore WordPress.WP.CapitalPDangit.Misspelled
     978
     979                // User saved as one who cannot bypass content_save_pre filter.
     980                $this->assertNotContains( '<script>', get_option( 'custom_html_2' ) );
     981                $this->assertContains( 'WordPress', get_option( 'custom_html_2' ) );
     982
     983                // User saved as one who also cannot bypass content_save_pre filter.
     984                $this->assertNotContains( '<script>', get_option( 'custom_html_3' ) );
     985                $this->assertContains( 'WordPress', get_option( 'custom_html_3' ) );
     986        }
     987
     988        /**
     989         * Get a manager for testing JSON corruption protection.
     990         *
     991         * @param string $uuid UUID.
     992         * @return WP_Customize_Manager Manager.
     993         */
     994        private function get_manager_for_testing_json_corruption_protection( $uuid ) {
     995                global $wp_customize;
     996                $wp_customize = new WP_Customize_Manager(
     997                        array(
     998                                'changeset_uuid' => $uuid,
     999                        )
     1000                );
     1001                for ( $i = 0; $i < 5; $i++ ) {
     1002                        $wp_customize->add_setting(
     1003                                sprintf( 'custom_html_%d', $i ),
     1004                                array(
     1005                                        'type'              => 'option',
     1006                                        'sanitize_callback' => array( $this, 'apply_content_save_pre_filters_if_not_main_admin_user' ),
     1007                                )
     1008                        );
     1009                }
     1010                return $wp_customize;
     1011        }
     1012
     1013        /**
     1014         * Sanitize content with Kses if the current user is not the main admin.
     1015         *
     1016         * @since 5.4.1
     1017         *
     1018         * @param string $content Content to sanitize.
     1019         * @return string Sanitized content.
     1020         */
     1021        public function apply_content_save_pre_filters_if_not_main_admin_user( $content ) {
     1022                if ( get_current_user_id() !== self::$admin_user_id ) {
     1023                        $content = apply_filters( 'content_save_pre', $content );
     1024                }
     1025                return $content;
     1026        }
     1027
     1028        /**
     1029         * Filter map_meta_cap to disallow unfiltered_html.
     1030         *
     1031         * @since 5.4.1
     1032         *
     1033         * @param array  $caps User's capabilities.
     1034         * @param string $cap  Requested cap.
     1035         * @return array Caps.
     1036         */
     1037        public function filter_map_meta_cap_to_disallow_unfiltered_html( $caps, $cap ) {
     1038                if ( 'unfiltered_html' === $cap ) {
     1039                        $caps = array( 'do_not_allow' );
     1040                }
     1041                return $caps;
     1042        }
     1043
     1044        /**
    8951045         * Call count for customize_changeset_save_data filter.
    8961046         *
  • branches/4.8/tests/phpunit/tests/formatting/SanitizeFileName.php

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

    r40564 r47649  
    916916        }
    917917
    918         function test_changing_email_invalidates_password_reset_key() {
     918        public function test_changing_email_invalidates_password_reset_key() {
    919919                global $wpdb;
    920920
     
    941941                        'user_nicename' => 'cat',
    942942                        'user_email'    => 'foo@bar.dev',
     943                );
     944                wp_update_user( $userdata );
     945
     946                $user = get_userdata( $user->ID );
     947                $this->assertEmpty( $user->user_activation_key );
     948        }
     949
     950        public function test_changing_password_invalidates_password_reset_key() {
     951                global $wpdb;
     952
     953                $user = $this->author;
     954                $wpdb->update( $wpdb->users, array( 'user_activation_key' => 'key' ), array( 'ID' => $user->ID ) );
     955                clean_user_cache( $user );
     956
     957                $user = get_userdata( $user->ID );
     958                $this->assertEquals( 'key', $user->user_activation_key );
     959
     960                $userdata = array(
     961                        'ID'        => $user->ID,
     962                        'user_pass' => 'password',
    943963                );
    944964                wp_update_user( $userdata );
Note: See TracChangeset for help on using the changeset viewer.