Skip to content

Commit 8711aa5

Browse files
committed
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 git-svn-id: https://develop.svn.wordpress.org/trunk@59753 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 3ff586a commit 8711aa5

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/wp-includes/post-template.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1780,6 +1780,7 @@ function get_the_password_form( $post = 0 ) {
17801780
$invalid_password_html = '';
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.
17851786
if ( ! empty( $post->ID ) && wp_get_raw_referer() === get_permalink( $post->ID ) && isset( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] ) ) {
@@ -1798,7 +1799,14 @@ function get_the_password_form( $post = 0 ) {
17981799
$class = ' password-form-error';
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>
18041812
';

src/wp-login.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -764,8 +764,10 @@ function wp_login_viewport_meta() {
764764
break;
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
}
771773

@@ -782,18 +784,17 @@ function wp_login_viewport_meta() {
782784
*
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+
$expire = apply_filters( 'post_password_expires', time() + 10 * DAY_IN_SECONDS );
787788

788-
if ( $referer ) {
789-
$secure = ( 'https' === parse_url( $referer, PHP_URL_SCHEME ) );
789+
if ( $redirect_to ) {
790+
$secure = ( 'https' === parse_url( $redirect_to, PHP_URL_SCHEME ) );
790791
} else {
791792
$secure = false;
792793
}
793794

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

799800
case 'logout':

0 commit comments

Comments
 (0)