mirror of
git://develop.git.wordpress.org/
synced 2025-01-17 12:58:25 +01:00
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.9 branch. Props: batmoo, ehti, nickdaugherty, peterwilsoncc, sergeybiryukov, sstoqnov, westi, westonruter, whyisjake, whyisjake, xknown. git-svn-id: https://develop.svn.wordpress.org/branches/4.9@47648 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
c089cc014b
commit
28a08a15c1
@ -668,7 +668,7 @@ class WP_Object_Cache {
|
||||
echo "</p>";
|
||||
echo '<ul>';
|
||||
foreach ($this->cache as $group => $cache) {
|
||||
echo "<li><strong>Group:</strong> $group - ( " . number_format( strlen( serialize( $cache ) ) / KB_IN_BYTES, 2 ) . 'k )</li>';
|
||||
echo '<li><strong>Group:</strong> ' . esc_html( $group ) . ' - ( ' . number_format( strlen( serialize( $cache ) ) / KB_IN_BYTES, 2 ) . 'k )</li>';
|
||||
}
|
||||
echo '</ul>';
|
||||
}
|
||||
|
@ -2859,13 +2859,12 @@ final class WP_Customize_Manager {
|
||||
$this->store_changeset_revision = $allow_revision;
|
||||
add_filter( 'wp_save_post_revision_post_has_changed', array( $this, '_filter_revision_post_has_changed' ), 5, 3 );
|
||||
|
||||
// Update the changeset post. The publish_customize_changeset action will cause the settings in the changeset to be saved via WP_Customize_Setting::save().
|
||||
$has_kses = ( false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' ) );
|
||||
if ( $has_kses ) {
|
||||
kses_remove_filters(); // Prevent KSES from corrupting JSON in post_content.
|
||||
}
|
||||
|
||||
// Note that updating a post with publish status will trigger WP_Customize_Manager::publish_changeset_values().
|
||||
/*
|
||||
* Update the changeset post. The publish_customize_changeset action will cause the settings in the
|
||||
* changeset to be saved via WP_Customize_Setting::save(). Updating a post with publish status will
|
||||
* trigger WP_Customize_Manager::publish_changeset_values().
|
||||
*/
|
||||
add_filter( 'wp_insert_post_data', array( $this, 'preserve_insert_changeset_post_content' ), 5, 3 );
|
||||
if ( $changeset_post_id ) {
|
||||
if ( $args['autosave'] && 'auto-draft' !== get_post_status( $changeset_post_id ) ) {
|
||||
// See _wp_translate_postdata() for why this is required as it will use the edit_post meta capability.
|
||||
@ -2892,9 +2891,9 @@ final class WP_Customize_Manager {
|
||||
$this->_changeset_post_id = $r; // Update cached post ID for the loaded changeset.
|
||||
}
|
||||
}
|
||||
if ( $has_kses ) {
|
||||
kses_init_filters();
|
||||
}
|
||||
|
||||
remove_filter( 'wp_insert_post_data', array( $this, 'preserve_insert_changeset_post_content' ), 5 );
|
||||
|
||||
$this->_changeset_data = null; // Reset so WP_Customize_Manager::changeset_data() will re-populate with updated contents.
|
||||
|
||||
remove_filter( 'wp_save_post_revision_post_has_changed', array( $this, '_filter_revision_post_has_changed' ) );
|
||||
@ -2911,6 +2910,51 @@ final class WP_Customize_Manager {
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Preserve the initial JSON post_content passed to save into the post.
|
||||
*
|
||||
* This is needed to prevent KSES and other {@see 'content_save_pre'} filters
|
||||
* from corrupting JSON data.
|
||||
*
|
||||
* Note that WP_Customize_Manager::validate_setting_values() have already
|
||||
* run on the setting values being serialized as JSON into the post content
|
||||
* so it is pre-sanitized.
|
||||
*
|
||||
* Also, the sanitization logic is re-run through the respective
|
||||
* WP_Customize_Setting::sanitize() method when being read out of the
|
||||
* changeset, via WP_Customize_Manager::post_value(), and this sanitized
|
||||
* value will also be sent into WP_Customize_Setting::update() for
|
||||
* persisting to the DB.
|
||||
*
|
||||
* Multiple users can collaborate on a single changeset, where one user may
|
||||
* have the unfiltered_html capability but another may not. A user with
|
||||
* unfiltered_html may add a script tag to some field which needs to be kept
|
||||
* intact even when another user updates the changeset to modify another field
|
||||
* when they do not have unfiltered_html.
|
||||
*
|
||||
* @since 5.4.1
|
||||
*
|
||||
* @param array $data An array of slashed and processed post data.
|
||||
* @param array $postarr An array of sanitized (and slashed) but otherwise unmodified post data.
|
||||
* @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed post data as originally passed to wp_insert_post().
|
||||
* @return array Filtered post data.
|
||||
*/
|
||||
public function preserve_insert_changeset_post_content( $data, $postarr, $unsanitized_postarr ) {
|
||||
if (
|
||||
isset( $data['post_type'] ) &&
|
||||
isset( $unsanitized_postarr['post_content'] ) &&
|
||||
'customize_changeset' === $data['post_type'] ||
|
||||
(
|
||||
'revision' === $data['post_type'] &&
|
||||
! empty( $data['post_parent'] ) &&
|
||||
'customize_changeset' === get_post_type( $data['post_parent'] )
|
||||
)
|
||||
) {
|
||||
$data['post_content'] = $unsanitized_postarr['post_content'];
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Trash or delete a changeset post.
|
||||
*
|
||||
|
@ -759,10 +759,6 @@ class WP_Query {
|
||||
$this->is_single = true;
|
||||
} elseif ( $qv['p'] ) {
|
||||
$this->is_single = true;
|
||||
} elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
|
||||
// If year, month, day, hour, minute, and second are set, a single
|
||||
// post is being queried.
|
||||
$this->is_single = true;
|
||||
} elseif ( '' != $qv['pagename'] || !empty($qv['page_id']) ) {
|
||||
$this->is_page = true;
|
||||
$this->is_single = false;
|
||||
|
@ -1776,6 +1776,24 @@ function remove_accents( $string ) {
|
||||
function sanitize_file_name( $filename ) {
|
||||
$filename_raw = $filename;
|
||||
$special_chars = array("?", "[", "]", "/", "\\", "=", "<", ">", ":", ";", ",", "'", "\"", "&", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}", "%", "+", chr(0));
|
||||
|
||||
// Check for support for utf8 in the installed PCRE library once and store the result in a static.
|
||||
static $utf8_pcre = null;
|
||||
if ( ! isset( $utf8_pcre ) ) {
|
||||
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
|
||||
$utf8_pcre = @preg_match( '/^./u', 'a' );
|
||||
}
|
||||
|
||||
if ( ! seems_utf8( $filename ) ) {
|
||||
$_ext = pathinfo( $filename, PATHINFO_EXTENSION );
|
||||
$_name = pathinfo( $filename, PATHINFO_FILENAME );
|
||||
$filename = sanitize_title_with_dashes( $_name ) . '.' . $_ext;
|
||||
}
|
||||
|
||||
if ( $utf8_pcre ) {
|
||||
$filename = preg_replace( "#\x{00a0}#siu", ' ', $filename );
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the list of characters to remove from a filename.
|
||||
*
|
||||
@ -1785,7 +1803,6 @@ function sanitize_file_name( $filename ) {
|
||||
* @param string $filename_raw Filename as it was passed into sanitize_file_name().
|
||||
*/
|
||||
$special_chars = apply_filters( 'sanitize_file_name_chars', $special_chars, $filename_raw );
|
||||
$filename = preg_replace( "#\x{00a0}#siu", ' ', $filename );
|
||||
$filename = str_replace( $special_chars, '', $filename );
|
||||
$filename = str_replace( array( '%20', '+' ), '-', $filename );
|
||||
$filename = preg_replace( '/[\r\n\t -]+/', '-', $filename );
|
||||
|
@ -3136,6 +3136,9 @@ function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) {
|
||||
function wp_insert_post( $postarr, $wp_error = false ) {
|
||||
global $wpdb;
|
||||
|
||||
// Capture original pre-sanitized array for passing into filters.
|
||||
$unsanitized_postarr = $postarr;
|
||||
|
||||
$user_id = get_current_user_id();
|
||||
|
||||
$defaults = array(
|
||||
@ -3432,21 +3435,27 @@ function wp_insert_post( $postarr, $wp_error = false ) {
|
||||
* Filters attachment post data before it is updated in or added to the database.
|
||||
*
|
||||
* @since 3.9.0
|
||||
* @since 5.4.1 `$unsanitized_postarr` argument added.
|
||||
*
|
||||
* @param array $data An array of sanitized attachment post data.
|
||||
* @param array $postarr An array of unsanitized attachment post data.
|
||||
* @param array $data An array of slashed, sanitized, and processed attachment post data.
|
||||
* @param array $postarr An array of slashed and sanitized attachment post data, but not processed.
|
||||
* @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed attachment post data
|
||||
* as originally passed to wp_insert_post().
|
||||
*/
|
||||
$data = apply_filters( 'wp_insert_attachment_data', $data, $postarr );
|
||||
$data = apply_filters( 'wp_insert_attachment_data', $data, $postarr, $unsanitized_postarr );
|
||||
} else {
|
||||
/**
|
||||
* Filters slashed post data just before it is inserted into the database.
|
||||
*
|
||||
* @since 2.7.0
|
||||
* @since 5.4.1 `$unsanitized_postarr` argument added.
|
||||
*
|
||||
* @param array $data An array of slashed post data.
|
||||
* @param array $postarr An array of sanitized, but otherwise unmodified post data.
|
||||
* @param array $data An array of slashed, sanitized, and processed post data.
|
||||
* @param array $postarr An array of sanitized (and slashed) but otherwise unmodified post data.
|
||||
* @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed post data as
|
||||
* originally passed to wp_insert_post().
|
||||
*/
|
||||
$data = apply_filters( 'wp_insert_post_data', $data, $postarr );
|
||||
$data = apply_filters( 'wp_insert_post_data', $data, $postarr, $unsanitized_postarr );
|
||||
}
|
||||
$data = wp_unslash( $data );
|
||||
$where = array( 'ID' => $post_ID );
|
||||
|
@ -1692,7 +1692,7 @@ function wp_insert_user( $userdata ) {
|
||||
$data = apply_filters( 'wp_pre_insert_user_data', $data, $update, $update ? (int) $ID : null );
|
||||
|
||||
if ( $update ) {
|
||||
if ( $user_email !== $old_user_data->user_email ) {
|
||||
if ( $user_email !== $old_user_data->user_email || $user_pass !== $old_user_data->user_pass ) {
|
||||
$data['user_activation_key'] = '';
|
||||
}
|
||||
$wpdb->update( $wpdb->users, $data, compact( 'ID' ) );
|
||||
|
@ -1089,6 +1089,171 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase {
|
||||
$this->assertCount( 3, wp_get_post_revisions( $manager->changeset_post_id() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test saving changeset post without Kses or other content_save_pre filters mutating content.
|
||||
*
|
||||
* @covers WP_Customize_Manager::save_changeset_post()
|
||||
*/
|
||||
public function test_save_changeset_post_without_kses_corrupting_json() {
|
||||
global $wp_customize;
|
||||
$lesser_admin_user_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
|
||||
|
||||
$uuid = wp_generate_uuid4();
|
||||
$wp_customize = new WP_Customize_Manager(
|
||||
array(
|
||||
'changeset_uuid' => $uuid,
|
||||
)
|
||||
);
|
||||
|
||||
add_filter( 'map_meta_cap', array( $this, 'filter_map_meta_cap_to_disallow_unfiltered_html' ), 10, 2 );
|
||||
kses_init();
|
||||
add_filter( 'content_save_pre', 'capital_P_dangit' );
|
||||
add_post_type_support( 'customize_changeset', 'revisions' );
|
||||
|
||||
$options = array(
|
||||
'custom_html_1' => '<script>document.write(" Wordpress 1")</script>',
|
||||
'custom_html_2' => '<script>document.write(" Wordpress 2")</script>',
|
||||
'custom_html_3' => '<script>document.write(" Wordpress 3")</script>',
|
||||
);
|
||||
|
||||
// Populate setting as user who can bypass content_save_pre filter.
|
||||
wp_set_current_user( self::$admin_user_id );
|
||||
$wp_customize = $this->get_manager_for_testing_json_corruption_protection( $uuid );
|
||||
$wp_customize->set_post_value( 'custom_html_1', $options['custom_html_1'] );
|
||||
$wp_customize->save_changeset_post(
|
||||
array(
|
||||
'status' => 'draft',
|
||||
)
|
||||
);
|
||||
|
||||
// Populate setting as user who cannot bypass content_save_pre filter.
|
||||
wp_set_current_user( $lesser_admin_user_id );
|
||||
$wp_customize = $this->get_manager_for_testing_json_corruption_protection( $uuid );
|
||||
$wp_customize->set_post_value( 'custom_html_2', $options['custom_html_2'] );
|
||||
$wp_customize->save_changeset_post(
|
||||
array(
|
||||
'autosave' => true,
|
||||
)
|
||||
);
|
||||
|
||||
/*
|
||||
* Ensure that the unsanitized value (the "POST data") is preserved in the autosave revision.
|
||||
* The value is sent through the sanitize function when it is read from the changeset.
|
||||
*/
|
||||
$autosave_revision = wp_get_post_autosave( $wp_customize->changeset_post_id(), get_current_user_id() );
|
||||
$saved_data = json_decode( $autosave_revision->post_content, true );
|
||||
$this->assertEquals( $options['custom_html_1'], $saved_data['custom_html_1']['value'] );
|
||||
$this->assertEquals( $options['custom_html_2'], $saved_data['custom_html_2']['value'] );
|
||||
|
||||
// Update post to discard autosave.
|
||||
$wp_customize->save_changeset_post(
|
||||
array(
|
||||
'status' => 'draft',
|
||||
)
|
||||
);
|
||||
|
||||
/*
|
||||
* Ensure that the unsanitized value (the "POST data") is preserved in the post content.
|
||||
* The value is sent through the sanitize function when it is read from the changeset.
|
||||
*/
|
||||
$wp_customize = $this->get_manager_for_testing_json_corruption_protection( $uuid );
|
||||
$saved_data = json_decode( get_post( $wp_customize->changeset_post_id() )->post_content, true );
|
||||
$this->assertEquals( $options['custom_html_1'], $saved_data['custom_html_1']['value'] );
|
||||
$this->assertEquals( $options['custom_html_2'], $saved_data['custom_html_2']['value'] );
|
||||
|
||||
/*
|
||||
* Ensure that the unsanitized value (the "POST data") is preserved in the revisions' content.
|
||||
* The value is sent through the sanitize function when it is read from the changeset.
|
||||
*/
|
||||
$revisions = wp_get_post_revisions( $wp_customize->changeset_post_id() );
|
||||
$revision = array_shift( $revisions );
|
||||
$saved_data = json_decode( $revision->post_content, true );
|
||||
$this->assertEquals( $options['custom_html_1'], $saved_data['custom_html_1']['value'] );
|
||||
$this->assertEquals( $options['custom_html_2'], $saved_data['custom_html_2']['value'] );
|
||||
|
||||
/*
|
||||
* Now when publishing the changeset, the unsanitized values will be read from the changeset
|
||||
* and sanitized according to the capabilities of the users who originally updated each
|
||||
* setting in the changeset to begin with.
|
||||
*/
|
||||
wp_set_current_user( $lesser_admin_user_id );
|
||||
$wp_customize = $this->get_manager_for_testing_json_corruption_protection( $uuid );
|
||||
$wp_customize->set_post_value( 'custom_html_3', $options['custom_html_3'] );
|
||||
$wp_customize->save_changeset_post(
|
||||
array(
|
||||
'status' => 'publish',
|
||||
)
|
||||
);
|
||||
|
||||
// User saved as one who can bypass content_save_pre filter.
|
||||
$this->assertContains( '<script>', get_option( 'custom_html_1' ) );
|
||||
$this->assertContains( 'Wordpress', get_option( 'custom_html_1' ) ); // phpcs:ignore WordPress.WP.CapitalPDangit.Misspelled
|
||||
|
||||
// User saved as one who cannot bypass content_save_pre filter.
|
||||
$this->assertNotContains( '<script>', get_option( 'custom_html_2' ) );
|
||||
$this->assertContains( 'WordPress', get_option( 'custom_html_2' ) );
|
||||
|
||||
// User saved as one who also cannot bypass content_save_pre filter.
|
||||
$this->assertNotContains( '<script>', get_option( 'custom_html_3' ) );
|
||||
$this->assertContains( 'WordPress', get_option( 'custom_html_3' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a manager for testing JSON corruption protection.
|
||||
*
|
||||
* @param string $uuid UUID.
|
||||
* @return WP_Customize_Manager Manager.
|
||||
*/
|
||||
private function get_manager_for_testing_json_corruption_protection( $uuid ) {
|
||||
global $wp_customize;
|
||||
$wp_customize = new WP_Customize_Manager(
|
||||
array(
|
||||
'changeset_uuid' => $uuid,
|
||||
)
|
||||
);
|
||||
for ( $i = 0; $i < 5; $i++ ) {
|
||||
$wp_customize->add_setting(
|
||||
sprintf( 'custom_html_%d', $i ),
|
||||
array(
|
||||
'type' => 'option',
|
||||
'sanitize_callback' => array( $this, 'apply_content_save_pre_filters_if_not_main_admin_user' ),
|
||||
)
|
||||
);
|
||||
}
|
||||
return $wp_customize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize content with Kses if the current user is not the main admin.
|
||||
*
|
||||
* @since 5.4.1
|
||||
*
|
||||
* @param string $content Content to sanitize.
|
||||
* @return string Sanitized content.
|
||||
*/
|
||||
public function apply_content_save_pre_filters_if_not_main_admin_user( $content ) {
|
||||
if ( get_current_user_id() !== self::$admin_user_id ) {
|
||||
$content = apply_filters( 'content_save_pre', $content );
|
||||
}
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter map_meta_cap to disallow unfiltered_html.
|
||||
*
|
||||
* @since 5.4.1
|
||||
*
|
||||
* @param array $caps User's capabilities.
|
||||
* @param string $cap Requested cap.
|
||||
* @return array Caps.
|
||||
*/
|
||||
public function filter_map_meta_cap_to_disallow_unfiltered_html( $caps, $cap ) {
|
||||
if ( 'unfiltered_html' === $cap ) {
|
||||
$caps = array( 'do_not_allow' );
|
||||
}
|
||||
return $caps;
|
||||
}
|
||||
|
||||
/**
|
||||
* Call count for customize_changeset_save_data filter.
|
||||
*
|
||||
|
@ -67,4 +67,20 @@ class Tests_Formatting_SanitizeFileName extends WP_UnitTestCase {
|
||||
// Test a filenames that becomes extensionless.
|
||||
$this->assertEquals( 'no-extension', sanitize_file_name( '_.no-extension' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider data_wp_filenames
|
||||
*/
|
||||
function test_replaces_invalid_utf8_characters( $input, $expected ) {
|
||||
$this->assertEquals( $expected, sanitize_file_name( $input ) );
|
||||
}
|
||||
|
||||
function data_wp_filenames() {
|
||||
return array(
|
||||
array( urldecode( '%B1myfile.png' ), 'myfile.png' ),
|
||||
array( urldecode( '%B1myfile' ), 'myfile' ),
|
||||
array( 'demo bar.png', 'demo-bar.png' ),
|
||||
array( 'demo' . json_decode( '"\u00a0"' ) . 'bar.png', 'demo-bar.png' ),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -917,7 +917,7 @@ class Tests_User extends WP_UnitTestCase {
|
||||
$this->assertEquals( $u, wp_cache_get( $updated_user->user_nicename, 'userslugs' ) );
|
||||
}
|
||||
|
||||
function test_changing_email_invalidates_password_reset_key() {
|
||||
public function test_changing_email_invalidates_password_reset_key() {
|
||||
global $wpdb;
|
||||
|
||||
$user = $this->author;
|
||||
@ -949,6 +949,26 @@ class Tests_User extends WP_UnitTestCase {
|
||||
$this->assertEmpty( $user->user_activation_key );
|
||||
}
|
||||
|
||||
public function test_changing_password_invalidates_password_reset_key() {
|
||||
global $wpdb;
|
||||
|
||||
$user = $this->author;
|
||||
$wpdb->update( $wpdb->users, array( 'user_activation_key' => 'key' ), array( 'ID' => $user->ID ) );
|
||||
clean_user_cache( $user );
|
||||
|
||||
$user = get_userdata( $user->ID );
|
||||
$this->assertEquals( 'key', $user->user_activation_key );
|
||||
|
||||
$userdata = array(
|
||||
'ID' => $user->ID,
|
||||
'user_pass' => 'password',
|
||||
);
|
||||
wp_update_user( $userdata );
|
||||
|
||||
$user = get_userdata( $user->ID );
|
||||
$this->assertEmpty( $user->user_activation_key );
|
||||
}
|
||||
|
||||
public function test_search_users_login() {
|
||||
$users = get_users( array( 'search' => 'user1', 'fields' => 'ID' ) );
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user