Files
seaweedfs/weed/s3api/s3api_public_access_block.go
T
Chris LuandClaude Opus 5.5 5afeb20ad6 s3: enforce BlockPublicPolicy and RestrictPublicBuckets for bucket policies (#12882)
* s3: refuse a public bucket policy when the bucket blocks public policies

PutBucketPolicy stored and enforced a policy granting access to every
principal even with BlockPublicPolicy set. Check the stored public access
block on the bucket entry that the policy write reads, and answer
AccessDenied for a public policy. A policy counts as public when an Allow
names a wildcard principal (or uses NotPrincipal) and no fixed-value
restricting condition such as aws:SourceIp or aws:SourceVpce narrows it.

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

* s3: keep a public bucket policy from granting anonymous access under RestrictPublicBuckets

With RestrictPublicBuckets set, an anonymous request was still allowed by
a public bucket policy. The bucket policy engine now treats such an Allow
as granting nothing when the bucket restricts public buckets and its policy
is public, so the request falls through to the remaining checks instead of
reading as an explicit deny. A bucket whose configuration cannot be read is
treated as restricted.

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

* s3: write a bucket policy only over the extended attributes it checked

setBucketPolicy rewrote the whole bucket entry it had read, so a public
access block set between the BlockPublicPolicy check and the write was
erased and the public policy stored anyway. Send the read extended
attributes, including an absent public access block, as the update's
expected values, and redo the read and check when they changed.

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

* s3: count more policy forms as public

An empty principal list and a principal holding a policy variable can both
match anyone, so they are public. An aws:SourceIp range wider than /8
(IPv4) or /32 (IPv6) no longer restricts a grant. An ARN condition value
with a fixed account, such as an access point ARN with a wildcard name,
does restrict it.

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

* s3: deny anonymous callers outright on a restricted bucket with a public policy

RestrictPublicBuckets limits a bucket with a public policy to callers of
the owning account, so dropping only the policy's own grant left public
ACLs able to serve anonymous reads. While the policy is public, the policy
engine now answers an anonymous request with a deny. It checks before and
after evaluating, so a policy replaced in between cannot let an anonymous
grant through. A bucket without a policy, or with a non-public one, is
unaffected.

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

* s3: count an empty principal string as public

A principal of "" compiles to no principal check, so it matches anyone,
the same as an empty list.

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

* s3: write a bucket policy only over the bucket entry it checked

The policy write replaced the whole bucket entry and asserted only the
public access block key, so a bucket setting first written between the
lookup and the write (versioning, for one) was dropped. Pin the write to
the entry it read; any concurrent change fails it and the lookup retries.

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

* s3: judge an IPv4-mapped aws:SourceIp range by its IPv4 prefix

::ffff:0.0.0.0/96 parses as a 128-bit network but matches every IPv4
caller, so it passed the /32 IPv6 bound and a public policy slipped past
BlockPublicPolicy and RestrictPublicBuckets. Apply the IPv4 /8 bound to
mapped ranges.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 02:23:12 -08:00

217 lines
6.7 KiB
Go

package s3api
import (
"encoding/xml"
"errors"
"fmt"
"net"
"strings"
"github.com/aws/aws-sdk-go/service/s3"
"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"
)
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
}
// storedPublicAccessBlock reads a bucket entry's public access block. An
// unreadable one blocks all public access.
func storedPublicAccessBlock(bucket string, extended map[string][]byte) *PublicAccessBlockConfiguration {
data, exists := extended[s3_constants.ExtPublicAccessBlockKey]
if !exists || len(data) == 0 {
return nil
}
pab, err := parsePublicAccessBlock(data)
if err != nil {
glog.Errorf("bucket %s public access block, blocking all public access: %v", bucket, err)
return &PublicAccessBlockConfiguration{BlockPublicAcls: true, IgnorePublicAcls: true, BlockPublicPolicy: true, RestrictPublicBuckets: true}
}
return pab
}
func (c *PublicAccessBlockConfiguration) blocksPublicAcls() bool {
return c != nil && c.BlockPublicAcls
}
func (c *PublicAccessBlockConfiguration) ignoresPublicAcls() bool {
return c != nil && c.IgnorePublicAcls
}
func (c *PublicAccessBlockConfiguration) blocksPublicPolicy() bool {
return c != nil && c.BlockPublicPolicy
}
func (c *PublicAccessBlockConfiguration) restrictsPublicBuckets() bool {
return c != nil && c.RestrictPublicBuckets
}
// restrictsPublicBuckets fails closed: a bucket whose configuration cannot be
// read is treated as restricted.
func (s3a *S3ApiServer) restrictsPublicBuckets(bucket string) bool {
config, errCode := s3a.getBucketConfig(bucket)
return errCode != s3err.ErrNone || config.PublicAccessBlock.restrictsPublicBuckets()
}
var errPublicPolicyBlocked = errors.New("bucket blocks public policies")
// restrictingConditionKeys are the condition keys that, with fixed values,
// keep an Allow to every principal from making a policy public.
var restrictingConditionKeys = map[string]bool{
"aws:sourceip": true,
"aws:sourcevpc": true,
"aws:sourcevpce": true,
"aws:sourcearn": true,
"aws:sourceaccount": true,
"aws:sourceowner": true,
"aws:principalaccount": true,
"aws:principalarn": true,
"aws:principalorgid": true,
"aws:principalorgpaths": true,
"aws:userid": true,
"s3:dataaccesspointarn": true,
"s3:dataaccesspointaccount": true,
}
// isPublicPolicy reports whether a bucket policy grants access to anyone, the
// way S3 Block Public Access judges it: an Allow to a wildcard principal (or
// to everyone but a NotPrincipal) that no fixed-value restricting condition
// narrows.
func isPublicPolicy(doc *policy_engine.PolicyDocument) bool {
if doc == nil {
return false
}
for _, statement := range doc.Statement {
if statement.Effect != policy_engine.PolicyEffectAllow {
continue
}
if statement.NotPrincipal == nil && !hasPublicPrincipal(statement.Principal.Strings()) {
continue
}
if !hasRestrictingCondition(statement.Condition) {
return true
}
}
return false
}
func hasPublicPrincipal(principals []string) bool {
if len(principals) == 0 {
return true
}
for _, principal := range principals {
if principal != "" && (!strings.ContainsAny(principal, "*?$") || hasFixedAccount(principal)) {
continue
}
return true
}
return false
}
// hasFixedAccount reports whether an ARN names one account, whatever
// wildcards its resource part holds.
func hasFixedAccount(arn string) bool {
parts := strings.SplitN(arn, ":", 6)
return len(parts) == 6 && parts[0] == "arn" && parts[4] != "" && !strings.ContainsAny(parts[4], "*?$")
}
func hasRestrictingCondition(conditions policy_engine.PolicyConditions) bool {
for operator, keys := range conditions {
op := strings.ToLower(operator)
if strings.HasSuffix(op, "ifexists") || strings.HasPrefix(op, "forallvalues:") || strings.Contains(op, "not") {
continue
}
op = strings.TrimPrefix(op, "foranyvalue:")
for key, values := range keys {
key = strings.ToLower(key)
if !restrictingConditionKeys[key] {
continue
}
if fixedConditionValues(op, key, values.Strings()) {
return true
}
}
}
return false
}
func fixedConditionValues(operator, key string, values []string) bool {
if len(values) == 0 {
return false
}
for _, value := range values {
if key == "aws:sourceip" {
if operator != "ipaddress" {
return false
}
if _, network, err := net.ParseCIDR(value); err == nil {
ones, bits := network.Mask.Size()
if bits == 128 && network.IP.To4() != nil {
ones, bits = ones-96, 32
}
if (bits == 32 && ones < 8) || (bits == 128 && ones < 32) {
return false
}
} else if net.ParseIP(value) == nil {
return false
}
continue
}
if !strings.HasPrefix(operator, "string") && !strings.HasPrefix(operator, "arn") {
return false
}
if value == "" || (strings.ContainsAny(value, "*?$") && !hasFixedAccount(value)) {
return false
}
}
return true
}
// 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
}