Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

Harden avatar cleanup ownership checks

LinkinStarsb575fe893bda
diff --git a/internal/service/content/user_service.go b/internal/service/content/user_service.go
index d0ebe875..c1f800ff 100644
--- a/internal/service/content/user_service.go
+++ b/internal/service/content/user_service.go
@@ -360,7 +360,7 @@ func (us *UserService) UpdateInfo(ctx context.Context, req *schema.UpdateInfoReq
cond := us.formatUserInfoForUpdateInfo(oldUserInfo, req)
- us.cleanUpRemovedAvatar(ctx, oldUserInfo.Avatar, cond.Avatar)
+ us.cleanUpRemovedAvatar(ctx, req.UserID, oldUserInfo.Avatar, cond.Avatar)
err = us.userRepo.UpdateInfo(ctx, cond)
if err != nil {
@@ -407,6 +407,7 @@ func (us *UserService) validateAvatarInfo(
func (us *UserService) cleanUpRemovedAvatar(
ctx context.Context,
+ updatingUserID string,
oldAvatarJSON string,
newAvatarJSON string,
) {
@@ -434,6 +435,13 @@ func (us *UserService) cleanUpRemovedAvatar(
log.Warn("no file record found for old avatar url:", oldAvatar.Custom)
return
}
+ if fileRecord.UserID != updatingUserID || fileRecord.Source != string(plugin.UserAvatar) {
+ log.Warnf(
+ "refuse to clean avatar url %q: file record owner/source mismatch (owner=%s source=%s updating_user=%s)",
+ oldAvatar.Custom, fileRecord.UserID, fileRecord.Source, updatingUserID,
+ )
+ return
+ }
if err := us.fileRecordService.DeleteAndMoveFileRecord(ctx, fileRecord); err != nil {
log.Error(err)
}