You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/09-advanced/06-security.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -200,13 +200,13 @@ The value of a `FileUpload` field is a string (or array of strings) pointing to
200
200
201
201
Filament allows this by default because legitimate features depend on it — for example, an action that sets a field to a pre-uploaded template file, or a "copy from another record" button. If your forms do not rely on such a flow, opt in to the built-in checks:
202
202
203
-
- For `FileUpload` fields, call [`preventFilePathTampering()`](../forms/file-upload#authorizing-existing-file-paths) to drop submitted paths that do not match the original value on the record.
204
-
- For `RichEditor` fields, call [`preventFileAttachmentPathTampering()`](../forms/rich-editor#securing-file-attachment-ids) to strip `data-id`values that are not already present in the record's stored content.
203
+
- For `FileUpload` fields, call [`preventFilePathTampering()`](../forms/file-upload#authorizing-existing-file-paths) to fail validation when a submitted path does not match the original value on the record.
204
+
- For `RichEditor` fields, call [`preventFileAttachmentPathTampering()`](../forms/rich-editor#securing-file-attachment-ids) to fail validation when a submitted `data-id`is not already present in the record's stored content.
205
205
206
206
Both methods compare submitted values against the attribute on the record via `$record->getOriginal()`, and both accept an `allowFilePathUsing` callback for paths that are legitimately added outside the record (such as shared template files). Newly uploaded files and images always pass through unchanged.
207
207
208
208
<Asidevariant="warning">
209
-
These checks require a record on the form, so on create pages every submitted existing path is rejected unless the `allowFilePathUsing` callback approves it. New uploads are unaffected.
209
+
These checks require a record on the form, so on create pages every submitted existing path fails validation unless the `allowFilePathUsing` callback approves it. New uploads are unaffected.
210
210
</Aside>
211
211
212
212
If you want these checks to apply across your entire application rather than remembering to add them to each field, enable them globally from a service provider's `boot()` method using `configureUsing()`:
Copy file name to clipboardExpand all lines: packages/forms/docs/09-file-upload.md
+15-3Lines changed: 15 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -196,10 +196,10 @@ FileUpload::make('avatar')
196
196
->preventFilePathTampering()
197
197
```
198
198
199
-
Filament compares every submitted string path against the value originally loaded from the record (via `$record->getOriginal()` for the attribute matching the field name). Paths that do not match are dropped before the record is saved and before any URL is generated for the field. Newly uploaded files always pass through, the field can still be cleared, and for `multiple()` fields each entry is checked individually.
199
+
Filament compares every submitted string path against the value originally loaded from the record (via `$record->getOriginal()` for the attribute matching the field name). Paths that do not match cause the field to fail validation, so the record is never saved with a tampered value. Newly uploaded files always pass through, the field can still be cleared, and for `multiple()` fields each entry is checked individually.
200
200
201
201
<Asidevariant="warning">
202
-
`preventFilePathTampering()` needs a record on the form. Without one — for example, on a create page — every submitted string path is rejected unless the [`allowFilePathUsing`](#allowing-additional-file-paths-with-a-callback) callback approves it. New uploads are unaffected.
202
+
`preventFilePathTampering()` needs a record on the form. Without one — for example, on a create page — every submitted string path fails validation unless the [`allowFilePathUsing`](#allowing-additional-file-paths-with-a-callback) callback approves it. New uploads are unaffected.
203
203
</Aside>
204
204
205
205
To apply this check to every `FileUpload` in your application without repeating it on each field, call `configureUsing()` in a service provider's `boot()` method:
@@ -216,7 +216,7 @@ Individual fields can still opt out by calling `preventFilePathTampering(false)`
216
216
217
217
### Allowing additional file paths with a callback
218
218
219
-
If your application legitimately references a path that is not on the record — for example, a button that selects a pre-uploaded template file — pass the `allowFilePathUsing` argument to approve it:
219
+
If your application legitimately references a path that is not on the record — for example, a button that selects a pre-uploaded template file — pass the `allowFilePathUsing` argument to approve it. Approved paths bypass the validation error:
220
220
221
221
```php
222
222
use Filament\Forms\Components\FileUpload;
@@ -229,6 +229,18 @@ FileUpload::make('avatar')
229
229
230
230
<UtilityInjectionset="formFields"version="5.x"extras="File;;string;;$file;;The submitted file path being authorized.">You can inject various utilities into the function passed to `allowFilePathUsing` as parameters.</UtilityInjection>
231
231
232
+
The validation error message can be customized via [`validationMessages()`](validation#customizing-validation-messages) using the `tampered` key:
233
+
234
+
```php
235
+
use Filament\Forms\Components\FileUpload;
236
+
237
+
FileUpload::make('avatar')
238
+
->preventFilePathTampering()
239
+
->validationMessages([
240
+
'tampered' => 'The selected attachment is not permitted.',
241
+
])
242
+
```
243
+
232
244
## Avatar mode
233
245
234
246
You can enable avatar mode for your file upload field using the `avatar()` method:
Copy file name to clipboardExpand all lines: packages/forms/docs/10-rich-editor.md
+15-3Lines changed: 15 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -412,12 +412,12 @@ RichEditor::make('content')
412
412
->preventFileAttachmentPathTampering()
413
413
```
414
414
415
-
Filament parses the record's original content (via `$record->getOriginal()` for the attribute matching the field name) and allows only the `data-id` values already present. Any other existing `data-id`has its `id` and `src` attributes removed before the record is saved and before any URL is generated. Newly uploaded images always pass through.
415
+
Filament parses the record's original content (via `$record->getOriginal()` for the attribute matching the field name) and allows only the `data-id` values already present. Any other existing `data-id`causes the field to fail validation, so the record is never saved with a tampered value. Newly uploaded images always pass through.
416
416
417
417
If you are using the [`spatie/laravel-medialibrary` plugin](https://filamentphp.com/plugins/filament-spatie-media-library#using-media-library-for-rich-editor-file-attachments) as the file attachment provider, this protection is already implicit — it looks up each `data-id` against the record's own media collection.
418
418
419
419
<Asidevariant="warning">
420
-
`preventFileAttachmentPathTampering()` needs a record on the form. Without one — for example, on a create page — every existing `data-id` is rejected unless the [`allowFilePathUsing`](#allowing-additional-data-id-values-with-a-callback) callback approves it. New uploads are unaffected.
420
+
`preventFileAttachmentPathTampering()` needs a record on the form. Without one — for example, on a create page — every existing `data-id` fails validation unless the [`allowFilePathUsing`](#allowing-additional-data-id-values-with-a-callback) callback approves it. New uploads are unaffected.
421
421
</Aside>
422
422
423
423
To apply this check to every `RichEditor` in your application without repeating it on each field, call `configureUsing()` in a service provider's `boot()` method:
@@ -434,7 +434,7 @@ Individual fields can still opt out by calling `preventFileAttachmentPathTamperi
434
434
435
435
#### Allowing additional `data-id` values with a callback
436
436
437
-
If your application legitimately references an identifier that is not on the record — for example, a "copy from another record" action — pass the `allowFilePathUsing` argument to approve it:
437
+
If your application legitimately references an identifier that is not on the record — for example, a "copy from another record" action — pass the `allowFilePathUsing` argument to approve it. Approved identifiers bypass the validation error:
438
438
439
439
```php
440
440
use Filament\Forms\Components\RichEditor;
@@ -447,6 +447,18 @@ RichEditor::make('content')
447
447
448
448
<UtilityInjectionset="formFields"version="5.x"extras="File;;string;;$file;;The submitted `data-id` value being authorized.">You can inject various utilities into the function passed to `allowFilePathUsing` as parameters.</UtilityInjection>
449
449
450
+
The validation error message can be customized via [`validationMessages()`](validation#customizing-validation-messages) using the `tampered` key:
451
+
452
+
```php
453
+
use Filament\Forms\Components\RichEditor;
454
+
455
+
RichEditor::make('content')
456
+
->preventFileAttachmentPathTampering()
457
+
->validationMessages([
458
+
'tampered' => 'The content references an image that is not permitted.',
459
+
])
460
+
```
461
+
450
462
### Validating uploaded images
451
463
452
464
You may use the `fileAttachmentsAcceptedFileTypes()` method to control a list of accepted mime types for uploaded images. By default, `image/png`, `image/jpeg`, `image/gif`, and `image/webp` are accepted:
0 commit comments