Skip to content

Commit 4863a92

Browse files
committed
Posts, Post Types: Add no-cache headers to password protected posts.
This instructs an intermediate cache, for example a proxy server, to not cache a password protected post both before and after a visitor has entered a password. Props brevilo, haozi, ironprogrammer, narenin Fixes #61711 git-svn-id: https://develop.svn.wordpress.org/trunk@59728 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 382211a commit 4863a92

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/wp-includes/class-wp.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,11 @@ public function send_headers() {
545545
if ( $post && pings_open( $post ) ) {
546546
$headers['X-Pingback'] = get_bloginfo( 'pingback_url', 'display' );
547547
}
548+
549+
// Send nocache headers for password protected posts to avoid unwanted caching.
550+
if ( ! empty( $post->post_password ) ) {
551+
$headers = array_merge( $headers, wp_get_nocache_headers() );
552+
}
548553
}
549554

550555
/**

tests/phpunit/tests/wp/sendHeaders.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* @covers WP::send_headers
77
*/
88
class Tests_WP_SendHeaders extends WP_UnitTestCase {
9+
protected $headers_sent = array();
910

1011
/**
1112
* @ticket 56068
@@ -35,4 +36,46 @@ function ( $headers ) {
3536
$post_id = self::factory()->post->create();
3637
$this->go_to( get_permalink( $post_id ) );
3738
}
39+
40+
/**
41+
* @ticket 61711
42+
*/
43+
public function test_send_headers_sets_cache_control_header_for_password_protected_posts() {
44+
$password = 'password';
45+
46+
add_filter(
47+
'wp_headers',
48+
function ( $headers ) {
49+
$this->headers_sent = $headers;
50+
return $headers;
51+
}
52+
);
53+
54+
$post_id = self::factory()->post->create(
55+
array(
56+
'post_password' => $password,
57+
)
58+
);
59+
$this->go_to( get_permalink( $post_id ) );
60+
61+
$headers_without_password = $this->headers_sent;
62+
$password_status_without_password = post_password_required( $post_id );
63+
64+
require_once ABSPATH . WPINC . '/class-phpass.php';
65+
66+
$hash = ( new PasswordHash( 8, true ) )->HashPassword( $password );
67+
68+
$_COOKIE[ 'wp-postpass_' . COOKIEHASH ] = $hash;
69+
70+
$this->go_to( get_permalink( $post_id ) );
71+
72+
$headers_with_password = $this->headers_sent;
73+
$password_status_with_password = post_password_required( $post_id );
74+
75+
$this->assertTrue( $password_status_without_password );
76+
$this->assertArrayHasKey( 'Cache-Control', $headers_without_password );
77+
78+
$this->assertFalse( $password_status_with_password );
79+
$this->assertArrayHasKey( 'Cache-Control', $headers_with_password );
80+
}
3881
}

0 commit comments

Comments
 (0)