Make WordPress Core


Ignore:
Timestamp:
04/29/2020 03:12:50 PM (7 years ago)
Author:
whyisjake
Message:

Customize: Add additional filters to Customizer to prevent JSON corruption.

This solution extends the wp_insert_post_data filter to pass in addition to the slashed/sanitized/processed data, and the slashed/sanitized/unprocessed data, to also pass the initial slashed/unsanitized/unprocessed data which was passed into wp_insert_post(). This then allows plugins to have complete control over how sanitization is performed based on the post type.

Props westonruter, peterwilsoncc, sstoqnov, whyisjake, xknown.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-customize-manager.php

    r47550 r47633  
    29292929                 */
    29302930
    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().
     2931                /*
     2932                 * Update the changeset post. The publish_customize_changeset action will cause the settings in the
     2933                 * changeset to be saved via WP_Customize_Setting::save(). Updating a post with publish status will
     2934                 * trigger WP_Customize_Manager::publish_changeset_values().
     2935                 */
     2936                add_filter( 'wp_insert_post_data', array( $this, 'preserve_insert_changeset_post_content' ), 5, 3 );
    29422937                if ( $changeset_post_id ) {
    29432938                        if ( $args['autosave'] && 'auto-draft' !== get_post_status( $changeset_post_id ) ) {
    … …  
    29702965                        }
    29712966                }
    2972 
    2973                 // Restore removed content filters.
    2974                 if ( $has_kses ) {
    2975                         kses_init_filters();
    2976                 }
    2977                 if ( $has_targeted_link_rel_filters ) {
    2978                         wp_init_targeted_link_rel_filters();
    2979                 }
     2967                remove_filter( 'wp_insert_post_data', array( $this, 'preserve_insert_changeset_post_content' ), 5 );
    29802968
    29812969                $this->_changeset_data = null; // Reset so WP_Customize_Manager::changeset_data() will re-populate with updated contents.
    … …  
    29932981
    29942982                return $response;
     2983        }
     2984
     2985        /**
     2986         * Preserve the initial JSON post_content passed to save into the post.
     2987         *
     2988         * This is needed to prevent KSES and other {@see 'content_save_pre'} filters
     2989         * from corrupting JSON data.
     2990         *
     2991         * Note that WP_Customize_Manager::validate_setting_values() have already
     2992         * run on the setting values being serialized as JSON into the post content
     2993         * so it is pre-sanitized.
     2994         *
     2995         * Also, the sanitization logic is re-run through the respective
     2996         * WP_Customize_Setting::sanitize() method when being read out of the
     2997         * changeset, via WP_Customize_Manager::post_value(), and this sanitized
     2998         * value will also be sent into WP_Customize_Setting::update() for
     2999         * persisting to the DB.
     3000         *
     3001         * Multiple users can collaborate on a single changeset, where one user may
     3002         * have the unfiltered_html capability but another may not. A user with
     3003         * unfiltered_html may add a script tag to some field which needs to be kept
     3004         * intact even when another user updates the changeset to modify another field
     3005         * when they do not have unfiltered_html.
     3006         *
     3007         * @since 5.4.1
     3008         *
     3009         * @param array $data                An array of slashed and processed post data.
     3010         * @param array $postarr             An array of sanitized (and slashed) but otherwise unmodified post data.
     3011         * @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed post data as originally passed to wp_insert_post().
     3012         * @return array Filtered post data.
     3013         */
     3014        public function preserve_insert_changeset_post_content( $data, $postarr, $unsanitized_postarr ) {
     3015                if (
     3016                        isset( $data['post_type'] ) &&
     3017                        isset( $unsanitized_postarr['post_content'] ) &&
     3018                        'customize_changeset' === $data['post_type'] ||
     3019                        (
     3020                                'revision' === $data['post_type'] &&
     3021                                ! empty( $data['post_parent'] ) &&
     3022                                'customize_changeset' === get_post_type( $data['post_parent'] )
     3023                        )
     3024                ) {
     3025                        $data['post_content'] = $unsanitized_postarr['post_content'];
     3026                }
     3027                return $data;
    29953028        }
    29963029
Note: See TracChangeset for help on using the changeset viewer.