SeaweedFS is a distributed storage system for object storage (S3), file systems, Iceberg table buckets, and Lance vector buckets, designed to handle billions of files with O(1) disk access and effortless horizontal scaling.
Go blob-storagecloud-drivedistributed-file-systemdistributed-storageerasure-codingfusehadoop-hdfshdfskuberneteslancelancedbobject-storageposixs3s3-storageseaweedfstiered-file-systemvector-search
fix(iam): deny IAM users with no policies instead of granting full access (#9317)
Chris Lu66d9b89cd2bb
diff --git a/weed/iam/integration/iam_manager.go b/weed/iam/integration/iam_manager.goindex bfff9cefdd..e3af563286 100644--- a/weed/iam/integration/iam_manager.go+++ b/weed/iam/integration/iam_manager.go@@ -450,13 +450,20 @@ func (m *IAMManager) IsActionAllowed(ctx context.Context, request *ActionRequestvar baseResult *policy.EvaluationResultvar err error- subjectPolicyCount := 0+ // hasManagedSubject is true once we've resolved the principal to a registered+ // IAM user or role (or the caller has supplied PolicyNames directly). For a+ // managed subject, "no matching statement" must deny — the DefaultEffect=Allow+ // fallback is only meant for the unmanaged zero-config startup case.+ hasManagedSubject := falseif isAdmin {// Admin always has base access allowedbaseResult = &policy.EvaluationResult{Effect: policy.EffectAllow}} else {policies := request.PolicyNames+ if len(policies) > 0 {+ hasManagedSubject = true+ }if len(policies) == 0 {// Extract role name from principal ARNroleName := utils.ExtractRoleNameFromPrincipal(request.Principal)@@ -472,6 +479,7 @@ func (m *IAMManager) IsActionAllowed(ctx context.Context, request *ActionRequestif err != nil || user == nil {return false, fmt.Errorf("user not found for principal: %s (user=%s)", request.Principal, userName)}+ hasManagedSubject = truepolicies = user.GetPolicyNames()} else {// Get role definition@@ -480,11 +488,10 @@ func (m *IAMManager) IsActionAllowed(ctx context.Context, request *ActionRequestreturn false, fmt.Errorf("role not found: %s", roleName)}+ hasManagedSubject = truepolicies = roleDef.AttachedPolicies}}- subjectPolicyCount = len(policies)-if bucketPolicyName != "" {// Enforce an upper bound on the number of policies to avoid excessive allocationsif len(policies) >= maxPoliciesForEvaluation {@@ -508,10 +515,11 @@ func (m *IAMManager) IsActionAllowed(ctx context.Context, request *ActionRequest}// Zero-config IAM uses DefaultEffect=Allow to preserve open-by-default behavior- // for requests without any subject policies. Once a user or role has attached- // policies, "no matching statement" must fall back to deny so the attachment- // actually scopes access.- if subjectPolicyCount > 0 && len(baseResult.MatchingStatements) == 0 {+ // for requests without any subject policies. Once we resolve the principal to+ // a registered IAM user or role (or the caller hands us policy names),+ // "no matching statement" must fall back to deny — otherwise a freshly+ // created user with zero policies would inherit full access.+ if hasManagedSubject && len(baseResult.MatchingStatements) == 0 {return false, nil}diff --git a/weed/iam/integration/iam_manager_no_policy_test.go b/weed/iam/integration/iam_manager_no_policy_test.gonew file mode 100644index 0000000000..500f0c43a5--- /dev/null+++ b/weed/iam/integration/iam_manager_no_policy_test.go@@ -0,0 +1,145 @@+package integration++import (+ "context"+ "testing"+ "time"++ "github.com/seaweedfs/seaweedfs/weed/iam/policy"+ "github.com/seaweedfs/seaweedfs/weed/iam/sts"+ "github.com/seaweedfs/seaweedfs/weed/pb/iam_pb"+ "github.com/stretchr/testify/assert"+ "github.com/stretchr/testify/require"+)++type fakeUserStore struct {+ users map[string]*iam_pb.Identity+}++func (f *fakeUserStore) GetUser(_ context.Context, name string) (*iam_pb.Identity, error) {+ if u, ok := f.users[name]; ok {+ return u, nil+ }+ return nil, nil+}++// TestIsActionAllowed_RegisteredUserWithoutPoliciesIsDenied is a regression test+// for a security issue where a user created via the S3 IAM API with no policies+// attached was granted access to every S3 action. The default-allow path was+// intended for unmanaged zero-config startup, not for explicitly registered IAM+// users with an empty policy set.+func TestIsActionAllowed_RegisteredUserWithoutPoliciesIsDenied(t *testing.T) {+ manager := NewIAMManager()+ require.NoError(t, manager.Initialize(&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{+ // Reproduce the zero-config setup: with no IAM config file, the+ // loader picks DefaultEffect=Allow for in-memory mode.+ DefaultEffect: string(policy.EffectAllow),+ StoreType: sts.StoreTypeMemory,+ },+ Roles: &RoleStoreConfig{StoreType: sts.StoreTypeMemory},+ }, func() string { return "localhost:8888" }))++ manager.SetUserStore(&fakeUserStore{users: map[string]*iam_pb.Identity{+ "newuser": {Name: "newuser"}, // no PolicyNames, no Actions+ }})++ allowed, err := manager.IsActionAllowed(context.Background(), &ActionRequest{+ Principal: "arn:aws:iam::seaweedfs:user/newuser",+ Action: "s3:DeleteBucket",+ Resource: "arn:aws:s3:::any-bucket",+ })+ require.NoError(t, err)+ assert.False(t, allowed, "registered user with no policies must not get default-allow access")+}++// TestIsActionAllowed_RegisteredRoleWithoutPoliciesIsDenied is the role-side+// counterpart of the registered-user regression: a managed role that exists+// but has zero AttachedPolicies must not inherit full access from the+// zero-config DefaultEffect=Allow fallback. Coderabbit flagged this as an+// uncovered branch of the same fix on PR #9317.+func TestIsActionAllowed_RegisteredRoleWithoutPoliciesIsDenied(t *testing.T) {+ manager := NewIAMManager()+ require.NoError(t, manager.Initialize(&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: string(policy.EffectAllow),+ StoreType: sts.StoreTypeMemory,+ },+ Roles: &RoleStoreConfig{StoreType: sts.StoreTypeMemory},+ }, func() string { return "localhost:8888" }))++ ctx := context.Background()+ require.NoError(t, manager.CreateRole(ctx, "", "EmptyRole", &RoleDefinition{+ RoleName: "EmptyRole",+ RoleArn: "arn:aws:iam::seaweedfs:role/EmptyRole",+ AttachedPolicies: nil, // explicit: managed role with no policies attached+ }))++ allowed, err := manager.IsActionAllowed(ctx, &ActionRequest{+ // Use the assumed-role principal form the STS path produces. The+ // IAM manager extracts role name via ExtractRoleNameFromPrincipal,+ // finds the role definition, and must deny because AttachedPolicies+ // is empty even though DefaultEffect is Allow.+ Principal: "arn:aws:sts::seaweedfs:assumed-role/EmptyRole/some-session",+ Action: "s3:DeleteBucket",+ Resource: "arn:aws:s3:::any-bucket",+ })+ require.NoError(t, err)+ assert.False(t, allowed, "registered role with no attached policies must not get default-allow access")+}++// TestIsActionAllowed_RegisteredUserWithNonMatchingPolicyIsDenied covers the+// adjacent case: the user has policies attached, but none of the policy+// statements match the requested action/resource. The DefaultEffect=Allow+// fallback must not silently grant access when an attached policy simply+// does not say anything about this action.+func TestIsActionAllowed_RegisteredUserWithNonMatchingPolicyIsDenied(t *testing.T) {+ manager := NewIAMManager()+ require.NoError(t, manager.Initialize(&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: string(policy.EffectAllow),+ StoreType: sts.StoreTypeMemory,+ },+ Roles: &RoleStoreConfig{StoreType: sts.StoreTypeMemory},+ }, func() string { return "localhost:8888" }))++ ctx := context.Background()+ require.NoError(t, manager.CreatePolicy(ctx, "", "ReadOnlyOneBucket", &policy.PolicyDocument{+ Version: "2012-10-17",+ Statement: []policy.Statement{{+ Effect: "Allow",+ Action: []string{"s3:GetObject"},+ Resource: []string{"arn:aws:s3:::reports/*"},+ }},+ }))++ manager.SetUserStore(&fakeUserStore{users: map[string]*iam_pb.Identity{+ "alice": {Name: "alice", PolicyNames: []string{"ReadOnlyOneBucket"}},+ }})++ allowed, err := manager.IsActionAllowed(ctx, &ActionRequest{+ Principal: "arn:aws:iam::seaweedfs:user/alice",+ Action: "s3:DeleteBucket",+ Resource: "arn:aws:s3:::other-bucket",+ })+ require.NoError(t, err)+ assert.False(t, allowed, "action outside the user's attached policy must be denied")+}