s3: list the buckets an attached IAM policy grants (#10458)

* s3: list the buckets an attached IAM policy grants

ListBuckets served an identity authorized by an attached IAM policy only
the buckets it had created itself. A user granted s3:ListBucket on a
bucket someone else provisioned could GetObject and ListObjectsV2 against
it, but the bucket never showed up in the listing any S3 client uses to
build its bucket picker.

The owner-index fast path is only valid for an identity whose grants name
every bucket it can reach, and the routing check assumed a policy could
never be enumerated. Read the names out of the policy instead: statements
that allow s3:ListBucket on a concrete bucket ARN become candidates, and
the per-bucket permission re-check still decides what is listed. A policy
that can reach a bucket it does not name -- a wildcard resource, a policy
variable, a NotResource, an STS session policy -- falls back to the full
scan, which evaluates the policy per bucket.

* s3: share the attached policy name lookup

authorizeWithIAM and the ListBuckets enumeration both built an identity's
policy names the same way, its own plus the ones from its enabled groups.
Pull that into one helper so group eligibility is decided in a single place.

* s3: read policy actions the way the IAM authorizer matches them

The IAM authorizer matches action names case-insensitively, so a policy
granting "S3:LISTBUCKET" or "S3:*" authorizes a list. The ListBuckets
classifier read those actions with the local case-sensitive matcher and
found no grant, so once the owner index was ready the buckets that policy
allows dropped out of the listing.

Match the action the looser way in the classifier: case-insensitive, and
true for any pattern holding a policy variable. Over-matching only costs a
candidate the per-bucket permission check then rejects, while under-matching
hides a bucket the caller can read.

* s3: infer a multipart grant in any case

The classifier matches action patterns case-insensitively but looked the
requested action up in a canonical-cased set, so "S3:UPLOADPART" missed the
s3:PutObject inference that the authorizer makes. Key the set for lookup in
lower case, matching how the IAM authorizer holds it.
This commit is contained in:
Chris Lu authored and GitHub committed 2026-07-27 16:42:01 -07:00
1 parent 5536d88fbb
commit 62c4333074
6 files changed
+332 -31

No files matched your search

+40
View File
@@ -644,6 +644,46 @@ func (engine *PolicyEngine) GetPolicyStatements(bucketName string) []PolicyState
return context.policy.Document.Statement
}
// BucketsAllowedForAction returns the buckets a policy names in the Allow
// statements that can match the action, and whether those names cover every
// bucket the policy can allow it on. A statement reaching buckets it does not
// name -- a wildcard resource, a policy variable, a NotResource -- leaves the
// set incomplete, as does an unknown policy name.
func (engine *PolicyEngine) BucketsAllowedForAction(policyName string, action string) (buckets []string, complete bool) {
engine.mutex.RLock()
context, exists := engine.contexts[policyName]
engine.mutex.RUnlock()
if !exists {
return nil, false
}
for _, statement := range context.policy.Document.Statement {
// A Deny only narrows what an Allow named.
if statement.Effect != PolicyEffectAllow || !statementMayAllowAction(statement.Action.Strings(), action) {
continue
}
if statement.Resource == nil || statement.NotResource != nil {
return nil, false
}
resources := statement.Resource.Strings()
if len(resources) == 0 {
return nil, false
}
for _, resource := range resources {
if PolicyVariableRegex.MatchString(resource) {
return nil, false
}
bucket := GetBucketFromResource(resource)
if bucket == "" || strings.ContainsAny(bucket, "*?") {
return nil, false
}
buckets = append(buckets, bucket)
}
}
return buckets, true
}
// ValidatePolicyForBucket validates if a policy is valid for a bucket
func (engine *PolicyEngine) ValidatePolicyForBucket(bucketName string, policyJSON string) error {
policy, err := ParsePolicy(policyJSON)