Skip to content

[5.x] Prevent protected pages being cached #10929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/Http/Responses/DataResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,13 @@ protected function getRedirect()

protected function protect()
{
app(Protection::class)
->setData($this->data)
->protect();
$protection = app(Protection::class)->setData($this->data);

$protection->protect();

if ($protection->scheme()) {
$this->headers['X-Statamic-Protected'] = true;
}

return $this;
}
Expand Down
8 changes: 6 additions & 2 deletions src/StaticCaching/Middleware/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,12 @@ private function shouldBeCached($request, $response)
return false;
}

// Draft and private pages should not be cached.
if ($response->headers->has('X-Statamic-Draft') || $response->headers->has('X-Statamic-Private')) {
// Draft, private and protected pages should not be cached.
if (
$response->headers->has('X-Statamic-Draft')
|| $response->headers->has('X-Statamic-Private')
|| $response->headers->has('X-Statamic-Protected')
) {
return false;
}

Expand Down
19 changes: 19 additions & 0 deletions tests/FrontendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,25 @@ public function past_private_entries_dont_get_statically_cached()
$this->markTestIncomplete();
}

#[Test]
public function header_is_added_to_protected_responses()
{
$page = $this->createPage('about');

$this
->get('/about')
->assertOk()
->assertHeaderMissing('X-Statamic-Protected');

$page->set('protect', 'logged_in')->save();

$this
->actingAs(User::make())
->get('/about')
->assertOk()
->assertHeader('X-Statamic-Protected', true);
}

#[Test]
public function key_variables_key_added()
{
Expand Down
Loading