Files
seaweedfs/weed/s3api/s3api_public_access_block.go
T
Chris LuandClaude Opus 5.5 b0c6297041 s3api: per-bucket Block Public Access (BlockPublicAcls, IgnorePublicAcls) (#12381)
* s3api: store and serve a bucket's PublicAccessBlock configuration

PutPublicAccessBlock, GetPublicAccessBlock and DeletePublicAccessBlock
answered NotImplemented. Store the configuration in the bucket entry's
extended attributes next to the ownership controls, carry it in the
cached BucketConfig, and answer Get on an unset bucket with
NoSuchPublicAccessBlockConfiguration like S3 does.

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

* s3api: refuse public ACLs when a bucket blocks them

With BlockPublicAcls set, a PutObject, CopyObject, CreateMultipartUpload,
POST Object, PutObjectAcl or PutBucketAcl whose canned ACL or grants name
the AllUsers or AuthenticatedUsers group is refused with AccessDenied.
Private and account grants keep working, so clients that send
x-amz-acl: private on every upload are unaffected.

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

* s3api: ignore public ACL grants for anonymous reads under IgnorePublicAcls

A bucket with IgnorePublicAcls no longer lets an AllUsers grant on the
bucket or on an object answer an anonymous read or listing. Bucket
policies and identity permissions are unaffected.

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

* s3api: reject PublicAccessBlock bodies with unknown elements

A misspelled element such as BlockPublicAcl was ignored, so the request
succeeded and stored the flag as false. Answer MalformedXML instead, and
store the configuration without the request's namespace.

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

* s3api: fail closed when a bucket's public access block cannot be read

A stored PublicAccessBlock that does not parse now blocks and ignores
public ACLs instead of acting as if none were set. PutBucketAcl and POST
Object return the bucket lookup error rather than skipping the
BlockPublicAcls check when the bucket config cannot be loaded.

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

* s3api: test BlockPublicAcls on copy, multipart initiate and a PUT-then-GET

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

* s3api: keep stored public access block bytes on unrelated bucket updates

An unreadable stored configuration was replaced with the in-memory
all-true fallback whenever another bucket setting was updated, losing
the original value. Write the key only when the update changes the
public access block.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 14:52:20 -08:00

68 lines
2.2 KiB
Go

package s3api
import (
"encoding/xml"
"fmt"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
)
const maxPublicAccessBlockConfigurationSize = 4 << 10
// PublicAccessBlockConfiguration is a bucket's S3 Block Public Access setting.
type PublicAccessBlockConfiguration struct {
XMLNS string `xml:"xmlns,attr,omitempty"`
XMLName xml.Name `xml:"PublicAccessBlockConfiguration"`
BlockPublicAcls bool `xml:"BlockPublicAcls"`
IgnorePublicAcls bool `xml:"IgnorePublicAcls"`
BlockPublicPolicy bool `xml:"BlockPublicPolicy"`
RestrictPublicBuckets bool `xml:"RestrictPublicBuckets"`
Unknown []xml.Name `xml:",any"`
}
func parsePublicAccessBlock(data []byte) (*PublicAccessBlockConfiguration, error) {
var config PublicAccessBlockConfiguration
if err := xml.Unmarshal(data, &config); err != nil {
return nil, err
}
if len(config.Unknown) > 0 {
return nil, fmt.Errorf("unknown element %s", config.Unknown[0].Local)
}
config.XMLNS, config.XMLName = "", xml.Name{}
return &config, nil
}
func (c *PublicAccessBlockConfiguration) blocksPublicAcls() bool {
return c != nil && c.BlockPublicAcls
}
func (c *PublicAccessBlockConfiguration) ignoresPublicAcls() bool {
return c != nil && c.IgnorePublicAcls
}
// hasPublicGrant reports whether any grant names the AllUsers or
// AuthenticatedUsers group, which is what S3 treats as a public ACL.
func hasPublicGrant(grants []*s3.Grant) bool {
for _, grant := range grants {
if grant == nil || grant.Grantee == nil || grant.Grantee.URI == nil {
continue
}
switch *grant.Grantee.URI {
case s3_constants.GranteeGroupAllUsers, s3_constants.GranteeGroupAuthenticatedUsers:
return true
}
}
return false
}
// rejectPublicGrants denies grants that would make an object or bucket
// public when the bucket blocks public ACLs.
func rejectPublicGrants(config *BucketConfig, grants []*s3.Grant) s3err.ErrorCode {
if config != nil && config.PublicAccessBlock.blocksPublicAcls() && hasPublicGrant(grants) {
return s3err.ErrAccessDenied
}
return s3err.ErrNone
}