-
-
Notifications
You must be signed in to change notification settings - Fork 641
[6.x] Ensure share_errors replaces CSRFs #15265
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
Open
ryanmitchell
wants to merge
6
commits into
statamic:6.x
Choose a base branch
from
ryanmitchell:fix/share-errors-csrf
base: 6.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+173
−6
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c47c6a7
Ensure share_errors replaces CSRFs
ryanmitchell cf1bfd1
:beer:
ryanmitchell bde70d5
review fixes
ryanmitchell 1f7e7a6
Merge branch '6.x' into fix/share-errors-csrf
ryanmitchell e01823f
SymfonyResponse
ryanmitchell 694ff00
Merge branch 'fix/share-errors-csrf' of https://github.com/ryanmitche…
ryanmitchell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,13 +4,22 @@ | |
|
|
||
| use Illuminate\Http\Request; | ||
| use Illuminate\Routing\Events\ResponsePrepared; | ||
| use Illuminate\Support\Facades\Cache; | ||
| use Orchestra\Testbench\Attributes\DefineEnvironment; | ||
| use PHPUnit\Framework\Attributes\Test; | ||
| use Statamic\Facades\Site; | ||
| use Statamic\StaticCaching\Cachers\ApplicationCacher; | ||
| use Statamic\StaticCaching\Replacer; | ||
| use Symfony\Component\HttpFoundation\Response; | ||
| use Tests\FakesViews; | ||
| use Tests\PreventSavingStacheItemsToDisk; | ||
| use Tests\TestCase; | ||
|
|
||
| class SharedErrorsStaticCachingTest extends TestCase | ||
| { | ||
| use FakesViews; | ||
| use PreventSavingStacheItemsToDisk; | ||
|
|
||
| private ApplicationCacher $cacher; | ||
|
|
||
| protected function setUp(): void | ||
|
|
@@ -75,4 +84,145 @@ private function shareError(int $status, string $content): void | |
| // the framework fires during the real response lifecycle. | ||
| event(new ResponsePrepared($request, $response)); | ||
| } | ||
|
|
||
| public function withTestReplacer($app) | ||
| { | ||
| $app['config']->set('statamic.static_caching.strategy', 'half'); | ||
| $app['config']->set('statamic.static_caching.share_errors', true); | ||
| $app['config']->set('statamic.static_caching.replacers', array_merge( | ||
| $app['config']->get('statamic.static_caching.replacers'), | ||
| ['test' => SharedErrorTestReplacer::class] | ||
| )); | ||
| } | ||
|
|
||
| #[Test] | ||
| #[DefineEnvironment('withTestReplacer')] | ||
| public function replacers_run_when_serving_a_shared_error() | ||
| { | ||
| Cache::flush(); | ||
|
|
||
| $this->withStandardFakeViews(); | ||
| $this->viewShouldReturnRaw('errors.layout', '{{ template_content }}'); | ||
| $this->viewShouldReturnRaw('errors.404', '404 LIVE_VALUE'); | ||
|
Comment on lines
+100
to
+106
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note — the test doesn't exercise the actual bug, or the interaction that breaks. Proving the mechanism with a synthetic
|
||
|
|
||
| // First 404 renders live and seeds the shared cache. The live response | ||
| // itself is untouched by the prepare step - only the clone that gets | ||
| // cached is. | ||
| $this->get('/this-does-not-exist') | ||
| ->assertNotFound() | ||
| ->assertSee('404 LIVE_VALUE', false); | ||
|
|
||
| // A different URL that also 404s is served from the shared cache. | ||
| // Regression: previously this replayed whatever copyError() captured | ||
| // before any replacer ran, and getCachedError() never ran replacers on | ||
| // the way out either - so a stale value (or, for CsrfTokenReplacer in | ||
| // production, a frozen CSRF token from a different session) leaked to | ||
| // every subsequent visitor forever. | ||
| $this->get('/this-also-does-not-exist') | ||
| ->assertNotFound() | ||
| ->assertSee('404 REPLACED_ON_SERVE', false); | ||
| } | ||
|
|
||
| public function shareErrorsWithHalfMeasure($app) | ||
| { | ||
| $app['config']->set('statamic.static_caching.strategy', 'half'); | ||
| $app['config']->set('statamic.static_caching.share_errors', true); | ||
| } | ||
|
|
||
| #[Test] | ||
| #[DefineEnvironment('shareErrorsWithHalfMeasure')] | ||
| public function each_session_gets_its_own_csrf_token_on_a_shared_error() | ||
| { | ||
| Cache::flush(); | ||
|
|
||
| $this->withStandardFakeViews(); | ||
| $this->viewShouldReturnRaw('errors.layout', '{{ template_content }}'); | ||
| $this->viewShouldReturnRaw('errors.404', '404 {{ csrf_token }}'); | ||
|
|
||
| // First 404 renders live and seeds the shared cache. | ||
| $this->withSession(['_token' => 'session-one-token']) | ||
| ->get('/nope-one') | ||
| ->assertNotFound() | ||
| ->assertSee('404 session-one-token', false); | ||
|
|
||
| // A different URL that also 404s is served from the shared cache, but | ||
| // for a different session. It must get its own token, not the one | ||
| // frozen into the shared cache by the first visitor. | ||
| $this->withSession(['_token' => 'session-two-token']) | ||
| ->get('/nope-two') | ||
| ->assertNotFound() | ||
| ->assertSee('404 session-two-token', false) | ||
| ->assertDontSee('session-one-token', false); | ||
|
|
||
| // A repeat hit on that second URL, now cached under its own URL, | ||
| // must still resolve session two's current token, not a frozen one. | ||
| $this->withSession(['_token' => 'session-two-token']) | ||
| ->get('/nope-two') | ||
| ->assertNotFound() | ||
| ->assertSee('404 session-two-token', false); | ||
| } | ||
|
|
||
| #[Test] | ||
| #[DefineEnvironment('shareErrorsWithHalfMeasure')] | ||
| public function nocache_region_inside_a_shared_error_stays_dynamic_across_repeat_hits() | ||
| { | ||
| Cache::flush(); | ||
|
|
||
| // Use a tag that outputs something dynamic. It just increments by | ||
| // one every time it's rendered. | ||
| app()->instance('example_count', 0); | ||
|
|
||
| (new class extends \Statamic\Tags\Tags | ||
| { | ||
| public static $handle = 'example_count'; | ||
|
|
||
| public function index() | ||
| { | ||
| $count = app('example_count'); | ||
| $count++; | ||
| app()->instance('example_count', $count); | ||
|
|
||
| return $count; | ||
| } | ||
| })::register(); | ||
|
|
||
| $this->withStandardFakeViews(); | ||
| $this->viewShouldReturnRaw('errors.layout', '{{ template_content }}'); | ||
| $this->viewShouldReturnRaw('errors.404', '404 {{ nocache }}{{ example_count }}{{ /nocache }}'); | ||
|
|
||
| // First 404 renders live and seeds the shared cache. | ||
| $this->get('/nope-one')->assertNotFound()->assertSee('404 1', false); | ||
|
|
||
| // A different URL that also 404s is served from the shared cache. | ||
| // Its nocache region must render fresh, not replay whatever the | ||
| // shared cache captured when it was seeded. | ||
| $this->get('/nope-two')->assertNotFound()->assertSee('404 2', false); | ||
|
|
||
| // Repeat hits to that second URL, now cached under its own URL, must | ||
| // keep rendering the region fresh too. Before the fix, the first | ||
| // request served from the shared cache baked the region's rendered | ||
| // output into the per-URL cache entry, so every later hit on that | ||
| // URL replayed the same frozen value forever - the same class of | ||
| // cross-visitor leak this PR fixes for CSRF tokens. | ||
| $this->get('/nope-two')->assertNotFound()->assertSee('404 3', false); | ||
| $this->get('/nope-two')->assertNotFound()->assertSee('404 4', false); | ||
| } | ||
| } | ||
|
|
||
| class SharedErrorTestReplacer implements Replacer | ||
| { | ||
| const PLACEHOLDER = 'TEST_TOKEN_PLACEHOLDER'; | ||
|
|
||
| public function prepareResponseToCache(Response $response, Response $initial) | ||
| { | ||
| // Mirrors CsrfTokenReplacer's behaviour for the "half" strategy: | ||
| // only the clone that gets cached is touched, not $initial (which | ||
| // is what's returned to this first request). | ||
| $response->setContent(str_replace('LIVE_VALUE', self::PLACEHOLDER, $response->getContent())); | ||
| } | ||
|
|
||
| public function replaceInCachedResponse(Response $response) | ||
| { | ||
| $response->setContent(str_replace(self::PLACEHOLDER, 'REPLACED_ON_SERVE', $response->getContent())); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note — this method silently gains a return value.
makeReplacementsAndCacheResponse()now returns$cachedResponse, but has no return type and a name that still reads as a command rather than a query. Either add: Response, or split building the prepared clone out from caching it so both call sites share it explicitly.