fix: end GetObjectAttributes part pagination correctly

GetObjectAttributes decided truncation by comparing the last returned
part number with the part count (cmd/object-handlers.go:720), which is
only a coincidence of contiguous numbering. Sparse parts 1/3 reported a
complete page as truncated with a marker that loops, and parts 1/3/5
with max-parts=1 stopped after part 3 and silently dropped part 5. Set
IsTruncated in the break that proves an eligible part was left
unreturned, and zero NextPartNumberMarker when the listing is complete,
as ListObjectParts already does. Also reject negative x-amz-max-parts
and x-amz-part-number-marker in getAndValidateAttributesOpts with the
same API errors ListObjectParts uses, instead of answering an invalid
request with an empty parts listing; an absent or zero max-parts still
means the default page size.

Tests: TestAPIGetObjectAttributesPartsPagination (sparse 1/3/5 and
contiguous 1/2 walks on ErasureSD and Erasure),
TestGetAndValidateAttributesOptsPartsRange, and the sparse variants of
TestAPIGetObjectAttributesMultipartLogicalPartSize. Compatibility: no
field is added or removed; IsTruncated and NextPartNumberMarker change
only where they were wrong, and negative pagination values that no SDK
sends now fail fast.

Fixes pgsty/silo#115

Signed-off-by: Feng Ruohang <rh@vonng.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L7qJqWwy8oFA6aCXWRzXQe
This commit is contained in:
Feng Ruohang
2026-09-05 15:40:55 +08:00
parent b5409ca112
commit 2cbd48a3c3
4 changed files with 319 additions and 2 deletions
+108
View File
@@ -18,9 +18,11 @@
package cmd
import (
"encoding/xml"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
xhttp "github.com/minio/minio/internal/http"
@@ -76,3 +78,109 @@ func TestGetAndValidateAttributesOpts(t *testing.T) {
})
}
}
// TestGetAndValidateAttributesOptsPartsRange asserts that GetObjectAttributes
// range checks the ObjectParts pagination headers the way ListObjectParts
// does: a negative value is rejected with the same API error, an absent or
// zero x-amz-max-parts means the default page size, and in-range values are
// passed through unchanged.
func TestGetAndValidateAttributesOptsPartsRange(t *testing.T) {
globalBucketVersioningSys = &BucketVersioningSys{}
bucket := minioMetaBucket
ctx := t.Context()
testCases := []struct {
name string
maxParts string
marker string
wantValid bool
wantErr APIErrorCode
wantArgument string
wantValue string
wantMaxParts int
wantMarker int
}{
{
name: "defaults",
wantValid: true,
wantMaxParts: maxPartsList,
},
{
name: "zero max-parts means the default page size",
maxParts: "0",
wantValid: true,
wantMaxParts: maxPartsList,
},
{
name: "in range values pass through",
maxParts: "10",
marker: "3",
wantValid: true,
wantMaxParts: 10,
wantMarker: 3,
},
{
name: "negative max-parts is rejected",
maxParts: "-1",
wantValid: false,
wantErr: ErrInvalidMaxParts,
wantArgument: strings.ToLower(xhttp.AmzMaxParts),
wantValue: "-1",
},
{
name: "negative part-number-marker is rejected",
marker: "-1",
wantValid: false,
wantErr: ErrInvalidPartNumberMarker,
wantArgument: strings.ToLower(xhttp.AmzPartNumberMarker),
wantValue: "-1",
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/testbucket/testobject?attributes", nil)
req.Header.Set(xhttp.AmzObjectAttributes, "ObjectParts")
if testCase.maxParts != "" {
req.Header.Set(xhttp.AmzMaxParts, testCase.maxParts)
}
if testCase.marker != "" {
req.Header.Set(xhttp.AmzPartNumberMarker, testCase.marker)
}
opts, valid := getAndValidateAttributesOpts(ctx, rec, req, bucket, "testobject")
if valid != testCase.wantValid {
t.Fatalf("want valid %v, got %v (%s)", testCase.wantValid, valid, rec.Body.String())
}
if testCase.wantValid {
if opts.MaxParts != testCase.wantMaxParts {
t.Errorf("want MaxParts %d, got %d", testCase.wantMaxParts, opts.MaxParts)
}
if opts.PartNumberMarker != testCase.wantMarker {
t.Errorf("want PartNumberMarker %d, got %d", testCase.wantMarker, opts.PartNumberMarker)
}
return
}
wantErr := errorCodes.ToAPIErr(testCase.wantErr)
if rec.Code != wantErr.HTTPStatusCode {
t.Errorf("want HTTP status %d, got %d", wantErr.HTTPStatusCode, rec.Code)
}
var errResp objectAttributesErrorResponse
if err := xml.Unmarshal(rec.Body.Bytes(), &errResp); err != nil {
t.Fatalf("decode error response: %v (%s)", err, rec.Body.String())
}
if errResp.Code != wantErr.Code || errResp.Message != wantErr.Description {
t.Errorf("want error %s/%q, got %s/%q", wantErr.Code, wantErr.Description, errResp.Code, errResp.Message)
}
if errResp.ArgumentName == nil || *errResp.ArgumentName != testCase.wantArgument {
t.Errorf("want ArgumentName %q, got %v", testCase.wantArgument, errResp.ArgumentName)
}
if errResp.ArgumentValue == nil || *errResp.ArgumentValue != testCase.wantValue {
t.Errorf("want ArgumentValue %q, got %v", testCase.wantValue, errResp.ArgumentValue)
}
})
}
}