Skip to content

Commit

Permalink
docs: add note about CSP when serving blobs
Browse files Browse the repository at this point in the history
  • Loading branch information
atinux committed Oct 22, 2024
1 parent 4d8c0bb commit 47448a2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
18 changes: 16 additions & 2 deletions docs/content/1.docs/2.features/blob.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ Returns [`BlobListResult`](#bloblistresult).

### `serve()`

Returns a blob's data.

Returns a blob's data and sets `Content-Type`, `Content-Length` and `ETag` headers.

::code-group
```ts [server/routes/images/[...pathname\\].get.ts]
Expand All @@ -91,6 +90,21 @@ export default eventHandler(async (event) => {
```
::

::important
To prevent XSS attacks, make sure to control the Content type of the blob you serve.
::

You can also set a `Content-Security-Policy` header to add an additional layer of security:

```ts [server/api/images/[...pathname\\].get.ts]
export default eventHandler(async (event) => {
const { pathname } = getRouterParams(event)

setHeader(event, 'Content-Security-Policy', 'default-src \'none\';')
return hubBlob().serve(event, pathname)
})
```

#### Params

::field-group
Expand Down
2 changes: 1 addition & 1 deletion playground/server/api/blob/[...pathname].get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ export default eventHandler(async (event) => {
const { pathname } = await getValidatedRouterParams(event, z.object({
pathname: z.string().min(1)
}).parse)

setHeader(event, 'Content-Security-Policy', 'default-src \'none\';')
return hubBlob().serve(event, pathname)
})

0 comments on commit 47448a2

Please sign in to comment.