Skip to content

Commit 4a62dbe

Browse files
committed
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.2 branch. Props: aduth, batmoo, ehti, ellatrix, jorgefilipecosta, nickdaugherty, noisysocks, pento, peterwilsoncc, sergeybiryukov, sstoqnov, talldanwp, westi, westonruter, whyisjake, whyisjake, xknown. git-svn-id: https://develop.svn.wordpress.org/branches/5.2@47645 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 037a827 commit 4a62dbe

11 files changed

Lines changed: 291 additions & 40 deletions

File tree

src/wp-includes/blocks/rss.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function render_block_core_rss( $attributes ) {
8080
}
8181

8282
$classes = 'grid' === $attributes['blockLayout'] ? ' is-grid columns-' . $attributes['columns'] : '';
83-
$list_items_markup = "<ul class='wp-block-rss{$classes}'>{$list_items}</ul>";
83+
$list_items_markup = sprintf( "<ul class='%s'>%s</ul>", esc_attr( 'wp-block-rss' . $classes ), $list_items );
8484

8585
// PHP 5.2 compatibility. See: http://simplepie.org/wiki/faq/i_m_getting_memory_leaks.
8686
$rss->__destruct();
@@ -93,7 +93,8 @@ function render_block_core_rss( $attributes ) {
9393
* Registers the `core/rss` block on server.
9494
*/
9595
function register_block_core_rss() {
96-
register_block_type( 'core/rss',
96+
register_block_type(
97+
'core/rss',
9798
array(
9899
'attributes' => array(
99100
'columns' => array(

src/wp-includes/blocks/search.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function render_block_core_search( $attributes ) {
4646

4747
return sprintf(
4848
'<form class="%s" role="search" method="get" action="%s">%s</form>',
49-
$class,
49+
esc_attr( $class ),
5050
esc_url( home_url( '/' ) ),
5151
$label_markup . $input_markup . $button_markup
5252
);

src/wp-includes/cache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ public function stats() {
695695
echo '</p>';
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>';
701701
}

src/wp-includes/class-wp-customize-manager.php

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2886,22 +2886,11 @@ function save_changeset_post( $args = array() ) {
28862886
add_filter( 'wp_save_post_revision_post_has_changed', array( $this, '_filter_revision_post_has_changed' ), 5, 3 );
28872887

28882888
/*
2889-
* Update the changeset post. The publish_customize_changeset action
2890-
* will cause the settings in the changeset to be saved via
2891-
* WP_Customize_Setting::save().
2889+
* Update the changeset post. The publish_customize_changeset action will cause the settings in the
2890+
* changeset to be saved via WP_Customize_Setting::save(). Updating a post with publish status will
2891+
* trigger WP_Customize_Manager::publish_changeset_values().
28922892
*/
2893-
2894-
// Prevent content filters from corrupting JSON in post_content.
2895-
$has_kses = ( false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' ) );
2896-
if ( $has_kses ) {
2897-
kses_remove_filters();
2898-
}
2899-
$has_targeted_link_rel_filters = ( false !== has_filter( 'content_save_pre', 'wp_targeted_link_rel' ) );
2900-
if ( $has_targeted_link_rel_filters ) {
2901-
wp_remove_targeted_link_rel_filters();
2902-
}
2903-
2904-
// Note that updating a post with publish status will trigger WP_Customize_Manager::publish_changeset_values().
2893+
add_filter( 'wp_insert_post_data', array( $this, 'preserve_insert_changeset_post_content' ), 5, 3 );
29052894
if ( $changeset_post_id ) {
29062895
if ( $args['autosave'] && 'auto-draft' !== get_post_status( $changeset_post_id ) ) {
29072896
// See _wp_translate_postdata() for why this is required as it will use the edit_post meta capability.
@@ -2928,14 +2917,7 @@ function save_changeset_post( $args = array() ) {
29282917
$this->_changeset_post_id = $r; // Update cached post ID for the loaded changeset.
29292918
}
29302919
}
2931-
2932-
// Restore removed content filters.
2933-
if ( $has_kses ) {
2934-
kses_init_filters();
2935-
}
2936-
if ( $has_targeted_link_rel_filters ) {
2937-
wp_init_targeted_link_rel_filters();
2938-
}
2920+
remove_filter( 'wp_insert_post_data', array( $this, 'preserve_insert_changeset_post_content' ), 5 );
29392921

29402922
$this->_changeset_data = null; // Reset so WP_Customize_Manager::changeset_data() will re-populate with updated contents.
29412923

@@ -2953,6 +2935,51 @@ function save_changeset_post( $args = array() ) {
29532935
return $response;
29542936
}
29552937

2938+
/**
2939+
* Preserve the initial JSON post_content passed to save into the post.
2940+
*
2941+
* This is needed to prevent KSES and other {@see 'content_save_pre'} filters
2942+
* from corrupting JSON data.
2943+
*
2944+
* Note that WP_Customize_Manager::validate_setting_values() have already
2945+
* run on the setting values being serialized as JSON into the post content
2946+
* so it is pre-sanitized.
2947+
*
2948+
* Also, the sanitization logic is re-run through the respective
2949+
* WP_Customize_Setting::sanitize() method when being read out of the
2950+
* changeset, via WP_Customize_Manager::post_value(), and this sanitized
2951+
* value will also be sent into WP_Customize_Setting::update() for
2952+
* persisting to the DB.
2953+
*
2954+
* Multiple users can collaborate on a single changeset, where one user may
2955+
* have the unfiltered_html capability but another may not. A user with
2956+
* unfiltered_html may add a script tag to some field which needs to be kept
2957+
* intact even when another user updates the changeset to modify another field
2958+
* when they do not have unfiltered_html.
2959+
*
2960+
* @since 5.4.1
2961+
*
2962+
* @param array $data An array of slashed and processed post data.
2963+
* @param array $postarr An array of sanitized (and slashed) but otherwise unmodified post data.
2964+
* @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed post data as originally passed to wp_insert_post().
2965+
* @return array Filtered post data.
2966+
*/
2967+
public function preserve_insert_changeset_post_content( $data, $postarr, $unsanitized_postarr ) {
2968+
if (
2969+
isset( $data['post_type'] ) &&
2970+
isset( $unsanitized_postarr['post_content'] ) &&
2971+
'customize_changeset' === $data['post_type'] ||
2972+
(
2973+
'revision' === $data['post_type'] &&
2974+
! empty( $data['post_parent'] ) &&
2975+
'customize_changeset' === get_post_type( $data['post_parent'] )
2976+
)
2977+
) {
2978+
$data['post_content'] = $unsanitized_postarr['post_content'];
2979+
}
2980+
return $data;
2981+
}
2982+
29562983
/**
29572984
* Trash or delete a changeset post.
29582985
*

src/wp-includes/class-wp-query.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -797,10 +797,6 @@ public function parse_query( $query = '' ) {
797797
$this->is_single = true;
798798
} elseif ( $qv['p'] ) {
799799
$this->is_single = true;
800-
} elseif ( ( '' !== $qv['hour'] ) && ( '' !== $qv['minute'] ) && ( '' !== $qv['second'] ) && ( '' != $qv['year'] ) && ( '' != $qv['monthnum'] ) && ( '' != $qv['day'] ) ) {
801-
// If year, month, day, hour, minute, and second are set, a single
802-
// post is being queried.
803-
$this->is_single = true;
804800
} elseif ( '' != $qv['pagename'] || ! empty( $qv['page_id'] ) ) {
805801
$this->is_page = true;
806802
$this->is_single = false;

src/wp-includes/formatting.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1998,6 +1998,24 @@ function remove_accents( $string ) {
19981998
function sanitize_file_name( $filename ) {
19991999
$filename_raw = $filename;
20002000
$special_chars = array( '?', '[', ']', '/', '\\', '=', '<', '>', ':', ';', ',', "'", '"', '&', '$', '#', '*', '(', ')', '|', '~', '`', '!', '{', '}', '%', '+', chr( 0 ) );
2001+
2002+
// Check for support for utf8 in the installed PCRE library once and store the result in a static.
2003+
static $utf8_pcre = null;
2004+
if ( ! isset( $utf8_pcre ) ) {
2005+
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
2006+
$utf8_pcre = @preg_match( '/^./u', 'a' );
2007+
}
2008+
2009+
if ( ! seems_utf8( $filename ) ) {
2010+
$_ext = pathinfo( $filename, PATHINFO_EXTENSION );
2011+
$_name = pathinfo( $filename, PATHINFO_FILENAME );
2012+
$filename = sanitize_title_with_dashes( $_name ) . '.' . $_ext;
2013+
}
2014+
2015+
if ( $utf8_pcre ) {
2016+
$filename = preg_replace( "#\x{00a0}#siu", ' ', $filename );
2017+
}
2018+
20012019
/**
20022020
* Filters the list of characters to remove from a filename.
20032021
*
@@ -2007,7 +2025,6 @@ function sanitize_file_name( $filename ) {
20072025
* @param string $filename_raw Filename as it was passed into sanitize_file_name().
20082026
*/
20092027
$special_chars = apply_filters( 'sanitize_file_name_chars', $special_chars, $filename_raw );
2010-
$filename = preg_replace( "#\x{00a0}#siu", ' ', $filename );
20112028
$filename = str_replace( $special_chars, '', $filename );
20122029
$filename = str_replace( array( '%20', '+' ), '-', $filename );
20132030
$filename = preg_replace( '/[\r\n\t -]+/', '-', $filename );

src/wp-includes/post.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3385,6 +3385,9 @@ function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) {
33853385
function wp_insert_post( $postarr, $wp_error = false ) {
33863386
global $wpdb;
33873387

3388+
// Capture original pre-sanitized array for passing into filters.
3389+
$unsanitized_postarr = $postarr;
3390+
33883391
$user_id = get_current_user_id();
33893392

33903393
$defaults = array(
@@ -3696,21 +3699,27 @@ function wp_insert_post( $postarr, $wp_error = false ) {
36963699
* Filters attachment post data before it is updated in or added to the database.
36973700
*
36983701
* @since 3.9.0
3702+
* @since 5.4.1 `$unsanitized_postarr` argument added.
36993703
*
3700-
* @param array $data An array of sanitized attachment post data.
3701-
* @param array $postarr An array of unsanitized attachment post data.
3704+
* @param array $data An array of slashed, sanitized, and processed attachment post data.
3705+
* @param array $postarr An array of slashed and sanitized attachment post data, but not processed.
3706+
* @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed attachment post data
3707+
* as originally passed to wp_insert_post().
37023708
*/
3703-
$data = apply_filters( 'wp_insert_attachment_data', $data, $postarr );
3709+
$data = apply_filters( 'wp_insert_attachment_data', $data, $postarr, $unsanitized_postarr );
37043710
} else {
37053711
/**
37063712
* Filters slashed post data just before it is inserted into the database.
37073713
*
37083714
* @since 2.7.0
3715+
* @since 5.4.1 `$unsanitized_postarr` argument added.
37093716
*
3710-
* @param array $data An array of slashed post data.
3711-
* @param array $postarr An array of sanitized, but otherwise unmodified post data.
3717+
* @param array $data An array of slashed, sanitized, and processed post data.
3718+
* @param array $postarr An array of sanitized (and slashed) but otherwise unmodified post data.
3719+
* @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed post data as
3720+
* originally passed to wp_insert_post().
37123721
*/
3713-
$data = apply_filters( 'wp_insert_post_data', $data, $postarr );
3722+
$data = apply_filters( 'wp_insert_post_data', $data, $postarr, $unsanitized_postarr );
37143723
}
37153724
$data = wp_unslash( $data );
37163725
$where = array( 'ID' => $post_ID );

src/wp-includes/user.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1773,7 +1773,7 @@ function wp_insert_user( $userdata ) {
17731773
$data = apply_filters( 'wp_pre_insert_user_data', $data, $update, $update ? (int) $ID : null );
17741774

17751775
if ( $update ) {
1776-
if ( $user_email !== $old_user_data->user_email ) {
1776+
if ( $user_email !== $old_user_data->user_email || $user_pass !== $old_user_data->user_pass ) {
17771777
$data['user_activation_key'] = '';
17781778
}
17791779
$wpdb->update( $wpdb->users, $data, compact( 'ID' ) );

0 commit comments

Comments
 (0)