Skip to content

Commit ca9dfaa

Browse files
authored
Merge pull request #614 from code16/sanitize-text
Sanitize HTML
2 parents 70d5072 + 74ecb8a commit ca9dfaa

36 files changed

Lines changed: 2325 additions & 1111 deletions

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"league/commonmark": "^2.4",
2828
"masterminds/html5": "^2.8",
2929
"spatie/image-optimizer": "^1.6",
30+
"symfony/html-sanitizer": "^7.3",
3031
"tightenco/ziggy": "^2.0"
3132
},
3233
"require-dev": {

demo/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"pragmarx/google2fa": "^8.0",
1818
"spatie/image-optimizer": "^1.6",
1919
"spatie/laravel-translatable": "^6.0",
20+
"symfony/html-sanitizer": "^7.3",
2021
"technikermathe/blade-lucide-icons": "^3.98",
2122
"tightenco/ziggy": "^1.8"
2223
},

demo/composer.lock

Lines changed: 803 additions & 431 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/guide/building-entity-list.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ Setting the label, allowing the column to be sortable and to display html is opt
5353
The optional `->setWidth()` method accepts either an integer (eg: `20` for 20%), a float (eg: `.2` for 20%) or a string (eg: `'20'` or `'20%'`); if missing, it will be deduced (you can use `->setWidthFill()` to force this last behavior).
5454
To hide the column on small screens, use `->hideOnSmallScreens()`.
5555

56+
::: warning
57+
HTML sanitization is enabled by default for list fields (to prevent XSS attacks when displaying the list). You can disable it by using `->setSanitizeHtml(false)` field method.
58+
:::
59+
5660
Sorting columns must be handled in the `getListData()` method, see below.
5761

5862
#### Add a badge field

docs/guide/form-fields/editor.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ SharpFormEditorField::make("description")
5555
]);
5656
```
5757

58+
::: warning
59+
HTML included using RAW_HTML button is not sanitized.
60+
:::
61+
5862
If you have editor embeds you can add them to the toolbar alongside other buttons (instead of the embeds dropdown) :
5963

6064
```php
@@ -91,6 +95,10 @@ Unset the max character count.
9195

9296
Display a character count in the status bar. Default is false.
9397

98+
### `setSanitizeHtml(bool $sanitizeHtml = true)`
99+
100+
Toggle HTML sanitization (enabled by default). See [security](#security).
101+
94102
## Embed images and files in content
95103

96104
The Editor field can embed images or regular files. To use this feature, you must first allow the field to handle uploads:
@@ -245,6 +253,10 @@ This method expects an array of embeds that could be inserted in the content, de
245253

246254
The [documentation on how to write an Embed class is available here](../form-editor-embeds.md).
247255

256+
## Security
257+
258+
Editor content is sanitized by default before storing the data (to prevent XSS attack when displaying HTML content). To disable sanitizing you can call `->setSanitizeHtml(false)`.
259+
248260
## Formatter
249261

250262
- `toFront`: expects a string; will extract embedded files for the front.

docs/guide/form-fields/text.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ Set a max character count.
3232

3333
Unset the max character count.
3434

35+
### `setSanitizeHtml()`
36+
37+
Enable HTML sanitization (to prevent XSS attacks if this field data is used as raw HTML).
3538

3639
## Formatter
3740

docs/guide/form-fields/textarea.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ Set a max character count.
1616

1717
Unset the max character count.
1818

19+
### `setSanitizeHtml()`
20+
21+
Enable HTML sanitization (to prevent XSS attacks if this field data is used as raw HTML).
1922

2023
## Formatter
2124

2225
- `toFront`: expect a string.
23-
- `fromFront`: returns a string.
26+
- `fromFront`: returns a string.

docs/guide/show-fields/text.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,18 @@ Reset the collapse configuration.
2020

2121
By default, the text is escaped. If you want to display HTML, set this to true.
2222

23+
### `setSanitizeHtml(bool $sanitizeHtml = true)`
24+
25+
HTML sanitization is enabled by default for text fields (to prevent XSS attacks when displaying the show). To disable it, call `->setSanitizeHtml(false)`.
26+
27+
2328
### `allowEmbeds(array $embeds)`
2429

2530
This method expects an array of embeds that could be inserted in the content, declared as full class names. An embed class must extend `Code16\Sharp\Form\Fields\Embeds\SharpFormEditorEmbed`.
2631

2732
The [documentation on how to write an Embed class is available here](../form-editor-embeds.md).
2833

34+
2935
## Transformer
3036

3137
For markdown-formatted texts, be sure to use the built-in `MarkdownAttributeTransformer`:

package-lock.json

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
"class-variance-authority": "^0.7.0",
6969
"clsx": "^2.1.1",
7070
"cropperjs": "^1.5.12",
71+
"dompurify": "^3.2.6",
7172
"filesize": "^10.1.0",
7273
"flexsearch": "^0.7.43",
7374
"leaflet": "^1.9.4",

0 commit comments

Comments
 (0)