Skip to content

Commit fb09f6d

Browse files
committed
Editor: Remove unwanted fields before saving posts.
The `meta_input`, `file`, and `guid` fields are not intended to be updated through user input. Merges [44047] to the 4.9 branch. git-svn-id: https://develop.svn.wordpress.org/branches/4.9@44053 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 07c82a2 commit fb09f6d

File tree

3 files changed

+41
-15
lines changed

3 files changed

+41
-15
lines changed

src/wp-admin/includes/ajax-actions.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2105,7 +2105,11 @@ function wp_ajax_upload_attachment() {
21052105
$post_id = null;
21062106
}
21072107

2108-
$post_data = isset( $_REQUEST['post_data'] ) ? $_REQUEST['post_data'] : array();
2108+
$post_data = ! empty( $_REQUEST['post_data'] ) ? _wp_get_allowed_postdata( _wp_translate_postdata( false, (array) $_REQUEST['post_data'] ) ) : array();
2109+
2110+
if ( is_wp_error( $post_data ) ) {
2111+
wp_die( $post_data->get_error_message() );
2112+
}
21092113

21102114
// If the context is custom header or background, make sure the uploaded file is an image.
21112115
if ( isset( $post_data['context'] ) && in_array( $post_data['context'], array( 'custom-header', 'custom-background' ) ) ) {

src/wp-admin/includes/post.php

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,27 @@ function _wp_translate_postdata( $update = false, $post_data = null ) {
174174
return $post_data;
175175
}
176176

177+
/**
178+
* Returns only allowed post data fields
179+
*
180+
* @since 4.9.9
181+
*
182+
* @param array $post_data Array of post data. Defaults to the contents of $_POST.
183+
* @return object|bool WP_Error on failure, true on success.
184+
*/
185+
function _wp_get_allowed_postdata( $post_data = null ) {
186+
if ( empty( $post_data ) ) {
187+
$post_data = $_POST;
188+
}
189+
190+
// Pass through errors
191+
if ( is_wp_error( $post_data ) ) {
192+
return $post_data;
193+
}
194+
195+
return array_diff_key( $post_data, array_flip( array( 'meta_input', 'file', 'guid' ) ) );
196+
}
197+
177198
/**
178199
* Update an existing post with values provided in $_POST.
179200
*
@@ -242,6 +263,7 @@ function edit_post( $post_data = null ) {
242263
$post_data = _wp_translate_postdata( true, $post_data );
243264
if ( is_wp_error($post_data) )
244265
wp_die( $post_data->get_error_message() );
266+
$translated = _wp_get_allowed_postdata( $post_data );
245267

246268
// Post Formats
247269
if ( isset( $post_data['post_format'] ) )
@@ -321,7 +343,7 @@ function edit_post( $post_data = null ) {
321343
$attachment_data = isset( $post_data['attachments'][ $post_ID ] ) ? $post_data['attachments'][ $post_ID ] : array();
322344

323345
/** This filter is documented in wp-admin/includes/media.php */
324-
$post_data = apply_filters( 'attachment_fields_to_save', $post_data, $attachment_data );
346+
$translated = apply_filters( 'attachment_fields_to_save', $translated, $attachment_data );
325347
}
326348

327349
// Convert taxonomy input to term IDs, to avoid ambiguity.
@@ -366,26 +388,26 @@ function edit_post( $post_data = null ) {
366388
}
367389
}
368390

369-
$post_data['tax_input'][ $taxonomy ] = $clean_terms;
391+
$translated['tax_input'][ $taxonomy ] = $clean_terms;
370392
}
371393
}
372394

373395
add_meta( $post_ID );
374396

375397
update_post_meta( $post_ID, '_edit_last', get_current_user_id() );
376398

377-
$success = wp_update_post( $post_data );
399+
$success = wp_update_post( $translated );
378400
// If the save failed, see if we can sanity check the main fields and try again
379401
if ( ! $success && is_callable( array( $wpdb, 'strip_invalid_text_for_column' ) ) ) {
380402
$fields = array( 'post_title', 'post_content', 'post_excerpt' );
381403

382404
foreach ( $fields as $field ) {
383-
if ( isset( $post_data[ $field ] ) ) {
384-
$post_data[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->posts, $field, $post_data[ $field ] );
405+
if ( isset( $translated[ $field ] ) ) {
406+
$translated[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->posts, $field, $translated[ $field ] );
385407
}
386408
}
387409

388-
wp_update_post( $post_data );
410+
wp_update_post( $translated );
389411
}
390412

391413
// Now that we have an ID we can fix any attachment anchor hrefs
@@ -545,27 +567,25 @@ function bulk_edit_posts( $post_data = null ) {
545567
unset( $post_data['tax_input']['category'] );
546568
}
547569

570+
$post_data['post_ID'] = $post_ID;
548571
$post_data['post_type'] = $post->post_type;
549572
$post_data['post_mime_type'] = $post->post_mime_type;
550-
$post_data['guid'] = $post->guid;
551573

552574
foreach ( array( 'comment_status', 'ping_status', 'post_author' ) as $field ) {
553575
if ( ! isset( $post_data[ $field ] ) ) {
554576
$post_data[ $field ] = $post->$field;
555577
}
556578
}
557579

558-
$post_data['ID'] = $post_ID;
559-
$post_data['post_ID'] = $post_ID;
560-
561580
$post_data = _wp_translate_postdata( true, $post_data );
562581
if ( is_wp_error( $post_data ) ) {
563582
$skipped[] = $post_ID;
564583
continue;
565584
}
585+
$post_data = _wp_get_allowed_postdata( $post_data );
566586

567-
if ( isset( $post_data['post_format'] ) ) {
568-
set_post_format( $post_ID, $post_data['post_format'] );
587+
if ( isset( $shared_post_data['post_format'] ) ) {
588+
set_post_format( $post_ID, $shared_post_data['post_format'] );
569589
unset( $post_data['tax_input']['post_format'] );
570590
}
571591

@@ -757,9 +777,10 @@ function wp_write_post() {
757777
$translated = _wp_translate_postdata( false );
758778
if ( is_wp_error($translated) )
759779
return $translated;
780+
$translated = _wp_get_allowed_postdata( $translated );
760781

761782
// Create the post.
762-
$post_ID = wp_insert_post( $_POST );
783+
$post_ID = wp_insert_post( $translated );
763784
if ( is_wp_error( $post_ID ) )
764785
return $post_ID;
765786

@@ -1685,6 +1706,7 @@ function wp_create_post_autosave( $post_data ) {
16851706
$post_data = _wp_translate_postdata( true, $post_data );
16861707
if ( is_wp_error( $post_data ) )
16871708
return $post_data;
1709+
$post_data = _wp_get_allowed_postdata( $post_data );
16881710

16891711
$post_author = get_current_user_id();
16901712

src/wp-admin/post.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@
190190

191191
// Update the thumbnail filename
192192
$newmeta = wp_get_attachment_metadata( $post_id, true );
193-
$newmeta['thumb'] = $_POST['thumb'];
193+
$newmeta['thumb'] = wp_basename( $_POST['thumb'] );
194194

195195
wp_update_attachment_metadata( $post_id, $newmeta );
196196

0 commit comments

Comments
 (0)