mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-10-08 07:17:48 +02:00
* s3err: add InvalidArgument and AuthorizationHeaderMalformed codes Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * s3api: answer unrecognized bucket PUT sub-resources with 501 A PUT on a bucket carrying an unrecognized query (logging, metrics, intelligent-tiering, ...) fell through to the bare CreateBucket route and returned BucketAlreadyOwnedByYou or re-created the bucket. AWS answers these with NotImplemented. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * s3api: reject malformed copy-source and multipart PUT parameters A malformed X-Amz-Copy-Source or a non-numeric partNumber fell through to the plain PutObject route and stored the body as a regular object. Answer them with InvalidArgument-class errors instead of writing data. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * s3api: verify x-amz-content-sha256 against the streamed body A PUT carrying a hex or base64 payload hash now streams through a verifier that reports a mismatch once the stream is exhausted, instead of storing an object that does not match its declared hash. The error is deferred so intermediate reads that drop (n>0, err) results cannot silently swallow it. Sentinel values (unsigned/streaming payloads) remain exempt and malformed values fail fast. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * s3api: map truncated SigV4 headers to the error for the missing field AWS answers an Authorization header missing Credential= with InvalidArgument and one missing or malformed Signature= with AuthorizationHeaderMalformed, instead of a generic MissingFields. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * s3api: answer IAM/STS failures in the query-protocol envelope Embedded IAM and STS routes now report authentication, form-parse and authorization failures with the IAM ErrorResponse body instead of the S3 Error envelope, so IAM SDK clients can parse them. Requests signed for s3 keep the S3 envelope, keyed off the credential scope. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * s3api: return 403 AccessDenied when the request has no Date Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * s3api: answer throttling rejections as SlowDown ErrTooManyRequest and ErrRequestBytesExceed reported made-up codes; AWS serves these throttling rejections as SlowDown. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * s3api: reject versionId requests on buckets that never had versioning GET, HEAD and DELETE carrying a non-empty versionId on an unversioned bucket now fail with InvalidArgument instead of being answered as a plain object request. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * s3api: answer DeleteObjects over 1000 keys with MalformedXML Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * s3api: InvalidArgument for non-numeric or out-of-range part numbers partNumber=abc, 0 and >10000 all resolve to InvalidArgument, matching AWS, instead of InvalidPart. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * iam: correct LimitExceeded, InvalidAction and ServiceFailure mappings LimitExceeded is a conflict (409), an unknown Action is InvalidAction (404) rather than NotImplemented, and internal failures report the IAM receiver fault type. Applies to both the embedded IAM endpoint and the standalone iamapi server. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * iam: refuse DeleteUser while access keys remain Deleting a user with live credentials orphaned its access keys; AWS answers DeleteConflict until they are removed first. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test/s3: add S3/IAM error-response compatibility harness * s3: return InvalidArgument for malformed x-amz-content-sha256 A header value that decodes to neither 32-byte hex nor base64 is a malformed argument, not a hash mismatch. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test: stop spawned mini when readiness times out A slow-starting server otherwise survives the failure path and keeps the S3 port occupied for the next run. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test: register atexit cleanup before setup A failed setup previously skipped cleanup, leaking the bucket and IAM user on persistent servers. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * s3api: reject unrouted subresources on the DELETE bucket catch-all PutBucketHandler gained the same guard when the route-level check moved into the handlers; DeleteBucketHandler was missed, so an authorized DELETE /bucket?logging could delete the bucket instead of answering NotImplemented. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test: tolerate unset fixture variables in cleanup and cover DELETE ?logging Cleanup now runs its IAM/multipart steps only when setup reached them, so an early setup failure still removes the bucket. Added a DELETE bucket-subresource case asserting NotImplemented and bucket survival. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test: fail a case when its side-effect check reports a regression A non-empty check note now fails the case, so a deleted bucket or an object created by a malformed request cannot slip through behind a passing status check. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --------- Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
315 lines
9.8 KiB
Go
315 lines
9.8 KiB
Go
package s3api
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/credential"
|
|
_ "github.com/seaweedfs/seaweedfs/weed/credential/memory"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
|
|
)
|
|
|
|
func TestGetRequestDataReader_ChunkedEncodingWithoutIAM(t *testing.T) {
|
|
// Create an S3ApiServer with IAM disabled
|
|
s3a := &S3ApiServer{
|
|
iam: NewIdentityAccessManagementWithStore(&S3ApiServerOption{}, nil, string(credential.StoreTypeMemory)),
|
|
}
|
|
// Ensure IAM is disabled for this test
|
|
s3a.iam.isAuthEnabled = false
|
|
|
|
tests := []struct {
|
|
name string
|
|
contentSha256 string
|
|
expectedError s3err.ErrorCode
|
|
shouldProcess bool
|
|
description string
|
|
}{
|
|
{
|
|
name: "RegularRequest",
|
|
contentSha256: "",
|
|
expectedError: s3err.ErrNone,
|
|
shouldProcess: false,
|
|
description: "Regular requests without chunked encoding should pass through unchanged",
|
|
},
|
|
{
|
|
name: "StreamingSignedWithoutIAM",
|
|
contentSha256: "STREAMING-AWS4-HMAC-SHA256-PAYLOAD",
|
|
expectedError: s3err.ErrAuthNotSetup,
|
|
shouldProcess: false,
|
|
description: "Streaming signed requests should fail when IAM is disabled",
|
|
},
|
|
{
|
|
name: "StreamingUnsignedWithoutIAM",
|
|
contentSha256: "STREAMING-UNSIGNED-PAYLOAD-TRAILER",
|
|
expectedError: s3err.ErrNone,
|
|
shouldProcess: true,
|
|
description: "Streaming unsigned requests should be processed even when IAM is disabled",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
body := strings.NewReader("test data")
|
|
req, _ := http.NewRequest("PUT", "/bucket/key", body)
|
|
|
|
if tt.contentSha256 != "" {
|
|
req.Header.Set("x-amz-content-sha256", tt.contentSha256)
|
|
}
|
|
|
|
dataReader, errCode := getRequestDataReader(s3a, req)
|
|
|
|
// Check error code
|
|
if errCode != tt.expectedError {
|
|
t.Errorf("Expected error code %v, got %v", tt.expectedError, errCode)
|
|
}
|
|
|
|
// For successful cases, check if processing occurred
|
|
if errCode == s3err.ErrNone {
|
|
if tt.shouldProcess {
|
|
// For chunked requests, the reader should be different from the original body
|
|
if dataReader == req.Body {
|
|
t.Error("Expected dataReader to be processed by newChunkedReader, but got raw request body")
|
|
}
|
|
} else {
|
|
// For regular requests, the reader should be the same as the original body
|
|
if dataReader != req.Body {
|
|
t.Error("Expected dataReader to be the same as request body for regular requests")
|
|
}
|
|
}
|
|
}
|
|
|
|
t.Logf("Test case: %s - %s", tt.name, tt.description)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetRequestDataReader_AuthTypeDetection(t *testing.T) {
|
|
// Create an S3ApiServer with IAM disabled
|
|
s3a := &S3ApiServer{
|
|
iam: NewIdentityAccessManagementWithStore(&S3ApiServerOption{}, nil, string(credential.StoreTypeMemory)),
|
|
}
|
|
s3a.iam.isAuthEnabled = false
|
|
|
|
// Test the specific case mentioned in the issue where chunked data
|
|
// with checksum headers would be stored incorrectly
|
|
t.Run("ChunkedDataWithChecksum", func(t *testing.T) {
|
|
// Simulate a request with chunked data and checksum trailer
|
|
body := strings.NewReader("test content")
|
|
req, _ := http.NewRequest("PUT", "/bucket/key", body)
|
|
req.Header.Set("x-amz-content-sha256", "STREAMING-UNSIGNED-PAYLOAD-TRAILER")
|
|
req.Header.Set("x-amz-trailer", "x-amz-checksum-crc32")
|
|
|
|
// Verify the auth type is detected correctly
|
|
authType := getRequestAuthType(req)
|
|
if authType != authTypeStreamingUnsigned {
|
|
t.Errorf("Expected authTypeStreamingUnsigned, got %v", authType)
|
|
}
|
|
|
|
// Verify the request is processed correctly
|
|
dataReader, errCode := getRequestDataReader(s3a, req)
|
|
if errCode != s3err.ErrNone {
|
|
t.Errorf("Expected no error, got %v", errCode)
|
|
}
|
|
|
|
// The dataReader should be processed by newChunkedReader
|
|
if dataReader == req.Body {
|
|
t.Error("Expected dataReader to be processed by newChunkedReader to handle chunked encoding")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestGetRequestDataReader_IAMEnabled(t *testing.T) {
|
|
// Create an S3ApiServer with IAM enabled
|
|
s3a := &S3ApiServer{
|
|
iam: NewIdentityAccessManagementWithStore(&S3ApiServerOption{}, nil, string(credential.StoreTypeMemory)),
|
|
}
|
|
s3a.iam.isAuthEnabled = true
|
|
|
|
t.Run("StreamingUnsignedWithIAMEnabled", func(t *testing.T) {
|
|
body := strings.NewReader("test data")
|
|
req, _ := http.NewRequest("PUT", "/bucket/key", body)
|
|
req.Header.Set("x-amz-content-sha256", "STREAMING-UNSIGNED-PAYLOAD-TRAILER")
|
|
|
|
dataReader, errCode := getRequestDataReader(s3a, req)
|
|
|
|
// Should succeed and be processed
|
|
if errCode != s3err.ErrNone {
|
|
t.Errorf("Expected no error, got %v", errCode)
|
|
}
|
|
|
|
// Should be processed by newChunkedReader
|
|
if dataReader == req.Body {
|
|
t.Error("Expected dataReader to be processed by newChunkedReader")
|
|
}
|
|
})
|
|
}
|
|
|
|
// Test helper to verify auth type detection works correctly
|
|
func TestAuthTypeDetection(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
headers map[string]string
|
|
expectedType authType
|
|
}{
|
|
{
|
|
name: "StreamingUnsigned",
|
|
headers: map[string]string{"x-amz-content-sha256": "STREAMING-UNSIGNED-PAYLOAD-TRAILER"},
|
|
expectedType: authTypeStreamingUnsigned,
|
|
},
|
|
{
|
|
name: "StreamingSigned",
|
|
headers: map[string]string{"x-amz-content-sha256": "STREAMING-AWS4-HMAC-SHA256-PAYLOAD"},
|
|
expectedType: authTypeStreamingSigned,
|
|
},
|
|
{
|
|
name: "Regular",
|
|
headers: map[string]string{},
|
|
expectedType: authTypeAnonymous,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
req, _ := http.NewRequest("PUT", "/bucket/key", strings.NewReader("test"))
|
|
for key, value := range tt.headers {
|
|
req.Header.Set(key, value)
|
|
}
|
|
|
|
authType := getRequestAuthType(req)
|
|
if authType != tt.expectedType {
|
|
t.Errorf("Expected auth type %v, got %v", tt.expectedType, authType)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func sha256Hex(t *testing.T, b []byte) string {
|
|
t.Helper()
|
|
sum := sha256.Sum256(b)
|
|
return hex.EncodeToString(sum[:])
|
|
}
|
|
|
|
func TestExpectedContentSha256(t *testing.T) {
|
|
good := sha256Hex(t, []byte("payload"))
|
|
|
|
tests := []struct {
|
|
name string
|
|
header string
|
|
wantValid bool
|
|
wantNil bool
|
|
}{
|
|
{"absent", "", true, true},
|
|
{"unsigned", unsignedPayload, true, true},
|
|
{"streaming signed", streamingContentSHA256, true, true},
|
|
{"streaming trailer", streamingContentSHA256Trailer, true, true},
|
|
{"streaming unsigned", streamingUnsignedPayload, true, true},
|
|
{"hex", good, true, false},
|
|
{"not hex", "nothex", false, true},
|
|
{"short hex", "deadbeef", false, true},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
r := httptest.NewRequest("PUT", "/b/o", nil)
|
|
if tt.header != "" {
|
|
r.Header.Set("X-Amz-Content-Sha256", tt.header)
|
|
}
|
|
got, valid := expectedContentSha256(r)
|
|
if valid != tt.wantValid {
|
|
t.Fatalf("valid=%v want %v", valid, tt.wantValid)
|
|
}
|
|
if (got == nil) != tt.wantNil {
|
|
t.Fatalf("expected nil=%v, got %x", tt.wantNil, got)
|
|
}
|
|
})
|
|
}
|
|
|
|
t.Run("base64 decodes to sha256", func(t *testing.T) {
|
|
sum := sha256.Sum256([]byte("payload"))
|
|
r := httptest.NewRequest("PUT", "/b/o", nil)
|
|
r.Header.Set("X-Amz-Content-Sha256", base64.StdEncoding.EncodeToString(sum[:]))
|
|
got, valid := expectedContentSha256(r)
|
|
if !valid || !bytes.Equal(got, sum[:]) {
|
|
t.Fatalf("valid=%v got=%x", valid, got)
|
|
}
|
|
})
|
|
}
|
|
|
|
func newVerifier(body string, expectedHex string) *contentSha256Verifier {
|
|
expected, _ := hex.DecodeString(expectedHex)
|
|
return &contentSha256Verifier{
|
|
reader: io.NopCloser(strings.NewReader(body)),
|
|
hasher: sha256.New(),
|
|
expected: expected,
|
|
}
|
|
}
|
|
|
|
func TestContentSha256VerifierMatch(t *testing.T) {
|
|
v := newVerifier("hello", sha256Hex(t, []byte("hello")))
|
|
got, err := io.ReadAll(v)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if string(got) != "hello" {
|
|
t.Fatalf("got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestContentSha256VerifierMismatch(t *testing.T) {
|
|
v := newVerifier("hello", sha256Hex(t, []byte("other")))
|
|
_, err := io.ReadAll(v)
|
|
if err == nil || !strings.Contains(err.Error(), "does not match") {
|
|
t.Fatalf("expected mismatch error, got %v", err)
|
|
}
|
|
if _, err := v.Read(make([]byte, 8)); err == nil {
|
|
t.Fatal("expected error to persist")
|
|
}
|
|
}
|
|
|
|
// mimeDetect performs a single Read and ignores its error; the verifier must
|
|
// defer the mismatch so a dropped (n>0, err) result still surfaces later.
|
|
func TestContentSha256VerifierSurvivesDroppedError(t *testing.T) {
|
|
v := newVerifier("hello", sha256Hex(t, []byte("other")))
|
|
buf := make([]byte, 512)
|
|
n, _ := v.Read(buf)
|
|
rest := io.MultiReader(bytes.NewReader(buf[:n]), v)
|
|
_, err := io.ReadAll(rest)
|
|
if err == nil || !strings.Contains(err.Error(), "does not match") {
|
|
t.Fatalf("expected deferred mismatch error, got %v", err)
|
|
}
|
|
}
|
|
|
|
// A zero-length body never reaches the verifier, so the declared hash is
|
|
// compared against the empty-payload digest up front.
|
|
func TestGetRequestDataReaderEmptyBodyHash(t *testing.T) {
|
|
s3a := &S3ApiServer{
|
|
iam: NewIdentityAccessManagementWithStore(&S3ApiServerOption{}, nil, string(credential.StoreTypeMemory)),
|
|
}
|
|
s3a.iam.isAuthEnabled = false
|
|
|
|
mismatch := httptest.NewRequest("PUT", "/b/dir/", http.NoBody)
|
|
mismatch.Header.Set("X-Amz-Content-Sha256", sha256Hex(t, []byte("other")))
|
|
if _, code := getRequestDataReader(s3a, mismatch); code != s3err.ErrContentSHA256Mismatch {
|
|
t.Fatalf("empty body with wrong hash: code=%v", code)
|
|
}
|
|
|
|
malformed := httptest.NewRequest("PUT", "/b/o", strings.NewReader("x"))
|
|
malformed.Header.Set("X-Amz-Content-Sha256", "nothex")
|
|
if _, code := getRequestDataReader(s3a, malformed); code != s3err.ErrInvalidArgument {
|
|
t.Fatalf("malformed sha256 header: code=%v", code)
|
|
}
|
|
|
|
emptySum := sha256.Sum256(nil)
|
|
match := httptest.NewRequest("PUT", "/b/dir/", http.NoBody)
|
|
match.Header.Set("X-Amz-Content-Sha256", hex.EncodeToString(emptySum[:]))
|
|
if _, code := getRequestDataReader(s3a, match); code != s3err.ErrNone {
|
|
t.Fatalf("empty body with empty hash: code=%v", code)
|
|
}
|
|
}
|