Skip to content

Commit

Permalink
[5.x] Prevent protected pages being cached (#10929)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Varga <[email protected]>
  • Loading branch information
duncanmcclean and jasonvarga authored Oct 18, 2024
1 parent 512a491 commit 35e57bc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
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

0 comments on commit 35e57bc

Please sign in to comment.