Skip to content

Commit 47448a2

Browse files
committed
docs: add note about CSP when serving blobs
1 parent 4d8c0bb commit 47448a2

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

docs/content/1.docs/2.features/blob.md

+16-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ Returns [`BlobListResult`](#bloblistresult).
7373

7474
### `serve()`
7575

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

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

93+
::important
94+
To prevent XSS attacks, make sure to control the Content type of the blob you serve.
95+
::
96+
97+
You can also set a `Content-Security-Policy` header to add an additional layer of security:
98+
99+
```ts [server/api/images/[...pathname\\].get.ts]
100+
export default eventHandler(async (event) => {
101+
const { pathname } = getRouterParams(event)
102+
103+
setHeader(event, 'Content-Security-Policy', 'default-src \'none\';')
104+
return hubBlob().serve(event, pathname)
105+
})
106+
```
107+
94108
#### Params
95109

96110
::field-group

playground/server/api/blob/[...pathname].get.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ export default eventHandler(async (event) => {
22
const { pathname } = await getValidatedRouterParams(event, z.object({
33
pathname: z.string().min(1)
44
}).parse)
5-
5+
setHeader(event, 'Content-Security-Policy', 'default-src \'none\';')
66
return hubBlob().serve(event, pathname)
77
})

0 commit comments

Comments
 (0)