Skip to content

Commit ce986fd

Browse files
committed
fix: Authorize upload by userId check
1 parent de7b303 commit ce986fd

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

server/middleware/s3.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// @ts-nocheck
22
export default defineEventHandler((event) => {
3-
const isS3Mutation = getRequestURL(event).pathname.includes('s3/mutation')
4-
const isS3Query = getRequestURL(event).pathname.includes('s3/query')
3+
const { pathname } = getRequestURL(event)
4+
const isS3Mutation = pathname.startsWith('/api/s3/mutation')
5+
const isS3Query = pathname.startsWith('/api/s3/query')
56

67
if (isS3Mutation) {
7-
checkUser(event)
8+
checkUpload(event)
89

910
// https://github.com/unjs/nitro/issues/1719
1011
const isBase64 = (value: string) => {

server/utils/auth.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { H3Event } from 'h3'
22
import { compareSync } from '#auth'
3+
import { getKey } from '#s3'
34

45
export async function checkDevice (event: H3Event) {
56
const deviceId = event.context.params?.id
@@ -29,3 +30,15 @@ export function checkUser (event: H3Event) {
2930
}
3031
throw createUnauthorizedError()
3132
}
33+
34+
export function checkUpload (event: H3Event) {
35+
const { userId } = checkUser(event)
36+
37+
const key = getKey(event)
38+
39+
const userIdFromKey = key.split('/')[0]
40+
41+
if (userId !== userIdFromKey) {
42+
throw createUnauthorizedError()
43+
}
44+
}

0 commit comments

Comments
 (0)