Add fileSize limit to extractors (#35200) (#35280)

Automatic Merge
Этот коммит содержится в:
Mattermost Build
2026-02-13 14:09:32 +02:00
коммит произвёл GitHub
родитель 053dcf62b6
Коммит eb8c99fe9c
12 изменённых файлов: 104 добавлений и 24 удалений

Просмотреть файл

@@ -14,6 +14,8 @@ import (
"strings"
"github.com/mholt/archives"
"github.com/mattermost/mattermost/server/v8/channels/utils"
)
type archiveExtractor struct {
@@ -38,7 +40,7 @@ func getExtAlsoTarGz(name string) string {
return filepath.Ext(name)
}
func (ae *archiveExtractor) Extract(name string, r io.ReadSeeker) (string, error) {
func (ae *archiveExtractor) Extract(name string, r io.ReadSeeker, maxFileSize int64) (string, error) {
ext := getExtAlsoTarGz(name)
// Create a temporary file, using `*` control the random component while preserving the extension.
@@ -81,12 +83,19 @@ func (ae *archiveExtractor) Extract(name string, r io.ReadSeeker) (string, error
}
defer file.Close()
data, err := io.ReadAll(file)
if err != nil {
return err
// Limit the size of decompressed archive entries to prevent
// memory exhaustion from zip bombs or other malicious archives.
var reader io.Reader = file
if maxFileSize > 0 {
reader = utils.NewLimitedReaderWithError(file, maxFileSize)
}
subtext, extractErr := ae.SubExtractor.Extract(filename, bytes.NewReader(data))
data, err := io.ReadAll(reader)
if err != nil {
return fmt.Errorf("error reading archive entry %s: %w", path, err)
}
subtext, extractErr := ae.SubExtractor.Extract(filename, bytes.NewReader(data), maxFileSize)
if extractErr == nil {
text.WriteString(subtext + " ")
}