fix(s3api): complete bucket-listing IAM condition handling (#12699)

* fix(s3api): complete bucket-listing IAM condition handling

Populate s3:delimiter and s3:max-keys for bucket. Add regression tests for listing conditions and variants.

Assisted-by: gpt-6.1-sol, deepseek-v4.1-flash

* fix(s3api): ignore max-keys for multipart listing policies

Assisted-by: GPT-6.1 Terra

* s3api: drop max-keys from multipart-upload listing policy conditions

Bucket policies and embedded IAM policies read condition values through
ExtractConditionValuesFromRequest, which still exposed max-keys on ?uploads
listings. Those page by max-uploads, so an unused max-keys parameter could
satisfy an s3:max-keys condition. Match the advanced IAM path and leave the
key absent there.

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

---------

Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
authored and GitHub committed 2026-11-17 17:05:00 -08:00
1 parent 5681be241e
commit 1d30b0de21
4 files changed
+142 -9

No files matched your search

@@ -3,6 +3,7 @@ package s3api
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
@@ -258,6 +259,97 @@ func TestAuthorizeAction_ListVersionsWithPromotedPrefix(t *testing.T) {
"s3:GetObject must not satisfy the uploads listing")
}
// Bucket-listing policies are evaluated by the IAM integration. Its authorizer
// must expose prefix, delimiter and max-keys to conditions, and resolve the
// listing variant (?versions, ?uploads) to its own action.
func TestAuthorizeAction_BucketListingConditions(t *testing.T) {
const bucket = "test-bucket"
ctx := context.Background()
iamManager := integration.NewIAMManager()
require.NoError(t, iamManager.Initialize(&integration.IAMConfig{
STS: &sts.STSConfig{
TokenDuration: sts.FlexibleDuration{Duration: time.Hour},
MaxSessionLength: sts.FlexibleDuration{Duration: 12 * time.Hour},
Issuer: "test-sts",
SigningKey: []byte("test-signing-key-32-characters-long"),
},
Policy: &policy.PolicyEngineConfig{DefaultEffect: "Deny", StoreType: "memory"},
Roles: &integration.RoleStoreConfig{StoreType: "memory"},
}, func() string { return "localhost:8888" }))
allow := func(name, action string, condition map[string]map[string]interface{}) {
t.Helper()
require.NoError(t, iamManager.CreatePolicy(ctx, "", name, &policy.PolicyDocument{
Version: "2012-10-17",
Statement: []policy.Statement{{
Effect: "Allow",
Action: []string{action},
Resource: []string{"arn:aws:s3:::" + bucket},
Condition: condition,
}},
}))
}
// One-level listing of home/: a prefix alone is recursive, so the
// delimiter is what narrows the grant.
homeOneLevel := map[string]map[string]interface{}{"StringEquals": {
"s3:prefix": "home/",
"s3:delimiter": "/",
}}
allow("HomeOneLevel", "s3:ListBucket", homeOneLevel)
allow("VersionsHomeOneLevel", "s3:ListBucketVersions", homeOneLevel)
allow("UploadsHomeOneLevel", "s3:ListBucketMultipartUploads", homeOneLevel)
allow("MaxKeysTen", "s3:ListBucket", map[string]map[string]interface{}{
"NumericLessThanEquals": {"s3:max-keys": 10},
})
allow("VersionsMaxKeysTen", "s3:ListBucketVersions", map[string]map[string]interface{}{
"NumericLessThanEquals": {"s3:max-keys": 10},
})
allow("UploadsMaxKeysTen", "s3:ListBucketMultipartUploads", map[string]map[string]interface{}{
"NumericLessThanEquals": {"s3:max-keys": 10},
})
s3iam := NewS3IAMIntegration(iamManager, "localhost:8888")
tests := []struct {
name string
policy string
url string
action Action
object string
expected s3err.ErrorCode
}{
{"ListObjectsV2 one level with the permitted delimiter", "HomeOneLevel", "/" + bucket + "?list-type=2&prefix=home/&delimiter=/", s3_constants.ACTION_LIST, "home/", s3err.ErrNone},
{"recursive ListObjectsV2 without a delimiter is denied", "HomeOneLevel", "/" + bucket + "?list-type=2&prefix=home/", s3_constants.ACTION_LIST, "home/", s3err.ErrAccessDenied},
{"nested prefix with the permitted delimiter is denied", "HomeOneLevel", "/" + bucket + "?list-type=2&prefix=home/bob/&delimiter=/", s3_constants.ACTION_LIST, "home/bob/", s3err.ErrAccessDenied},
{"different delimiter is denied", "HomeOneLevel", "/" + bucket + "?list-type=2&prefix=home/&delimiter=.", s3_constants.ACTION_LIST, "home/", s3err.ErrAccessDenied},
{"bucket root listing is denied", "HomeOneLevel", "/" + bucket + "?list-type=2&delimiter=/", s3_constants.ACTION_LIST, "", s3err.ErrAccessDenied},
{"ListObjectVersions one level is allowed", "VersionsHomeOneLevel", "/" + bucket + "?versions&prefix=home/&delimiter=/", s3_constants.ACTION_LIST, "home/", s3err.ErrNone},
{"ListObjectVersions without a delimiter is denied", "VersionsHomeOneLevel", "/" + bucket + "?versions&prefix=home/", s3_constants.ACTION_LIST, "home/", s3err.ErrAccessDenied},
{"ListMultipartUploads one level is allowed", "UploadsHomeOneLevel", "/" + bucket + "?uploads&prefix=home/&delimiter=/", s3_constants.ACTION_READ, "", s3err.ErrNone},
{"ListMultipartUploads without a delimiter is denied", "UploadsHomeOneLevel", "/" + bucket + "?uploads&prefix=home/", s3_constants.ACTION_READ, "", s3err.ErrAccessDenied},
{"max-keys within s3:max-keys is allowed", "MaxKeysTen", "/" + bucket + "?list-type=2&prefix=home/&delimiter=/&max-keys=5", s3_constants.ACTION_LIST, "home/", s3err.ErrNone},
{"max-keys above s3:max-keys is denied", "MaxKeysTen", "/" + bucket + "?list-type=2&prefix=home/&delimiter=/&max-keys=20", s3_constants.ACTION_LIST, "home/", s3err.ErrAccessDenied},
{"versions max-keys within limit is allowed", "VersionsMaxKeysTen", "/" + bucket + "?versions&max-keys=5", s3_constants.ACTION_LIST, "", s3err.ErrNone},
{"versions max-keys above limit is denied", "VersionsMaxKeysTen", "/" + bucket + "?versions&max-keys=20", s3_constants.ACTION_LIST, "", s3err.ErrAccessDenied},
{"uploads max-uploads does not satisfy max-keys", "UploadsMaxKeysTen", "/" + bucket + "?uploads&max-uploads=5", s3_constants.ACTION_READ, "", s3err.ErrAccessDenied},
{"uploads spoofed max-keys does not satisfy policy", "UploadsMaxKeysTen", "/" + bucket + "?uploads&max-uploads=1000&max-keys=5", s3_constants.ACTION_READ, "", s3err.ErrAccessDenied},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
identity := &IAMIdentity{
Name: "reader",
Principal: "arn:aws:iam::000000000000:user/reader",
PolicyNames: []string{tt.policy},
}
req := httptest.NewRequest("GET", tt.url, http.NoBody)
require.Equal(t, tt.expected,
s3iam.AuthorizeAction(ctx, identity, tt.action, bucket, tt.object, req))
})
}
}
func mustPolicy(t *testing.T, doc map[string]any) string {
t.Helper()
b, err := json.Marshal(doc)
+2 -1
View File
@@ -454,7 +454,8 @@ func (engine *PolicyEngine) ExtractConditionValuesFromRequest(r *http.Request) m
values["s3:prefix"] = []string{prefix}
}
if maxKeys := r.URL.Query().Get("max-keys"); maxKeys != "" {
// Multipart listings use max-uploads, so max-keys cannot constrain them.
if maxKeys := r.URL.Query().Get("max-keys"); maxKeys != "" && !r.URL.Query().Has("uploads") {
values["s3:max-keys"] = []string{maxKeys}
}
+17
View File
@@ -4,6 +4,7 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"slices"
"strings"
"testing"
@@ -419,6 +420,22 @@ func TestExtractConditionValuesFromRequest(t *testing.T) {
}
}
func TestExtractConditionValuesFromRequestMaxKeys(t *testing.T) {
for _, tt := range []struct {
query string
want []string
}{
{"list-type=2&max-keys=5", []string{"5"}},
{"versions&max-keys=5", []string{"5"}},
{"uploads&max-uploads=1000&max-keys=5", nil},
} {
req := &http.Request{Method: "GET", URL: &url.URL{Path: "/test-bucket", RawQuery: tt.query}, RemoteAddr: "10.0.0.5:54321"}
if got := NewPolicyEngine().ExtractConditionValuesFromRequest(req)["s3:max-keys"]; !slices.Equal(got, tt.want) {
t.Errorf("%s: s3:max-keys = %v, want %v", tt.query, got, tt.want)
}
}
}
func TestExtractConditionValuesFromRequestSourceIPPrecedence(t *testing.T) {
tests := []struct {
name string
+31 -8
View File
@@ -245,14 +245,7 @@ func (s3iam *S3IAMIntegration) AuthorizeAction(ctx context.Context, identity *IA
// See https://github.com/seaweedfs/seaweedfs/issues/8969
resourceObjectKey := objectKey
if isBucketListingRequest(r, action) {
listPrefix := r.URL.Query().Get("prefix")
if listPrefix != "" {
requestContext["s3:prefix"] = listPrefix
} else if objectKey != "" && objectKey != "/" {
requestContext["s3:prefix"] = objectKey
} else {
requestContext["s3:prefix"] = ""
}
addBucketListingConditions(requestContext, r, objectKey)
resourceObjectKey = ""
}
@@ -310,6 +303,36 @@ func (s3iam *S3IAMIntegration) AuthorizeAction(ctx context.Context, identity *IA
return s3err.ErrNone
}
// addBucketListingConditions adds the condition keys AWS exposes for bucket
// listing requests. An omitted prefix is the empty prefix, matching AWS; an
// omitted delimiter or max-keys stays absent so conditions on them fail closed.
func addBucketListingConditions(requestContext map[string]interface{}, r *http.Request, objectKey string) {
if r == nil || r.URL == nil {
return
}
query := r.URL.Query()
listPrefix := query.Get("prefix")
if listPrefix != "" {
requestContext["s3:prefix"] = listPrefix
} else if objectKey != "" && objectKey != "/" {
requestContext["s3:prefix"] = objectKey
} else {
requestContext["s3:prefix"] = ""
}
if delimiter := query.Get("delimiter"); delimiter != "" {
requestContext["s3:delimiter"] = delimiter
}
// Multipart listings use max-uploads, so max-keys cannot constrain them.
if !query.Has("uploads") {
if maxKeys := query.Get("max-keys"); maxKeys != "" {
requestContext["s3:max-keys"] = maxKeys
}
}
}
// ValidateTrustPolicyForPrincipal delegates to IAMManager to validate trust policy
func (s3iam *S3IAMIntegration) ValidateTrustPolicyForPrincipal(ctx context.Context, roleArn, principalArn string) error {
if s3iam.iamManager == nil {