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>
112 lines
3.4 KiB
Go
112 lines
3.4 KiB
Go
package s3api
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"errors"
|
|
"hash"
|
|
"io"
|
|
"net/http"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
|
|
)
|
|
|
|
// getRequestDataReader returns the appropriate reader for the request body.
|
|
// When IAM is disabled, it still processes chunked transfer encoding for
|
|
// authTypeStreamingUnsigned to strip checksum headers and extract the actual data.
|
|
// This fixes issues where chunked data with checksums would be stored incorrectly
|
|
// when IAM is not enabled.
|
|
func getRequestDataReader(s3a *S3ApiServer, r *http.Request) (io.ReadCloser, s3err.ErrorCode) {
|
|
var s3ErrCode s3err.ErrorCode
|
|
dataReader := r.Body
|
|
rAuthType := getRequestAuthType(r)
|
|
if s3a.iam.isEnabled() {
|
|
if rAuthType == authTypeStreamingSigned || rAuthType == authTypeStreamingUnsigned {
|
|
dataReader, s3ErrCode = s3a.iam.newChunkedReader(r)
|
|
}
|
|
} else {
|
|
switch rAuthType {
|
|
case authTypeStreamingSigned:
|
|
s3ErrCode = s3err.ErrAuthNotSetup
|
|
case authTypeStreamingUnsigned:
|
|
// Even when IAM is disabled, we still need to handle chunked transfer encoding
|
|
// to strip checksum headers and process the data correctly
|
|
dataReader, s3ErrCode = s3a.iam.newChunkedReader(r)
|
|
}
|
|
}
|
|
if s3ErrCode != s3err.ErrNone {
|
|
return nil, s3ErrCode
|
|
}
|
|
|
|
expected, valid := expectedContentSha256(r)
|
|
if !valid {
|
|
return nil, s3err.ErrInvalidArgument
|
|
}
|
|
if expected != nil && dataReader != nil {
|
|
if r.ContentLength == 0 {
|
|
// Handlers may never read an empty body, so check it now.
|
|
emptySum := sha256.Sum256(nil)
|
|
if !bytes.Equal(expected, emptySum[:]) {
|
|
return nil, s3err.ErrContentSHA256Mismatch
|
|
}
|
|
return dataReader, s3err.ErrNone
|
|
}
|
|
dataReader = &contentSha256Verifier{reader: dataReader, hasher: sha256.New(), expected: expected}
|
|
}
|
|
return dataReader, s3err.ErrNone
|
|
}
|
|
|
|
// expectedContentSha256 decodes the x-amz-content-sha256 header when it declares
|
|
// a payload hash — hex or base64 per SigV4 — so the body can be checked as it
|
|
// streams. Sentinel values (streaming, unsigned) carry no hash and are skipped;
|
|
// anything that decodes to neither returns valid=false.
|
|
func expectedContentSha256(r *http.Request) (expected []byte, valid bool) {
|
|
v := r.Header.Get("X-Amz-Content-Sha256")
|
|
if v == "" || v == unsignedPayload || v == streamingContentSHA256 ||
|
|
v == streamingContentSHA256Trailer || v == streamingUnsignedPayload {
|
|
return nil, true
|
|
}
|
|
if decoded, err := hex.DecodeString(v); err == nil && len(decoded) == sha256.Size {
|
|
return decoded, true
|
|
}
|
|
if decoded, err := base64.StdEncoding.DecodeString(v); err == nil && len(decoded) == sha256.Size {
|
|
return decoded, true
|
|
}
|
|
return nil, false
|
|
}
|
|
|
|
// contentSha256Verifier checks the decoded x-amz-content-sha256 once the stream
|
|
// is exhausted; handlers that never consume the body are unaffected.
|
|
type contentSha256Verifier struct {
|
|
reader io.ReadCloser
|
|
hasher hash.Hash
|
|
expected []byte
|
|
done bool
|
|
pending error
|
|
}
|
|
|
|
func (v *contentSha256Verifier) Read(p []byte) (int, error) {
|
|
if v.pending != nil {
|
|
return 0, v.pending
|
|
}
|
|
n, err := v.reader.Read(p)
|
|
v.hasher.Write(p[:n])
|
|
if err == io.EOF && !v.done {
|
|
v.done = true
|
|
if !bytes.Equal(v.hasher.Sum(nil), v.expected) {
|
|
v.pending = errors.New(s3err.ErrMsgContentSha256Mismatch)
|
|
err = nil
|
|
}
|
|
}
|
|
if n == 0 && v.pending != nil {
|
|
return 0, v.pending
|
|
}
|
|
return n, err
|
|
}
|
|
|
|
func (v *contentSha256Verifier) Close() error {
|
|
return v.reader.Close()
|
|
}
|