Repository navigation
feat: SanitizedHtml cast for rich-editor HTML #39
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?php | ||
|
|
||
| namespace Saucebase\Core\Casts; | ||
|
|
||
| use Illuminate\Contracts\Database\Eloquent\CastsAttributes; | ||
| use Illuminate\Database\Eloquent\Model; | ||
| use Illuminate\Support\Str; | ||
|
|
||
| /** | ||
| * Rich-editor HTML that a page renders as markup (`v-html`, `dangerouslySetInnerHTML`). | ||
| * | ||
| * Cleaned on every read with Filament's sanitizer (`Str::sanitizeHtml()`), so scripts and | ||
| * event handlers never reach a visitor whoever wrote the value: the admin, an import, a | ||
| * provider. Stored as written, so tightening the sanitizer later loses nothing. | ||
| * | ||
| * @implements CastsAttributes<string|null, string|null> | ||
| */ | ||
| class SanitizedHtml implements CastsAttributes | ||
| { | ||
| /** @param array<string, mixed> $attributes */ | ||
| public function get(Model $model, string $key, mixed $value, array $attributes): ?string | ||
| { | ||
| return $value === null ? null : Str::sanitizeHtml((string) $value); | ||
| } | ||
|
|
||
| /** @param array<string, mixed> $attributes */ | ||
| public function set(Model $model, string $key, mixed $value, array $attributes): ?string | ||
| { | ||
| return $value === null ? null : (string) $value; | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| <?php | ||
|
|
||
| namespace Saucebase\Core\Tests\Unit\Casts; | ||
|
|
||
| use Illuminate\Database\Eloquent\Model; | ||
| use Saucebase\Core\Casts\SanitizedHtml; | ||
| use Saucebase\Core\Tests\TestCase; | ||
|
|
||
| /** | ||
| * Rich-editor HTML a page renders as markup (`v-html`): cleaned on every read, stored as | ||
| * written, so nothing an editor saved is lost and nothing that runs reaches a visitor. | ||
| */ | ||
| class SanitizedHtmlTest extends TestCase | ||
| { | ||
| private function model(?string $html): Model | ||
| { | ||
| $model = new class extends Model | ||
| { | ||
| protected $casts = ['body' => SanitizedHtml::class]; | ||
| }; | ||
| $model->setRawAttributes(['body' => $html]); | ||
|
|
||
| return $model; | ||
| } | ||
|
|
||
| public function test_markup_that_runs_is_removed_and_formatting_kept(): void | ||
| { | ||
| $body = $this->model('<p><strong>Sale</strong><img src=x onerror="alert(1)"><script>alert(2)</script></p>')->getAttribute('body'); | ||
|
|
||
| $this->assertStringContainsString('<strong>Sale</strong>', $body); | ||
| $this->assertStringNotContainsString('onerror', $body); | ||
| $this->assertStringNotContainsString('<script', $body); | ||
| } | ||
|
|
||
| public function test_null_stays_null(): void | ||
| { | ||
| $this->assertNull($this->model(null)->getAttribute('body')); | ||
| } | ||
|
|
||
| public function test_the_stored_value_is_what_was_written(): void | ||
| { | ||
| $model = $this->model(null); | ||
| $model->setAttribute('body', '<p onclick="x()">Hi</p>'); | ||
|
|
||
| $this->assertSame('<p onclick="x()">Hi</p>', $model->getAttributes()['body']); | ||
| } | ||
| } |
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.
Script executed:
Repository: saucebase-dev/core
Length of output: 156
Script executed:
Repository: saucebase-dev/core
Length of output: 3950
Web query:
Filament v5.10.3 Str sanitizeHtml inline styles position fixed SupportServiceProvider untrusted content documentationResult:
Script executed:
Repository: saucebase-dev/core
Length of output: 6211
Web query:
Filament v5.10.3 SupportServiceProvider Str macro sanitizeHtml HtmlSanitizerInterface untrusted style position fixedResult:
Qualify this guidance for untrusted HTML.
Filament’s default sanitizer permits inline CSS such as
position: fixed, which can create an attacker-controlled overlay. Add a warning for applications that render untrusted content.Suggested addition
Committable suggestion