mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-29 20:34:28 +00:00
fix(security): improve access control and storage security (#518)
* fix(security): improve access control and storage security * fix(security): set NODE_ENV in dockerfiles and sign local storage get urls with lru cache
This commit is contained in:
+29
-15
@@ -1,10 +1,38 @@
|
||||
import { Hono } from 'hono'
|
||||
import { buildContentDisposition, s3Service } from '@shumai/core/src/s3/s3'
|
||||
import {
|
||||
buildContentDisposition,
|
||||
getStorageBackend,
|
||||
verifyLocalGetUrlSignature,
|
||||
} from '@shumai/core/src/s3/s3'
|
||||
import { serveStatic } from 'hono/bun'
|
||||
|
||||
const route = new Hono()
|
||||
.use('*', async (c, next) => {
|
||||
if (getStorageBackend() !== 'local') {
|
||||
return c.notFound()
|
||||
}
|
||||
return next()
|
||||
})
|
||||
.get(
|
||||
'/*',
|
||||
async (c, next) => {
|
||||
const fullPath = c.req.path.replace(/^\/files\//, '').replace(/^\//, '')
|
||||
const slashIdx = fullPath.indexOf('/')
|
||||
if (slashIdx === -1) {
|
||||
return c.notFound()
|
||||
}
|
||||
const bucket = fullPath.slice(0, slashIdx)
|
||||
const key = fullPath.slice(slashIdx + 1)
|
||||
|
||||
const signature = c.req.query('Signature')
|
||||
const expires = c.req.query('Expires')
|
||||
|
||||
if (!verifyLocalGetUrlSignature(bucket, key, signature, expires)) {
|
||||
return c.json({ error: 'Forbidden: invalid or expired signature' }, 403)
|
||||
}
|
||||
|
||||
return next()
|
||||
},
|
||||
serveStatic({
|
||||
root: './data',
|
||||
rewriteRequestPath: (path) => path.replace(/^\/files\//, ''),
|
||||
@@ -15,19 +43,5 @@ const route = new Hono()
|
||||
},
|
||||
}),
|
||||
)
|
||||
.put('/:bucket/:key{.+}', async (c) => {
|
||||
const bucket = c.req.param('bucket')
|
||||
const key = c.req.param('key')
|
||||
const contentLength = parseInt(c.req.header('content-length') || '0', 10)
|
||||
|
||||
try {
|
||||
const body = c.req.raw.body ?? (await c.req.arrayBuffer())
|
||||
await s3Service.putObject(bucket, key, body, contentLength)
|
||||
return c.text('OK')
|
||||
} catch (e: unknown) {
|
||||
const error = e as Error
|
||||
return c.text(error.message || 'Internal Server Error', 500)
|
||||
}
|
||||
})
|
||||
|
||||
export default route
|
||||
|
||||
Reference in New Issue
Block a user