mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-11-19 02:15:02 +01:00
* s3api: reject unknown elements in PutBucketNotificationConfiguration xml.Unmarshal skips children that match no field, so a configuration whose only child was an unmapped element parsed as empty and returned success while nothing was stored. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test/s3/tigris_compat: fail test-with-server when the server does not stop A passing suite returned success even when stop-server gave up with the server still running. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
280 lines
10 KiB
Go
280 lines
10 KiB
Go
package s3api
|
|
|
|
import (
|
|
"encoding/xml"
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/policy_engine"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
|
|
)
|
|
|
|
// putBucketMiscConfigMaxBodyBytes caps the request body of the configuration
|
|
// PUTs in this file; a valid payload is a few hundred bytes.
|
|
const putBucketMiscConfigMaxBodyBytes = 64 * 1024
|
|
|
|
type policyStatusResponse struct {
|
|
XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ PolicyStatus"`
|
|
IsPublic bool `xml:"IsPublic"`
|
|
}
|
|
|
|
type accelerateConfigurationResponse struct {
|
|
XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ AccelerateConfiguration"`
|
|
Status string `xml:"Status"`
|
|
}
|
|
|
|
type bucketLoggingStatusResponse struct {
|
|
XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ BucketLoggingStatus"`
|
|
}
|
|
|
|
type notificationConfigurationResponse struct {
|
|
XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ NotificationConfiguration"`
|
|
}
|
|
|
|
type accelerateConfigurationRequest struct {
|
|
XMLName xml.Name `xml:"AccelerateConfiguration"`
|
|
Status string `xml:"Status"`
|
|
}
|
|
|
|
type notificationConfigurationRequest struct {
|
|
XMLName xml.Name `xml:"NotificationConfiguration"`
|
|
Topics []struct{} `xml:"TopicConfiguration"`
|
|
Queues []struct{} `xml:"QueueConfiguration"`
|
|
Functions []struct{} `xml:"CloudFunctionConfiguration"`
|
|
EventBridge []struct{} `xml:"EventBridgeConfiguration"`
|
|
Unknown []struct{} `xml:",any"`
|
|
}
|
|
|
|
// GetBucketPolicyStatusHandler reports whether the bucket policy grants public access.
|
|
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketPolicyStatus.html
|
|
func (s3a *S3ApiServer) GetBucketPolicyStatusHandler(w http.ResponseWriter, r *http.Request) {
|
|
bucket, _ := s3_constants.GetBucketAndObject(r)
|
|
|
|
if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
|
|
s3err.WriteErrorResponse(w, r, err)
|
|
return
|
|
}
|
|
|
|
policyDoc, err := s3a.getBucketPolicy(bucket)
|
|
if err != nil {
|
|
if errors.Is(err, ErrPolicyNotFound) {
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchBucketPolicy)
|
|
} else if errors.Is(err, ErrBucketNotFound) {
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchBucket)
|
|
} else {
|
|
glog.Errorf("GetBucketPolicyStatusHandler load policy %s: %v", bucket, err)
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
|
|
}
|
|
return
|
|
}
|
|
|
|
writeSuccessResponseXML(w, r, policyStatusResponse{IsPublic: isPolicyPublic(policyDoc)})
|
|
}
|
|
|
|
// isPolicyPublic returns true if any Allow statement grants access to "*" without restricting conditions.
|
|
func isPolicyPublic(doc *policy_engine.PolicyDocument) bool {
|
|
if doc == nil {
|
|
return false
|
|
}
|
|
for _, st := range doc.Statement {
|
|
if st.Effect != policy_engine.PolicyEffectAllow {
|
|
continue
|
|
}
|
|
if len(st.Condition) > 0 {
|
|
continue
|
|
}
|
|
for _, p := range st.Principal.Strings() {
|
|
if p == "*" {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// PutBucketRequestPaymentHandler accepts only Payer=BucketOwner; Requester is rejected.
|
|
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutBucketRequestPayment.html
|
|
func (s3a *S3ApiServer) PutBucketRequestPaymentHandler(w http.ResponseWriter, r *http.Request) {
|
|
bucket, _ := s3_constants.GetBucketAndObject(r)
|
|
|
|
if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
|
|
s3err.WriteErrorResponse(w, r, err)
|
|
return
|
|
}
|
|
|
|
defer r.Body.Close()
|
|
body, err := readRequestBody(r, putBucketMiscConfigMaxBodyBytes)
|
|
if err != nil {
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrMalformedXML)
|
|
return
|
|
}
|
|
|
|
var cfg RequestPaymentConfiguration
|
|
if err := xml.Unmarshal(body, &cfg); err != nil {
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrMalformedXML)
|
|
return
|
|
}
|
|
|
|
if cfg.Payer != "BucketOwner" {
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrMalformedXML)
|
|
return
|
|
}
|
|
|
|
writeSuccessResponseEmpty(w, r)
|
|
}
|
|
|
|
// GetBucketAccelerateConfigurationHandler returns a static Suspended status.
|
|
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketAccelerateConfiguration.html
|
|
func (s3a *S3ApiServer) GetBucketAccelerateConfigurationHandler(w http.ResponseWriter, r *http.Request) {
|
|
bucket, _ := s3_constants.GetBucketAndObject(r)
|
|
if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
|
|
s3err.WriteErrorResponse(w, r, err)
|
|
return
|
|
}
|
|
writeSuccessResponseXML(w, r, accelerateConfigurationResponse{Status: "Suspended"})
|
|
}
|
|
|
|
// PutBucketAccelerateConfigurationHandler accepts Status=Suspended, the state
|
|
// GetBucketAccelerateConfigurationHandler always reports; Enabled is not implemented.
|
|
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutBucketAccelerateConfiguration.html
|
|
func (s3a *S3ApiServer) PutBucketAccelerateConfigurationHandler(w http.ResponseWriter, r *http.Request) {
|
|
bucket, _ := s3_constants.GetBucketAndObject(r)
|
|
if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
|
|
s3err.WriteErrorResponse(w, r, err)
|
|
return
|
|
}
|
|
|
|
defer r.Body.Close()
|
|
body, err := readRequestBody(r, putBucketMiscConfigMaxBodyBytes)
|
|
if err != nil {
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrMalformedXML)
|
|
return
|
|
}
|
|
var cfg accelerateConfigurationRequest
|
|
if err := xml.Unmarshal(body, &cfg); err != nil {
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrMalformedXML)
|
|
return
|
|
}
|
|
|
|
switch cfg.Status {
|
|
case "Suspended":
|
|
writeSuccessResponseEmpty(w, r)
|
|
case "Enabled":
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrNotImplemented)
|
|
default:
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrMalformedXML)
|
|
}
|
|
}
|
|
|
|
// PutBucketNotificationConfigurationHandler accepts an empty configuration, the
|
|
// one GetBucketNotificationConfigurationHandler always reports; any destination
|
|
// is not implemented.
|
|
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutBucketNotificationConfiguration.html
|
|
func (s3a *S3ApiServer) PutBucketNotificationConfigurationHandler(w http.ResponseWriter, r *http.Request) {
|
|
bucket, _ := s3_constants.GetBucketAndObject(r)
|
|
if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
|
|
s3err.WriteErrorResponse(w, r, err)
|
|
return
|
|
}
|
|
|
|
defer r.Body.Close()
|
|
body, err := readRequestBody(r, putBucketMiscConfigMaxBodyBytes)
|
|
if err != nil {
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrMalformedXML)
|
|
return
|
|
}
|
|
var cfg notificationConfigurationRequest
|
|
if err := xml.Unmarshal(body, &cfg); err != nil || len(cfg.Unknown) > 0 {
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrMalformedXML)
|
|
return
|
|
}
|
|
if len(cfg.Topics)+len(cfg.Queues)+len(cfg.Functions)+len(cfg.EventBridge) > 0 {
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrNotImplemented)
|
|
return
|
|
}
|
|
writeSuccessResponseEmpty(w, r)
|
|
}
|
|
|
|
// GetBucketLoggingHandler returns an empty BucketLoggingStatus (logging disabled).
|
|
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketLogging.html
|
|
func (s3a *S3ApiServer) GetBucketLoggingHandler(w http.ResponseWriter, r *http.Request) {
|
|
bucket, _ := s3_constants.GetBucketAndObject(r)
|
|
if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
|
|
s3err.WriteErrorResponse(w, r, err)
|
|
return
|
|
}
|
|
writeSuccessResponseXML(w, r, bucketLoggingStatusResponse{})
|
|
}
|
|
|
|
// GetBucketNotificationConfigurationHandler returns an empty configuration; SeaweedFS
|
|
// has no bucket event notifications, and AWS answers an unconfigured bucket the same way.
|
|
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketNotificationConfiguration.html
|
|
func (s3a *S3ApiServer) GetBucketNotificationConfigurationHandler(w http.ResponseWriter, r *http.Request) {
|
|
bucket, _ := s3_constants.GetBucketAndObject(r)
|
|
if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
|
|
s3err.WriteErrorResponse(w, r, err)
|
|
return
|
|
}
|
|
writeSuccessResponseXML(w, r, notificationConfigurationResponse{})
|
|
}
|
|
|
|
// GetBucketReplicationHandler reports that no replication configuration exists.
|
|
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketReplication.html
|
|
func (s3a *S3ApiServer) GetBucketReplicationHandler(w http.ResponseWriter, r *http.Request) {
|
|
bucket, _ := s3_constants.GetBucketAndObject(r)
|
|
if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
|
|
s3err.WriteErrorResponse(w, r, err)
|
|
return
|
|
}
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrReplicationConfigurationNotFound)
|
|
}
|
|
|
|
// GetBucketWebsiteHandler reports that no website configuration exists.
|
|
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketWebsite.html
|
|
func (s3a *S3ApiServer) GetBucketWebsiteHandler(w http.ResponseWriter, r *http.Request) {
|
|
bucket, _ := s3_constants.GetBucketAndObject(r)
|
|
if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
|
|
s3err.WriteErrorResponse(w, r, err)
|
|
return
|
|
}
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchWebsiteConfiguration)
|
|
}
|
|
|
|
// listObjectsQueryParams are the query keys a ListObjects/ListObjectsV2 request may
|
|
// carry. x-id is stamped by the AWS SDKs and carries no meaning for the server; the
|
|
// rest of the non-listing keys are what a presigned URL signs into the query string.
|
|
var listObjectsQueryParams = map[string]bool{
|
|
"prefix": true, "delimiter": true, "marker": true, "max-keys": true,
|
|
"encoding-type": true, "list-type": true, "continuation-token": true,
|
|
"start-after": true, "fetch-owner": true, "expected-bucket-owner": true,
|
|
"allow-unordered": true, "x-id": true,
|
|
// SigV2 presigned URLs.
|
|
"AWSAccessKeyId": true, "Signature": true, "Expires": true,
|
|
}
|
|
|
|
// unroutedBucketSubresource names a query key on a bucket GET that no route claimed.
|
|
// Everything SeaweedFS implements is matched by its own route before the ListObjects
|
|
// catch-all, so anything left is a subresource it does not implement - and answering
|
|
// it with a bucket listing is worse than saying so.
|
|
func unroutedBucketSubresource(r *http.Request) (string, bool) {
|
|
return unroutedSubresource(r, listObjectsQueryParams)
|
|
}
|
|
|
|
// unroutedSubresource names a query key that no route claimed, outside the
|
|
// request's own parameters, SigV2 presign keys, and SigV4 presign keys
|
|
// (all "X-Amz-" prefixed).
|
|
func unroutedSubresource(r *http.Request, allowed map[string]bool) (string, bool) {
|
|
for key := range r.URL.Query() {
|
|
switch {
|
|
case allowed[key], strings.HasPrefix(key, "X-Amz-"):
|
|
case key == "x-id", key == "AWSAccessKeyId", key == "Signature", key == "Expires":
|
|
default:
|
|
return key, true
|
|
}
|
|
}
|
|
return "", false
|
|
}
|