Make WordPress Core

Changeset 59753


Ignore:
Timestamp:
02/03/2025 04:53:16 PM (18 months ago)
Author:
johnbillion
Message:

Posts, Post Types: Explicitly pass a redirect URL for the post permalink when submitting the post password form.

This allows the subsequent redirect to behave as expected if a site is using a strict referrer policy on the front end which prevents the full referrer from being sent.

Props zodiac1978, yogeshbhutkar, hbhalodia, mukesh27.

Fixes #62881

Location:
trunk/src
Files:
2 edited

Legend:

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

    r59737 r59753  
    17811781        $aria                  = '';
    17821782        $class                 = '';
     1783        $redirect_field        = '';
    17831784
    17841785        // If the referrer is the same as the current request, the user has entered an invalid password.
     
    17991800        }
    18001801
    1801         $output = '<form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" class="post-password-form' . $class . '" method="post">' . $invalid_password_html . '
     1802        if ( ! empty( $post->ID ) ) {
     1803                $redirect_field = sprintf(
     1804                        '<input type="hidden" name="redirect_to" value="%s" />',
     1805                        esc_attr( get_permalink( $post->ID ) )
     1806                );
     1807        }
     1808
     1809        $output = '<form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" class="post-password-form' . $class . '" method="post">' . $redirect_field . $invalid_password_html . '
    18021810        <p>' . __( 'This content is password protected. To view it please enter your password below:' ) . '</p>
    18031811        <p><label for="' . $field_id . '">' . __( 'Password:' ) . ' <input name="post_password" id="' . $field_id . '" type="password" spellcheck="false" required size="20"' . $aria . ' /></label> <input type="submit" name="Submit" value="' . esc_attr_x( 'Enter', 'post password form' ) . '" /></p></form>
  • trunk/src/wp-login.php

    r59671 r59753  
    765765
    766766        case 'postpass':
     767                $redirect_to = $_POST['redirect_to'] ?? wp_get_referer();
     768
    767769                if ( ! isset( $_POST['post_password'] ) || ! is_string( $_POST['post_password'] ) ) {
    768                         wp_safe_redirect( wp_get_referer() );
     770                        wp_safe_redirect( $redirect_to );
    769771                        exit;
    770772                }
     
    783785                 * @param int $expires The expiry time, as passed to setcookie().
    784786                 */
    785                 $expire  = apply_filters( 'post_password_expires', time() + 10 * DAY_IN_SECONDS );
    786                 $referer = wp_get_referer();
    787 
    788                 if ( $referer ) {
    789                         $secure = ( 'https' === parse_url( $referer, PHP_URL_SCHEME ) );
     787                $expire = apply_filters( 'post_password_expires', time() + 10 * DAY_IN_SECONDS );
     788
     789                if ( $redirect_to ) {
     790                        $secure = ( 'https' === parse_url( $redirect_to, PHP_URL_SCHEME ) );
    790791                } else {
    791792                        $secure = false;
     
    794795                setcookie( 'wp-postpass_' . COOKIEHASH, $hasher->HashPassword( wp_unslash( $_POST['post_password'] ) ), $expire, COOKIEPATH, COOKIE_DOMAIN, $secure );
    795796
    796                 wp_safe_redirect( wp_get_referer() );
     797                wp_safe_redirect( $redirect_to );
    797798                exit;
    798799
Note: See TracChangeset for help on using the changeset viewer.