From 48bc1f28647a458b853911ff0ac40da77bd8288c Mon Sep 17 00:00:00 2001 From: roble Date: Sat, 7 Nov 2026 12:55:10 +0000 Subject: [PATCH 1/3] refactor: cast post content with core's SanitizedHtml instead of sanitising in the controller (sc-1131) Co-Authored-By: Claude Opus 5.5 --- .../boost/skills/saucebase-blog-development/SKILL.md | 2 +- src/Http/Controllers/BlogController.php | 3 +-- src/Models/Post.php | 3 +++ tests/Unit/PostTest.php | 11 +++++++++++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/resources/boost/skills/saucebase-blog-development/SKILL.md b/resources/boost/skills/saucebase-blog-development/SKILL.md index 167cdcf..ba34922 100644 --- a/resources/boost/skills/saucebase-blog-development/SKILL.md +++ b/resources/boost/skills/saucebase-blog-development/SKILL.md @@ -68,7 +68,7 @@ The card's title link stretches over the whole card (`after:absolute after:inset ### Post Content Is Sanitized on Output -The rich-editor HTML is rendered with `v-html` / `dangerouslySetInnerHTML`. `BlogController::show()` passes it through `Str::sanitizeHtml()` (Filament's Symfony sanitizer) first, so scripts and event handlers never reach the page. Keep that call if the controller changes. +The rich-editor HTML is rendered with `v-html` / `dangerouslySetInnerHTML`. `Post::content` is cast with core's `SanitizedHtml`, which runs `Str::sanitizeHtml()` (Filament's Symfony sanitizer) on every read and stores what was written, so scripts and event handlers never reach a page, whichever endpoint sends it. Keep the cast (sc-1131). The JSON-LD `` cannot break out of it. diff --git a/src/Http/Controllers/BlogController.php b/src/Http/Controllers/BlogController.php index 31b6bc2..1c7b6e6 100644 --- a/src/Http/Controllers/BlogController.php +++ b/src/Http/Controllers/BlogController.php @@ -5,7 +5,6 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; -use Illuminate\Support\Str; use Inertia\Inertia; use Inertia\Response; use Modules\Blog\Data\CategoryData; @@ -136,7 +135,7 @@ private function render(Post $post, bool $preview = false): Response ->map(fn (Post $p) => PostData::fromPost($p)); return Inertia::render('Blog::Show', [ - 'post' => PostData::fromPost($post, content: Str::sanitizeHtml($post->content)), + 'post' => PostData::fromPost($post, content: $post->content), 'related' => $related, 'preview' => $preview, ])->withSSR(); diff --git a/src/Models/Post.php b/src/Models/Post.php index 7acaf41..bc94d75 100644 --- a/src/Models/Post.php +++ b/src/Models/Post.php @@ -15,6 +15,7 @@ use Modules\Blog\Contracts\Redirectable; use Modules\Blog\Enums\PostStatus; use Modules\Blog\Traits\HasRedirects; +use Saucebase\Core\Casts\SanitizedHtml; use Spatie\Feed\Feedable; use Spatie\Feed\FeedItem; use Spatie\MediaLibrary\HasMedia; @@ -73,6 +74,8 @@ protected function casts(): array return [ 'status' => PostStatus::class, 'published_at' => 'datetime', + // Rendered as markup on the post page; sanitised on every read. + 'content' => SanitizedHtml::class, ]; } diff --git a/tests/Unit/PostTest.php b/tests/Unit/PostTest.php index 80ce741..8247323 100644 --- a/tests/Unit/PostTest.php +++ b/tests/Unit/PostTest.php @@ -7,6 +7,17 @@ class PostTest extends TestCase { + /** Every read is safe markup, so no page or endpoint has to remember to clean it. */ + public function test_content_is_sanitised_on_read(): void + { + $post = new Post; + $post->setRawAttributes(['content' => '

Hi

']); + + $this->assertStringContainsString('Hi', $post->content); + $this->assertStringNotContainsString('onerror', $post->content); + $this->assertStringNotContainsString('content); + } + public function test_cover_media_collection_is_registered_as_single_file(): void { $post = new Post; From e18c53aa45ba6690c61d0d66e194ef083d909c95 Mon Sep 17 00:00:00 2001 From: roble Date: Sat, 7 Nov 2026 16:55:09 +0000 Subject: [PATCH 2/3] chore: require core ^2.12.0 for the SanitizedHtml cast Co-Authored-By: Claude Opus 5.5 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 54fd4be..d43dbd1 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ } ], "require": { - "saucebase/core": "^2.8", + "saucebase/core": "^2.12.0", "spatie/laravel-feed": "^4.5" }, "extra": { From 326b5aaa39c8be9846771f03b51c9fd3ed418a15 Mon Sep 17 00:00:00 2001 From: roble Date: Sat, 7 Nov 2026 17:26:30 +0000 Subject: [PATCH 3/3] fix: load the stored post content into the editor, not the sanitised read The sanitiser cuts input at 500,000 bytes, so saving any other field wrote the cut copy back over a long post. Co-Authored-By: Claude Opus 5.5 --- .../Resources/Blog/Schemas/PostForm.php | 3 +++ tests/Feature/PostResourceTest.php | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/Filament/Resources/Blog/Schemas/PostForm.php b/src/Filament/Resources/Blog/Schemas/PostForm.php index a1da5ef..517d5c3 100644 --- a/src/Filament/Resources/Blog/Schemas/PostForm.php +++ b/src/Filament/Resources/Blog/Schemas/PostForm.php @@ -39,6 +39,9 @@ public static function configure(Schema $schema): Schema RichEditor::make('content') ->hiddenLabel() + // What is stored, not the sanitised read: the sanitiser cuts input + // at 500,000 bytes, and saving any field would write the cut copy back. + ->formatStateUsing(fn (?Post $record, mixed $state): mixed => $record?->getRawOriginal('content') ?? $state) ->required(), Section::make(__('Excerpt')) diff --git a/tests/Feature/PostResourceTest.php b/tests/Feature/PostResourceTest.php index d5ac797..8d9d726 100644 --- a/tests/Feature/PostResourceTest.php +++ b/tests/Feature/PostResourceTest.php @@ -4,6 +4,7 @@ use App\Models\User; use Illuminate\Foundation\Testing\RefreshDatabase; +use Illuminate\Support\Facades\DB; use Livewire\Livewire; use Modules\Blog\Enums\PostStatus; use Modules\Blog\Filament\Resources\Blog\Pages\CreatePost; @@ -114,6 +115,27 @@ public function test_admin_can_edit_post(): void $this->assertDatabaseHas('blog_posts', ['id' => $post->id, 'title' => 'Updated Title']); } + /** + * The editor gets what is stored, not the sanitised read: the sanitiser cuts input at + * 500,000 bytes, and saving any other field would write the cut copy back. + */ + public function test_saving_another_field_keeps_long_content_whole(): void + { + $content = str_repeat('

Long post.

', 40_000); + $post = Post::factory()->create(['author_id' => $this->admin->id, 'content' => $content]); + $this->assertGreaterThan(500_000, strlen($content)); + + $this->actingAs($this->admin); + + Livewire::test(EditPost::class, ['record' => $post->id]) + ->fillForm(['title' => 'Renamed']) + ->call('save') + ->assertHasNoFormErrors(); + + $stored = (string) DB::table('blog_posts')->where('id', $post->id)->value('content'); + $this->assertSame(strlen($content), strlen($stored), 'the stored content was cut'); + } + public function test_admin_can_delete_post(): void { $post = Post::factory()->create();