[MM-64402] Improve validation of imported attachments (#31201)
* Improve validation of imported attachments * Simplify multiple errors handling * Improve logic * Fix abs paths in tests * Remove redundant clean * Implement additional validation * Fix absolute paths in test * Add additional tests --------- Co-authored-by: Lorenzo Gallegos <1328683+enzowritescode@users.noreply.github.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
07239a5217
Коммит
d38c27f96f
@@ -7,6 +7,7 @@ import (
|
||||
"archive/zip"
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@@ -45,20 +46,32 @@ func processAttachmentPaths(c request.CTX, files *[]imports.AttachmentImportData
|
||||
if files == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var ok bool
|
||||
var errs []error
|
||||
for i, f := range *files {
|
||||
if f.Path != nil {
|
||||
path := filepath.Join(basePath, *f.Path)
|
||||
originalPath := *f.Path
|
||||
|
||||
path, valid := imports.ValidateAttachmentPathForImport(originalPath, basePath)
|
||||
|
||||
*f.Path = path
|
||||
|
||||
if !valid {
|
||||
errs = append(errs, fmt.Errorf("invalid attachment path %q", originalPath))
|
||||
continue
|
||||
}
|
||||
|
||||
if len(filesMap) > 0 {
|
||||
if (*files)[i].Data, ok = filesMap[path]; !ok {
|
||||
return fmt.Errorf("attachment %q not found in map", path)
|
||||
if (*files)[i].Data, ok = filesMap[*f.Path]; !ok {
|
||||
errs = append(errs, fmt.Errorf("attachment %q not found in map", originalPath))
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
|
||||
func processAttachments(c request.CTX, line *imports.LineImportData, basePath string, filesMap map[string]*zip.File) error {
|
||||
@@ -88,7 +101,11 @@ func processAttachments(c request.CTX, line *imports.LineImportData, basePath st
|
||||
}
|
||||
case "user":
|
||||
if line.User.ProfileImage != nil {
|
||||
path := filepath.Join(basePath, *line.User.ProfileImage)
|
||||
path, valid := imports.ValidateAttachmentPathForImport(*line.User.ProfileImage, basePath)
|
||||
if !valid {
|
||||
return fmt.Errorf("invalid profile image path %q", *line.User.ProfileImage)
|
||||
}
|
||||
|
||||
*line.User.ProfileImage = path
|
||||
if len(filesMap) > 0 {
|
||||
if line.User.ProfileImageData, ok = filesMap[path]; !ok {
|
||||
@@ -98,7 +115,11 @@ func processAttachments(c request.CTX, line *imports.LineImportData, basePath st
|
||||
}
|
||||
case "bot":
|
||||
if line.Bot.ProfileImage != nil {
|
||||
path := filepath.Join(basePath, *line.Bot.ProfileImage)
|
||||
path, valid := imports.ValidateAttachmentPathForImport(*line.Bot.ProfileImage, basePath)
|
||||
if !valid {
|
||||
return fmt.Errorf("invalid bot profile image path %q", *line.Bot.ProfileImage)
|
||||
}
|
||||
|
||||
*line.Bot.ProfileImage = path
|
||||
if len(filesMap) > 0 {
|
||||
if line.Bot.ProfileImageData, ok = filesMap[path]; !ok {
|
||||
@@ -108,7 +129,11 @@ func processAttachments(c request.CTX, line *imports.LineImportData, basePath st
|
||||
}
|
||||
case "emoji":
|
||||
if line.Emoji.Image != nil {
|
||||
path := filepath.Join(basePath, *line.Emoji.Image)
|
||||
path, valid := imports.ValidateAttachmentPathForImport(*line.Emoji.Image, basePath)
|
||||
if !valid {
|
||||
return fmt.Errorf("invalid emoji image path %q", *line.Emoji.Image)
|
||||
}
|
||||
|
||||
*line.Emoji.Image = path
|
||||
if len(filesMap) > 0 {
|
||||
if line.Emoji.Data, ok = filesMap[path]; !ok {
|
||||
|
||||
Ссылка в новой задаче
Block a user