From 5d9e922deaea27fac20e417ce5392cc5e832fc52 Mon Sep 17 00:00:00 2001 From: audrasjb Date: Mon, 17 Oct 2022 17:57:59 +0000 Subject: [PATCH] Grouped backports to the 5.6 branch. - Editor: Bump @wordpress packages for the branch, - Media: Refactor search by filename within the admin, - REST API: Lockdown post parameter of the terms endpoint, - Customize: Escape blogname option in underscores templates, - Query: Validate relation in `WP_Date_Query`, - Posts, Post types: Apply KSES to post-by-email content, - General: Validate host on "Are you sure?" screen, - Posts, Post types: Remove emails from post-by-email logs, - Pings/trackbacks: Apply KSES to all trackbacks, - Mail: Reset PHPMailer properties between use, - Comments: Apply kses when editing comments, - Widgets: Escape RSS error messages for display. Merges [54521-54530] to the 5.6 branch. Props audrasjb, costdev, cu121, dd32, davidbaumwald, ehtis, johnbillion, johnjamesjacoby, martinkrcho, matveb, oztaser, paulkevan, peterwilsoncc, ravipatel, SergeyBiryukov, talldanwp, timothyblynjacobs, tykoted, voldemortensen, vortfu, xknown. Built from https://develop.svn.wordpress.org/branches/5.6@54555 git-svn-id: http://core.svn.wordpress.org/branches/5.6@54110 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/ajax-actions.php | 2 +- wp-admin/includes/post.php | 2 +- wp-includes/class-wp-date-query.php | 23 +++- wp-includes/class-wp-query.php | 30 ++++- wp-includes/comment.php | 13 ++ ...lass-wp-customize-header-image-control.php | 6 +- .../class-wp-customize-site-icon-control.php | 2 +- wp-includes/deprecated.php | 18 +++ wp-includes/functions.php | 4 +- wp-includes/media-template.php | 2 +- wp-includes/pluggable.php | 2 + wp-includes/post.php | 126 +++++++++++++----- .../class-wp-rest-attachments-controller.php | 2 +- .../class-wp-rest-terms-controller.php | 53 ++++++++ wp-includes/widgets.php | 4 +- wp-mail.php | 5 +- wp-trackback.php | 3 + 17 files changed, 249 insertions(+), 48 deletions(-) diff --git a/wp-admin/includes/ajax-actions.php b/wp-admin/includes/ajax-actions.php index 6ff2d2d9fd..016a0da028 100644 --- a/wp-admin/includes/ajax-actions.php +++ b/wp-admin/includes/ajax-actions.php @@ -2975,7 +2975,7 @@ function wp_ajax_query_attachments() { // Filter query clauses to include filenames. if ( isset( $query['s'] ) ) { - add_filter( 'posts_clauses', '_filter_query_attachment_filenames' ); + add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' ); } /** diff --git a/wp-admin/includes/post.php b/wp-admin/includes/post.php index fbf11b3ded..356e4e0ed8 100644 --- a/wp-admin/includes/post.php +++ b/wp-admin/includes/post.php @@ -1269,7 +1269,7 @@ function wp_edit_attachments_query_vars( $q = false ) { // Filter query clauses to include filenames. if ( isset( $q['s'] ) ) { - add_filter( 'posts_clauses', '_filter_query_attachment_filenames' ); + add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' ); } return $q; diff --git a/wp-includes/class-wp-date-query.php b/wp-includes/class-wp-date-query.php index 1bab5ef2c8..fb33b00e4f 100644 --- a/wp-includes/class-wp-date-query.php +++ b/wp-includes/class-wp-date-query.php @@ -149,8 +149,8 @@ class WP_Date_Query { return; } - if ( isset( $date_query['relation'] ) && 'OR' === strtoupper( $date_query['relation'] ) ) { - $this->relation = 'OR'; + if ( isset( $date_query['relation'] ) ) { + $this->relation = $this->sanitize_relation( $date_query['relation'] ); } else { $this->relation = 'AND'; } @@ -219,6 +219,9 @@ class WP_Date_Query { $this->validate_date_values( $queries ); } + // Sanitize the relation parameter. + $queries['relation'] = $this->sanitize_relation( $queries['relation'] ); + foreach ( $queries as $key => $q ) { if ( ! is_array( $q ) || in_array( $key, $this->time_keys, true ) ) { // This is a first-order query. Trust the values and sanitize when building SQL. @@ -1039,4 +1042,20 @@ class WP_Date_Query { return $wpdb->prepare( "DATE_FORMAT( $column, %s ) $compare %f", $format, $time ); } + + /** + * Sanitizes a 'relation' operator. + * + * @since 6.0.3 + * + * @param string $relation Raw relation key from the query argument. + * @return string Sanitized relation ('AND' or 'OR'). + */ + public function sanitize_relation( $relation ) { + if ( 'OR' === strtoupper( $relation ) ) { + return 'OR'; + } else { + return 'AND'; + } + } } diff --git a/wp-includes/class-wp-query.php b/wp-includes/class-wp-query.php index 4badca5447..579070a4a7 100644 --- a/wp-includes/class-wp-query.php +++ b/wp-includes/class-wp-query.php @@ -441,6 +441,13 @@ class WP_Query { */ public $thumbnails_cached = false; + /** + * Controls whether an attachment query should include filenames or not. + * + * @since 6.0.3 + * @var bool + */ + protected $allow_query_attachment_by_filename = false; /** * Cached list of search stopwords. * @@ -1391,7 +1398,12 @@ class WP_Query { } $like = $n . $wpdb->esc_like( $term ) . $n; - $search .= $wpdb->prepare( "{$searchand}(({$wpdb->posts}.post_title $like_op %s) $andor_op ({$wpdb->posts}.post_excerpt $like_op %s) $andor_op ({$wpdb->posts}.post_content $like_op %s))", $like, $like, $like ); + + if ( ! empty( $this->allow_query_attachment_by_filename ) ) { + $search .= $wpdb->prepare( "{$searchand}(({$wpdb->posts}.post_title $like_op %s) $andor_op ({$wpdb->posts}.post_excerpt $like_op %s) $andor_op ({$wpdb->posts}.post_content $like_op %s) $andor_op (sq1.meta_value $like_op %s))", $like, $like, $like, $like ); + } else { + $search .= $wpdb->prepare( "{$searchand}(({$wpdb->posts}.post_title $like_op %s) $andor_op ({$wpdb->posts}.post_excerpt $like_op %s) $andor_op ({$wpdb->posts}.post_content $like_op %s))", $like, $like, $like ); + } $searchand = ' AND '; } @@ -1784,6 +1796,16 @@ class WP_Query { // Fill again in case 'pre_get_posts' unset some vars. $q = $this->fill_query_vars( $q ); + /** + * Filters whether an attachment query should include filenames or not. + * + * @since 6.0.3 + * + * @param bool $allow_query_attachment_by_filename Whether or not to include filenames. + */ + $this->allow_query_attachment_by_filename = apply_filters( 'wp_allow_query_attachment_by_filename', false ); + remove_all_filters( 'wp_allow_query_attachment_by_filename' ); + // Parse meta query. $this->meta_query = new WP_Meta_Query(); $this->meta_query->parse_query_vars( $q ); @@ -2215,7 +2237,7 @@ class WP_Query { } } - if ( ! empty( $this->tax_query->queries ) || ! empty( $this->meta_query->queries ) ) { + if ( ! empty( $this->tax_query->queries ) || ! empty( $this->meta_query->queries ) || ! empty( $this->allow_query_attachment_by_filename ) ) { $groupby = "{$wpdb->posts}.ID"; } @@ -2292,6 +2314,10 @@ class WP_Query { } $where .= $search . $whichauthor . $whichmimetype; + if ( ! empty( $this->allow_query_attachment_by_filename ) ) { + $join .= " LEFT JOIN {$wpdb->postmeta} AS sq1 ON ( {$wpdb->posts}.ID = sq1.post_id AND sq1.meta_key = '_wp_attached_file' )"; + } + if ( ! empty( $this->meta_query->queries ) ) { $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this ); $join .= $clauses['join']; diff --git a/wp-includes/comment.php b/wp-includes/comment.php index ac37540be0..b798c3148f 100644 --- a/wp-includes/comment.php +++ b/wp-includes/comment.php @@ -2459,6 +2459,15 @@ function wp_update_comment( $commentarr, $wp_error = false ) { } } + $filter_comment = false; + if ( ! has_filter( 'pre_comment_content', 'wp_filter_kses' ) ) { + $filter_comment = ! user_can( isset( $comment['user_id'] ) ? $comment['user_id'] : 0, 'unfiltered_html' ); + } + + if ( $filter_comment ) { + add_filter( 'pre_comment_content', 'wp_filter_kses' ); + } + // Escape data pulled from DB. $comment = wp_slash( $comment ); @@ -2469,6 +2478,10 @@ function wp_update_comment( $commentarr, $wp_error = false ) { $commentarr = wp_filter_comment( $commentarr ); + if ( $filter_comment ) { + remove_filter( 'pre_comment_content', 'wp_filter_kses' ); + } + // Now extract the merged array. $data = wp_unslash( $commentarr ); diff --git a/wp-includes/customize/class-wp-customize-header-image-control.php b/wp-includes/customize/class-wp-customize-header-image-control.php index 82c1714261..701b9ea102 100644 --- a/wp-includes/customize/class-wp-customize-header-image-control.php +++ b/wp-includes/customize/class-wp-customize-header-image-control.php @@ -130,10 +130,10 @@ class WP_Customize_Header_Image_Control extends WP_Customize_Image_Control { <# } else { #> <# if ( data.type === 'uploaded' ) { #> @@ -158,7 +158,7 @@ class WP_Customize_Header_Image_Control extends WP_Customize_Image_Control { <# } else { #> - {{{data.header.alt_text || data.header.description}}} + {{data.header.alt_text || data.header.description}} <# } #> <# } else { #> diff --git a/wp-includes/customize/class-wp-customize-site-icon-control.php b/wp-includes/customize/class-wp-customize-site-icon-control.php index d21395c39c..74823713dd 100644 --- a/wp-includes/customize/class-wp-customize-site-icon-control.php +++ b/wp-includes/customize/class-wp-customize-site-icon-control.php @@ -68,7 +68,7 @@ class WP_Customize_Site_Icon_Control extends WP_Customize_Cropped_Image_Control
<?php esc_attr_e( 'Preview as a browser icon' ); ?>
- + <?php esc_attr_e( 'Preview as an app icon' ); ?> diff --git a/wp-includes/deprecated.php b/wp-includes/deprecated.php index 09d82f872f..f23298cf93 100644 --- a/wp-includes/deprecated.php +++ b/wp-includes/deprecated.php @@ -4135,3 +4135,21 @@ function wp_slash_strings_only( $value ) { function addslashes_strings_only( $value ) { return is_string( $value ) ? addslashes( $value ) : $value; } + +/** + * Filter the SQL clauses of an attachment query to include filenames. + * + * @since 4.7.0 + * @deprecated 6.0.3 + * @access private + * + * @param array $clauses An array including WHERE, GROUP BY, JOIN, ORDER BY, + * DISTINCT, fields (SELECT), and LIMITS clauses. + * @return array The unmodified clauses. + */ +function _filter_query_attachment_filenames( $clauses ) { + _deprecated_function( __FUNCTION__, '6.0.3', 'add_filter( "wp_allow_query_attachment_by_filename", "__return_true" )'); + remove_filter( 'posts_clauses', __FUNCTION__ ); + return $clauses; +} + diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 8ae35dac85..ea6ec03372 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -3287,10 +3287,12 @@ function wp_nonce_ays( $action ) { } else { $html = __( 'The link you followed has expired.' ); if ( wp_get_referer() ) { + $wp_http_referer = remove_query_arg( 'updated', wp_get_referer() ); + $wp_http_referer = wp_validate_redirect( esc_url_raw( $wp_http_referer ) ); $html .= '

'; $html .= sprintf( '%s', - esc_url( remove_query_arg( 'updated', wp_get_referer() ) ), + esc_url( $wp_http_referer ), __( 'Please try again.' ) ); } diff --git a/wp-includes/media-template.php b/wp-includes/media-template.php index 2b887c475a..2db8df6fa9 100644 --- a/wp-includes/media-template.php +++ b/wp-includes/media-template.php @@ -1493,7 +1493,7 @@ function wp_print_media_templates() {

<?php esc_attr_e( 'Preview as a browser icon' ); ?>
- + diff --git a/wp-includes/pluggable.php b/wp-includes/pluggable.php index 68187fcf05..219dca52cb 100644 --- a/wp-includes/pluggable.php +++ b/wp-includes/pluggable.php @@ -317,6 +317,8 @@ if ( ! function_exists( 'wp_mail' ) ) : $phpmailer->clearAttachments(); $phpmailer->clearCustomHeaders(); $phpmailer->clearReplyTos(); + $phpmailer->Body = ''; + $phpmailer->AltBody = ''; // Set "From" name and email. diff --git a/wp-includes/post.php b/wp-includes/post.php index ec63c17865..bd04be41e5 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -1987,6 +1987,7 @@ function set_post_type( $post_id = 0, $post_type = 'post' ) { * @since 4.4.0 * @since 4.5.0 Added the ability to pass a post type name in addition to object. * @since 4.6.0 Converted the `$post_type` parameter to accept a `WP_Post_Type` object. + * @since 5.9.0 Added `is_post_type_viewable` hook to filter the result. * * @param string|WP_Post_Type $post_type Post type name or object. * @return bool Whether the post type should be considered viewable. @@ -1994,12 +1995,105 @@ function set_post_type( $post_id = 0, $post_type = 'post' ) { function is_post_type_viewable( $post_type ) { if ( is_scalar( $post_type ) ) { $post_type = get_post_type_object( $post_type ); + if ( ! $post_type ) { return false; } } - return $post_type->publicly_queryable || ( $post_type->_builtin && $post_type->public ); + if ( ! is_object( $post_type ) ) { + return false; + } + + $is_viewable = $post_type->publicly_queryable || ( $post_type->_builtin && $post_type->public ); + + /** + * Filters whether a post type is considered "viewable". + * + * The returned filtered value must be a boolean type to ensure + * `is_post_type_viewable()` only returns a boolean. This strictness + * is by design to maintain backwards-compatibility and guard against + * potential type errors in PHP 8.1+. Non-boolean values (even falsey + * and truthy values) will result in the function returning false. + * + * @since 5.9.0 + * + * @param bool $is_viewable Whether the post type is "viewable" (strict type). + * @param WP_Post_Type $post_type Post type object. + */ + return true === apply_filters( 'is_post_type_viewable', $is_viewable, $post_type ); +} + +/** + * Determines whether a post status is considered "viewable". + * + * For built-in post statuses such as publish and private, the 'public' value will be evaluated. + * For all others, the 'publicly_queryable' value will be used. + * + * @since 5.7.0 + * @since 5.9.0 Added `is_post_status_viewable` hook to filter the result. + * + * @param string|stdClass $post_status Post status name or object. + * @return bool Whether the post status should be considered viewable. + */ +function is_post_status_viewable( $post_status ) { + if ( is_scalar( $post_status ) ) { + $post_status = get_post_status_object( $post_status ); + + if ( ! $post_status ) { + return false; + } + } + + if ( + ! is_object( $post_status ) || + $post_status->internal || + $post_status->protected + ) { + return false; + } + + $is_viewable = $post_status->publicly_queryable || ( $post_status->_builtin && $post_status->public ); + + /** + * Filters whether a post status is considered "viewable". + * + * The returned filtered value must be a boolean type to ensure + * `is_post_status_viewable()` only returns a boolean. This strictness + * is by design to maintain backwards-compatibility and guard against + * potential type errors in PHP 8.1+. Non-boolean values (even falsey + * and truthy values) will result in the function returning false. + * + * @since 5.9.0 + * + * @param bool $is_viewable Whether the post status is "viewable" (strict type). + * @param stdClass $post_status Post status object. + */ + return true === apply_filters( 'is_post_status_viewable', $is_viewable, $post_status ); +} + +/** + * Determines whether a post is publicly viewable. + * + * Posts are considered publicly viewable if both the post status and post type + * are viewable. + * + * @since 5.7.0 + * + * @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post. + * @return bool Whether the post is publicly viewable. + */ +function is_post_publicly_viewable( $post = null ) { + $post = get_post( $post ); + + if ( ! $post ) { + return false; + } + + $post_type = get_post_type( $post ); + $post_status = get_post_status( $post ); + + return is_post_type_viewable( $post_type ) && is_post_status_viewable( $post_status ); } /** @@ -7474,36 +7568,6 @@ function wp_add_trashed_suffix_to_post_name_for_post( $post ) { return $post_name; } -/** - * Filters the SQL clauses of an attachment query to include filenames. - * - * @since 4.7.0 - * @access private - * - * @global wpdb $wpdb WordPress database abstraction object. - * - * @param string[] $clauses An array including WHERE, GROUP BY, JOIN, ORDER BY, - * DISTINCT, fields (SELECT), and LIMITS clauses. - * @return string[] The modified array of clauses. - */ -function _filter_query_attachment_filenames( $clauses ) { - global $wpdb; - remove_filter( 'posts_clauses', __FUNCTION__ ); - - // Add a LEFT JOIN of the postmeta table so we don't trample existing JOINs. - $clauses['join'] .= " LEFT JOIN {$wpdb->postmeta} AS sq1 ON ( {$wpdb->posts}.ID = sq1.post_id AND sq1.meta_key = '_wp_attached_file' )"; - - $clauses['groupby'] = "{$wpdb->posts}.ID"; - - $clauses['where'] = preg_replace( - "/\({$wpdb->posts}.post_content (NOT LIKE|LIKE) (\'[^']+\')\)/", - '$0 OR ( sq1.meta_value $1 $2 )', - $clauses['where'] - ); - - return $clauses; -} - /** * Sets the last changed time for the 'posts' cache group. * diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php index 3eb0490960..82531a9969 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php @@ -89,7 +89,7 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { // Filter query clauses to include filenames. if ( isset( $query_args['s'] ) ) { - add_filter( 'posts_clauses', '_filter_query_attachment_filenames' ); + add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' ); } return $query_args; diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php index ddc6380873..20e0e9da72 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php @@ -134,6 +134,35 @@ class WP_REST_Terms_Controller extends WP_REST_Controller { ); } + /** + * Checks if the terms for a post can be read. + * + * @since 6.0.3 + * + * @param WP_Post $post Post object. + * @param WP_REST_Request $request Full details about the request. + * @return bool Whether the terms for the post can be read. + */ + public function check_read_terms_permission_for_post( $post, $request ) { + // If the requested post isn't associated with this taxonomy, deny access. + if ( ! is_object_in_taxonomy( $post->post_type, $this->taxonomy ) ) { + return false; + } + + // Grant access if the post is publicly viewable. + if ( is_post_publicly_viewable( $post ) ) { + return true; + } + + // Otherwise grant access if the post is readable by the logged in user. + if ( current_user_can( 'read_post', $post->ID ) ) { + return true; + } + + // Otherwise, deny access. + return false; + } + /** * Checks if a request has access to read terms in the specified taxonomy. * @@ -157,6 +186,30 @@ class WP_REST_Terms_Controller extends WP_REST_Controller { ); } + if ( ! empty( $request['post'] ) ) { + $post = get_post( $request['post'] ); + + if ( ! $post ) { + return new WP_Error( + 'rest_post_invalid_id', + __( 'Invalid post ID.' ), + array( + 'status' => 400, + ) + ); + } + + if ( ! $this->check_read_terms_permission_for_post( $post, $request ) ) { + return new WP_Error( + 'rest_forbidden_context', + __( 'Sorry, you are not allowed to view terms for this post.' ), + array( + 'status' => rest_authorization_required_code(), + ) + ); + } + } + return true; } diff --git a/wp-includes/widgets.php b/wp-includes/widgets.php index db06222a51..7d8724369a 100644 --- a/wp-includes/widgets.php +++ b/wp-includes/widgets.php @@ -1522,7 +1522,7 @@ function wp_widget_rss_output( $rss, $args = array() ) { if ( is_wp_error( $rss ) ) { if ( is_admin() || current_user_can( 'manage_options' ) ) { - echo '

' . __( 'RSS Error:' ) . ' ' . $rss->get_error_message() . '

'; + echo '

' . __( 'RSS Error:' ) . ' ' . esc_html( $rss->get_error_message() ) . '

'; } return; } @@ -1645,7 +1645,7 @@ function wp_widget_rss_form( $args, $inputs = null ) { $args['show_date'] = isset( $args['show_date'] ) ? (int) $args['show_date'] : (int) $inputs['show_date']; if ( ! empty( $args['error'] ) ) { - echo '

' . __( 'RSS Error:' ) . ' ' . $args['error'] . '

'; + echo '

' . __( 'RSS Error:' ) . ' ' . esc_html( $args['error'] ) . '

'; } $esc_number = esc_attr( $args['number'] ); diff --git a/wp-mail.php b/wp-mail.php index 1d5fbedf03..9b93176320 100644 --- a/wp-mail.php +++ b/wp-mail.php @@ -65,6 +65,9 @@ if ( 0 === $count ) { wp_die( __( 'There doesn’t seem to be any new mail.' ) ); } +// Always run as an unauthenticated user. +wp_set_current_user( 0 ); + for ( $i = 1; $i <= $count; $i++ ) { $message = $pop3->get( $i ); @@ -131,8 +134,6 @@ for ( $i = 1; $i <= $count; $i++ ) { } $author = sanitize_email( $author ); if ( is_email( $author ) ) { - /* translators: %s: Post author email address. */ - echo '

' . sprintf( __( 'Author is %s' ), $author ) . '

'; $userdata = get_user_by( 'email', $author ); if ( ! empty( $userdata ) ) { $post_author = $userdata->ID; diff --git a/wp-trackback.php b/wp-trackback.php index 3d4f6056f5..4757727a58 100644 --- a/wp-trackback.php +++ b/wp-trackback.php @@ -13,6 +13,9 @@ if ( empty( $wp ) ) { wp( array( 'tb' => '1' ) ); } +// Always run as an unauthenticated user. +wp_set_current_user( 0 ); + /** * Response to a trackback. *