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
+54 -15
View File
@@ -16,11 +16,14 @@ describe('S3 API', () => {
vi.resetModules()
})
it('GET /files/* calls serveStatic with correct options', async () => {
it('GET /files/* calls serveStatic with correct options when valid signature is provided', async () => {
const { signLocalGetUrl } = await import('@shumai/core/src/s3/s3')
const { signature, expires } = signLocalGetUrl('b1', 'test.webp')
const { default: route } = await import('./s3')
const app = new Hono().route('/files', route)
const res = await app.request('/files/b1/test.webp')
const res = await app.request(`/files/b1/test.webp?Signature=${signature}&Expires=${expires}`)
expect(res.status).toBe(200)
expect(await res.text()).toBe('static-file')
@@ -39,7 +42,40 @@ describe('S3 API', () => {
}
})
it('GET /files/* returns 403 Forbidden when signature is missing', async () => {
const { default: route } = await import('./s3')
const app = new Hono().route('/files', route)
const res = await app.request('/files/b1/test.webp')
expect(res.status).toBe(403)
const json = await res.json()
expect(json.error).toContain('Forbidden')
})
it('GET /files/* returns 403 Forbidden when signature is invalid', async () => {
const { default: route } = await import('./s3')
const app = new Hono().route('/files', route)
const res = await app.request('/files/b1/test.webp?Signature=invalidsig&Expires=9999999999')
expect(res.status).toBe(403)
})
it('GET /files/* returns 403 Forbidden when signature is expired', async () => {
const { signLocalGetUrl } = await import('@shumai/core/src/s3/s3')
// Signed with -10 seconds (expired)
const { signature, expires } = signLocalGetUrl('b1', 'test.webp', -10)
const { default: route } = await import('./s3')
const app = new Hono().route('/files', route)
const res = await app.request(`/files/b1/test.webp?Signature=${signature}&Expires=${expires}`)
expect(res.status).toBe(403)
})
it('GET /files/* sets Content-Disposition with filename when download=1 & filename query params are present', async () => {
const { signLocalGetUrl } = await import('@shumai/core/src/s3/s3')
const { signature, expires } = signLocalGetUrl('b1', 'test.webp')
// We need to capture the onFound callback from serveStatic options
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let onFoundCallback: ((path: string, c: any) => void) | undefined
@@ -54,7 +90,9 @@ describe('S3 API', () => {
const { default: route } = await import('./s3')
const app = new Hono().route('/files', route)
await app.request('/files/b1/test.webp?download=1&filename=foo.png')
await app.request(
`/files/b1/test.webp?Signature=${signature}&Expires=${expires}&download=1&filename=foo.png`,
)
expect(onFoundCallback).toBeDefined()
@@ -101,10 +139,7 @@ describe('S3 API', () => {
}
})
it('PUT /files/:bucket/:key calls s3Service.putObject', async () => {
const { s3Service: service } = await import('@shumai/core/src/s3/s3')
const mockPut = vi.spyOn(service, 'putObject').mockResolvedValue(undefined)
it('PUT /files/:bucket/:key returns 404', async () => {
const { default: route } = await import('./s3')
const app = new Hono().route('/files', route)
const body = new TextEncoder().encode('test-data')
@@ -117,13 +152,17 @@ describe('S3 API', () => {
body,
})
expect(res.status).toBe(200)
expect(await res.text()).toBe('OK')
expect(mockPut).toHaveBeenCalledWith(
'b1',
'test.webp',
expect.any(ReadableStream),
body.byteLength,
)
expect(res.status).toBe(404)
})
it('GET /files/* returns 404 when STORAGE_BACKEND is not local', async () => {
const s3Module = await import('@shumai/core/src/s3/s3')
vi.spyOn(s3Module, 'getStorageBackend').mockReturnValue('s3')
const { default: route } = await import('./s3')
const app = new Hono().route('/files', route)
const res = await app.request('/files/b1/test.webp')
expect(res.status).toBe(404)
})
})