Make WordPress Core


Ignore:
Timestamp:
04/29/2020 04:18:07 PM (7 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:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.8

  • 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         *
Note: See TracChangeset for help on using the changeset viewer.