|
| 1 | +// Copyright (c) 2015-2026 MinIO, Inc. |
| 2 | +// |
| 3 | +// This file is part of MinIO Object Storage stack |
| 4 | +// |
| 5 | +// This program is free software: you can redistribute it and/or modify |
| 6 | +// it under the terms of the GNU Affero General Public License as published by |
| 7 | +// the Free Software Foundation, either version 3 of the License, or |
| 8 | +// (at your option) any later version. |
| 9 | +// |
| 10 | +// This program is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU Affero General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU Affero General Public License |
| 16 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +package cmd |
| 19 | + |
| 20 | +import ( |
| 21 | + "bytes" |
| 22 | + "encoding/base64" |
| 23 | + "encoding/xml" |
| 24 | + "net/http" |
| 25 | + "net/http/httptest" |
| 26 | + "strings" |
| 27 | + "testing" |
| 28 | + |
| 29 | + "github.com/minio/minio/internal/auth" |
| 30 | + xhttp "github.com/minio/minio/internal/http" |
| 31 | +) |
| 32 | + |
| 33 | +func TestAPIRejectsUnsupportedChecksumHeaders(t *testing.T) { |
| 34 | + defer DetectTestLeak(t)() |
| 35 | + ExecObjectLayerAPITest(ExecObjectLayerAPITestArgs{ |
| 36 | + t: t, |
| 37 | + objAPITest: testAPIRejectsUnsupportedChecksumHeaders, |
| 38 | + endpoints: []string{"CopyObject", "NewMultipart", "PutObject", "PutObjectPart"}, |
| 39 | + }) |
| 40 | +} |
| 41 | + |
| 42 | +func testAPIRejectsUnsupportedChecksumHeaders(obj ObjectLayer, instanceType, bucketName string, |
| 43 | + apiRouter http.Handler, credentials auth.Credentials, t *testing.T, |
| 44 | +) { |
| 45 | + data := []byte("unsupported-checksum") |
| 46 | + unsupportedValue := base64.StdEncoding.EncodeToString(make([]byte, 64)) |
| 47 | + |
| 48 | + put := func(object string, headers map[string]string) *httptest.ResponseRecorder { |
| 49 | + t.Helper() |
| 50 | + req, err := newTestSignedRequestV4(http.MethodPut, getPutObjectURL("", bucketName, object), |
| 51 | + int64(len(data)), bytes.NewReader(data), credentials.AccessKey, credentials.SecretKey, headers) |
| 52 | + if err != nil { |
| 53 | + t.Fatal(err) |
| 54 | + } |
| 55 | + rec := httptest.NewRecorder() |
| 56 | + apiRouter.ServeHTTP(rec, req) |
| 57 | + return rec |
| 58 | + } |
| 59 | + assertRejected := func(name string, rec *httptest.ResponseRecorder) { |
| 60 | + t.Helper() |
| 61 | + if rec.Code != http.StatusBadRequest || !strings.Contains(rec.Body.String(), "<Code>InvalidArgument</Code>") { |
| 62 | + t.Fatalf("%s: %s returned %d, want InvalidArgument: %s", instanceType, name, rec.Code, rec.Body.String()) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + for _, algorithm := range []string{"md5", "sha512", "xxhash64", "xxhash3", "xxhash128", "future"} { |
| 67 | + object := "checksums/unsupported-" + algorithm |
| 68 | + assertRejected(algorithm, put(object, map[string]string{ |
| 69 | + "x-amz-sdk-checksum-algorithm": "SHA512", |
| 70 | + "x-amz-checksum-" + algorithm: unsupportedValue, |
| 71 | + })) |
| 72 | + if _, err := obj.GetObjectInfo(t.Context(), bucketName, object, ObjectOptions{}); !isErrObjectNotFound(err) { |
| 73 | + t.Fatalf("%s: rejected %s checksum stored an object: %v", instanceType, algorithm, err) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + assertRejected("unsupported trailer", put("checksums/unsupported-trailer", map[string]string{ |
| 78 | + xhttp.AmzTrailer: "x-amz-checksum-sha512", |
| 79 | + })) |
| 80 | + |
| 81 | + newMultipart := func(name string, headers map[string]string) *httptest.ResponseRecorder { |
| 82 | + t.Helper() |
| 83 | + req, err := newTestSignedRequestV4(http.MethodPost, getNewMultipartURL("", bucketName, name), |
| 84 | + 0, nil, credentials.AccessKey, credentials.SecretKey, headers) |
| 85 | + if err != nil { |
| 86 | + t.Fatal(err) |
| 87 | + } |
| 88 | + rec := httptest.NewRecorder() |
| 89 | + apiRouter.ServeHTTP(rec, req) |
| 90 | + return rec |
| 91 | + } |
| 92 | + assertRejected("NewMultipartUpload value header", newMultipart("checksums/mp-value", map[string]string{ |
| 93 | + "x-amz-checksum-sha512": unsupportedValue, |
| 94 | + })) |
| 95 | + assertRejected("NewMultipartUpload trailer", newMultipart("checksums/mp-trailer", map[string]string{ |
| 96 | + xhttp.AmzTrailer: "x-amz-checksum-sha512", |
| 97 | + })) |
| 98 | + |
| 99 | + rec := newMultipart("checksums/mp-part", nil) |
| 100 | + if rec.Code != http.StatusOK { |
| 101 | + t.Fatalf("%s: NewMultipartUpload setup returned %d: %s", instanceType, rec.Code, rec.Body.String()) |
| 102 | + } |
| 103 | + var initiated InitiateMultipartUploadResponse |
| 104 | + if err := xml.Unmarshal(rec.Body.Bytes(), &initiated); err != nil { |
| 105 | + t.Fatal(err) |
| 106 | + } |
| 107 | + req, err := newTestSignedRequestV4(http.MethodPut, |
| 108 | + getPutObjectPartURL("", bucketName, "checksums/mp-part", initiated.UploadID, "1"), |
| 109 | + int64(len(data)), bytes.NewReader(data), credentials.AccessKey, credentials.SecretKey, |
| 110 | + map[string]string{"x-amz-checksum-sha512": unsupportedValue}) |
| 111 | + if err != nil { |
| 112 | + t.Fatal(err) |
| 113 | + } |
| 114 | + rec = httptest.NewRecorder() |
| 115 | + apiRouter.ServeHTTP(rec, req) |
| 116 | + assertRejected("UploadPart", rec) |
| 117 | + parts, err := obj.ListObjectParts(t.Context(), bucketName, "checksums/mp-part", initiated.UploadID, 0, 1000, ObjectOptions{}) |
| 118 | + if err != nil { |
| 119 | + t.Fatal(err) |
| 120 | + } |
| 121 | + if len(parts.Parts) != 0 { |
| 122 | + t.Fatalf("%s: rejected UploadPart stored %d parts", instanceType, len(parts.Parts)) |
| 123 | + } |
| 124 | + if err := obj.AbortMultipartUpload(t.Context(), bucketName, "checksums/mp-part", initiated.UploadID, ObjectOptions{}); err != nil { |
| 125 | + t.Fatal(err) |
| 126 | + } |
| 127 | + |
| 128 | + source := "checksums/source" |
| 129 | + putCopyChecksumSource(t, apiRouter, credentials, bucketName, source, data, nil) |
| 130 | + rec = copyChecksumRequest(t, apiRouter, credentials, bucketName, source, "checksums/copy", map[string]string{ |
| 131 | + "x-amz-checksum-sha512": unsupportedValue, |
| 132 | + }) |
| 133 | + assertRejected("CopyObject", rec) |
| 134 | + if _, err := obj.GetObjectInfo(t.Context(), bucketName, "checksums/copy", ObjectOptions{}); !isErrObjectNotFound(err) { |
| 135 | + t.Fatalf("%s: rejected CopyObject stored a destination: %v", instanceType, err) |
| 136 | + } |
| 137 | +} |
0 commit comments