diff --git a/weed/s3api/s3_sse_kms.go b/weed/s3api/s3_sse_kms.go index a068a40ecc..bf5f40e4b6 100644 --- a/weed/s3api/s3_sse_kms.go +++ b/weed/s3api/s3_sse_kms.go @@ -789,7 +789,7 @@ func ParseSSEKMSCopyHeaders(r *http.Request) (destKeyID string, encryptionContex dstBucket, dstObject := s3_constants.GetBucketAndObject(r) encryptionContext, err = buildRequestEncryptionContext(dstBucket, dstObject, bucketKeyEnabled, r.Header.Get(s3_constants.AmzServerSideEncryptionContext)) if err != nil { - return "", nil, false, err + return "", nil, false, &CopyValidationError{Code: s3err.ErrInvalidRequest, Message: err.Error()} } return destKeyID, encryptionContext, bucketKeyEnabled, nil diff --git a/weed/s3api/s3api_object_handlers_copy_kms_context_test.go b/weed/s3api/s3api_object_handlers_copy_kms_context_test.go index 8c148bf7e4..85899404ea 100644 --- a/weed/s3api/s3api_object_handlers_copy_kms_context_test.go +++ b/weed/s3api/s3api_object_handlers_copy_kms_context_test.go @@ -5,6 +5,7 @@ import ( "fmt" "net/http" "net/http/httptest" + "net/url" "testing" "github.com/gorilla/mux" @@ -32,9 +33,9 @@ func newCopyKMSContextTestServer(t *testing.T, source *filer_pb.Entry, bucketEnc return s3a, filer } -func copyToDestination(s3a *S3ApiServer, headers map[string]string) *httptest.ResponseRecorder { - req := httptest.NewRequest(http.MethodPut, "http://s3/b/destination", nil) - req = mux.SetURLVars(req, map[string]string{"bucket": "b", "object": "destination"}) +func copyToDestination(s3a *S3ApiServer, destination string, headers map[string]string) *httptest.ResponseRecorder { + req := httptest.NewRequest(http.MethodPut, "http://s3/b/"+url.PathEscape(destination), nil) + req = mux.SetURLVars(req, map[string]string{"bucket": "b", "object": destination}) req.Header.Set("X-Amz-Copy-Source", "/b/source") for k, v := range headers { req.Header.Set(k, v) @@ -83,7 +84,7 @@ func TestCopyObjectSSEKMSContextKeepsTheDestinationARN(t *testing.T) { } { t.Run(name, func(t *testing.T) { s3a, filer := newCopyKMSContextTestServer(t, tc.source, tc.bucketEncryption) - rr := copyToDestination(s3a, tc.headers) + rr := copyToDestination(s3a, "destination", tc.headers) require.Equal(t, http.StatusOK, rr.Code, rr.Body.String()) filer.mu.Lock() @@ -101,24 +102,28 @@ func TestCopyObjectSSEKMSContextKeepsTheDestinationARN(t *testing.T) { func TestCopyObjectRejectsAContextTheKeyServiceRefuses(t *testing.T) { kmsKey := SetupTestKMS(t) t.Cleanup(kmsKey.Cleanup) - for name, contextHeader := range map[string]string{ - "not base64": "not base64!", - "not JSON": base64.StdEncoding.EncodeToString([]byte("team=a")), - "control character": base64.StdEncoding.EncodeToString([]byte(`{"team":"a\tb"}`)), + for name, tc := range map[string]struct { + destination string + contextHeader string + }{ + "not base64": {"destination", "not base64!"}, + "not JSON": {"destination", base64.StdEncoding.EncodeToString([]byte("team=a"))}, + "control character": {"destination", base64.StdEncoding.EncodeToString([]byte(`{"team":"a\tb"}`))}, + "control character in the destination key": {"dst\tkey", base64.StdEncoding.EncodeToString([]byte(`{"team":"a"}`))}, } { t.Run(name, func(t *testing.T) { s3a, filer := newCopyKMSContextTestServer(t, &filer_pb.Entry{Name: "source", Content: []byte("data"), Attributes: &filer_pb.FuseAttributes{FileMode: defaultFileMode, FileSize: 4, Mime: "text/plain"}}, nil) - rr := copyToDestination(s3a, map[string]string{ + rr := copyToDestination(s3a, tc.destination, map[string]string{ s3_constants.AmzServerSideEncryption: "aws:kms", s3_constants.AmzServerSideEncryptionAwsKmsKeyId: kmsKey.KeyID, - s3_constants.AmzServerSideEncryptionContext: contextHeader, + s3_constants.AmzServerSideEncryptionContext: tc.contextHeader, }) require.Equal(t, http.StatusBadRequest, rr.Code, rr.Body.String()) require.Contains(t, rr.Body.String(), "InvalidRequest") filer.mu.Lock() defer filer.mu.Unlock() - require.NotContains(t, filer.entries, "/buckets/b/destination") + require.NotContains(t, filer.entries, "/buckets/b/"+tc.destination) }) } } diff --git a/weed/s3api/s3api_object_handlers_copy_unified.go b/weed/s3api/s3api_object_handlers_copy_unified.go index 3cfa54257f..6bf9d36460 100644 --- a/weed/s3api/s3api_object_handlers_copy_unified.go +++ b/weed/s3api/s3api_object_handlers_copy_unified.go @@ -99,6 +99,11 @@ func (s3a *S3ApiServer) mapCopyErrorToS3Error(err error) s3err.ErrorCode { return ssecErr } + var validationErr *CopyValidationError + if errors.As(err, &validationErr) { + return validationErr.Code + } + // Default to internal error for unknown errors return s3err.ErrInternalError }