Files
seaweedfs/weed/s3api/s3api_path_validation.go
T
Chris LuandClaude Opus 5.5 b1f86ddfa2 s3: support PutBucketDefaultStorageClass and GetBucketDefaultStorageClass (#13282)
* s3: add PutBucketDefaultStorageClass and GetBucketDefaultStorageClass

The setting is kept in the bucket entry's extended attributes next to
the other bucket configuration, so every gateway sees it through the
bucket config cache. Only STANDARD and INTELLIGENT_TIERING are accepted,
matching the AWS model; STANDARD clears the stored value.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3: give new objects the bucket default storage class

PutObject, POST uploads, CreateMultipartUpload and CopyObject store the
bucket default when the request names no storage class. The request
headers are left alone, so bucket-policy storage-class conditions and
the self-copy change check still see only what the client sent. Upload
parts are not tagged; a completed upload takes the class recorded when
it was initiated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3: test the bucket default storage class APIs and write default

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3: fail a write whose bucket default storage class cannot be read

A failed bucket config lookup used to fall back to STANDARD, so a write
could store a different class than the bucket default asks for. The
write now returns the lookup error instead. A request that names its
own class still needs no lookup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3: give extracted ZIP directory members the bucket default storage class

Directory members are created through mkdir rather than putToFiler, so
they stored no class while the files next to them did.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3: reject a default storage class body with trailing data

The decoder stopped after the first element, so a valid configuration
followed by another element or stray bytes was accepted. The body is
now read with a size bound and only whitespace, comments or processing
instructions may follow the configuration.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* go.mod: bump aws-sdk-go-v2/service/s3 to v1.117.1

The release adds the PutBucketDefaultStorageClass and
GetBucketDefaultStorageClass client operations.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3: test the bucket default storage class through the AWS SDK

Covers get/set, an unsupported class, the setting stored in the bucket
entry and read back by a fresh bucket config, PUT, explicit classes,
multipart initiation, copy, existing objects, and per-API grants.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3: let each default storage class subtest set up its own state

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3: assert the explicit STANDARD class exactly in the default class test

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3: note that the bucket default storage class is a label only

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-02 04:05:48 -08:00

128 lines
4.3 KiB
Go

package s3api
import (
"net/http"
"net/url"
"strings"
"github.com/gorilla/mux"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
)
func hasPathSegmentQuery(rawQuery string) bool {
if strings.Contains(rawQuery, "versionId") || strings.Contains(rawQuery, "uploadId") {
return true
}
if !strings.Contains(rawQuery, "%") {
return false
}
for rawQuery != "" {
field := rawQuery
if i := strings.IndexByte(rawQuery, '&'); i >= 0 {
field, rawQuery = rawQuery[:i], rawQuery[i+1:]
} else {
rawQuery = ""
}
if i := strings.IndexByte(field, '='); i >= 0 {
field = field[:i]
}
if !strings.Contains(field, "%") {
continue
}
key, err := url.QueryUnescape(field)
if err == nil && (key == "versionId" || key == "uploadId") {
return true
}
}
return false
}
// operationSubresources are the query keys that select which operation a request
// is, so at most one may appear. The router matches them in registration order
// and the IAM action resolver in its own, so a request carrying two is
// authorized as one operation and served as another: `PUT /bucket?policy&tagging`
// authorizes as PutBucketTagging and runs PutBucketPolicy. Keys left out here
// (versionId, partNumber, prefix, ...) modify an operation instead of selecting
// one and may accompany any of these.
var operationSubresources = map[string]bool{
"accelerate": true, "acl": true, "analytics": true, "attributes": true,
"cors": true, "default-storage-class": true, "delete": true, "encryption": true, "intelligent-tiering": true,
"inventory": true, "legal-hold": true, "lifecycle": true, "list-type": true,
"location": true, "logging": true, "metrics": true, "notification": true,
"object-lock": true, "ownershipControls": true, "policy": true, "policyStatus": true,
"publicAccessBlock": true, "renameObject": true, "replication": true,
"requestPayment": true, "retention": true, "seaweedfs-quota": true,
"tagging": true, "uploadId": true,
"uploads": true, "versioning": true, "versions": true, "website": true,
}
func hasAmbiguousSubresource(query url.Values) bool {
seen := 0
for key := range query {
// bucketQueryActions keys select an operation by definition, so they
// count even if operationSubresources was not updated for them.
if _, ok := bucketQueryActions[key]; !ok && !operationSubresources[key] {
continue
}
if seen++; seen > 1 {
return true
}
}
return false
}
func hasInvalidPathSegment(values []string) bool {
for _, value := range values {
if value != "" && !s3_constants.IsValidPathSegment(value) {
return true
}
}
return false
}
// validateRequestPath rejects requests whose captured {bucket}/{object} mux
// vars would normalize to a parent-directory traversal once joined into a
// filer path. The router runs with mux.NewRouter().SkipClean(true), so
// segments like `..` survive routing; the filer's util.JoinPath later collapses
// them via filepath.Join. Without this guard, `GET /bucket-A/../evil-bucket/k`
// matches as bucket=bucket-A, object=../evil-bucket/k, the filer resolves the
// read against evil-bucket, while IAM authorizes against bucket-A.
func validateRequestPath(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
// When a var is in the matched route it must be non-empty: an empty
// bucket would let downstream path.Join collapse it and let the object
// key pick the bucket.
if bucket, ok := vars["bucket"]; ok {
if bucket == "" || !s3_constants.IsValidBucketName(bucket) {
s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
return
}
}
if object, ok := vars["object"]; ok {
if object == "" || !s3_constants.IsValidObjectKey(object) {
s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
return
}
}
if r.URL.RawQuery != "" {
query := r.URL.Query()
if hasAmbiguousSubresource(query) {
s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
return
}
// versionId and uploadId are later used as filer entry names, and
// the encoded spelling of either still names one.
if hasPathSegmentQuery(r.URL.RawQuery) {
if hasInvalidPathSegment(query["versionId"]) || hasInvalidPathSegment(query["uploadId"]) {
s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
return
}
}
}
next.ServeHTTP(w, r)
})
}