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:
Yiling-J
2026-11-05 20:49:38 +08:00
committed by GitHub
parent 0dc0bbeb03
commit 65e2ac1383
26 changed files with 811 additions and 208 deletions
+29 -15
View File
@@ -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