Make WordPress Core


Ignore:
Timestamp:
01/25/2024 05:34:54 PM (22 months ago)
Author:
jorbin
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/canonical.php

    r57318 r57357  
    551551
    552552    if ( is_attachment() && ! get_option( 'wp_attachment_pages_enabled' ) ) {
    553         $attachment_id = get_query_var( 'attachment_id' );
    554 
    555         if ( current_user_can( 'read_post', $attachment_id ) ) {
    556             $redirect_url = wp_get_attachment_url( $attachment_id );
    557 
    558             $is_attachment_redirect = true;
    559         }
     553        $attachment_id        = get_query_var( 'attachment_id' );
     554        $attachment_post      = get_post( $attachment_id );
     555        $attachment_parent_id = $attachment_post ? $attachment_post->post_parent : 0;
     556
     557        $attachment_url = wp_get_attachment_url( $attachment_id );
     558        if ( $attachment_url !== $redirect_url ) {
     559            /*
     560            * If an attachment is attached to a post, it inherits the parent post's status. Fetch the
     561            * parent post to check its status later.
     562            */
     563            if ( $attachment_parent_id ) {
     564                $redirect_obj = get_post( $attachment_parent_id );
     565            }
     566            $redirect_url = $attachment_url;
     567        }
     568
     569        $is_attachment_redirect = true;
    560570    }
    561571
Note: See TracChangeset for help on using the changeset viewer.