From bc1c66e16eb0c65482af3d5935dd1d752acd9f7b Mon Sep 17 00:00:00 2001 From: Johann150 Date: Wed, 3 Aug 2022 11:00:48 +0200 Subject: [PATCH] remove admin/drive/cleanup API This API endpoint is not working correctly and can cause unintended data loss: It may remove emojis that have been imported from other instances. See also https://github.com/misskey-dev/misskey/issues/8222 --- ...659516638000-drive-file-user-constraint.js | 13 +++++++++ .../backend/src/models/entities/drive-file.ts | 2 +- packages/backend/src/server/api/endpoints.ts | 2 -- .../api/endpoints/admin/drive/cleanup.ts | 28 ------------------- 4 files changed, 14 insertions(+), 31 deletions(-) create mode 100644 packages/backend/migration/1659516638000-drive-file-user-constraint.js delete mode 100644 packages/backend/src/server/api/endpoints/admin/drive/cleanup.ts diff --git a/packages/backend/migration/1659516638000-drive-file-user-constraint.js b/packages/backend/migration/1659516638000-drive-file-user-constraint.js new file mode 100644 index 000000000..42a5cfbba --- /dev/null +++ b/packages/backend/migration/1659516638000-drive-file-user-constraint.js @@ -0,0 +1,13 @@ +export class driveFileUserConstraint1659516638000 { + name = 'driveFileUserConstraint1659516638000'; + + async up(queryRunner) { + await queryRunner.query(`ALTER TABLE "drive_file" DROP CONSTRAINT "FK_860fa6f6c7df5bb887249fba22e"`); + await queryRunner.query(`ALTER TABLE "drive_file" ADD CONSTRAINT "FK_860fa6f6c7df5bb887249fba22e" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE RESTRICT`); + } + + async down(queryRunner) { + await queryRunner.query(`ALTER TABLE "drive_file" DROP CONSTRAINT "FK_860fa6f6c7df5bb887249fba22e"`); + await queryRunner.query(`ALTER TABLE "drive_file" ADD CONSTRAINT "FK_860fa6f6c7df5bb887249fba22e" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE SET NULL`); + } +} diff --git a/packages/backend/src/models/entities/drive-file.ts b/packages/backend/src/models/entities/drive-file.ts index a636d1d51..1b175be8e 100644 --- a/packages/backend/src/models/entities/drive-file.ts +++ b/packages/backend/src/models/entities/drive-file.ts @@ -24,7 +24,7 @@ export class DriveFile { public userId: User['id'] | null; @ManyToOne(type => User, { - onDelete: 'SET NULL', + onDelete: 'RESTRICT', }) @JoinColumn() public user: User | null; diff --git a/packages/backend/src/server/api/endpoints.ts b/packages/backend/src/server/api/endpoints.ts index 9eedfd53c..88c389040 100644 --- a/packages/backend/src/server/api/endpoints.ts +++ b/packages/backend/src/server/api/endpoints.ts @@ -10,7 +10,6 @@ import * as ep___admin_announcements_list from './endpoints/admin/announcements/ import * as ep___admin_announcements_update from './endpoints/admin/announcements/update.js'; import * as ep___admin_deleteAllFilesOfAUser from './endpoints/admin/delete-all-files-of-a-user.js'; import * as ep___admin_drive_cleanRemoteFiles from './endpoints/admin/drive/clean-remote-files.js'; -import * as ep___admin_drive_cleanup from './endpoints/admin/drive/cleanup.js'; import * as ep___admin_drive_files from './endpoints/admin/drive/files.js'; import * as ep___admin_drive_showFile from './endpoints/admin/drive/show-file.js'; import * as ep___admin_emoji_addAliasesBulk from './endpoints/admin/emoji/add-aliases-bulk.js'; @@ -320,7 +319,6 @@ const eps = [ ['admin/announcements/update', ep___admin_announcements_update], ['admin/delete-all-files-of-a-user', ep___admin_deleteAllFilesOfAUser], ['admin/drive/clean-remote-files', ep___admin_drive_cleanRemoteFiles], - ['admin/drive/cleanup', ep___admin_drive_cleanup], ['admin/drive/files', ep___admin_drive_files], ['admin/drive/show-file', ep___admin_drive_showFile], ['admin/emoji/add-aliases-bulk', ep___admin_emoji_addAliasesBulk], diff --git a/packages/backend/src/server/api/endpoints/admin/drive/cleanup.ts b/packages/backend/src/server/api/endpoints/admin/drive/cleanup.ts deleted file mode 100644 index 3db942e6c..000000000 --- a/packages/backend/src/server/api/endpoints/admin/drive/cleanup.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { IsNull } from 'typeorm'; -import define from '../../../define.js'; -import { deleteFile } from '@/services/drive/delete-file.js'; -import { DriveFiles } from '@/models/index.js'; - -export const meta = { - tags: ['admin'], - - requireCredential: true, - requireModerator: true, -} as const; - -export const paramDef = { - type: 'object', - properties: {}, - required: [], -} as const; - -// eslint-disable-next-line import/no-default-export -export default define(meta, paramDef, async (ps, me) => { - const files = await DriveFiles.findBy({ - userId: IsNull(), - }); - - for (const file of files) { - deleteFile(file); - } -});