mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-19 14:16:29 +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:
@@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
|
||||
|
||||
### Fixed
|
||||
|
||||
- **security**: ⚠️ Several security improvements and access control hardenings. Upgrading to this release is strongly recommended.
|
||||
|
||||
### Changed
|
||||
|
||||
|
||||
@@ -30,6 +30,8 @@ RUN bun run scripts/extract-runtime-deps.ts
|
||||
# Stage 2: Runner
|
||||
FROM oven/bun:1.4.4 AS runner
|
||||
|
||||
ENV NODE_ENV=production
|
||||
|
||||
# Install system dependencies and Jellyfin-FFmpeg (with VA-API support)
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
python3 \
|
||||
|
||||
@@ -25,6 +25,8 @@ RUN bun run scripts/extract-runtime-deps.ts
|
||||
# Stage 2: Runner
|
||||
FROM oven/bun:1.4.4 AS runner
|
||||
|
||||
ENV NODE_ENV=production
|
||||
|
||||
# Install transcoding system dependencies and Jellyfin-FFmpeg (with VA-API/hardware acceleration support)
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
curl \
|
||||
|
||||
@@ -2,16 +2,19 @@ import { Hono } from 'hono'
|
||||
import { teamService } from '@shumai/core/src/team/team'
|
||||
import { userService } from '@shumai/core/src/user/user'
|
||||
import { auth } from '@shumai/core/src/auth/auth'
|
||||
import { createGuestToken } from '@shumai/core/src/auth/guest-token'
|
||||
import { isArchiveStorageEnabled } from '@shumai/core/src/s3/archive-storage'
|
||||
import { z } from 'zod'
|
||||
import { zValidator } from '@hono/zod-validator'
|
||||
import { ulid } from 'ulid'
|
||||
import { setCookie } from 'hono/cookie'
|
||||
|
||||
const app = new Hono()
|
||||
|
||||
const identifySchema = z.object({
|
||||
username: z.string().min(1),
|
||||
email: z.string().email(),
|
||||
shareId: z.string().min(1).optional(),
|
||||
})
|
||||
|
||||
const route = app
|
||||
@@ -30,7 +33,7 @@ const route = app
|
||||
return c.json({ id: session?.user?.id ?? null })
|
||||
})
|
||||
.post('/identify', zValidator('json', identifySchema), async (c) => {
|
||||
const { username, email } = c.req.valid('json')
|
||||
const { username, email, shareId } = c.req.valid('json')
|
||||
|
||||
const dummyEmail = `guest_${ulid()}@guest.local`
|
||||
const user = await userService.createGuestUser({
|
||||
@@ -39,6 +42,17 @@ const route = app
|
||||
guestEmail: email,
|
||||
})
|
||||
|
||||
if (shareId) {
|
||||
const token = createGuestToken({ userId: user.id, shareId })
|
||||
setCookie(c, `shumai_guest_${shareId}`, token, {
|
||||
httpOnly: true,
|
||||
path: '/',
|
||||
sameSite: 'Lax',
|
||||
secure: c.req.url.startsWith('https://') || process.env.NODE_ENV === 'production',
|
||||
maxAge: 30 * 24 * 60 * 60,
|
||||
})
|
||||
}
|
||||
|
||||
return c.json({ id: user.id })
|
||||
})
|
||||
.all('/auth/*', (c) => {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Hono } from 'hono'
|
||||
import type { Context } from 'hono'
|
||||
import { getCookie } from 'hono/cookie'
|
||||
import { zValidator } from '@hono/zod-validator'
|
||||
import { shareService } from '@shumai/core/src/share/share'
|
||||
import { assetService } from '@shumai/core/src/asset/asset'
|
||||
@@ -12,6 +13,7 @@ import type { Prisma as PrismaType } from '@shumai/db'
|
||||
import { projectService } from '@shumai/core/src/project/project'
|
||||
import { userService } from '@shumai/core/src/user/user'
|
||||
import { auth } from '@shumai/core/src/auth/auth'
|
||||
import { verifyGuestToken } from '@shumai/core/src/auth/guest-token'
|
||||
import { createCommentRequestSchema } from '@shumai/dtos'
|
||||
import { notificationService } from '@shumai/core/src/notification/notification'
|
||||
import {
|
||||
@@ -160,7 +162,7 @@ const route = app
|
||||
const password = c.req.header('x-share-password')
|
||||
try {
|
||||
const shareLink = await shareService.getShareLink(shareId)
|
||||
await shareService.verifyPublicAccess(shareLink.rootFolderId, password)
|
||||
await shareService.verifyPublicAccess(shareId, shareLink.rootFolderId, password)
|
||||
return c.json({
|
||||
id: shareLink.id,
|
||||
name: shareLink.name,
|
||||
@@ -184,7 +186,7 @@ const route = app
|
||||
|
||||
try {
|
||||
const shareLink = await shareService.getShareLink(shareId)
|
||||
await shareService.verifyPublicAccess(shareLink.rootFolderId, password)
|
||||
await shareService.verifyPublicAccess(shareId, shareLink.rootFolderId, password)
|
||||
|
||||
const fields = await metadataService.listProjectFields(null, shareLink.projectId)
|
||||
const fieldVisibility = (shareLink.fieldVisibility || {}) as Record<string, boolean>
|
||||
@@ -202,13 +204,14 @@ const route = app
|
||||
'/shares/:shareId/folders/:folderId/children',
|
||||
zValidator('query', sharedChildrenRequestSchema),
|
||||
async (c) => {
|
||||
const shareId = c.req.param('shareId')
|
||||
const folderId = c.req.param('folderId')
|
||||
const password =
|
||||
c.req.header('x-share-password') || c.req.query('password') || c.req.query('p')
|
||||
const req = c.req.valid('query')
|
||||
|
||||
try {
|
||||
const shareLink = await shareService.verifyPublicAccess(folderId, password)
|
||||
const shareLink = await shareService.verifyPublicAccess(shareId, folderId, password)
|
||||
|
||||
let sort: string | undefined
|
||||
let order: string | undefined
|
||||
@@ -264,11 +267,12 @@ const route = app
|
||||
},
|
||||
)
|
||||
.get('/shares/:shareId/files/:fileId', async (c) => {
|
||||
const shareId = c.req.param('shareId')
|
||||
const fileId = c.req.param('fileId')
|
||||
const password = c.req.header('x-share-password') || c.req.query('password') || c.req.query('p')
|
||||
|
||||
try {
|
||||
const shareLink = await shareService.verifyPublicAccess(fileId, password)
|
||||
const shareLink = await shareService.verifyPublicAccess(shareId, fileId, password)
|
||||
|
||||
let asset = await assetService.getAsset({ assetId: fileId })
|
||||
|
||||
@@ -303,12 +307,13 @@ const route = app
|
||||
'/shares/:shareId/files/:fileId/download-url',
|
||||
zValidator('json', z.object({ key: z.string().min(1) })),
|
||||
async (c) => {
|
||||
const shareId = c.req.param('shareId')
|
||||
const fileId = c.req.param('fileId')
|
||||
const password = c.req.header('x-share-password')
|
||||
const { key } = c.req.valid('json')
|
||||
|
||||
try {
|
||||
const shareLink = await shareService.verifyPublicAccess(fileId, password)
|
||||
const shareLink = await shareService.verifyPublicAccess(shareId, fileId, password)
|
||||
|
||||
if (!shareLink.allowDownload) {
|
||||
throw new ShareLinkDownloadDisabledError('Download is disabled for this share link')
|
||||
@@ -329,12 +334,13 @@ const route = app
|
||||
'/shares/:shareId/files/:fileId/comments',
|
||||
zValidator('query', paginationParamsSchema),
|
||||
async (c) => {
|
||||
const shareId = c.req.param('shareId')
|
||||
const fileId = c.req.param('fileId')
|
||||
const password = c.req.header('x-share-password')
|
||||
const req = c.req.valid('query')
|
||||
|
||||
try {
|
||||
await shareService.verifyPublicAccess(fileId, password)
|
||||
await shareService.verifyPublicAccess(shareId, fileId, password)
|
||||
|
||||
const targetFileId = await assetService.resolveTargetAssetId(fileId)
|
||||
let userId: string | undefined
|
||||
@@ -343,6 +349,18 @@ const route = app
|
||||
})
|
||||
if (session?.user) {
|
||||
userId = session.user.id
|
||||
} else {
|
||||
const cookieName = `shumai_guest_${shareId}`
|
||||
const token = getCookie(c, cookieName) || getCookie(c, 'shumai_guest')
|
||||
if (token) {
|
||||
const verified = verifyGuestToken(token, shareId)
|
||||
if (verified) {
|
||||
const guestUser = await userService.getUserById(verified.userId)
|
||||
if (guestUser && guestUser.guestEmail) {
|
||||
userId = guestUser.id
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
const res = await assetService.listComments(targetFileId, req, userId)
|
||||
return c.json(res)
|
||||
@@ -358,12 +376,15 @@ const route = app
|
||||
const shareId = c.req.param('shareId')
|
||||
const fileId = c.req.param('fileId')
|
||||
const password = c.req.header('x-share-password')
|
||||
const guestUserId = c.req.header('x-guest-user-id')
|
||||
const req = c.req.valid('json')
|
||||
|
||||
if (req.attachmentIds && req.attachmentIds.length > 0) {
|
||||
return c.json({ error: 'Attachments are not allowed on public share comments' }, 400)
|
||||
}
|
||||
|
||||
try {
|
||||
const shareLink = await shareService.getShareLink(shareId)
|
||||
await shareService.verifyPublicAccess(fileId, password)
|
||||
await shareService.verifyPublicAccess(shareId, fileId, password)
|
||||
|
||||
const targetFileId = await assetService.resolveTargetAssetId(fileId)
|
||||
|
||||
@@ -377,10 +398,17 @@ const route = app
|
||||
|
||||
if (session?.user) {
|
||||
userId = session.user.id
|
||||
} else if (guestUserId) {
|
||||
const guestUser = await userService.getUserById(guestUserId)
|
||||
if (guestUser) {
|
||||
userId = guestUser.id
|
||||
} else {
|
||||
const cookieName = `shumai_guest_${shareId}`
|
||||
const token = getCookie(c, cookieName) || getCookie(c, 'shumai_guest')
|
||||
if (token) {
|
||||
const verified = verifyGuestToken(token, shareId)
|
||||
if (verified) {
|
||||
const guestUser = await userService.getUserById(verified.userId)
|
||||
if (guestUser && guestUser.guestEmail) {
|
||||
userId = guestUser.id
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -400,7 +428,7 @@ const route = app
|
||||
|
||||
const notifType = req.replyToId ? 'reply_created' : 'comment_created'
|
||||
|
||||
notificationService.create({
|
||||
await notificationService.create({
|
||||
type: notifType,
|
||||
teamId: teamId,
|
||||
creatorId: userId,
|
||||
@@ -415,11 +443,12 @@ const route = app
|
||||
},
|
||||
)
|
||||
.get('/shares/:shareId/files/:fileId/m3u8', async (c) => {
|
||||
const shareId = c.req.param('shareId')
|
||||
const fileId = c.req.param('fileId')
|
||||
const password = c.req.header('x-share-password') || c.req.query('password') || c.req.query('p')
|
||||
|
||||
try {
|
||||
const shareLink = await shareService.verifyPublicAccess(fileId, password)
|
||||
const shareLink = await shareService.verifyPublicAccess(shareId, fileId, password)
|
||||
const targetAssetId = await assetService.resolveLatestVersionId(fileId)
|
||||
let playlist = await assetService.getHlsMasterPlaylist({
|
||||
assetId: targetAssetId,
|
||||
@@ -438,11 +467,12 @@ const route = app
|
||||
}
|
||||
})
|
||||
.get('/shares/:shareId/files/:fileId/m3u8/master.m3u8', async (c) => {
|
||||
const shareId = c.req.param('shareId')
|
||||
const fileId = c.req.param('fileId')
|
||||
const password = c.req.header('x-share-password') || c.req.query('password') || c.req.query('p')
|
||||
|
||||
try {
|
||||
const shareLink = await shareService.verifyPublicAccess(fileId, password)
|
||||
const shareLink = await shareService.verifyPublicAccess(shareId, fileId, password)
|
||||
const targetAssetId = await assetService.resolveLatestVersionId(fileId)
|
||||
let playlist = await assetService.getHlsMasterPlaylist({
|
||||
assetId: targetAssetId,
|
||||
@@ -461,6 +491,7 @@ const route = app
|
||||
}
|
||||
})
|
||||
.get('/shares/:shareId/files/:fileId/m3u8/:resolution', async (c) => {
|
||||
const shareId = c.req.param('shareId')
|
||||
const fileId = c.req.param('fileId')
|
||||
let resolution = c.req.param('resolution')
|
||||
if (resolution.endsWith('.m3u8')) {
|
||||
@@ -469,7 +500,7 @@ const route = app
|
||||
const password = c.req.header('x-share-password') || c.req.query('password') || c.req.query('p')
|
||||
|
||||
try {
|
||||
const shareLink = await shareService.verifyPublicAccess(fileId, password)
|
||||
const shareLink = await shareService.verifyPublicAccess(shareId, fileId, password)
|
||||
const targetAssetId = await assetService.resolveLatestVersionId(fileId)
|
||||
const playlist = await assetService.getHlsVariantPlaylist({
|
||||
assetId: targetAssetId,
|
||||
@@ -483,12 +514,13 @@ const route = app
|
||||
}
|
||||
})
|
||||
.get('/shares/:shareId/files/:fileId/m3u8/:resolution/index.m3u8', async (c) => {
|
||||
const shareId = c.req.param('shareId')
|
||||
const fileId = c.req.param('fileId')
|
||||
const resolution = c.req.param('resolution')
|
||||
const password = c.req.header('x-share-password') || c.req.query('password') || c.req.query('p')
|
||||
|
||||
try {
|
||||
const shareLink = await shareService.verifyPublicAccess(fileId, password)
|
||||
const shareLink = await shareService.verifyPublicAccess(shareId, fileId, password)
|
||||
const targetAssetId = await assetService.resolveLatestVersionId(fileId)
|
||||
const playlist = await assetService.getHlsVariantPlaylist({
|
||||
assetId: targetAssetId,
|
||||
|
||||
+54
-15
@@ -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)
|
||||
})
|
||||
})
|
||||
|
||||
+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
|
||||
|
||||
@@ -7,6 +7,9 @@ import { shareService } from '@shumai/core/src/share/share'
|
||||
import { assetService } from '@shumai/core/src/asset/asset'
|
||||
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
||||
import { ShareLinkPasswordInvalidError, ShareLinkExpiredError } from '@shumai/core/src/share/errors'
|
||||
import { createGuestToken } from '@shumai/core/src/auth/guest-token'
|
||||
import { userService } from '@shumai/core/src/user/user'
|
||||
import { notificationService } from '@shumai/core/src/notification/notification'
|
||||
|
||||
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
|
||||
import { watermarkService } from '@shumai/core/src/watermark/watermark'
|
||||
@@ -121,7 +124,7 @@ describe('Share API', () => {
|
||||
expect(body.name).toBe('Public Share')
|
||||
expect(body.hasPassword).toBe(true)
|
||||
expect(body.allowDownload).toBe(false)
|
||||
expect(shareService.verifyPublicAccess).toHaveBeenCalledWith('folder1', 'pass')
|
||||
expect(shareService.verifyPublicAccess).toHaveBeenCalledWith('share1', 'folder1', 'pass')
|
||||
})
|
||||
|
||||
test('Unauthorized', async () => {
|
||||
@@ -206,7 +209,7 @@ describe('Share API', () => {
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
expect(shareService.verifyPublicAccess).toHaveBeenCalledWith('folder1', 'pass')
|
||||
expect(shareService.verifyPublicAccess).toHaveBeenCalledWith('share1', 'folder1', 'pass')
|
||||
expect(assetService.listChildren).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
assetId: 'folder1',
|
||||
@@ -989,7 +992,7 @@ describe('Share API', () => {
|
||||
expect(res.status).toBe(200)
|
||||
const body = await res.json()
|
||||
expect(body.url).toBe('https://s3.example.com/download')
|
||||
expect(shareService.verifyPublicAccess).toHaveBeenCalledWith('symlink1', undefined)
|
||||
expect(shareService.verifyPublicAccess).toHaveBeenCalledWith('share1', 'symlink1', undefined)
|
||||
expect(assetService.getDownloadUrl).toHaveBeenCalledWith('symlink1', 'files/target1/fly.png')
|
||||
})
|
||||
|
||||
@@ -1033,7 +1036,7 @@ describe('Share API', () => {
|
||||
expect(res.headers.get('content-type')).toContain('application/vnd.apple.mpegurl')
|
||||
const text = await res.text()
|
||||
expect(text).toContain('1080p/index.m3u8?p=secret123')
|
||||
expect(shareService.verifyPublicAccess).toHaveBeenCalledWith('file1', 'secret123')
|
||||
expect(shareService.verifyPublicAccess).toHaveBeenCalledWith('share1', 'file1', 'secret123')
|
||||
expect(assetService.getHlsMasterPlaylist).toHaveBeenCalledWith({
|
||||
assetId: 'file1',
|
||||
watermarkConfigId: null,
|
||||
@@ -1093,4 +1096,106 @@ describe('Share API', () => {
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('POST /shares/:shareId/files/:fileId/comments', () => {
|
||||
test('rejects comments with attachmentIds on public shares', async () => {
|
||||
const res = await app.request('/shares/share1/files/file1/comments', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
message: 'hi',
|
||||
attachmentIds: ['att1'],
|
||||
}),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(400)
|
||||
const body = await res.json()
|
||||
expect(body.error).toContain('Attachments are not allowed')
|
||||
})
|
||||
|
||||
test('rejects spoofed x-guest-user-id header without cookie', async () => {
|
||||
vi.spyOn(shareService, 'getShareLink').mockResolvedValue({
|
||||
id: 'share1',
|
||||
projectId: 'p1',
|
||||
} as unknown as Awaited<ReturnType<typeof shareService.getShareLink>>)
|
||||
vi.spyOn(shareService, 'verifyPublicAccess').mockResolvedValue({
|
||||
id: 'share1',
|
||||
} as unknown as Awaited<ReturnType<typeof shareService.verifyPublicAccess>>)
|
||||
vi.spyOn(assetService, 'resolveTargetAssetId').mockResolvedValue('file1')
|
||||
|
||||
const res = await app.request('/shares/share1/files/file1/comments', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'x-guest-user-id': 'victim_user',
|
||||
},
|
||||
body: JSON.stringify({ message: 'spoofed comment', attachmentIds: [] }),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(401)
|
||||
})
|
||||
|
||||
test('allows valid guest cookie bound to shareId and creates comment', async () => {
|
||||
vi.spyOn(shareService, 'getShareLink').mockResolvedValue({
|
||||
id: 'share1',
|
||||
projectId: 'p1',
|
||||
} as unknown as Awaited<ReturnType<typeof shareService.getShareLink>>)
|
||||
vi.spyOn(shareService, 'verifyPublicAccess').mockResolvedValue({
|
||||
id: 'share1',
|
||||
} as unknown as Awaited<ReturnType<typeof shareService.verifyPublicAccess>>)
|
||||
vi.spyOn(assetService, 'resolveTargetAssetId').mockResolvedValue('file1')
|
||||
vi.spyOn(userService, 'getUserById').mockResolvedValue({
|
||||
id: 'guest1',
|
||||
name: 'Guest Alice',
|
||||
guestEmail: 'alice@guest.com',
|
||||
} as unknown as Awaited<ReturnType<typeof userService.getUserById>>)
|
||||
vi.spyOn(assetService, 'createComment').mockResolvedValue({
|
||||
id: 'comm1',
|
||||
message: 'hello from alice',
|
||||
} as unknown as Awaited<ReturnType<typeof assetService.createComment>>)
|
||||
vi.spyOn(notificationService, 'create').mockResolvedValue(
|
||||
undefined as unknown as Awaited<ReturnType<typeof notificationService.create>>,
|
||||
)
|
||||
|
||||
const token = createGuestToken({ userId: 'guest1', shareId: 'share1' })
|
||||
const res = await app.request('/shares/share1/files/file1/comments', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Cookie: `shumai_guest_share1=${token}`,
|
||||
},
|
||||
body: JSON.stringify({ message: 'hello from alice', attachmentIds: [] }),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(201)
|
||||
expect(assetService.createComment).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
userId: 'guest1',
|
||||
message: 'hello from alice',
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
test('rejects guest cookie bound to a different shareId', async () => {
|
||||
vi.spyOn(shareService, 'getShareLink').mockResolvedValue({
|
||||
id: 'share1',
|
||||
projectId: 'p1',
|
||||
} as unknown as Awaited<ReturnType<typeof shareService.getShareLink>>)
|
||||
vi.spyOn(shareService, 'verifyPublicAccess').mockResolvedValue({
|
||||
id: 'share1',
|
||||
} as unknown as Awaited<ReturnType<typeof shareService.verifyPublicAccess>>)
|
||||
|
||||
const wrongToken = createGuestToken({ userId: 'guest1', shareId: 'other-share' })
|
||||
const res = await app.request('/shares/share1/files/file1/comments', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Cookie: `shumai_guest_share1=${wrongToken}`,
|
||||
},
|
||||
body: JSON.stringify({ message: 'should fail', attachmentIds: [] }),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(401)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -5,7 +5,6 @@ import teamRoute from './team'
|
||||
import { authMiddleware } from './middleware/auth'
|
||||
import { teamService } from '@shumai/core/src/team/team'
|
||||
import { userMetadataService } from '@shumai/core/src/user-metadata/user-metadata'
|
||||
import { notificationService } from '@shumai/core/src/notification/notification'
|
||||
import { authzService, ResourceType, Permission } from '@shumai/core/src/authz/authz'
|
||||
|
||||
vi.mock('@shumai/core/src/authz/authz', () => ({
|
||||
@@ -80,14 +79,12 @@ describe('team api', () => {
|
||||
const app = new Hono().use('*', authMiddleware).route('/', teamRoute)
|
||||
let mockCreateTeam: any // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||
let mockGetUserTeams: any // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||
let mockJoinTeam: any // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||
let mockGetMe: any // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||
let mockGetTeamMembers: any // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||
let mockGetSettings: any // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||
let mockUpdateSettings: any // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||
let mockListUserMetadata: any // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||
let mockUpsertUserMetadata: any // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||
let mockCreateNotification: any // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||
let mockGetSandboxSettings: any // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||
let mockUpdateSandboxSettings: any // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||
let mockUpdateMe: any // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||
@@ -95,14 +92,12 @@ describe('team api', () => {
|
||||
beforeEach(() => {
|
||||
mockCreateTeam = vi.spyOn(teamService, 'createTeam')
|
||||
mockGetUserTeams = vi.spyOn(teamService, 'getUserTeams')
|
||||
mockJoinTeam = vi.spyOn(teamService, 'joinTeam')
|
||||
mockGetMe = vi.spyOn(teamService, 'getMe')
|
||||
mockGetTeamMembers = vi.spyOn(teamService, 'getTeamMembers')
|
||||
mockGetSettings = vi.spyOn(teamService, 'getSettings')
|
||||
mockUpdateSettings = vi.spyOn(teamService, 'updateSettings')
|
||||
mockListUserMetadata = vi.spyOn(userMetadataService, 'listMetadata')
|
||||
mockUpsertUserMetadata = vi.spyOn(userMetadataService, 'upsertMetadata')
|
||||
mockCreateNotification = vi.spyOn(notificationService, 'create').mockResolvedValue()
|
||||
mockGetSandboxSettings = vi.spyOn(teamService, 'getSandboxSettings')
|
||||
mockUpdateSandboxSettings = vi.spyOn(teamService, 'updateSandboxSettings')
|
||||
mockUpdateMe = vi.spyOn(teamService, 'updateMe')
|
||||
@@ -148,20 +143,9 @@ describe('team api', () => {
|
||||
expect(mockGetUserTeams).toHaveBeenCalledWith(expect.objectContaining({ userId: 'user1' }))
|
||||
})
|
||||
|
||||
it('POST /teams/:teamId/members joins team and sends notification', async () => {
|
||||
mockJoinTeam.mockResolvedValue(undefined)
|
||||
|
||||
it('POST /teams/:teamId/members returns 404', async () => {
|
||||
const res = await app.request('/teams/t1/members', { method: 'POST' })
|
||||
|
||||
expect(res.status).toBe(204)
|
||||
expect(mockJoinTeam).toHaveBeenCalledWith({ teamId: 't1', userId: 'user1' })
|
||||
expect(mockCreateNotification).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
type: 'new_user_join_team',
|
||||
teamId: 't1',
|
||||
userId: 'user1',
|
||||
}),
|
||||
)
|
||||
expect(res.status).toBe(404)
|
||||
})
|
||||
|
||||
it('GET /teams/:teamId/me returns user role in team', async () => {
|
||||
|
||||
@@ -3,7 +3,6 @@ import { zValidator } from '@hono/zod-validator'
|
||||
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
||||
import { teamService } from '@shumai/core/src/team/team'
|
||||
import { userMetadataService } from '@shumai/core/src/user-metadata/user-metadata'
|
||||
import { notificationService } from '@shumai/core/src/notification/notification'
|
||||
import {
|
||||
createTeamRequestSchema,
|
||||
getUserTeamsRequestSchema,
|
||||
@@ -31,7 +30,6 @@ import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
|
||||
import { workflowTaskService } from '@shumai/core/src/workflowTask/workflowTask'
|
||||
import { turntableService } from '@shumai/core/src/turntable/turntable'
|
||||
import { gotenbergService } from '@shumai/core/src/gotenberg/gotenberg'
|
||||
import { NotificationType } from '@shumai/db'
|
||||
import type { Prisma } from '@shumai/db'
|
||||
|
||||
type User = Prisma.UserGetPayload<Record<string, never>>
|
||||
@@ -67,30 +65,6 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
},
|
||||
})
|
||||
})
|
||||
.post('/teams/:teamId/members', async (c) => {
|
||||
const user = c.get('user')
|
||||
const teamId = c.req.param('teamId')
|
||||
|
||||
await teamService.joinTeam({
|
||||
teamId,
|
||||
userId: user.id,
|
||||
})
|
||||
|
||||
await notificationService.create({
|
||||
type: NotificationType.new_user_join_team,
|
||||
teamId,
|
||||
userId: user.id,
|
||||
})
|
||||
|
||||
await auditLogService.logAction({
|
||||
action: AuditAction.team_member_add,
|
||||
teamId,
|
||||
userId: user.id,
|
||||
itemId: user.id,
|
||||
})
|
||||
|
||||
return new Response(null, { status: 204 })
|
||||
})
|
||||
.get('/teams/:teamId/me', async (c) => {
|
||||
const user = c.get('user')
|
||||
const teamId = c.req.param('teamId')
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { describe, expect, it, vi, beforeEach } from 'vitest'
|
||||
import { Hono } from 'hono'
|
||||
import uploadRoute from './upload'
|
||||
import uploadRoute, { localUploadRoute } from './upload'
|
||||
import { authMiddleware } from './middleware/auth'
|
||||
import { uploadService } from '@shumai/core/src/upload/upload'
|
||||
import * as s3Module from '@shumai/core/src/s3/s3'
|
||||
import type { CreateUploadTaskResponse, TaskInfo } from '@shumai/dtos'
|
||||
import type { PaginatedData } from '@shumai/core/src/pagination'
|
||||
import { authzService, ResourceType, Permission } from '@shumai/core/src/authz/authz'
|
||||
@@ -243,4 +244,15 @@ describe('Upload API', () => {
|
||||
)
|
||||
expect(uploadService.cancelUploadTask).toHaveBeenCalledWith('team1', 'user1', 'task1')
|
||||
})
|
||||
|
||||
describe('PUT /upload/local', () => {
|
||||
it('returns 404 when STORAGE_BACKEND is s3', async () => {
|
||||
vi.spyOn(s3Module, 'getStorageBackend').mockReturnValue('s3')
|
||||
const localApp = new Hono().route('/', localUploadRoute)
|
||||
const res = await localApp.request('/upload/local?bucket=b&key=k&Signature=sig', {
|
||||
method: 'PUT',
|
||||
})
|
||||
expect(res.status).toBe(404)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -14,7 +14,7 @@ import { notificationService } from '@shumai/core/src/notification/notification'
|
||||
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
|
||||
import { assetService } from '@shumai/core/src/asset/asset'
|
||||
import { NotificationType } from '@shumai/db'
|
||||
import { s3Service, verifyLocalUrlSignature } from '@shumai/core/src/s3/s3'
|
||||
import { getStorageBackend, s3Service, verifyLocalUrlSignature } from '@shumai/core/src/s3/s3'
|
||||
import type { Prisma } from '@shumai/db'
|
||||
|
||||
type User = Prisma.UserGetPayload<Record<string, never>>
|
||||
@@ -23,6 +23,10 @@ export const localUploadRoute = new Hono().put(
|
||||
'/upload/local',
|
||||
zValidator('query', localUploadQuerySchema),
|
||||
async (c) => {
|
||||
if (getStorageBackend() !== 'local') {
|
||||
return c.text('Not found', 404)
|
||||
}
|
||||
|
||||
const { bucket, key, Signature } = c.req.valid('query')
|
||||
|
||||
if (!verifyLocalUrlSignature(bucket, key, Signature)) {
|
||||
|
||||
@@ -1959,7 +1959,12 @@ export class AssetService {
|
||||
const attachmentAsset = await tx.asset.findUnique({
|
||||
where: { id: attachmentId },
|
||||
})
|
||||
if (attachmentAsset) {
|
||||
if (
|
||||
attachmentAsset &&
|
||||
(attachmentAsset.type === AssetType.attachment ||
|
||||
attachmentAsset.type === AssetType.file) &&
|
||||
attachmentAsset.projectId === a.projectId
|
||||
) {
|
||||
await tx.assetCommentAttachment.create({
|
||||
data: {
|
||||
assetId: attachmentAsset.id,
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { createGuestToken, verifyGuestToken } from './guest-token'
|
||||
|
||||
describe('guest-token', () => {
|
||||
it('creates and verifies a valid token for the expected shareId', () => {
|
||||
const token = createGuestToken({ userId: 'u1', shareId: 'share1' })
|
||||
const verified = verifyGuestToken(token, 'share1')
|
||||
expect(verified).not.toBeNull()
|
||||
expect(verified?.userId).toBe('u1')
|
||||
expect(verified?.shareId).toBe('share1')
|
||||
})
|
||||
|
||||
it('rejects a token when verified against a different shareId', () => {
|
||||
const token = createGuestToken({ userId: 'u1', shareId: 'share1' })
|
||||
const verified = verifyGuestToken(token, 'share2')
|
||||
expect(verified).toBeNull()
|
||||
})
|
||||
|
||||
it('rejects an expired token', () => {
|
||||
const token = createGuestToken({ userId: 'u1', shareId: 'share1' }, -10)
|
||||
const verified = verifyGuestToken(token, 'share1')
|
||||
expect(verified).toBeNull()
|
||||
})
|
||||
|
||||
it('rejects a tampered signature', () => {
|
||||
const token = createGuestToken({ userId: 'u1', shareId: 'share1' })
|
||||
const [payload] = token.split('.')
|
||||
const tampered = `${payload}.invalidsignature`
|
||||
const verified = verifyGuestToken(tampered, 'share1')
|
||||
expect(verified).toBeNull()
|
||||
})
|
||||
|
||||
it('rejects malformed token strings', () => {
|
||||
expect(verifyGuestToken('', 'share1')).toBeNull()
|
||||
expect(verifyGuestToken('singlepart', 'share1')).toBeNull()
|
||||
expect(verifyGuestToken('a.b.c', 'share1')).toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,69 @@
|
||||
import * as crypto from 'crypto'
|
||||
|
||||
export interface GuestTokenPayload {
|
||||
userId: string
|
||||
shareId: string
|
||||
expiresAt: number
|
||||
}
|
||||
|
||||
const GUEST_TOKEN_CONTEXT = 'shumai-guest-token:v1'
|
||||
const DEFAULT_TTL_SECONDS = 30 * 24 * 60 * 60 // 30 days
|
||||
|
||||
function getSecret(): string {
|
||||
return process.env.BETTER_AUTH_SECRET || 'shumai-guest-secret'
|
||||
}
|
||||
|
||||
function signPayload(payloadB64: string): string {
|
||||
return crypto
|
||||
.createHmac('sha256', getSecret())
|
||||
.update(`${GUEST_TOKEN_CONTEXT}:${payloadB64}`)
|
||||
.digest('base64url')
|
||||
}
|
||||
|
||||
export function createGuestToken(
|
||||
params: { userId: string; shareId: string },
|
||||
ttlSeconds: number = DEFAULT_TTL_SECONDS,
|
||||
): string {
|
||||
const payload: GuestTokenPayload = {
|
||||
userId: params.userId,
|
||||
shareId: params.shareId,
|
||||
expiresAt: Date.now() + ttlSeconds * 1000,
|
||||
}
|
||||
const payloadB64 = Buffer.from(JSON.stringify(payload)).toString('base64url')
|
||||
const signature = signPayload(payloadB64)
|
||||
return `${payloadB64}.${signature}`
|
||||
}
|
||||
|
||||
export function verifyGuestToken(token: string, expectedShareId: string): GuestTokenPayload | null {
|
||||
if (!token) return null
|
||||
const [payloadB64, signature, ...rest] = token.split('.')
|
||||
if (!payloadB64 || !signature || rest.length > 0) {
|
||||
return null
|
||||
}
|
||||
|
||||
const expectedSignature = signPayload(payloadB64)
|
||||
const actualBuf = Buffer.from(signature)
|
||||
const expectedBuf = Buffer.from(expectedSignature)
|
||||
|
||||
if (actualBuf.length !== expectedBuf.length || !crypto.timingSafeEqual(actualBuf, expectedBuf)) {
|
||||
return null
|
||||
}
|
||||
|
||||
try {
|
||||
const payload = JSON.parse(
|
||||
Buffer.from(payloadB64, 'base64url').toString('utf8'),
|
||||
) as GuestTokenPayload
|
||||
if (!payload.userId || !payload.shareId || typeof payload.expiresAt !== 'number') {
|
||||
return null
|
||||
}
|
||||
if (payload.shareId !== expectedShareId) {
|
||||
return null
|
||||
}
|
||||
if (Date.now() > payload.expiresAt) {
|
||||
return null
|
||||
}
|
||||
return payload
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
@@ -141,4 +141,33 @@ describe('Invite Service', () => {
|
||||
expect(updatedTm?.scope).toBe('team')
|
||||
expect(updatedTm?.role).toBe('editor')
|
||||
})
|
||||
|
||||
it('atomically claims invite and rejects concurrent redemptions', async () => {
|
||||
const inv = await inviteService.createTeamInvite({
|
||||
teamId: team.id,
|
||||
role: 'editor',
|
||||
inviterId: admin.id,
|
||||
})
|
||||
|
||||
const userA = await prisma.user.create({
|
||||
data: { name: 'userA', email: 'userA-invite@example.com', password: 'pass' },
|
||||
})
|
||||
const userB = await prisma.user.create({
|
||||
data: { name: 'userB', email: 'userB-invite@example.com', password: 'pass' },
|
||||
})
|
||||
|
||||
const results = await Promise.allSettled([
|
||||
inviteService.consumeInvite(inv.code, userA.id),
|
||||
inviteService.consumeInvite(inv.code, userB.id),
|
||||
])
|
||||
|
||||
const fulfilled = results.filter((r) => r.status === 'fulfilled')
|
||||
const rejected = results.filter((r) => r.status === 'rejected')
|
||||
|
||||
expect(fulfilled.length).toBe(1)
|
||||
expect(rejected.length).toBe(1)
|
||||
if (rejected[0].status === 'rejected') {
|
||||
expect(rejected[0].reason.message).toContain('invite code already used')
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
@@ -84,7 +84,11 @@ export class InviteService {
|
||||
include: { team: true, project: true },
|
||||
})
|
||||
|
||||
if (inv.used) {
|
||||
const claimed = await tx.invite.updateMany({
|
||||
where: { id: inv.id, used: false },
|
||||
data: { used: true },
|
||||
})
|
||||
if (claimed.count !== 1) {
|
||||
throw new Error('invite code already used')
|
||||
}
|
||||
|
||||
@@ -155,11 +159,6 @@ export class InviteService {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
await tx.invite.update({
|
||||
where: { id: inv.id },
|
||||
data: { used: true },
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,13 @@ import {
|
||||
ListPartsCommand,
|
||||
AbortMultipartUploadCommand,
|
||||
} from '@aws-sdk/client-s3'
|
||||
import { buildContentDisposition, LocalStorageService, S3StorageService } from './s3'
|
||||
import {
|
||||
buildContentDisposition,
|
||||
LocalStorageService,
|
||||
S3StorageService,
|
||||
signLocalGetUrl,
|
||||
verifyLocalGetUrlSignature,
|
||||
} from './s3'
|
||||
|
||||
const s3ClientConstructorSpy = vi.fn()
|
||||
const s3SendSpy = vi.fn()
|
||||
@@ -710,19 +716,25 @@ describe('S3Service implementations', () => {
|
||||
|
||||
it('should generate a local presign URL', async () => {
|
||||
const url = await localS3.presign('my-bucket', 'dir/file.txt', 'GET')
|
||||
expect(url).toBe('http://localhost:3000/files/my-bucket/dir/file.txt')
|
||||
expect(url).toContain('http://localhost:3000/files/my-bucket/dir/file.txt?')
|
||||
expect(url).toContain('Signature=')
|
||||
expect(url).toContain('Expires=')
|
||||
})
|
||||
|
||||
it('should generate a local presign URL with download param', async () => {
|
||||
const url = await localS3.presign('my-bucket', 'dir/file.txt', 'GET', true)
|
||||
expect(url).toBe('http://localhost:3000/files/my-bucket/dir/file.txt?download=1')
|
||||
expect(url).toContain('http://localhost:3000/files/my-bucket/dir/file.txt?')
|
||||
expect(url).toContain('Signature=')
|
||||
expect(url).toContain('Expires=')
|
||||
expect(url).toContain('download=1')
|
||||
})
|
||||
|
||||
it('should include an encoded filename in the local download URL', async () => {
|
||||
const url = await localS3.presign('my-bucket', 'dir/file.txt', 'GET', true, 'foo bar.png')
|
||||
expect(url).toBe(
|
||||
'http://localhost:3000/files/my-bucket/dir/file.txt?download=1&filename=foo%20bar.png',
|
||||
)
|
||||
expect(url).toContain('http://localhost:3000/files/my-bucket/dir/file.txt?')
|
||||
expect(url).toContain('Signature=')
|
||||
expect(url).toContain('Expires=')
|
||||
expect(url).toContain('download=1&filename=foo%20bar.png')
|
||||
})
|
||||
|
||||
it('should not append a filename param when none is provided', async () => {
|
||||
@@ -730,6 +742,23 @@ describe('S3Service implementations', () => {
|
||||
expect(url).not.toContain('filename=')
|
||||
})
|
||||
|
||||
it('should cache GET presigned URLs using LRU cache', async () => {
|
||||
const url1 = await localS3.presign('my-bucket', 'dir/file.txt', 'GET')
|
||||
const url2 = await localS3.presign('my-bucket', 'dir/file.txt', 'GET')
|
||||
expect(url1).toBe(url2)
|
||||
|
||||
const urlOther = await localS3.presign('my-bucket', 'dir/other.txt', 'GET')
|
||||
expect(urlOther).not.toBe(url1)
|
||||
})
|
||||
|
||||
it('should not cache download URLs', async () => {
|
||||
const url1 = await localS3.presign('my-bucket', 'dir/file.txt', 'GET', true)
|
||||
const url2 = await localS3.presign('my-bucket', 'dir/file.txt', 'GET', true)
|
||||
// download URLs include download=1
|
||||
expect(url1).toContain('download=1')
|
||||
expect(url2).toContain('download=1')
|
||||
})
|
||||
|
||||
it('should throw an error for unsupported presign methods', async () => {
|
||||
await expect(localS3.presign('b', 'k', 'POST')).rejects.toThrow()
|
||||
})
|
||||
@@ -742,7 +771,9 @@ describe('S3Service implementations', () => {
|
||||
|
||||
it('should resolveInput to presigned URL if file does not exist on disk', async () => {
|
||||
const input = await localS3.resolveInput('my-bucket', 'nonexistent.mp4')
|
||||
expect(input).toBe('http://localhost:3000/files/my-bucket/nonexistent.mp4')
|
||||
expect(input).toContain('http://localhost:3000/files/my-bucket/nonexistent.mp4?')
|
||||
expect(input).toContain('Signature=')
|
||||
expect(input).toContain('Expires=')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -832,4 +863,63 @@ describe('S3Service implementations', () => {
|
||||
).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('signLocalGetUrl and verifyLocalGetUrlSignature', () => {
|
||||
it('signs and verifies valid local get url', () => {
|
||||
const { signature, expires } = signLocalGetUrl('my-bucket', 'files/123/file.mp4')
|
||||
expect(signature).toBeDefined()
|
||||
expect(expires).toBeGreaterThan(Math.floor(Date.now() / 1000))
|
||||
|
||||
const isValid = verifyLocalGetUrlSignature(
|
||||
'my-bucket',
|
||||
'files/123/file.mp4',
|
||||
signature,
|
||||
String(expires),
|
||||
)
|
||||
expect(isValid).toBe(true)
|
||||
})
|
||||
|
||||
it('rejects expired local get url signature', () => {
|
||||
// 10 seconds in the past
|
||||
const { signature, expires } = signLocalGetUrl('my-bucket', 'files/123/file.mp4', -10)
|
||||
const isValid = verifyLocalGetUrlSignature(
|
||||
'my-bucket',
|
||||
'files/123/file.mp4',
|
||||
signature,
|
||||
String(expires),
|
||||
)
|
||||
expect(isValid).toBe(false)
|
||||
})
|
||||
|
||||
it('rejects tampered bucket or key', () => {
|
||||
const { signature, expires } = signLocalGetUrl('my-bucket', 'files/123/file.mp4')
|
||||
expect(
|
||||
verifyLocalGetUrlSignature(
|
||||
'other-bucket',
|
||||
'files/123/file.mp4',
|
||||
signature,
|
||||
String(expires),
|
||||
),
|
||||
).toBe(false)
|
||||
|
||||
expect(
|
||||
verifyLocalGetUrlSignature('my-bucket', 'files/999/other.mp4', signature, String(expires)),
|
||||
).toBe(false)
|
||||
})
|
||||
|
||||
it('rejects tampered or malformed signature or expires', () => {
|
||||
expect(
|
||||
verifyLocalGetUrlSignature('my-bucket', 'files/123/file.mp4', 'badsig', '1234567890'),
|
||||
).toBe(false)
|
||||
expect(
|
||||
verifyLocalGetUrlSignature('my-bucket', 'files/123/file.mp4', undefined, '1234567890'),
|
||||
).toBe(false)
|
||||
expect(
|
||||
verifyLocalGetUrlSignature('my-bucket', 'files/123/file.mp4', 'badsig', undefined),
|
||||
).toBe(false)
|
||||
expect(
|
||||
verifyLocalGetUrlSignature('my-bucket', 'files/123/file.mp4', 'badsig', 'not-a-number'),
|
||||
).toBe(false)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -46,6 +46,48 @@ export function verifyLocalUrlSignature(bucket: string, key: string, signature:
|
||||
}
|
||||
}
|
||||
|
||||
export const DEFAULT_LOCAL_GET_EXPIRY_SECONDS = 12 * 3600
|
||||
|
||||
export function signLocalGetUrl(
|
||||
bucket: string,
|
||||
key: string,
|
||||
expiresInSeconds: number = DEFAULT_LOCAL_GET_EXPIRY_SECONDS,
|
||||
): { signature: string; expires: number } {
|
||||
const expires = Math.floor(Date.now() / 1000) + expiresInSeconds
|
||||
const secret = process.env.BETTER_AUTH_SECRET || 'shumai-local-storage-secret'
|
||||
const hmac = crypto.createHmac('sha256', secret)
|
||||
hmac.update(`GET:${bucket}/${key}:${expires}`)
|
||||
const signature = hmac.digest('hex')
|
||||
return { signature, expires }
|
||||
}
|
||||
|
||||
export function verifyLocalGetUrlSignature(
|
||||
bucket: string,
|
||||
key: string,
|
||||
signature: string | undefined | null,
|
||||
expiresStr: string | undefined | null,
|
||||
): boolean {
|
||||
if (!signature || !expiresStr) return false
|
||||
const expires = parseInt(expiresStr, 10)
|
||||
if (isNaN(expires)) return false
|
||||
|
||||
const now = Math.floor(Date.now() / 1000)
|
||||
if (now > expires) return false
|
||||
|
||||
const secret = process.env.BETTER_AUTH_SECRET || 'shumai-local-storage-secret'
|
||||
const hmac = crypto.createHmac('sha256', secret)
|
||||
hmac.update(`GET:${bucket}/${key}:${expires}`)
|
||||
const expected = hmac.digest('hex')
|
||||
try {
|
||||
const sigBuf = Buffer.from(signature, 'hex')
|
||||
const expBuf = Buffer.from(expected, 'hex')
|
||||
if (sigBuf.length !== expBuf.length) return false
|
||||
return crypto.timingSafeEqual(sigBuf, expBuf)
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a Content-Disposition header value for downloads.
|
||||
* - Falls back to plain `attachment` when no filename is provided.
|
||||
@@ -350,7 +392,7 @@ export interface S3Service {
|
||||
download?: boolean,
|
||||
filename?: string,
|
||||
internal?: boolean,
|
||||
/** URL lifetime; defaults to PRESIGNED_UPLOAD_URL_EXPIRES_IN (12h) for PUT, or PRESIGNED_URL_EXPIRES_IN (5h) for GET. Ignored by local storage. */
|
||||
/** URL lifetime; defaults to PRESIGNED_UPLOAD_URL_EXPIRES_IN (12h) for PUT, or PRESIGNED_URL_EXPIRES_IN (5h) for GET, or DEFAULT_LOCAL_GET_EXPIRY_SECONDS (12h) for local. */
|
||||
expiresInSeconds?: number,
|
||||
) => Promise<string>
|
||||
presignMultipart: (
|
||||
@@ -770,6 +812,7 @@ export class S3StorageService implements S3Service {
|
||||
export class LocalStorageService implements S3Service {
|
||||
private basePath: string
|
||||
private endpoint: string
|
||||
private presignCache = new LruTtlCache<string, string>(50000)
|
||||
|
||||
constructor(endpoint: string, basePath: string = 'data') {
|
||||
this.endpoint = endpoint.replace(/\/$/, '')
|
||||
@@ -1010,6 +1053,8 @@ export class LocalStorageService implements S3Service {
|
||||
method: string,
|
||||
download?: boolean,
|
||||
filename?: string,
|
||||
_internal?: boolean,
|
||||
expiresInSeconds?: number,
|
||||
): Promise<string> {
|
||||
if (method !== 'GET' && method !== 'PUT') {
|
||||
throw new Error(`Invalid method: ${method}`)
|
||||
@@ -1017,13 +1062,31 @@ export class LocalStorageService implements S3Service {
|
||||
if (method === 'PUT') {
|
||||
return `${this.endpoint}${signLocalUrl(bucket, key)}`
|
||||
}
|
||||
let url = `${this.endpoint}/files/${bucket}/${key}`
|
||||
|
||||
const lifetimeSeconds = expiresInSeconds ?? DEFAULT_LOCAL_GET_EXPIRY_SECONDS
|
||||
const cacheTtlMs = (lifetimeSeconds * 1000) / 2
|
||||
const cacheKey = `${bucket}/${key}?download=${download || false}&filename=${filename || ''}&expires=${expiresInSeconds ?? 'default'}`
|
||||
|
||||
if (method === 'GET' && !download) {
|
||||
const cached = this.presignCache.get(cacheKey)
|
||||
if (cached) {
|
||||
return cached
|
||||
}
|
||||
}
|
||||
|
||||
const { signature, expires } = signLocalGetUrl(bucket, key, lifetimeSeconds)
|
||||
let url = `${this.endpoint}/files/${bucket}/${key}?Signature=${signature}&Expires=${expires}`
|
||||
if (download) {
|
||||
url += '?download=1'
|
||||
url += '&download=1'
|
||||
if (filename) {
|
||||
url += `&filename=${encodeURIComponent(filename)}`
|
||||
}
|
||||
}
|
||||
|
||||
if (method === 'GET' && !download) {
|
||||
this.presignCache.set(cacheKey, url, cacheTtlMs)
|
||||
}
|
||||
|
||||
return url
|
||||
}
|
||||
|
||||
|
||||
@@ -150,11 +150,11 @@ describe('ShareService', () => {
|
||||
expect(symlink).toBeDefined()
|
||||
|
||||
// Test password check
|
||||
await expect(shareService.verifyPublicAccess(symlink!.id, 'wrong')).rejects.toThrow(
|
||||
'Invalid password',
|
||||
)
|
||||
await expect(
|
||||
shareService.verifyPublicAccess(symlink!.id, 'secret-password'),
|
||||
shareService.verifyPublicAccess(shareLink.id, symlink!.id, 'wrong'),
|
||||
).rejects.toThrow('Invalid password')
|
||||
await expect(
|
||||
shareService.verifyPublicAccess(shareLink.id, symlink!.id, 'secret-password'),
|
||||
).resolves.toBeDefined()
|
||||
|
||||
// Test expiration
|
||||
@@ -162,9 +162,9 @@ describe('ShareService', () => {
|
||||
where: { id: shareLink.id },
|
||||
data: { expireAt: new Date(Date.now() - 1000) },
|
||||
})
|
||||
await expect(shareService.verifyPublicAccess(symlink!.id, 'secret-password')).rejects.toThrow(
|
||||
'expired',
|
||||
)
|
||||
await expect(
|
||||
shareService.verifyPublicAccess(shareLink.id, symlink!.id, 'secret-password'),
|
||||
).rejects.toThrow('expired')
|
||||
})
|
||||
|
||||
it('traces deep ancestors for public access', async () => {
|
||||
@@ -192,7 +192,7 @@ describe('ShareService', () => {
|
||||
},
|
||||
})
|
||||
|
||||
await expect(shareService.verifyPublicAccess(deepFile.id)).resolves.toBeDefined()
|
||||
await expect(shareService.verifyPublicAccess(shareLink.id, deepFile.id)).resolves.toBeDefined()
|
||||
})
|
||||
|
||||
it('verifies public access via symlink to a folder', async () => {
|
||||
@@ -220,10 +220,11 @@ describe('ShareService', () => {
|
||||
await shareService.addAssetToShare(shareLink.id, { assetIds: [realFolder.id] })
|
||||
|
||||
// 3. Verify access to the file inside the real folder (which is reached via the symlinked folder)
|
||||
await expect(shareService.verifyPublicAccess(realFile.id)).resolves.toBeDefined()
|
||||
await expect(shareService.verifyPublicAccess(shareLink.id, realFile.id)).resolves.toBeDefined()
|
||||
})
|
||||
|
||||
it('fails public access for non-shared assets', async () => {
|
||||
const shareLink = await shareService.createShareLink(projectId, { name: 'Empty Share' })
|
||||
const privateFile = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'private.txt',
|
||||
@@ -232,7 +233,7 @@ describe('ShareService', () => {
|
||||
projectId,
|
||||
},
|
||||
})
|
||||
await expect(shareService.verifyPublicAccess(privateFile.id)).rejects.toThrow(
|
||||
await expect(shareService.verifyPublicAccess(shareLink.id, privateFile.id)).rejects.toThrow(
|
||||
'Asset is not shared',
|
||||
)
|
||||
})
|
||||
@@ -373,8 +374,8 @@ describe('ShareService', () => {
|
||||
where: { targetId: file.id, parentId: shareLink.rootFolderId },
|
||||
})
|
||||
expect(symlink).toBeDefined()
|
||||
await expect(shareService.verifyPublicAccess(file.id)).resolves.toBeDefined()
|
||||
await expect(shareService.verifyPublicAccess(symlink!.id)).resolves.toBeDefined()
|
||||
await expect(shareService.verifyPublicAccess(shareLink.id, file.id)).resolves.toBeDefined()
|
||||
await expect(shareService.verifyPublicAccess(shareLink.id, symlink!.id)).resolves.toBeDefined()
|
||||
|
||||
// 1. Soft-delete the file (move to trash)
|
||||
await assetService.deleteAssets([file.id])
|
||||
@@ -393,8 +394,8 @@ describe('ShareService', () => {
|
||||
expect(afterDeleteShareRoot?.fileCount).toBe(0)
|
||||
|
||||
// Verify: public access is rejected
|
||||
await expect(shareService.verifyPublicAccess(file.id)).rejects.toThrow()
|
||||
await expect(shareService.verifyPublicAccess(symlink!.id)).rejects.toThrow()
|
||||
await expect(shareService.verifyPublicAccess(shareLink.id, file.id)).rejects.toThrow()
|
||||
await expect(shareService.verifyPublicAccess(shareLink.id, symlink!.id)).rejects.toThrow()
|
||||
|
||||
// Verify: project's recently-deleted view does NOT list symlinks
|
||||
const recentlyDeleted = await assetService.listChildren({
|
||||
@@ -423,8 +424,8 @@ describe('ShareService', () => {
|
||||
expect(afterRestoreShareRoot?.fileCount).toBe(1)
|
||||
|
||||
// Verify: public access works again
|
||||
await expect(shareService.verifyPublicAccess(file.id)).resolves.toBeDefined()
|
||||
await expect(shareService.verifyPublicAccess(symlink!.id)).resolves.toBeDefined()
|
||||
await expect(shareService.verifyPublicAccess(shareLink.id, file.id)).resolves.toBeDefined()
|
||||
await expect(shareService.verifyPublicAccess(shareLink.id, symlink!.id)).resolves.toBeDefined()
|
||||
})
|
||||
|
||||
it('hides soft-deleted folders from share link and restores them upon folder restoration', async () => {
|
||||
@@ -463,7 +464,7 @@ describe('ShareService', () => {
|
||||
})
|
||||
expect(initialChildren.data).toHaveLength(1)
|
||||
expect(initialChildren.data[0].name).toBe('Shared Folder')
|
||||
await expect(shareService.verifyPublicAccess(childFile.id)).resolves.toBeDefined()
|
||||
await expect(shareService.verifyPublicAccess(shareLink.id, childFile.id)).resolves.toBeDefined()
|
||||
|
||||
// 1. Soft-delete the folder
|
||||
await assetService.deleteAssets([folder.id])
|
||||
@@ -476,7 +477,7 @@ describe('ShareService', () => {
|
||||
expect(afterDeleteChildren.data).toHaveLength(0)
|
||||
|
||||
// Verify: access to child file inside folder is rejected
|
||||
await expect(shareService.verifyPublicAccess(childFile.id)).rejects.toThrow()
|
||||
await expect(shareService.verifyPublicAccess(shareLink.id, childFile.id)).rejects.toThrow()
|
||||
|
||||
// 2. Restore the folder
|
||||
await assetService.restoreAssets([folder.id])
|
||||
@@ -490,6 +491,46 @@ describe('ShareService', () => {
|
||||
expect(afterRestoreChildren.data[0].name).toBe('Shared Folder')
|
||||
|
||||
// Verify: access to child file inside folder is restored
|
||||
await expect(shareService.verifyPublicAccess(childFile.id)).resolves.toBeDefined()
|
||||
await expect(shareService.verifyPublicAccess(shareLink.id, childFile.id)).resolves.toBeDefined()
|
||||
})
|
||||
|
||||
it('binds public access strictly to the requested shareId', async () => {
|
||||
const fileA = await prisma.asset.create({
|
||||
data: { name: 'fileA.txt', type: AssetType.file, status: 'processed', projectId },
|
||||
})
|
||||
const fileB = await prisma.asset.create({
|
||||
data: { name: 'fileB.txt', type: AssetType.file, status: 'processed', projectId },
|
||||
})
|
||||
|
||||
const shareOpen = await shareService.createShareLink(projectId, { name: 'Open Share' })
|
||||
const shareProtected = await shareService.createShareLink(projectId, {
|
||||
name: 'Protected Share',
|
||||
password: 'password123',
|
||||
})
|
||||
|
||||
await shareService.addAssetToShare(shareOpen.id, { assetIds: [fileA.id, fileB.id] })
|
||||
await shareService.addAssetToShare(shareProtected.id, { assetIds: [fileA.id] })
|
||||
|
||||
// fileA in protected share requires password
|
||||
await expect(
|
||||
shareService.verifyPublicAccess(shareProtected.id, fileA.id, 'wrong'),
|
||||
).rejects.toThrow('Invalid password')
|
||||
await expect(
|
||||
shareService.verifyPublicAccess(shareProtected.id, fileA.id, 'password123'),
|
||||
).resolves.toBeDefined()
|
||||
|
||||
// fileB is only in shareOpen, accessing it via shareProtected must fail even with password
|
||||
await expect(
|
||||
shareService.verifyPublicAccess(shareProtected.id, fileB.id, 'password123'),
|
||||
).rejects.toThrow('Asset is not shared')
|
||||
|
||||
// Disabled share must reject access even if file sits in another active share
|
||||
await prisma.shareLink.update({
|
||||
where: { id: shareProtected.id },
|
||||
data: { isDisabled: true },
|
||||
})
|
||||
await expect(
|
||||
shareService.verifyPublicAccess(shareProtected.id, fileA.id, 'password123'),
|
||||
).rejects.toThrow('disabled')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -249,57 +249,16 @@ export class ShareService {
|
||||
}
|
||||
|
||||
async verifyPublicAccess(
|
||||
shareLinkId: string,
|
||||
assetId: string,
|
||||
providedPassword?: string,
|
||||
): Promise<Prisma.ShareLinkGetPayload<true>> {
|
||||
// 1. Trace ancestors of the current asset in the real tree
|
||||
// 2. Find if any ancestor (including itself) is the target of a symlink that belongs to a share
|
||||
// 3. Or if the asset itself is a descendant of a 'share' type folder (direct share link root or subfolder within)
|
||||
|
||||
const rows = await prisma.$queryRaw<{ shareRootId: string }[]>`
|
||||
WITH RECURSIVE
|
||||
-- Trace ancestors in the real asset tree
|
||||
real_ancestors AS (
|
||||
SELECT id, parent_id, type::text FROM assets WHERE id = ${assetId} AND is_deleted = false
|
||||
UNION ALL
|
||||
SELECT a.id, a.parent_id, a.type::text FROM assets a
|
||||
INNER JOIN real_ancestors ra ON ra.parent_id = a.id
|
||||
WHERE a.is_deleted = false
|
||||
),
|
||||
-- Find all symlinks pointing to any of these real ancestors
|
||||
found_symlinks AS (
|
||||
SELECT s.id, s.parent_id, s.type::text FROM assets s
|
||||
INNER JOIN real_ancestors ra ON s.target_id = ra.id
|
||||
WHERE s.type = 'symlink' AND s.is_deleted = false
|
||||
),
|
||||
-- Combine: start with either real ancestors that are 'share' type, or symlinks that lead to 'share' type
|
||||
search_starts AS (
|
||||
SELECT id, parent_id, type FROM real_ancestors WHERE type = 'share'
|
||||
UNION
|
||||
SELECT id, parent_id, type FROM found_symlinks
|
||||
),
|
||||
-- Trace UP from these search starts to find the ultimate 'share' root
|
||||
share_trace AS (
|
||||
SELECT id, parent_id, type FROM search_starts
|
||||
UNION ALL
|
||||
SELECT a.id, a.parent_id, a.type::text FROM assets a
|
||||
INNER JOIN share_trace st ON st.parent_id = a.id
|
||||
WHERE a.is_deleted = false
|
||||
)
|
||||
SELECT id as "shareRootId" FROM share_trace WHERE type = 'share' LIMIT 1;
|
||||
`
|
||||
|
||||
if (rows.length === 0) {
|
||||
throw new ShareLinkNotFoundError('Asset is not shared')
|
||||
}
|
||||
|
||||
const shareRootId = rows[0].shareRootId
|
||||
const shareLink = await prisma.shareLink.findUnique({
|
||||
where: { rootFolderId: shareRootId },
|
||||
where: { id: shareLinkId },
|
||||
})
|
||||
|
||||
if (!shareLink) {
|
||||
throw new ShareLinkNotFoundError('Share link not found for this asset')
|
||||
throw new ShareLinkNotFoundError('Share link not found')
|
||||
}
|
||||
|
||||
if (shareLink.isDisabled) {
|
||||
@@ -314,6 +273,43 @@ export class ShareService {
|
||||
throw new ShareLinkPasswordInvalidError('Invalid password for share link')
|
||||
}
|
||||
|
||||
const rows = await prisma.$queryRaw<{ id: string }[]>`
|
||||
WITH RECURSIVE
|
||||
-- Trace ancestors in the real asset tree
|
||||
real_ancestors AS (
|
||||
SELECT id, parent_id, type::text, target_id FROM assets WHERE id = ${assetId} AND is_deleted = false
|
||||
UNION ALL
|
||||
SELECT a.id, a.parent_id, a.type::text, a.target_id FROM assets a
|
||||
INNER JOIN real_ancestors ra ON ra.parent_id = a.id
|
||||
WHERE a.is_deleted = false
|
||||
),
|
||||
-- Find all symlinks pointing to any of these real ancestors
|
||||
found_symlinks AS (
|
||||
SELECT s.id, s.parent_id, s.type::text FROM assets s
|
||||
INNER JOIN real_ancestors ra ON s.target_id = ra.id
|
||||
WHERE s.type = 'symlink' AND s.is_deleted = false
|
||||
),
|
||||
-- Combine: start with either real ancestors, or symlinks that lead to them
|
||||
search_starts AS (
|
||||
SELECT id, parent_id, type FROM real_ancestors
|
||||
UNION
|
||||
SELECT id, parent_id, type FROM found_symlinks
|
||||
),
|
||||
-- Trace UP from these search starts to find the ultimate 'share' root
|
||||
share_trace AS (
|
||||
SELECT id, parent_id, type FROM search_starts
|
||||
UNION ALL
|
||||
SELECT a.id, a.parent_id, a.type::text FROM assets a
|
||||
INNER JOIN share_trace st ON st.parent_id = a.id
|
||||
WHERE a.is_deleted = false
|
||||
)
|
||||
SELECT id FROM share_trace WHERE id = ${shareLink.rootFolderId} LIMIT 1;
|
||||
`
|
||||
|
||||
if (rows.length === 0) {
|
||||
throw new ShareLinkNotFoundError('Asset is not shared')
|
||||
}
|
||||
|
||||
return shareLink
|
||||
}
|
||||
|
||||
|
||||
@@ -170,6 +170,7 @@ describe('UploadService', () => {
|
||||
type: AssetType.file,
|
||||
project: { connect: { id: projectId } },
|
||||
parent: { connect: { id: parentId } },
|
||||
task: { connect: { id: task.id } },
|
||||
status: AssetStatus.uploading,
|
||||
storageKey: {
|
||||
connectOrCreate: {
|
||||
@@ -228,6 +229,7 @@ describe('UploadService', () => {
|
||||
type: AssetType.file,
|
||||
project: { connect: { id: projectId } },
|
||||
parent: { connect: { id: parentId } },
|
||||
task: { connect: { id: task.id } },
|
||||
status: AssetStatus.uploading,
|
||||
storageKey: {
|
||||
connectOrCreate: {
|
||||
@@ -268,6 +270,7 @@ describe('UploadService', () => {
|
||||
type: AssetType.file,
|
||||
project: { connect: { id: projectId } },
|
||||
parent: { connect: { id: parentId } },
|
||||
task: { connect: { id: task.id } },
|
||||
status: AssetStatus.uploading,
|
||||
storageKey: {
|
||||
connectOrCreate: {
|
||||
@@ -303,6 +306,7 @@ describe('UploadService', () => {
|
||||
type: AssetType.file,
|
||||
project: { connect: { id: projectId } },
|
||||
parent: { connect: { id: parentId } },
|
||||
task: { connect: { id: task.id } },
|
||||
status: AssetStatus.uploading,
|
||||
storageKey: {
|
||||
connectOrCreate: {
|
||||
@@ -336,6 +340,7 @@ describe('UploadService', () => {
|
||||
type: AssetType.file,
|
||||
project: { connect: { id: projectId } },
|
||||
parent: { connect: { id: parentId } },
|
||||
task: { connect: { id: task.id } },
|
||||
status: AssetStatus.uploading,
|
||||
storageKey: {
|
||||
connectOrCreate: {
|
||||
@@ -366,6 +371,7 @@ describe('UploadService', () => {
|
||||
type: AssetType.file,
|
||||
project: { connect: { id: projectId } },
|
||||
parent: { connect: { id: parentId } },
|
||||
task: { connect: { id: task.id } },
|
||||
status: AssetStatus.uploading,
|
||||
storageKey: {
|
||||
connectOrCreate: {
|
||||
@@ -399,6 +405,7 @@ describe('UploadService', () => {
|
||||
type: AssetType.file,
|
||||
project: { connect: { id: projectId } },
|
||||
parent: { connect: { id: parentId } },
|
||||
task: { connect: { id: task.id } },
|
||||
status: AssetStatus.uploading,
|
||||
storageKey: {
|
||||
connectOrCreate: {
|
||||
@@ -429,6 +436,7 @@ describe('UploadService', () => {
|
||||
type: AssetType.file,
|
||||
project: { connect: { id: projectId } },
|
||||
parent: { connect: { id: parentId } },
|
||||
task: { connect: { id: task.id } },
|
||||
status: AssetStatus.uploading,
|
||||
storageKey: {
|
||||
connectOrCreate: {
|
||||
@@ -484,6 +492,7 @@ describe('UploadService', () => {
|
||||
type: AssetType.file,
|
||||
project: { connect: { id: projectId } },
|
||||
parent: { connect: { id: parentId } },
|
||||
task: { connect: { id: task.id } },
|
||||
status: AssetStatus.uploading,
|
||||
storageKey: {
|
||||
connectOrCreate: {
|
||||
@@ -512,6 +521,7 @@ describe('UploadService', () => {
|
||||
type: AssetType.file,
|
||||
project: { connect: { id: projectId } },
|
||||
parent: { connect: { id: parentId } },
|
||||
task: { connect: { id: htmlTask.id } },
|
||||
status: AssetStatus.uploading,
|
||||
storageKey: {
|
||||
connectOrCreate: {
|
||||
@@ -547,6 +557,7 @@ describe('UploadService', () => {
|
||||
type: AssetType.file,
|
||||
project: { connect: { id: projectId } },
|
||||
parent: { connect: { id: parentId } },
|
||||
task: { connect: { id: task.id } },
|
||||
status: AssetStatus.uploading,
|
||||
storageKey: {
|
||||
connectOrCreate: {
|
||||
@@ -584,6 +595,7 @@ describe('UploadService', () => {
|
||||
type: AssetType.file,
|
||||
project: { connect: { id: projectId } },
|
||||
parent: { connect: { id: parentId } },
|
||||
task: { connect: { id: task.id } },
|
||||
status: AssetStatus.uploading,
|
||||
storageKey: {
|
||||
connectOrCreate: {
|
||||
@@ -616,6 +628,7 @@ describe('UploadService', () => {
|
||||
type: AssetType.file,
|
||||
project: { connect: { id: projectId } },
|
||||
parent: { connect: { id: parentId } },
|
||||
task: { connect: { id: task.id } },
|
||||
status: AssetStatus.uploading,
|
||||
storageKey: {
|
||||
connectOrCreate: {
|
||||
@@ -649,6 +662,7 @@ describe('UploadService', () => {
|
||||
type: AssetType.file,
|
||||
project: { connect: { id: projectId } },
|
||||
parent: { connect: { id: parentId } },
|
||||
task: { connect: { id: task.id } },
|
||||
status: AssetStatus.uploading,
|
||||
storageKey: {
|
||||
connectOrCreate: {
|
||||
@@ -752,6 +766,7 @@ describe('UploadService', () => {
|
||||
type: AssetType.file,
|
||||
project: { connect: { id: projectId } },
|
||||
parent: { connect: { id: stack.id } },
|
||||
task: { connect: { id: task.id } },
|
||||
status: AssetStatus.uploading,
|
||||
storageKey: {
|
||||
connectOrCreate: {
|
||||
@@ -1120,6 +1135,68 @@ describe('UploadService', () => {
|
||||
})
|
||||
expect(deletedAsset).toBeNull()
|
||||
})
|
||||
|
||||
it('should not increment task count if error arrives for an already finished or deleted asset', async () => {
|
||||
const task = await prisma.task.create({
|
||||
data: {
|
||||
creatorId: userId,
|
||||
type: 'upload',
|
||||
name: 'task-late-err',
|
||||
total: 2,
|
||||
uploaded: 1,
|
||||
status: TaskStatus.uploading,
|
||||
},
|
||||
})
|
||||
|
||||
const storageKey = await prisma.storageKey.create({
|
||||
data: { key: 'files/test/already-uploaded.mp4' },
|
||||
})
|
||||
const asset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'already-uploaded.mp4',
|
||||
type: AssetType.file,
|
||||
projectId,
|
||||
storageKeyId: storageKey.id,
|
||||
taskId: task.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
|
||||
await uploadService.confirmFileUpload(userId, task.id, {
|
||||
fileId: asset.id,
|
||||
errorMessage: 'Late network error',
|
||||
})
|
||||
|
||||
const taskAfter = await prisma.task.findUniqueOrThrow({ where: { id: task.id } })
|
||||
expect(taskAfter.uploaded).toBe(1)
|
||||
})
|
||||
|
||||
it('should reject confirmation if asset does not belong to specified task', async () => {
|
||||
const task1 = await prisma.task.create({
|
||||
data: { creatorId: userId, type: 'upload', total: 1, status: TaskStatus.uploading },
|
||||
})
|
||||
const task2 = await prisma.task.create({
|
||||
data: { creatorId: userId, type: 'upload', total: 1, status: TaskStatus.uploading },
|
||||
})
|
||||
|
||||
const storageKey = await prisma.storageKey.create({
|
||||
data: { key: 'files/test/task-mismatch.mp4' },
|
||||
})
|
||||
const asset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'task-mismatch.mp4',
|
||||
type: AssetType.file,
|
||||
projectId,
|
||||
storageKeyId: storageKey.id,
|
||||
taskId: task1.id,
|
||||
status: AssetStatus.uploading,
|
||||
},
|
||||
})
|
||||
|
||||
await expect(
|
||||
uploadService.confirmFileUpload(userId, task2.id, { fileId: asset.id }),
|
||||
).rejects.toThrow('Asset does not belong to specified task')
|
||||
})
|
||||
})
|
||||
|
||||
describe('cancelUploadTask', () => {
|
||||
|
||||
@@ -206,23 +206,28 @@ export class UploadService {
|
||||
},
|
||||
})
|
||||
if (!asset) throw new Error('Asset not found')
|
||||
if (asset.taskId !== taskId) {
|
||||
throw new Error('Asset does not belong to specified task')
|
||||
}
|
||||
if (!asset.project) throw new Error('Project not found for asset')
|
||||
const key = asset.storageKey?.key
|
||||
if (!key) throw new Error('Asset has no key')
|
||||
|
||||
if (req.errorMessage) {
|
||||
await this.discardUploadingAsset(asset)
|
||||
const t = await this.prismaClient.task.findUnique({ where: { id: taskId } })
|
||||
if (t) {
|
||||
const updatedTask = await this.prismaClient.task.update({
|
||||
where: { id: taskId },
|
||||
data: { uploaded: { increment: 1 } },
|
||||
})
|
||||
if (updatedTask.uploaded === updatedTask.total) {
|
||||
await this.prismaClient.task.update({
|
||||
const discarded = await this.discardUploadingAsset(asset)
|
||||
if (discarded) {
|
||||
const t = await this.prismaClient.task.findUnique({ where: { id: taskId } })
|
||||
if (t) {
|
||||
const updatedTask = await this.prismaClient.task.update({
|
||||
where: { id: taskId },
|
||||
data: { status: TaskStatus.completed },
|
||||
data: { uploaded: { increment: 1 } },
|
||||
})
|
||||
if (updatedTask.uploaded === updatedTask.total) {
|
||||
await this.prismaClient.task.update({
|
||||
where: { id: taskId },
|
||||
data: { status: TaskStatus.completed },
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
@@ -242,15 +247,22 @@ export class UploadService {
|
||||
resolvedMediaType = Bun.file(asset.name).type || 'application/octet-stream'
|
||||
}
|
||||
|
||||
// Update asset
|
||||
const updatedAsset = await tx.asset.update({
|
||||
where: { id: asset.id },
|
||||
// Claim status transition atomically
|
||||
const claim = await tx.asset.updateMany({
|
||||
where: {
|
||||
id: asset.id,
|
||||
taskId: taskId,
|
||||
status: AssetStatus.uploading,
|
||||
},
|
||||
data: {
|
||||
status: AssetStatus.uploaded,
|
||||
sizeByte: size,
|
||||
mediaType: resolvedMediaType,
|
||||
},
|
||||
})
|
||||
if (claim.count !== 1) {
|
||||
throw new Error('Asset is not in uploading status')
|
||||
}
|
||||
|
||||
// Update parent and ancestors
|
||||
if (asset.parentId) {
|
||||
@@ -259,7 +271,7 @@ export class UploadService {
|
||||
data: { fileCount: { increment: 1 } },
|
||||
})
|
||||
|
||||
await assetService.updateAncestorsSize(tx, asset.parentId, updatedAsset.sizeByte)
|
||||
await assetService.updateAncestorsSize(tx, asset.parentId, size)
|
||||
}
|
||||
|
||||
// Update task
|
||||
@@ -533,7 +545,7 @@ export class UploadService {
|
||||
storageKey: { key: string } | null
|
||||
},
|
||||
requestedUploadId?: string,
|
||||
): Promise<void> {
|
||||
): Promise<boolean> {
|
||||
const deleteResult = await this.prismaClient.asset.deleteMany({
|
||||
where: {
|
||||
id: asset.id,
|
||||
@@ -542,7 +554,7 @@ export class UploadService {
|
||||
})
|
||||
|
||||
if (deleteResult.count === 0) {
|
||||
return
|
||||
return false
|
||||
}
|
||||
|
||||
const bucket = process.env.S3_BUCKET || 'shumai'
|
||||
@@ -568,6 +580,8 @@ export class UploadService {
|
||||
.delete({ where: { id: asset.storageKeyId } })
|
||||
.catch(() => {})
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
async cancelUploadTask(
|
||||
|
||||
@@ -199,14 +199,12 @@ export function FileViewerRightSidebar({
|
||||
attachmentIds,
|
||||
annotations,
|
||||
replyToId,
|
||||
guestUserId,
|
||||
second,
|
||||
}: {
|
||||
text: string
|
||||
attachmentIds: string[]
|
||||
annotations?: Annotation[]
|
||||
replyToId?: string | null
|
||||
guestUserId?: string
|
||||
second?: number | null
|
||||
}) => {
|
||||
if (isPublic) {
|
||||
@@ -225,7 +223,6 @@ export function FileViewerRightSidebar({
|
||||
{
|
||||
headers: {
|
||||
'x-share-password': password,
|
||||
...(guestUserId ? { 'x-guest-user-id': guestUserId } : {}),
|
||||
},
|
||||
},
|
||||
)
|
||||
@@ -310,7 +307,7 @@ export function FileViewerRightSidebar({
|
||||
setIsGuestPopupOpen(true)
|
||||
return
|
||||
}
|
||||
createComment({ text, attachmentIds, annotations, replyToId, guestUserId, second })
|
||||
createComment({ text, attachmentIds, annotations, replyToId, second })
|
||||
} else {
|
||||
createComment({ text, attachmentIds, annotations, replyToId, second })
|
||||
}
|
||||
@@ -320,10 +317,9 @@ export function FileViewerRightSidebar({
|
||||
setReplyingTo(null)
|
||||
}
|
||||
|
||||
const handleGuestSuccess = (guestUserId: string) => {
|
||||
console.log('[handleGuestSuccess] guestUserId:', guestUserId)
|
||||
const handleGuestSuccess = () => {
|
||||
if (pendingComment) {
|
||||
createComment({ ...pendingComment, guestUserId })
|
||||
createComment({ ...pendingComment })
|
||||
setPendingComment(null)
|
||||
setReplyingTo(null)
|
||||
}
|
||||
@@ -473,6 +469,7 @@ export function FileViewerRightSidebar({
|
||||
isOpen={isGuestPopupOpen}
|
||||
onClose={() => setIsGuestPopupOpen(false)}
|
||||
onSuccess={handleGuestSuccess}
|
||||
shareId={shareId || undefined}
|
||||
>
|
||||
<ChatInput
|
||||
projectId={projectId}
|
||||
|
||||
@@ -170,14 +170,12 @@ export function MobileFileBottomSheet({
|
||||
attachmentIds,
|
||||
annotations,
|
||||
replyToId,
|
||||
guestUserId,
|
||||
second,
|
||||
}: {
|
||||
text: string
|
||||
attachmentIds: string[]
|
||||
annotations?: Annotation[]
|
||||
replyToId?: string | null
|
||||
guestUserId?: string
|
||||
second?: number | null
|
||||
}) => {
|
||||
if (isPublic) {
|
||||
@@ -196,7 +194,6 @@ export function MobileFileBottomSheet({
|
||||
{
|
||||
headers: {
|
||||
'x-share-password': password,
|
||||
...(guestUserId ? { 'x-guest-user-id': guestUserId } : {}),
|
||||
},
|
||||
},
|
||||
)
|
||||
@@ -285,7 +282,6 @@ export function MobileFileBottomSheet({
|
||||
attachmentIds,
|
||||
annotations,
|
||||
replyToId: actualReplyToId,
|
||||
guestUserId,
|
||||
second,
|
||||
})
|
||||
}
|
||||
@@ -295,9 +291,9 @@ export function MobileFileBottomSheet({
|
||||
setReplyingTo(null)
|
||||
}
|
||||
|
||||
const handleGuestSuccess = (guestUserId: string) => {
|
||||
const handleGuestSuccess = () => {
|
||||
if (pendingComment) {
|
||||
createComment({ ...pendingComment, guestUserId })
|
||||
createComment({ ...pendingComment })
|
||||
setPendingComment(null)
|
||||
setReplyingTo(null)
|
||||
}
|
||||
@@ -442,6 +438,7 @@ export function MobileFileBottomSheet({
|
||||
isOpen={isGuestPopupOpen}
|
||||
onClose={() => setIsGuestPopupOpen(false)}
|
||||
onSuccess={handleGuestSuccess}
|
||||
shareId={shareId || undefined}
|
||||
>
|
||||
<ChatInput
|
||||
projectId={projectId}
|
||||
|
||||
@@ -13,6 +13,7 @@ interface GuestIdentityPopupProps {
|
||||
isOpen: boolean
|
||||
onClose: () => void
|
||||
onSuccess: (guestUserId: string) => void
|
||||
shareId?: string
|
||||
children?: React.ReactNode
|
||||
}
|
||||
|
||||
@@ -20,6 +21,7 @@ export function GuestIdentityPopup({
|
||||
isOpen,
|
||||
onClose,
|
||||
onSuccess,
|
||||
shareId,
|
||||
children,
|
||||
}: GuestIdentityPopupProps) {
|
||||
const [name, setName] = useState('')
|
||||
@@ -34,7 +36,7 @@ export function GuestIdentityPopup({
|
||||
|
||||
try {
|
||||
const res = await client.api.identify.$post({
|
||||
json: { username: name, email: email },
|
||||
json: { username: name, email: email, shareId: shareId || undefined },
|
||||
})
|
||||
|
||||
if (!res.ok) {
|
||||
@@ -43,6 +45,9 @@ export function GuestIdentityPopup({
|
||||
}
|
||||
|
||||
const { id } = await res.json()
|
||||
if (shareId) {
|
||||
localStorage.setItem(`guest_user_id_${shareId}`, id)
|
||||
}
|
||||
localStorage.setItem('guest_user_id', id)
|
||||
onSuccess(id)
|
||||
onClose()
|
||||
|
||||
Reference in New Issue
Block a user