Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

rest: reject negative sizes in HTTP response headers - fixes #10041

Yousef Issa5f0e72c03f8e
diff --git a/lib/rest/headers.go b/lib/rest/headers.go
index 6fc5a2479..c1b19af96 100644
--- a/lib/rest/headers.go
+++ b/lib/rest/headers.go
@@ -165,7 +165,7 @@ func ParseSizeFromHeaders(headers http.Header) (size int64) {
var contentLength = headers.Get("Content-Length")
if len(contentLength) != 0 {
var err error
- if size, err = strconv.ParseInt(contentLength, 10, 64); err != nil {
+ if size, err = strconv.ParseInt(contentLength, 10, 64); err != nil || size < 0 {
return -1
}
}
@@ -183,7 +183,7 @@ func ParseSizeFromHeaders(headers http.Header) (size int64) {
return -1
}
ret, err := strconv.ParseInt(contentRange[slash+1:], 10, 64)
- if err != nil {
+ if err != nil || ret < 0 {
return -1
}
return ret
diff --git a/lib/rest/headers_test.go b/lib/rest/headers_test.go
index 9901cc56f..3289b62b5 100644
--- a/lib/rest/headers_test.go
+++ b/lib/rest/headers_test.go
@@ -16,6 +16,12 @@ func TestParseSizeFromHeaders(t *testing.T) {
"", "", -1,
}, {
"42", "", 42,
+ }, {
+ "0", "", 0,
+ }, {
+ "-5", "", -1,
+ }, {
+ "-5", "bytes 0-1/42", -1,
}, {
"42", "invalid", -1,
}, {
@@ -26,6 +32,12 @@ func TestParseSizeFromHeaders(t *testing.T) {
"12", "otherUnit 22-33/42", -1,
}, {
"12", "bytes 22-33/*", -1,
+ }, {
+ "", "bytes 0-1/-5", -1,
+ }, {
+ "2", "bytes 0-1/-5", -1,
+ }, {
+ "", "bytes */0", 0,
}, {
"0", "bytes */42", 42,
}}