A local-first personal finance app
Fix authorization bypass allowing non-owners to delete shared budgets (#6338)
Copilotfc89c74445fe
diff --git a/packages/desktop-client/src/components/modals/manager/DeleteFileModal.tsx b/packages/desktop-client/src/components/modals/manager/DeleteFileModal.tsxindex c40ece7cfb..be209a5915 100644--- a/packages/desktop-client/src/components/modals/manager/DeleteFileModal.tsx+++ b/packages/desktop-client/src/components/modals/manager/DeleteFileModal.tsx@@ -14,7 +14,7 @@ import {} from '@desktop-client/components/common/Modal';import { useSyncServerStatus } from '@desktop-client/hooks/useSyncServerStatus';import { type Modal as ModalType } from '@desktop-client/modals/modalsSlice';-import { useDispatch } from '@desktop-client/redux';+import { useDispatch, useSelector } from '@desktop-client/redux';type DeleteFileModalProps = Extract<ModalType,@@ -31,6 +31,15 @@ export function DeleteFileModal({ file }: DeleteFileModalProps) {const serverStatus = useSyncServerStatus();const dispatch = useDispatch();+ // Get current user info to check ownership+ const userData = useSelector(state => state.user.data);+ const currentUserId = userData?.userId;++ // Check if current user is the owner or has admin permissions+ const isOwner = 'owner' in file && file.owner === currentUserId;+ const isAdmin = userData?.permission === 'ADMIN';+ const canDeleteFromServer = isOwner || isAdmin;+const [loadingState, setLoadingState] = useState<'cloud' | 'local' | null>(null,);@@ -53,57 +62,65 @@ export function DeleteFileModal({ file }: DeleteFileModalProps) {lineHeight: '1.5em',}}>- {isCloudFile && (- <>+ {isCloudFile &&+ (canDeleteFromServer ? (+ <>+ <Text>+ <Trans>+ This is a <strong>hosted file</strong> which means it is+ stored on your server to make it available for download on+ any device. You can delete it from the server, which will+ also remove it from all of your devices.+ </Trans>+ </Text>++ {serverStatus === 'online' ? (+ <ButtonWithLoading+ variant="primary"+ isLoading={loadingState === 'cloud'}+ style={{+ backgroundColor: theme.errorText,+ alignSelf: 'center',+ border: 0,+ padding: '10px 30px',+ fontSize: 14,+ }}+ onPress={async () => {+ setLoadingState('cloud');+ await dispatch(+ deleteBudget({+ id: 'id' in file ? file.id : undefined,+ cloudFileId: file.cloudFileId,+ }),+ );+ setLoadingState(null);++ close();+ }}+ >+ <Trans>Delete file from all devices</Trans>+ </ButtonWithLoading>+ ) : (+ <Button+ isDisabled+ style={{+ alignSelf: 'center',+ padding: '10px 30px',+ fontSize: 14,+ }}+ >+ <Trans>Server is not available</Trans>+ </Button>+ )}+ </>+ ) : (<Text><Trans>- This is a <strong>hosted file</strong> which means it is- stored on your server to make it available for download on- any device. You can delete it from the server, which will- also remove it from all of your devices.+ This is a <strong>hosted file</strong> shared with you. Only+ the file owner can delete it from the server.</Trans></Text>-- {serverStatus === 'online' ? (- <ButtonWithLoading- variant="primary"- isLoading={loadingState === 'cloud'}- style={{- backgroundColor: theme.errorText,- alignSelf: 'center',- border: 0,- padding: '10px 30px',- fontSize: 14,- }}- onPress={async () => {- setLoadingState('cloud');- await dispatch(- deleteBudget({- id: 'id' in file ? file.id : undefined,- cloudFileId: file.cloudFileId,- }),- );- setLoadingState(null);-- close();- }}- >- <Trans>Delete file from all devices</Trans>- </ButtonWithLoading>- ) : (- <Button- isDisabled- style={{- alignSelf: 'center',- padding: '10px 30px',- fontSize: 14,- }}- >- <Trans>Server is not available</Trans>- </Button>- )}- </>- )}+ ))}{'id' in file && (<>diff --git a/packages/sync-server/src/app-sync.test.ts b/packages/sync-server/src/app-sync.test.tsindex 82c99edadb..419864b94b 100644--- a/packages/sync-server/src/app-sync.test.ts+++ b/packages/sync-server/src/app-sync.test.ts@@ -715,6 +715,88 @@ describe('/delete-user-file', () => {]);expect(rows[0].deleted).toBe(1);});++ it('returns 403 if the user is not the owner and not an admin', async () => {+ const accountDb = getAccountDb();+ const fileId = crypto.randomBytes(16).toString('hex');++ // Insert a file owned by another user+ accountDb.mutate(+ 'INSERT OR IGNORE INTO files (id, deleted, owner) VALUES (?, FALSE, ?)',+ [fileId, 'differentUser'],+ );++ // Try to delete with a non-admin, non-owner user+ const res = await request(app)+ .post('/delete-user-file')+ .set('x-actual-token', 'valid-token-user')+ .send({ fileId });++ expect(res.statusCode).toEqual(403);+ expect(res.body).toEqual({+ status: 'error',+ reason: 'forbidden',+ details: 'file-delete-not-allowed',+ });++ // Verify that the file is NOT deleted+ const rows = accountDb.all('SELECT deleted FROM files WHERE id = ?', [+ fileId,+ ]);+ expect(rows[0].deleted).toBe(0);+ });++ it('allows the file owner to delete the file', async () => {+ const accountDb = getAccountDb();+ const fileId = crypto.randomBytes(16).toString('hex');++ // Insert a file owned by genericUser+ accountDb.mutate(+ 'INSERT OR IGNORE INTO files (id, deleted, owner) VALUES (?, FALSE, ?)',+ [fileId, 'genericUser'],+ );++ // Delete with the owner user+ const res = await request(app)+ .post('/delete-user-file')+ .set('x-actual-token', 'valid-token-user')+ .send({ fileId });++ expect(res.statusCode).toEqual(200);+ expect(res.body).toEqual({ status: 'ok' });++ // Verify that the file is deleted+ const rows = accountDb.all('SELECT deleted FROM files WHERE id = ?', [+ fileId,+ ]);+ expect(rows[0].deleted).toBe(1);+ });++ it('allows an admin to delete any file', async () => {+ const accountDb = getAccountDb();+ const fileId = crypto.randomBytes(16).toString('hex');++ // Insert a file owned by another user+ accountDb.mutate(+ 'INSERT OR IGNORE INTO files (id, deleted, owner) VALUES (?, FALSE, ?)',+ [fileId, 'someOtherUser'],+ );++ // Delete with an admin user+ const res = await request(app)+ .post('/delete-user-file')+ .set('x-actual-token', 'valid-token-admin')+ .send({ fileId });++ expect(res.statusCode).toEqual(200);+ expect(res.body).toEqual({ status: 'ok' });++ // Verify that the file is deleted+ const rows = accountDb.all('SELECT deleted FROM files WHERE id = ?', [+ fileId,+ ]);+ expect(rows[0].deleted).toBe(1);+ });});describe('/sync', () => {diff --git a/packages/sync-server/src/app-sync.ts b/packages/sync-server/src/app-sync.tsindex a1c5606600..11a8611094 100644--- a/packages/sync-server/src/app-sync.ts+++ b/packages/sync-server/src/app-sync.ts@@ -7,7 +7,7 @@ import { SyncProtoBuf } from '@actual-app/crdt';import express from 'express';import { v4 as uuidv4 } from 'uuid';-import { getAccountDb } from './account-db';+import { getAccountDb, isAdmin } from './account-db';import { FileNotFound } from './app-sync/errors';import {File,@@ -404,7 +404,23 @@ app.post('/delete-user-file', (req, res) => {}const filesService = new FilesService(getAccountDb());- if (!verifyFileExists(fileId, filesService, res, 'file-not-found')) {+ const file = verifyFileExists(fileId, filesService, res, 'file-not-found');+ if (!file) {+ return;+ }++ // Check if user has permission to delete the file+ const { user_id: userId } = res.locals;++ const isOwner = file.owner === userId;+ const isServerAdmin = isAdmin(userId);++ if (!isOwner && !isServerAdmin) {+ res.status(403).send({+ status: 'error',+ reason: 'forbidden',+ details: 'file-delete-not-allowed',+ });return;}diff --git a/upcoming-release-notes/6338.md b/upcoming-release-notes/6338.mdnew file mode 100644index 0000000000..343f3714c7--- /dev/null+++ b/upcoming-release-notes/6338.md@@ -0,0 +1,6 @@+---+category: Bugfix+authors: [Copilot]+---++Fix authorization bypass preventing non-owners from deleting shared budgets in the application.