diff --git a/wp-admin/includes/ajax-actions.php b/wp-admin/includes/ajax-actions.php
index e553f9cf65..e7c933c046 100644
--- a/wp-admin/includes/ajax-actions.php
+++ b/wp-admin/includes/ajax-actions.php
@@ -2987,13 +2987,29 @@ function wp_ajax_parse_media_shortcode() {
$shortcode = wp_unslash( $_POST['shortcode'] );
+ // Only process previews for media related shortcodes:
+ $found_shortcodes = get_shortcode_tags_in_content( $shortcode );
+ $media_shortcodes = array(
+ 'audio',
+ 'embed',
+ 'playlist',
+ 'video',
+ 'gallery',
+ );
+
+ $other_shortcodes = array_diff( $found_shortcodes, $media_shortcodes );
+
+ if ( ! empty( $other_shortcodes ) ) {
+ wp_send_json_error();
+ }
+
if ( ! empty( $_POST['post_ID'] ) ) {
$post = get_post( (int) $_POST['post_ID'] );
}
// the embed shortcode requires a post
if ( ! $post || ! current_user_can( 'edit_post', $post->ID ) ) {
- if ( 'embed' === $shortcode ) {
+ if ( in_array( 'embed', $found_shortcodes, true ) ) {
wp_send_json_error();
}
} else {
diff --git a/wp-admin/includes/class-wp-comments-list-table.php b/wp-admin/includes/class-wp-comments-list-table.php
index a5f96939d5..e4fb62ffaf 100644
--- a/wp-admin/includes/class-wp-comments-list-table.php
+++ b/wp-admin/includes/class-wp-comments-list-table.php
@@ -495,6 +495,19 @@ class WP_Comments_List_Table extends WP_List_Table {
}
$this->user_can = current_user_can( 'edit_comment', $comment->comment_ID );
+ $edit_post_cap = $post ? 'edit_post' : 'edit_posts';
+ if (
+ current_user_can( $edit_post_cap, $comment->comment_post_ID ) ||
+ (
+ empty( $post->post_password ) &&
+ current_user_can( 'read_post', $comment->comment_post_ID )
+ )
+ ) {
+ // The user has access to the post
+ } else {
+ return false;
+ }
+
echo "
\n";
diff --git a/wp-admin/includes/class-wp-list-table.php b/wp-admin/includes/class-wp-list-table.php
index 32d0883947..f96bab0564 100644
--- a/wp-admin/includes/class-wp-list-table.php
+++ b/wp-admin/includes/class-wp-list-table.php
@@ -655,7 +655,20 @@ class WP_List_Table {
$approved_phrase = sprintf( _n( '%s approved comment', '%s approved comments', $approved_comments ), $approved_comments_number );
$pending_phrase = sprintf( _n( '%s pending comment', '%s pending comments', $pending_comments ), $pending_comments_number );
- // No comments at all.
+ $post_object = get_post( $post_id );
+ $edit_post_cap = $post_object ? 'edit_post' : 'edit_posts';
+ if (
+ current_user_can( $edit_post_cap, $post_id ) ||
+ (
+ empty( $post_object->post_password ) &&
+ current_user_can( 'read_post', $post_id )
+ )
+ ) {
+ // The user has access to the post and thus can see comments
+ } else {
+ return false;
+ }
+
if ( ! $approved_comments && ! $pending_comments ) {
printf( '—%s',
__( 'No comments' )
diff --git a/wp-admin/includes/dashboard.php b/wp-admin/includes/dashboard.php
index 5a234857d8..e60b3ca6ea 100644
--- a/wp-admin/includes/dashboard.php
+++ b/wp-admin/includes/dashboard.php
@@ -906,8 +906,18 @@ function wp_dashboard_recent_comments( $total_items = 5 ) {
echo '' . __( 'Recent Comments' ) . '
';
echo '';
if ( current_user_can( 'edit_posts' ) ) {
diff --git a/wp-includes/class-wp-theme.php b/wp-includes/class-wp-theme.php
index 6619b3ba79..e981b7691e 100644
--- a/wp-includes/class-wp-theme.php
+++ b/wp-includes/class-wp-theme.php
@@ -527,6 +527,28 @@ final class WP_Theme implements ArrayAccess {
return isset( $this->parent ) ? $this->parent : false;
}
+ /**
+ * Perform reinitialization tasks.
+ *
+ * Prevents a callback from being injected during unserialization of an object.
+ *
+ * @return void
+ */
+ public function __wakeup() {
+ if ( $this->parent && ! $this->parent instanceof self ) {
+ throw new UnexpectedValueException();
+ }
+ if ( $this->headers && ! is_array( $this->headers ) ) {
+ throw new UnexpectedValueException();
+ }
+ foreach ( $this->headers as $value ) {
+ if ( ! is_string( $value ) ) {
+ throw new UnexpectedValueException();
+ }
+ }
+ $this->headers_sanitized = array();
+ }
+
/**
* Adds theme data to cache.
*
@@ -1371,4 +1393,16 @@ final class WP_Theme implements ArrayAccess {
// Don't mark up; Do translate.
return strnatcasecmp( $a->display( 'Name', false, true ), $b->display( 'Name', false, true ) );
}
+
+ private static function _check_headers_property_has_correct_type( $headers ) {
+ if ( ! is_array( $headers ) ) {
+ return false;
+ }
+ foreach ( $headers as $key => $value ) {
+ if ( ! is_string( $key ) || ! is_string( $value ) ) {
+ return false;
+ }
+ }
+ return true;
+ }
}
diff --git a/wp-includes/media.php b/wp-includes/media.php
index 33ad2c1dce..2087364929 100644
--- a/wp-includes/media.php
+++ b/wp-includes/media.php
@@ -1633,11 +1633,24 @@ function gallery_shortcode( $attr ) {
$attachments[$val->ID] = $_attachments[$key];
}
} elseif ( ! empty( $atts['exclude'] ) ) {
+ $post_parent_id = $id;
$attachments = get_children( array( 'post_parent' => $id, 'exclude' => $atts['exclude'], 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) );
} else {
+ $post_parent_id = $id;
$attachments = get_children( array( 'post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) );
}
+ if ( ! empty( $post_parent_id ) ) {
+ $post_parent = get_post( $post_parent_id );
+
+ // terminate the shortcode execution if user cannot read the post or password-protected
+ if (
+ ( ! is_post_publicly_viewable( $post_parent->ID ) && ! current_user_can( 'read_post', $post_parent->ID ) )
+ || post_password_required( $post_parent ) ) {
+ return '';
+ }
+ }
+
if ( empty( $attachments ) ) {
return '';
}
@@ -1937,6 +1950,15 @@ function wp_playlist_shortcode( $attr ) {
$attachments = get_children( $args );
}
+ if ( ! empty( $args['post_parent'] ) ) {
+ $post_parent = get_post( $id );
+
+ // terminate the shortcode execution if user cannot read the post or password-protected
+ if ( ! current_user_can( 'read_post', $post_parent->ID ) || post_password_required( $post_parent ) ) {
+ return '';
+ }
+ }
+
if ( empty( $attachments ) ) {
return '';
}
diff --git a/wp-includes/rest-api.php b/wp-includes/rest-api.php
index 76ff1d7123..d994686b3a 100644
--- a/wp-includes/rest-api.php
+++ b/wp-includes/rest-api.php
@@ -592,6 +592,7 @@ function rest_cookie_check_errors( $result ) {
$result = wp_verify_nonce( $nonce, 'wp_rest' );
if ( ! $result ) {
+ add_filter( 'rest_send_nocache_headers', '__return_true', 20 );
return new WP_Error( 'rest_cookie_invalid_nonce', __( 'Cookie nonce is invalid' ), array( 'status' => 403 ) );
}
diff --git a/wp-includes/rest-api/class-wp-rest-server.php b/wp-includes/rest-api/class-wp-rest-server.php
index ebf58de2bb..fcf72d5abd 100644
--- a/wp-includes/rest-api/class-wp-rest-server.php
+++ b/wp-includes/rest-api/class-wp-rest-server.php
@@ -237,21 +237,7 @@ class WP_REST_Server {
$this->send_header( 'Access-Control-Allow-Headers', 'Authorization' );
/**
- * Send nocache headers on authenticated requests.
- *
- * @since 4.4.0
- *
- * @param bool $rest_send_nocache_headers Whether to send no-cache headers.
- */
- $send_no_cache_headers = apply_filters( 'rest_send_nocache_headers', is_user_logged_in() );
- if ( $send_no_cache_headers ) {
- foreach ( wp_get_nocache_headers() as $header => $header_value ) {
- $this->send_header( $header, $header_value );
- }
- }
-
- /**
- * Filter whether the REST API is enabled.
+ * Filters whether the REST API is enabled.
*
* @since 4.4.0
*
@@ -314,10 +300,12 @@ class WP_REST_Server {
* $_GET['_method']. If that is not set, we check for the HTTP_X_HTTP_METHOD_OVERRIDE
* header.
*/
+ $method_overridden = false;
if ( isset( $_GET['_method'] ) ) {
$request->set_method( $_GET['_method'] );
} elseif ( isset( $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] ) ) {
$request->set_method( $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] );
+ $method_overridden = true;
}
$result = $this->check_authentication();
@@ -376,6 +364,24 @@ class WP_REST_Server {
*/
$served = apply_filters( 'rest_pre_serve_request', false, $result, $request, $this );
+ /**
+ * Filters whether to send nocache headers on a REST API request.
+ *
+ * @since 4.4.0
+ * @since 6.x.x Moved the block to catch the filter added on rest_cookie_check_errors() from rest-api.php
+ *
+ * @param bool $rest_send_nocache_headers Whether to send no-cache headers.
+ */
+ $send_no_cache_headers = apply_filters( 'rest_send_nocache_headers', is_user_logged_in() );
+
+ // send no cache headers if the $send_no_cache_headers is true
+ // OR if the HTTP_X_HTTP_METHOD_OVERRIDE is used but resulted a 4xx response code.
+ if ( $send_no_cache_headers || ( true === $method_overridden && strpos( $code, '4' ) === 0 ) ) {
+ foreach ( wp_get_nocache_headers() as $header => $header_value ) {
+ $this->send_header( $header, $header_value );
+ }
+ }
+
if ( ! $served ) {
if ( 'HEAD' === $request->get_method() ) {
return null;
diff --git a/wp-includes/shortcodes.php b/wp-includes/shortcodes.php
index 132d63a5dc..7090605536 100644
--- a/wp-includes/shortcodes.php
+++ b/wp-includes/shortcodes.php
@@ -185,7 +185,45 @@ function has_shortcode( $content, $tag ) {
}
/**
- * Search content for shortcodes and filter shortcodes through their hooks.
+ * Returns a list of registered shortcode names found in the given content.
+ *
+ * Example usage:
+ *
+ * get_shortcode_tags_in_content( '[audio src="file.mp3"][/audio] [foo] [gallery ids="1,2,3"]' );
+ * // array( 'audio', 'gallery' )
+ *
+ * @since 6.3.2
+ *
+ * @param string $content The content to check.
+ * @return string[] An array of registered shortcode names found in the content.
+ */
+function get_shortcode_tags_in_content( $content ) {
+ if ( false === strpos( $content, '[' ) ) {
+ return array();
+ }
+
+ preg_match_all( '/' . get_shortcode_regex() . '/', $content, $matches, PREG_SET_ORDER );
+ if ( empty( $matches ) ) {
+ return array();
+ }
+
+ $tags = array();
+ foreach ( $matches as $shortcode ) {
+ $tags[] = $shortcode[2];
+
+ if ( ! empty( $shortcode[5] ) ) {
+ $deep_tags = get_shortcode_tags_in_content( $shortcode[5] );
+ if ( ! empty( $deep_tags ) ) {
+ $tags = array_merge( $tags, $deep_tags );
+ }
+ }
+ }
+
+ return $tags;
+}
+
+/**
+ * Searches content for shortcodes and filter shortcodes through their hooks.
*
* If there are no shortcode tags defined, then the content will be returned
* without any filtering. This might cause issues when plugins are disabled but