Skip to content

Commit 60fa4e2

Browse files
committed
docs: add content type handling and upload permissions to Upload plugin documentation
AdminForth/1866/https-claude.aicodeartifact1b9
1 parent f9212c5 commit 60fa4e2

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

adminforth/documentation/docs/tutorial/09-Plugins/05-0-upload.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,37 @@ new UploadPlugin({
312312
313313
314314
315+
## Content types and who can upload
316+
317+
The content type a file is stored with is derived from its extension on the backend, never taken from the browser. The storage replays that content type when the file is served back, so a client which asks for `text/html` would be able to store a page that executes scripts on your admin origin with the session of whoever opens it.
318+
319+
The plugin knows the common image, video, audio, document, archive and font extensions. If an extension can't be resolved to a content type, no upload URL is issued, and `allowedFileExtensions` is validated at startup, so a typo or an unknown extension fails fast:
320+
321+
```
322+
Upload plugin can't resolve content type for extension "jpgg". Provide contentTypeByExtension in plugin options
323+
```
324+
325+
Use `contentTypeByExtension` to add extensions the plugin doesn't know, or to override the built-in mapping:
326+
327+
```ts title="./index.ts"
328+
new UploadPlugin({
329+
...
330+
allowedFileExtensions: ['jpg', 'png', 'dwg'],
331+
//diff-add
332+
contentTypeByExtension: {
333+
//diff-add
334+
dwg: 'application/acad',
335+
//diff-add
336+
},
337+
})
338+
```
339+
340+
:::warning
341+
Mapping an extension to a content type which browsers execute (`text/html`, `image/svg+xml`, `application/xhtml+xml`, ...) allows anyone who can upload files to run scripts on your admin panel origin with the session of the user who opens the file. Only do it if your storage serves files from a separate origin or with `Content-Disposition: attachment`.
342+
:::
343+
344+
Requesting an upload URL requires permission to write the record the file belongs to: `edit` on the target record when editing an existing one, `create` otherwise. Users who can only view the resource get `403` even if they call the API directly, and `maxFileSize` is enforced on the backend as well, not only in the browser.
345+
315346
## Image generation
316347
317348
Upload plugin supports AI generation for images. Yo use it you need to install image generation adapter.

0 commit comments

Comments
 (0)