|
| 1 | +import { AuditAction, AuditResourceType, recordAudit } from '@sim/audit' |
| 2 | +import { createLogger } from '@sim/logger' |
| 3 | +import type { ShareAuthType } from '@/lib/api/contracts/public-shares' |
| 4 | +import { ShareFile } from '@/lib/copilot/generated/tool-catalog-v1' |
| 5 | +import { ensureWorkspaceAccess } from '@/lib/copilot/tools/handlers/access' |
| 6 | +import { |
| 7 | + assertServerToolNotAborted, |
| 8 | + type BaseServerTool, |
| 9 | + type ServerToolContext, |
| 10 | +} from '@/lib/copilot/tools/server/base-tool' |
| 11 | +import { ShareValidationError, upsertFileShare } from '@/lib/public-shares/share-manager' |
| 12 | +import { |
| 13 | + getWorkspaceFile, |
| 14 | + resolveWorkspaceFileReference, |
| 15 | +} from '@/lib/uploads/contexts/workspace/workspace-file-manager' |
| 16 | +import { |
| 17 | + PublicFileSharingNotAllowedError, |
| 18 | + validatePublicFileSharing, |
| 19 | +} from '@/ee/access-control/utils/permission-check' |
| 20 | + |
| 21 | +const logger = createLogger('ShareFileServerTool') |
| 22 | + |
| 23 | +interface ShareFileArgs { |
| 24 | + path?: string |
| 25 | + fileId?: string |
| 26 | + action?: 'share' | 'unshare' |
| 27 | + authType?: ShareAuthType |
| 28 | + password?: string |
| 29 | + allowedEmails?: string[] |
| 30 | + args?: Record<string, unknown> |
| 31 | +} |
| 32 | + |
| 33 | +interface ShareFileResult { |
| 34 | + success: boolean |
| 35 | + message: string |
| 36 | + data?: { |
| 37 | + url: string |
| 38 | + token: string |
| 39 | + authType: ShareAuthType |
| 40 | + hasPassword: boolean |
| 41 | + isActive: boolean |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +export const shareFileServerTool: BaseServerTool<ShareFileArgs, ShareFileResult> = { |
| 46 | + name: ShareFile.id, |
| 47 | + async execute(params: ShareFileArgs, context?: ServerToolContext): Promise<ShareFileResult> { |
| 48 | + if (!context?.userId) { |
| 49 | + throw new Error('Authentication required') |
| 50 | + } |
| 51 | + const workspaceId = context.workspaceId |
| 52 | + if (!workspaceId) { |
| 53 | + return { success: false, message: 'Workspace ID is required' } |
| 54 | + } |
| 55 | + await ensureWorkspaceAccess(workspaceId, context.userId, 'write') |
| 56 | + |
| 57 | + const nested = params.args |
| 58 | + const path = params.path || (nested?.path as string) || '' |
| 59 | + const legacyFileId = params.fileId || (nested?.fileId as string) || '' |
| 60 | + const action = (params.action || (nested?.action as string) || 'share') as 'share' | 'unshare' |
| 61 | + const authType = (params.authType || (nested?.authType as ShareAuthType | undefined)) as |
| 62 | + | ShareAuthType |
| 63 | + | undefined |
| 64 | + const password = params.password || (nested?.password as string) || undefined |
| 65 | + const allowedEmails = |
| 66 | + params.allowedEmails || (nested?.allowedEmails as string[] | undefined) || undefined |
| 67 | + |
| 68 | + const targetRef = path || legacyFileId |
| 69 | + if (!targetRef) return { success: false, message: 'path is required' } |
| 70 | + |
| 71 | + const existingFile = path |
| 72 | + ? await resolveWorkspaceFileReference(workspaceId, path) |
| 73 | + : await getWorkspaceFile(workspaceId, legacyFileId) |
| 74 | + if (!existingFile) { |
| 75 | + return { success: false, message: `File not found: ${targetRef}` } |
| 76 | + } |
| 77 | + const fileId = existingFile.id |
| 78 | + const isActive = action !== 'unshare' |
| 79 | + |
| 80 | + // Enabling a share is gated by the org's access-control policy (both the |
| 81 | + // master on/off and the per-auth-type allow-list); disabling is always |
| 82 | + // allowed so users can still un-share after the policy is turned on. |
| 83 | + if (isActive) { |
| 84 | + try { |
| 85 | + await validatePublicFileSharing(context.userId, workspaceId, authType ?? 'public') |
| 86 | + } catch (error) { |
| 87 | + if (error instanceof PublicFileSharingNotAllowedError) { |
| 88 | + return { success: false, message: error.message } |
| 89 | + } |
| 90 | + throw error |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + assertServerToolNotAborted(context) |
| 95 | + |
| 96 | + let share |
| 97 | + try { |
| 98 | + share = await upsertFileShare({ |
| 99 | + workspaceId, |
| 100 | + fileId, |
| 101 | + userId: context.userId, |
| 102 | + isActive, |
| 103 | + authType, |
| 104 | + password, |
| 105 | + allowedEmails, |
| 106 | + }) |
| 107 | + } catch (error) { |
| 108 | + if (error instanceof ShareValidationError) { |
| 109 | + return { success: false, message: error.message } |
| 110 | + } |
| 111 | + throw error |
| 112 | + } |
| 113 | + |
| 114 | + logger.info(`${isActive ? 'Enabled' : 'Disabled'} share for file via share_file`, { |
| 115 | + fileId, |
| 116 | + workspaceId, |
| 117 | + authType: share.authType, |
| 118 | + userId: context.userId, |
| 119 | + }) |
| 120 | + |
| 121 | + recordAudit({ |
| 122 | + workspaceId, |
| 123 | + actorId: context.userId, |
| 124 | + action: isActive ? AuditAction.FILE_SHARED : AuditAction.FILE_SHARE_DISABLED, |
| 125 | + resourceType: AuditResourceType.FILE, |
| 126 | + resourceId: fileId, |
| 127 | + resourceName: existingFile.name, |
| 128 | + description: `${isActive ? 'Enabled' : 'Disabled'} public share for "${existingFile.name}"`, |
| 129 | + }) |
| 130 | + |
| 131 | + if (!isActive) { |
| 132 | + return { |
| 133 | + success: true, |
| 134 | + message: `Stopped sharing "${existingFile.name}". The previous link no longer works.`, |
| 135 | + data: { |
| 136 | + url: share.url, |
| 137 | + token: share.token, |
| 138 | + authType: share.authType, |
| 139 | + hasPassword: share.hasPassword, |
| 140 | + isActive: share.isActive, |
| 141 | + }, |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + const authNote = |
| 146 | + share.authType === 'password' |
| 147 | + ? ' (password-protected — share the password separately)' |
| 148 | + : share.authType === 'email' |
| 149 | + ? ' (restricted to allowed emails via one-time code)' |
| 150 | + : share.authType === 'sso' |
| 151 | + ? ' (restricted to allowed emails via SSO)' |
| 152 | + : '' |
| 153 | + |
| 154 | + return { |
| 155 | + success: true, |
| 156 | + message: `Shared "${existingFile.name}"${authNote}: ${share.url}`, |
| 157 | + data: { |
| 158 | + url: share.url, |
| 159 | + token: share.token, |
| 160 | + authType: share.authType, |
| 161 | + hasPassword: share.hasPassword, |
| 162 | + isActive: share.isActive, |
| 163 | + }, |
| 164 | + } |
| 165 | + }, |
| 166 | +} |
0 commit comments