diff --git a/resources/boost/skills/saucebase-filament-development/SKILL.md b/resources/boost/skills/saucebase-filament-development/SKILL.md index a12d00b..417ee72 100644 --- a/resources/boost/skills/saucebase-filament-development/SKILL.md +++ b/resources/boost/skills/saucebase-filament-development/SKILL.md @@ -200,6 +200,8 @@ class FeatureForm Use `->components([...])` (not `->schema([...])`). Layout components (`Grid`, `Section`) come from `Filament\Schemas\Components\`. +A `RichEditor` field whose HTML a page renders as markup (`v-html`, `dangerouslySetInnerHTML`) is cast with `Saucebase\Core\Casts\SanitizedHtml` on the model: `'description' => SanitizedHtml::class`. It runs `Str::sanitizeHtml()` on every read and stores what was written, so scripts and event handlers never reach a visitor, whoever wrote the value. + --- ## Table Schema Pattern diff --git a/src/Casts/SanitizedHtml.php b/src/Casts/SanitizedHtml.php new file mode 100644 index 0000000..50a8ad8 --- /dev/null +++ b/src/Casts/SanitizedHtml.php @@ -0,0 +1,31 @@ + + */ +class SanitizedHtml implements CastsAttributes +{ + /** @param array $attributes */ + public function get(Model $model, string $key, mixed $value, array $attributes): ?string + { + return $value === null ? null : Str::sanitizeHtml((string) $value); + } + + /** @param array $attributes */ + public function set(Model $model, string $key, mixed $value, array $attributes): ?string + { + return $value === null ? null : (string) $value; + } +} diff --git a/tests/Unit/Casts/SanitizedHtmlTest.php b/tests/Unit/Casts/SanitizedHtmlTest.php new file mode 100644 index 0000000..c5c7b14 --- /dev/null +++ b/tests/Unit/Casts/SanitizedHtmlTest.php @@ -0,0 +1,47 @@ + SanitizedHtml::class]; + }; + $model->setRawAttributes(['body' => $html]); + + return $model; + } + + public function test_markup_that_runs_is_removed_and_formatting_kept(): void + { + $body = $this->model('

Sale

')->getAttribute('body'); + + $this->assertStringContainsString('Sale', $body); + $this->assertStringNotContainsString('onerror', $body); + $this->assertStringNotContainsString('assertNull($this->model(null)->getAttribute('body')); + } + + public function test_the_stored_value_is_what_was_written(): void + { + $model = $this->model(null); + $model->setAttribute('body', '

Hi

'); + + $this->assertSame('

Hi

', $model->getAttributes()['body']); + } +}