Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

admin/drive/cleanup could delete imported custom emoji #8222

Open
Johann150 opened this issue Jan 29, 2022 · 4 comments
Open

admin/drive/cleanup could delete imported custom emoji #8222

Johann150 opened this issue Jan 29, 2022 · 4 comments
Labels
🐛Bug Unexpected behavior packages/backend Server side specific issue/PR

Comments

@Johann150
Copy link
Contributor

Johann150 commented Jan 29, 2022

💡 Summary

/api/admin/drive/cleanup deletes all drive files where userId = NULL:

const files = await DriveFiles.find({
userId: IsNull(),
});

This could accidentally delete imported emoji since they are created with userId set to NULL:

driveFile = await uploadFromUrl({ url: emoji.originalUrl, user: null, force: true });

🔧❓ Possible Solutions

  1. Check that files are not used as emojis in /api/admin/drive/cleanup.
    This would probably be complicated, but could be done with the TypeORM equivalent of
... LEFT JOIN emoji ON drive_file.url = emoji."originalUrl" WHERE emoji.id IS NULL
  1. Make @instance.actor the owner of imported emojis.
  2. Remove this API endpoint because drive files for deleted users are already deleted when the account is deleted:

Additional Info

related to #7304

@Johann150 Johann150 added the ⚠️bug? This might be a bug label Jan 29, 2022
@syuilo syuilo added 🐛Bug Unexpected behavior and removed ⚠️bug? This might be a bug labels Jan 30, 2022
@syuilo
Copy link
Member

syuilo commented Jan 30, 2022

There is also the option of not using Drive to manage file data used internally by the system, such as emoji (e.g., create a more general file table and have DriveFile refer to it).

@Johann150
Copy link
Contributor Author

Johann150 commented Jan 30, 2022 via email

@Johann150
Copy link
Contributor Author

I think option 1 is too complicated.

If option 2 is implemented, we should also add a NOT NULL constraint to drive_file.user. Maybe the default for the column could be set after @instance.actor is created, but that sounds like a bad idea since it is not part of the usual database management with migrations, so it could easily get broken. Even without the default value of the column, it sounds like a good idea because I wouldn't be sure if other parts of Misskey do not also have different interpretations of the drive_file.user value.

However it would also implicate that @instance.actor is also subject to the drive usage limit, which could be a good and a bad thing. It would stop too many emoji being added to an instance (there are known performance issues with the emoji picker if many emojis are present). On the other hand it might also be an annoyance for instance administrators & moderators. Syuilo's suggestion would also avoid this issue, but continuing to use NULL could also avoid it and is already implemented and simpler.

It would also be possible to combine options 2 and 3.

However, in full view with the drive quota problem in mind I think option 3 is the best.

@Johann150 Johann150 added the packages/backend Server side specific issue/PR label Feb 26, 2022
@Johann150
Copy link
Contributor Author

If option 3 is implemented, I'm not sure if this constraint should be changed as well:

TABLE "drive_file" CONSTRAINT "FK_860fa6f6c7df5bb887249fba22e" FOREIGN KEY ("userId") REFERENCES "user"(id) ON DELETE SET NULL

Setting to null on delete does not make sense because it would move the file to the drive of the system. Cascading the delete is also not a good idea because the file on disk has to be deleted outside of the database as well.

Maybe it should be changed to ON DELETE RESTRICT to make sure all drive files are deleted before a user can be deleted. As commented above this procedure is already followed by Misskey anway, it would just correctly mirror it in the database structure.

Jeder321 pushed a commit to Jeder321/misskey that referenced this issue Aug 15, 2022
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 misskey-dev#8222
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛Bug Unexpected behavior packages/backend Server side specific issue/PR
Projects
None yet
Development

No branches or pull requests

2 participants