mirror of
https://github.com/go-gitea/gitea.git
synced 2026-08-22 21:53:34 +00:00
fix(packages): limit Maven checksum uploads (#39028)
Bound checksum uploads to the maximum usable digest length before buffering their content.
This commit is contained in:
@@ -30,6 +30,8 @@ import (
|
||||
packages_service "gitea.dev/services/packages"
|
||||
)
|
||||
|
||||
const maxChecksumSize = sha512.Size*2 + 1
|
||||
|
||||
const (
|
||||
mavenMetadataFile = "maven-metadata.xml"
|
||||
extensionMD5 = ".md5"
|
||||
@@ -260,12 +262,21 @@ func UploadPackageFile(ctx *context.Context) {
|
||||
}
|
||||
defer releaser()
|
||||
|
||||
buf, err := packages_module.CreateHashedBufferFromReader(ctx.Req.Body)
|
||||
ext := path.Ext(params.Filename)
|
||||
reader := io.Reader(ctx.Req.Body)
|
||||
if isChecksumExtension(ext) {
|
||||
reader = io.LimitReader(reader, maxChecksumSize+1)
|
||||
}
|
||||
buf, err := packages_module.CreateHashedBufferFromReader(reader)
|
||||
if err != nil {
|
||||
apiError(ctx, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
defer buf.Close()
|
||||
if isChecksumExtension(ext) && !isChecksumSizeAllowed(buf.Size()) {
|
||||
apiError(ctx, http.StatusRequestEntityTooLarge, "checksum is too large")
|
||||
return
|
||||
}
|
||||
|
||||
pvci := &packages_service.PackageCreationInfo{
|
||||
PackageInfo: packages_service.PackageInfo{
|
||||
@@ -291,8 +302,6 @@ func UploadPackageFile(ctx *context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
ext := path.Ext(params.Filename)
|
||||
|
||||
// Do not upload checksum files but compare the hashes.
|
||||
if isChecksumExtension(ext) {
|
||||
pv, err := packages_model.GetVersionByNameAndVersion(ctx, pvci.Owner.ID, pvci.PackageType, pvci.Name, pvci.Version)
|
||||
@@ -404,6 +413,10 @@ func UploadPackageFile(ctx *context.Context) {
|
||||
ctx.Status(http.StatusCreated)
|
||||
}
|
||||
|
||||
func isChecksumSizeAllowed(size int64) bool {
|
||||
return size <= maxChecksumSize
|
||||
}
|
||||
|
||||
func isChecksumExtension(ext string) bool {
|
||||
return ext == extensionMD5 || ext == extensionSHA1 || ext == extensionSHA256 || ext == extensionSHA512
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package maven
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestChecksumSizeAllowed(t *testing.T) {
|
||||
assert.True(t, isChecksumSizeAllowed(maxChecksumSize))
|
||||
assert.False(t, isChecksumSizeAllowed(maxChecksumSize+1))
|
||||
}
|
||||
Reference in New Issue
Block a user