Media: Redirect inactive attachment pages for logged-out users.

Ensure logged out users are redirected to the media file when attachment pages are inactive. This removes the read_post capability check from the canonical redirects as anonymous users lack the permission.

This was previously committed in [57310] before being reverted in [57318]. This update includes a fix to cover instances where revealing a URL could be considered a data leak and greatly expands the unit tests to ensure that this is covered along with many other instances.

Follow-up to [56657], [56658], [56711], [57310], [57318].

Props peterwilsoncc, jorbin, afercia, aristath, chesio, joppuyo, jorbin, lakshmananphp, poena, sergeybiryukov, swissspidy, johnbillion.
Fixes #59866.
See #57913.

Built from https://develop.svn.wordpress.org/trunk@57357


git-svn-id: http://core.svn.wordpress.org/trunk@56863 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Aaron Jorbin 2024-01-25 17:36:15 +00:00
parent 9f3a76079b
commit 793b30bc30
2 changed files with 16 additions and 6 deletions

View File

@ -550,13 +550,23 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
$is_attachment_redirect = false; $is_attachment_redirect = false;
if ( is_attachment() && ! get_option( 'wp_attachment_pages_enabled' ) ) { if ( is_attachment() && ! get_option( 'wp_attachment_pages_enabled' ) ) {
$attachment_id = get_query_var( 'attachment_id' ); $attachment_id = get_query_var( 'attachment_id' );
$attachment_post = get_post( $attachment_id );
$attachment_parent_id = $attachment_post ? $attachment_post->post_parent : 0;
if ( current_user_can( 'read_post', $attachment_id ) ) { $attachment_url = wp_get_attachment_url( $attachment_id );
$redirect_url = wp_get_attachment_url( $attachment_id ); if ( $attachment_url !== $redirect_url ) {
/*
$is_attachment_redirect = true; * If an attachment is attached to a post, it inherits the parent post's status. Fetch the
* parent post to check its status later.
*/
if ( $attachment_parent_id ) {
$redirect_obj = get_post( $attachment_parent_id );
}
$redirect_url = $attachment_url;
} }
$is_attachment_redirect = true;
} }
$redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] ); $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.5-alpha-57356'; $wp_version = '6.5-alpha-57357';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.