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": { 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/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/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/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(); 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