fix: validate group avatar attachments
This commit is contained in:
+22
-13
@@ -4655,6 +4655,12 @@ func serveIMGroupCreate(wrt http.ResponseWriter, req *http.Request) {
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
if settings.AvatarFileID != "" {
|
||||
if err := linkIMGroupAvatarAttachment(principal.TenantID, topicName, settings.AvatarFileID); err != nil {
|
||||
writeIMError(wrt, req, http.StatusBadRequest, 30000, imReasonMalformedRequest, "malformed request", map[string]any{"field": "avatar_file_id"})
|
||||
return
|
||||
}
|
||||
}
|
||||
memberIDs := make([]types.Uid, 0, len(body.MemberUserIDs))
|
||||
seen := map[types.Uid]bool{principal.Uid: true}
|
||||
for _, rawID := range body.MemberUserIDs {
|
||||
@@ -4671,11 +4677,6 @@ func serveIMGroupCreate(wrt http.ResponseWriter, req *http.Request) {
|
||||
writeIMRelationError(wrt, req, err)
|
||||
return
|
||||
}
|
||||
if settings.AvatarFileID != "" {
|
||||
if err := store.Files.LinkAttachments(principal.TenantID, topicName, types.ZeroUid, []string{settings.AvatarFileID}); err != nil {
|
||||
logs.Warn.Printf("group[%s]: failed to link avatar attachment: %v", topicName, err)
|
||||
}
|
||||
}
|
||||
dispatchIMGroupEvents(principal.TenantID)
|
||||
created, err := store.Topics.Get(principal.TenantID, topicName)
|
||||
if err != nil {
|
||||
@@ -4932,7 +4933,14 @@ func serveIMGroupDetail(wrt http.ResponseWriter, req *http.Request, topic string
|
||||
public["fn"] = name
|
||||
}
|
||||
if body.AvatarFileID != nil {
|
||||
public["avatar_file_id"] = strings.TrimSpace(*body.AvatarFileID)
|
||||
avatar := strings.TrimSpace(*body.AvatarFileID)
|
||||
if avatar != "" {
|
||||
if err := linkIMGroupAvatarAttachment(principal.TenantID, topic, avatar); err != nil {
|
||||
writeIMError(wrt, req, http.StatusBadRequest, 30000, imReasonMalformedRequest, "malformed request", map[string]any{"field": "avatar_file_id"})
|
||||
return
|
||||
}
|
||||
}
|
||||
public["avatar_file_id"] = avatar
|
||||
}
|
||||
if body.Description != nil {
|
||||
description := strings.TrimSpace(*body.Description)
|
||||
@@ -4961,13 +4969,6 @@ func serveIMGroupDetail(wrt http.ResponseWriter, req *http.Request, topic string
|
||||
return
|
||||
}
|
||||
}
|
||||
if body.AvatarFileID != nil {
|
||||
if avatar := strings.TrimSpace(*body.AvatarFileID); avatar != "" {
|
||||
if err := store.Files.LinkAttachments(principal.TenantID, topic, types.ZeroUid, []string{avatar}); err != nil {
|
||||
logs.Warn.Printf("group[%s]: failed to link avatar attachment: %v", topic, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
dispatchIMGroupEvents(principal.TenantID)
|
||||
updated, err := store.Topics.Get(principal.TenantID, topic)
|
||||
if err != nil {
|
||||
@@ -6949,6 +6950,14 @@ func normalizeIMUploadedFileID(raw string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func linkIMGroupAvatarAttachment(tenantID types.TenantID, topic, avatarFileID string) error {
|
||||
fileID := normalizeIMUploadedFileID(avatarFileID)
|
||||
if fileID == "" {
|
||||
return types.ErrMalformed
|
||||
}
|
||||
return store.Files.LinkAttachments(tenantID, topic, types.ZeroUid, []string{fileID})
|
||||
}
|
||||
|
||||
func parseIMLimit(raw string, fallback, max int) int {
|
||||
limit := fallback
|
||||
if raw != "" {
|
||||
|
||||
Reference in New Issue
Block a user