Skip to content

Commit f8094c4

Browse files
[5.x] Fix CSRF token on pages excluded from static caching (#14056)
1 parent 259c585 commit f8094c4

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

src/StaticCaching/Middleware/Cache.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Statamic\Facades\StaticCache;
1414
use Statamic\Statamic;
1515
use Statamic\StaticCaching\Cacher;
16+
use Statamic\StaticCaching\Cachers\AbstractCacher;
1617
use Statamic\StaticCaching\Cachers\ApplicationCacher;
1718
use Statamic\StaticCaching\Cachers\FileCacher;
1819
use Statamic\StaticCaching\Cachers\NullCacher;
@@ -199,6 +200,10 @@ private function shouldBeCached($request, $response)
199200
return false;
200201
}
201202

203+
if ($this->cacher instanceof AbstractCacher && $this->cacher->isExcluded($this->cacher->getUrl($request))) {
204+
return false;
205+
}
206+
202207
return true;
203208
}
204209

tests/StaticCaching/FullMeasureStaticCachingTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,72 @@ public function it_should_add_the_javascript_if_there_is_a_csrf_token()
159159
$this->assertTrue(file_exists($this->dir.'/about_.html'));
160160
$this->assertEquals('<html><body>STATAMIC_CSRF_TOKEN<script>js here</script></body></html>', file_get_contents($this->dir.'/about_.html'));
161161
}
162+
163+
#[Test]
164+
public function excluded_pages_should_have_real_csrf_token()
165+
{
166+
config(['statamic.static_caching.exclude' => [
167+
'urls' => ['/about'],
168+
]]);
169+
170+
$this->withFakeViews();
171+
$this->viewShouldReturnRaw('layout', '<html><body>{{ template_content }}</body></html>');
172+
$this->viewShouldReturnRaw('default', '{{ csrf_token }}');
173+
174+
$this->createPage('about');
175+
176+
$response = $this
177+
->get('/about')
178+
->assertOk();
179+
180+
// The response should have the real CSRF token, not the placeholder.
181+
$this->assertEquals('<html><body>'.csrf_token().'</body></html>', $response->getContent());
182+
183+
// The page should not be cached.
184+
$this->assertFalse(file_exists($this->dir.'/about_.html'));
185+
}
186+
187+
#[Test]
188+
public function excluded_pages_should_have_nocache_regions_replaced()
189+
{
190+
config(['statamic.static_caching.exclude' => [
191+
'urls' => ['/about'],
192+
]]);
193+
194+
app()->instance('example_count', 0);
195+
196+
(new class extends \Statamic\Tags\Tags
197+
{
198+
public static $handle = 'example_count';
199+
200+
public function index()
201+
{
202+
$count = app('example_count');
203+
$count++;
204+
app()->instance('example_count', $count);
205+
206+
return $count;
207+
}
208+
})::register();
209+
210+
$this->withFakeViews();
211+
$this->viewShouldReturnRaw('layout', '<html><body>{{ template_content }}</body></html>');
212+
$this->viewShouldReturnRaw('default', '{{ example_count }} {{ nocache }}{{ example_count }}{{ /nocache }}');
213+
214+
$this->createPage('about');
215+
216+
StaticCache::nocacheJs('js here');
217+
StaticCache::nocachePlaceholder('<svg>Loading...</svg>');
218+
219+
$response = $this
220+
->get('/about')
221+
->assertOk();
222+
223+
// The response should have the nocache regions replaced with rendered content, no placeholders or JS.
224+
$this->assertEquals('<html><body>1 2</body></html>', $response->getContent());
225+
$this->assertStringNotContainsString('<script>', $response->getContent());
226+
227+
// The page should not be cached.
228+
$this->assertFalse(file_exists($this->dir.'/about_.html'));
229+
}
162230
}

0 commit comments

Comments
 (0)