Skip to content

Commit e65e7a3

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. 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.0 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.0@47647 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 18cdae1 commit e65e7a3

File tree

9 files changed

+291
-24
lines changed

9 files changed

+291
-24
lines changed

src/wp-includes/cache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ public function stats() {
668668
echo "</p>";
669669
echo '<ul>';
670670
foreach ($this->cache as $group => $cache) {
671-
echo "<li><strong>Group:</strong> $group - ( " . number_format( strlen( serialize( $cache ) ) / KB_IN_BYTES, 2 ) . 'k )</li>';
671+
echo '<li><strong>Group:</strong> ' . esc_html( $group ) . ' - ( ' . number_format( strlen( serialize( $cache ) ) / KB_IN_BYTES, 2 ) . 'k )</li>';
672672
}
673673
echo '</ul>';
674674
}

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

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2859,13 +2859,12 @@ function save_changeset_post( $args = array() ) {
28592859
$this->store_changeset_revision = $allow_revision;
28602860
add_filter( 'wp_save_post_revision_post_has_changed', array( $this, '_filter_revision_post_has_changed' ), 5, 3 );
28612861

2862-
// Update the changeset post. The publish_customize_changeset action will cause the settings in the changeset to be saved via WP_Customize_Setting::save().
2863-
$has_kses = ( false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' ) );
2864-
if ( $has_kses ) {
2865-
kses_remove_filters(); // Prevent KSES from corrupting JSON in post_content.
2866-
}
2867-
2868-
// Note that updating a post with publish status will trigger WP_Customize_Manager::publish_changeset_values().
2862+
/*
2863+
* Update the changeset post. The publish_customize_changeset action will cause the settings in the
2864+
* changeset to be saved via WP_Customize_Setting::save(). Updating a post with publish status will
2865+
* trigger WP_Customize_Manager::publish_changeset_values().
2866+
*/
2867+
add_filter( 'wp_insert_post_data', array( $this, 'preserve_insert_changeset_post_content' ), 5, 3 );
28692868
if ( $changeset_post_id ) {
28702869
if ( $args['autosave'] && 'auto-draft' !== get_post_status( $changeset_post_id ) ) {
28712870
// See _wp_translate_postdata() for why this is required as it will use the edit_post meta capability.
@@ -2892,9 +2891,9 @@ function save_changeset_post( $args = array() ) {
28922891
$this->_changeset_post_id = $r; // Update cached post ID for the loaded changeset.
28932892
}
28942893
}
2895-
if ( $has_kses ) {
2896-
kses_init_filters();
2897-
}
2894+
2895+
remove_filter( 'wp_insert_post_data', array( $this, 'preserve_insert_changeset_post_content' ), 5 );
2896+
28982897
$this->_changeset_data = null; // Reset so WP_Customize_Manager::changeset_data() will re-populate with updated contents.
28992898

29002899
remove_filter( 'wp_save_post_revision_post_has_changed', array( $this, '_filter_revision_post_has_changed' ) );
@@ -2911,6 +2910,51 @@ function save_changeset_post( $args = array() ) {
29112910
return $response;
29122911
}
29132912

2913+
/**
2914+
* Preserve the initial JSON post_content passed to save into the post.
2915+
*
2916+
* This is needed to prevent KSES and other {@see 'content_save_pre'} filters
2917+
* from corrupting JSON data.
2918+
*
2919+
* Note that WP_Customize_Manager::validate_setting_values() have already
2920+
* run on the setting values being serialized as JSON into the post content
2921+
* so it is pre-sanitized.
2922+
*
2923+
* Also, the sanitization logic is re-run through the respective
2924+
* WP_Customize_Setting::sanitize() method when being read out of the
2925+
* changeset, via WP_Customize_Manager::post_value(), and this sanitized
2926+
* value will also be sent into WP_Customize_Setting::update() for
2927+
* persisting to the DB.
2928+
*
2929+
* Multiple users can collaborate on a single changeset, where one user may
2930+
* have the unfiltered_html capability but another may not. A user with
2931+
* unfiltered_html may add a script tag to some field which needs to be kept
2932+
* intact even when another user updates the changeset to modify another field
2933+
* when they do not have unfiltered_html.
2934+
*
2935+
* @since 5.4.1
2936+
*
2937+
* @param array $data An array of slashed and processed post data.
2938+
* @param array $postarr An array of sanitized (and slashed) but otherwise unmodified post data.
2939+
* @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed post data as originally passed to wp_insert_post().
2940+
* @return array Filtered post data.
2941+
*/
2942+
public function preserve_insert_changeset_post_content( $data, $postarr, $unsanitized_postarr ) {
2943+
if (
2944+
isset( $data['post_type'] ) &&
2945+
isset( $unsanitized_postarr['post_content'] ) &&
2946+
'customize_changeset' === $data['post_type'] ||
2947+
(
2948+
'revision' === $data['post_type'] &&
2949+
! empty( $data['post_parent'] ) &&
2950+
'customize_changeset' === get_post_type( $data['post_parent'] )
2951+
)
2952+
) {
2953+
$data['post_content'] = $unsanitized_postarr['post_content'];
2954+
}
2955+
return $data;
2956+
}
2957+
29142958
/**
29152959
* Trash or delete a changeset post.
29162960
*

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -759,10 +759,6 @@ public function parse_query( $query = '' ) {
759759
$this->is_single = true;
760760
} elseif ( $qv['p'] ) {
761761
$this->is_single = true;
762-
} elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
763-
// If year, month, day, hour, minute, and second are set, a single
764-
// post is being queried.
765-
$this->is_single = true;
766762
} elseif ( '' != $qv['pagename'] || !empty($qv['page_id']) ) {
767763
$this->is_page = true;
768764
$this->is_single = false;

src/wp-includes/formatting.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1776,6 +1776,24 @@ function remove_accents( $string ) {
17761776
function sanitize_file_name( $filename ) {
17771777
$filename_raw = $filename;
17781778
$special_chars = array("?", "[", "]", "/", "\\", "=", "<", ">", ":", ";", ",", "'", "\"", "&", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}", "%", "+", chr(0));
1779+
1780+
// Check for support for utf8 in the installed PCRE library once and store the result in a static.
1781+
static $utf8_pcre = null;
1782+
if ( ! isset( $utf8_pcre ) ) {
1783+
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
1784+
$utf8_pcre = @preg_match( '/^./u', 'a' );
1785+
}
1786+
1787+
if ( ! seems_utf8( $filename ) ) {
1788+
$_ext = pathinfo( $filename, PATHINFO_EXTENSION );
1789+
$_name = pathinfo( $filename, PATHINFO_FILENAME );
1790+
$filename = sanitize_title_with_dashes( $_name ) . '.' . $_ext;
1791+
}
1792+
1793+
if ( $utf8_pcre ) {
1794+
$filename = preg_replace( "#\x{00a0}#siu", ' ', $filename );
1795+
}
1796+
17791797
/**
17801798
* Filters the list of characters to remove from a filename.
17811799
*
@@ -1785,7 +1803,6 @@ function sanitize_file_name( $filename ) {
17851803
* @param string $filename_raw Filename as it was passed into sanitize_file_name().
17861804
*/
17871805
$special_chars = apply_filters( 'sanitize_file_name_chars', $special_chars, $filename_raw );
1788-
$filename = preg_replace( "#\x{00a0}#siu", ' ', $filename );
17891806
$filename = str_replace( $special_chars, '', $filename );
17901807
$filename = str_replace( array( '%20', '+' ), '-', $filename );
17911808
$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
@@ -3203,6 +3203,9 @@ function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) {
32033203
function wp_insert_post( $postarr, $wp_error = false ) {
32043204
global $wpdb;
32053205

3206+
// Capture original pre-sanitized array for passing into filters.
3207+
$unsanitized_postarr = $postarr;
3208+
32063209
$user_id = get_current_user_id();
32073210

32083211
$defaults = array(
@@ -3506,21 +3509,27 @@ function wp_insert_post( $postarr, $wp_error = false ) {
35063509
* Filters attachment post data before it is updated in or added to the database.
35073510
*
35083511
* @since 3.9.0
3512+
* @since 5.4.1 `$unsanitized_postarr` argument added.
35093513
*
3510-
* @param array $data An array of sanitized attachment post data.
3511-
* @param array $postarr An array of unsanitized attachment post data.
3514+
* @param array $data An array of slashed, sanitized, and processed attachment post data.
3515+
* @param array $postarr An array of slashed and sanitized attachment post data, but not processed.
3516+
* @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed attachment post data
3517+
* as originally passed to wp_insert_post().
35123518
*/
3513-
$data = apply_filters( 'wp_insert_attachment_data', $data, $postarr );
3519+
$data = apply_filters( 'wp_insert_attachment_data', $data, $postarr, $unsanitized_postarr );
35143520
} else {
35153521
/**
35163522
* Filters slashed post data just before it is inserted into the database.
35173523
*
35183524
* @since 2.7.0
3525+
* @since 5.4.1 `$unsanitized_postarr` argument added.
35193526
*
3520-
* @param array $data An array of slashed post data.
3521-
* @param array $postarr An array of sanitized, but otherwise unmodified post data.
3527+
* @param array $data An array of slashed, sanitized, and processed post data.
3528+
* @param array $postarr An array of sanitized (and slashed) but otherwise unmodified post data.
3529+
* @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed post data as
3530+
* originally passed to wp_insert_post().
35223531
*/
3523-
$data = apply_filters( 'wp_insert_post_data', $data, $postarr );
3532+
$data = apply_filters( 'wp_insert_post_data', $data, $postarr, $unsanitized_postarr );
35243533
}
35253534
$data = wp_unslash( $data );
35263535
$where = array( 'ID' => $post_ID );

src/wp-includes/user.php

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

17021702
if ( $update ) {
1703-
if ( $user_email !== $old_user_data->user_email ) {
1703+
if ( $user_email !== $old_user_data->user_email || $user_pass !== $old_user_data->user_pass ) {
17041704
$data['user_activation_key'] = '';
17051705
}
17061706
$wpdb->update( $wpdb->users, $data, compact( 'ID' ) );

tests/phpunit/tests/customize/manager.php

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,171 @@ function test_save_changeset_post_without_theme_activation() {
10891089
$this->assertCount( 3, wp_get_post_revisions( $manager->changeset_post_id() ) );
10901090
}
10911091

1092+
/**
1093+
* Test saving changeset post without Kses or other content_save_pre filters mutating content.
1094+
*
1095+
* @covers WP_Customize_Manager::save_changeset_post()
1096+
*/
1097+
public function test_save_changeset_post_without_kses_corrupting_json() {
1098+
global $wp_customize;
1099+
$lesser_admin_user_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
1100+
1101+
$uuid = wp_generate_uuid4();
1102+
$wp_customize = new WP_Customize_Manager(
1103+
array(
1104+
'changeset_uuid' => $uuid,
1105+
)
1106+
);
1107+
1108+
add_filter( 'map_meta_cap', array( $this, 'filter_map_meta_cap_to_disallow_unfiltered_html' ), 10, 2 );
1109+
kses_init();
1110+
add_filter( 'content_save_pre', 'capital_P_dangit' );
1111+
add_post_type_support( 'customize_changeset', 'revisions' );
1112+
1113+
$options = array(
1114+
'custom_html_1' => '<script>document.write(" Wordpress 1")</script>',
1115+
'custom_html_2' => '<script>document.write(" Wordpress 2")</script>',
1116+
'custom_html_3' => '<script>document.write(" Wordpress 3")</script>',
1117+
);
1118+
1119+
// Populate setting as user who can bypass content_save_pre filter.
1120+
wp_set_current_user( self::$admin_user_id );
1121+
$wp_customize = $this->get_manager_for_testing_json_corruption_protection( $uuid );
1122+
$wp_customize->set_post_value( 'custom_html_1', $options['custom_html_1'] );
1123+
$wp_customize->save_changeset_post(
1124+
array(
1125+
'status' => 'draft',
1126+
)
1127+
);
1128+
1129+
// Populate setting as user who cannot bypass content_save_pre filter.
1130+
wp_set_current_user( $lesser_admin_user_id );
1131+
$wp_customize = $this->get_manager_for_testing_json_corruption_protection( $uuid );
1132+
$wp_customize->set_post_value( 'custom_html_2', $options['custom_html_2'] );
1133+
$wp_customize->save_changeset_post(
1134+
array(
1135+
'autosave' => true,
1136+
)
1137+
);
1138+
1139+
/*
1140+
* Ensure that the unsanitized value (the "POST data") is preserved in the autosave revision.
1141+
* The value is sent through the sanitize function when it is read from the changeset.
1142+
*/
1143+
$autosave_revision = wp_get_post_autosave( $wp_customize->changeset_post_id(), get_current_user_id() );
1144+
$saved_data = json_decode( $autosave_revision->post_content, true );
1145+
$this->assertEquals( $options['custom_html_1'], $saved_data['custom_html_1']['value'] );
1146+
$this->assertEquals( $options['custom_html_2'], $saved_data['custom_html_2']['value'] );
1147+
1148+
// Update post to discard autosave.
1149+
$wp_customize->save_changeset_post(
1150+
array(
1151+
'status' => 'draft',
1152+
)
1153+
);
1154+
1155+
/*
1156+
* Ensure that the unsanitized value (the "POST data") is preserved in the post content.
1157+
* The value is sent through the sanitize function when it is read from the changeset.
1158+
*/
1159+
$wp_customize = $this->get_manager_for_testing_json_corruption_protection( $uuid );
1160+
$saved_data = json_decode( get_post( $wp_customize->changeset_post_id() )->post_content, true );
1161+
$this->assertEquals( $options['custom_html_1'], $saved_data['custom_html_1']['value'] );
1162+
$this->assertEquals( $options['custom_html_2'], $saved_data['custom_html_2']['value'] );
1163+
1164+
/*
1165+
* Ensure that the unsanitized value (the "POST data") is preserved in the revisions' content.
1166+
* The value is sent through the sanitize function when it is read from the changeset.
1167+
*/
1168+
$revisions = wp_get_post_revisions( $wp_customize->changeset_post_id() );
1169+
$revision = array_shift( $revisions );
1170+
$saved_data = json_decode( $revision->post_content, true );
1171+
$this->assertEquals( $options['custom_html_1'], $saved_data['custom_html_1']['value'] );
1172+
$this->assertEquals( $options['custom_html_2'], $saved_data['custom_html_2']['value'] );
1173+
1174+
/*
1175+
* Now when publishing the changeset, the unsanitized values will be read from the changeset
1176+
* and sanitized according to the capabilities of the users who originally updated each
1177+
* setting in the changeset to begin with.
1178+
*/
1179+
wp_set_current_user( $lesser_admin_user_id );
1180+
$wp_customize = $this->get_manager_for_testing_json_corruption_protection( $uuid );
1181+
$wp_customize->set_post_value( 'custom_html_3', $options['custom_html_3'] );
1182+
$wp_customize->save_changeset_post(
1183+
array(
1184+
'status' => 'publish',
1185+
)
1186+
);
1187+
1188+
// User saved as one who can bypass content_save_pre filter.
1189+
$this->assertContains( '<script>', get_option( 'custom_html_1' ) );
1190+
$this->assertContains( 'Wordpress', get_option( 'custom_html_1' ) ); // phpcs:ignore WordPress.WP.CapitalPDangit.Misspelled
1191+
1192+
// User saved as one who cannot bypass content_save_pre filter.
1193+
$this->assertNotContains( '<script>', get_option( 'custom_html_2' ) );
1194+
$this->assertContains( 'WordPress', get_option( 'custom_html_2' ) );
1195+
1196+
// User saved as one who also cannot bypass content_save_pre filter.
1197+
$this->assertNotContains( '<script>', get_option( 'custom_html_3' ) );
1198+
$this->assertContains( 'WordPress', get_option( 'custom_html_3' ) );
1199+
}
1200+
1201+
/**
1202+
* Get a manager for testing JSON corruption protection.
1203+
*
1204+
* @param string $uuid UUID.
1205+
* @return WP_Customize_Manager Manager.
1206+
*/
1207+
private function get_manager_for_testing_json_corruption_protection( $uuid ) {
1208+
global $wp_customize;
1209+
$wp_customize = new WP_Customize_Manager(
1210+
array(
1211+
'changeset_uuid' => $uuid,
1212+
)
1213+
);
1214+
for ( $i = 0; $i < 5; $i++ ) {
1215+
$wp_customize->add_setting(
1216+
sprintf( 'custom_html_%d', $i ),
1217+
array(
1218+
'type' => 'option',
1219+
'sanitize_callback' => array( $this, 'apply_content_save_pre_filters_if_not_main_admin_user' ),
1220+
)
1221+
);
1222+
}
1223+
return $wp_customize;
1224+
}
1225+
1226+
/**
1227+
* Sanitize content with Kses if the current user is not the main admin.
1228+
*
1229+
* @since 5.4.1
1230+
*
1231+
* @param string $content Content to sanitize.
1232+
* @return string Sanitized content.
1233+
*/
1234+
public function apply_content_save_pre_filters_if_not_main_admin_user( $content ) {
1235+
if ( get_current_user_id() !== self::$admin_user_id ) {
1236+
$content = apply_filters( 'content_save_pre', $content );
1237+
}
1238+
return $content;
1239+
}
1240+
1241+
/**
1242+
* Filter map_meta_cap to disallow unfiltered_html.
1243+
*
1244+
* @since 5.4.1
1245+
*
1246+
* @param array $caps User's capabilities.
1247+
* @param string $cap Requested cap.
1248+
* @return array Caps.
1249+
*/
1250+
public function filter_map_meta_cap_to_disallow_unfiltered_html( $caps, $cap ) {
1251+
if ( 'unfiltered_html' === $cap ) {
1252+
$caps = array( 'do_not_allow' );
1253+
}
1254+
return $caps;
1255+
}
1256+
10921257
/**
10931258
* Call count for customize_changeset_save_data filter.
10941259
*

tests/phpunit/tests/formatting/SanitizeFileName.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,20 @@ function test_replaces_unnammed_file_extensionless() {
6767
// Test a filenames that becomes extensionless.
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
}

0 commit comments

Comments
 (0)