mirror of
https://github.com/go-gitea/gitea.git
synced 2026-08-30 02:40:44 +00:00
Backport #39025 by @bircni Bound the number and aggregate size of Swift manifests retained from an uploaded archive. Co-authored-by: bircni <bircni@icloud.com>
This commit is contained in:
@@ -21,6 +21,7 @@ import (
|
|||||||
var (
|
var (
|
||||||
ErrMissingManifestFile = util.NewInvalidArgumentErrorf("Package.swift file is missing")
|
ErrMissingManifestFile = util.NewInvalidArgumentErrorf("Package.swift file is missing")
|
||||||
ErrManifestFileTooLarge = util.NewInvalidArgumentErrorf("Package.swift file is too large")
|
ErrManifestFileTooLarge = util.NewInvalidArgumentErrorf("Package.swift file is too large")
|
||||||
|
ErrManifestFilesTooLarge = util.NewInvalidArgumentErrorf("Package.swift files are too large")
|
||||||
ErrInvalidManifestVersion = util.NewInvalidArgumentErrorf("manifest version is invalid")
|
ErrInvalidManifestVersion = util.NewInvalidArgumentErrorf("manifest version is invalid")
|
||||||
|
|
||||||
manifestPattern = regexp.MustCompile(`\APackage(?:@swift-(\d+(?:\.\d+)?(?:\.\d+)?))?\.swift\z`)
|
manifestPattern = regexp.MustCompile(`\APackage(?:@swift-(\d+(?:\.\d+)?(?:\.\d+)?))?\.swift\z`)
|
||||||
@@ -29,6 +30,8 @@ var (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
maxManifestFileSize = 128 * 1024
|
maxManifestFileSize = 128 * 1024
|
||||||
|
maxManifestFiles = 64
|
||||||
|
maxManifestSize = maxManifestFiles * maxManifestFileSize
|
||||||
|
|
||||||
PropertyScope = "swift.scope"
|
PropertyScope = "swift.scope"
|
||||||
PropertyName = "swift.name"
|
PropertyName = "swift.name"
|
||||||
@@ -140,6 +143,16 @@ func ParsePackage(sr io.ReaderAt, size int64, mr io.Reader) (*Package, error) {
|
|||||||
manifestFiles = append(manifestFiles, file)
|
manifestFiles = append(manifestFiles, file)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if len(manifestFiles) > maxManifestFiles {
|
||||||
|
return nil, ErrManifestFilesTooLarge
|
||||||
|
}
|
||||||
|
var manifestSize uint64
|
||||||
|
for _, file := range manifestFiles {
|
||||||
|
manifestSize += file.UncompressedSize64
|
||||||
|
}
|
||||||
|
if manifestSize > maxManifestSize {
|
||||||
|
return nil, ErrManifestFilesTooLarge
|
||||||
|
}
|
||||||
|
|
||||||
for _, file := range manifestFiles {
|
for _, file := range manifestFiles {
|
||||||
manifestMatch := manifestPattern.FindStringSubmatch(path.Base(file.Name))
|
manifestMatch := manifestPattern.FindStringSubmatch(path.Base(file.Name))
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ package swift
|
|||||||
import (
|
import (
|
||||||
"archive/zip"
|
"archive/zip"
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -54,6 +55,19 @@ func TestParsePackage(t *testing.T) {
|
|||||||
assert.ErrorIs(t, err, ErrManifestFileTooLarge)
|
assert.ErrorIs(t, err, ErrManifestFileTooLarge)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("TooManyManifestFiles", func(t *testing.T) {
|
||||||
|
entries := make([][2]string, 0, maxManifestFiles+1)
|
||||||
|
entries = append(entries, [2]string{"Package.swift", "// swift-tools-version:5.7"})
|
||||||
|
for i := range maxManifestFiles {
|
||||||
|
entries = append(entries, [2]string{fmt.Sprintf("Package@swift-5.%d.swift", i), "// swift-tools-version:5.7"})
|
||||||
|
}
|
||||||
|
|
||||||
|
data := writeOrderedZipArchive(entries)
|
||||||
|
p, err := ParsePackage(bytes.NewReader(data.Bytes()), int64(data.Len()), nil)
|
||||||
|
assert.Nil(t, p)
|
||||||
|
assert.ErrorIs(t, err, ErrManifestFilesTooLarge)
|
||||||
|
})
|
||||||
|
|
||||||
t.Run("WithoutMetadata", func(t *testing.T) {
|
t.Run("WithoutMetadata", func(t *testing.T) {
|
||||||
content1 := "// swift-tools-version:5.7\n//\n// Package.swift"
|
content1 := "// swift-tools-version:5.7\n//\n// Package.swift"
|
||||||
content2 := "// swift-tools-version:5.6\n//\n// Package@swift-5.6.swift"
|
content2 := "// swift-tools-version:5.6\n//\n// Package@swift-5.6.swift"
|
||||||
|
|||||||
Reference in New Issue
Block a user