补齐安全码与已验证邮箱二选一授权回归覆盖

This commit is contained in:
xuans committed 2026-10-02 18:36:39 +08:00
1 parent 96656d9b21
commit f965affcaa
8 files changed
+203 -53

No files matched your search

+57
View File
@@ -2,6 +2,63 @@ package access
import "testing"
func TestRecoveryOptionAuthorization(t *testing.T) {
for _, capability := range []Capability{CapabilityPasswordReset, CapabilityUserSecurityManage, CapabilityNotificationManage} {
for _, ready := range []struct {
name string
code bool
email bool
}{{"neither", false, false}, {"code", true, false}, {"email", false, true}, {"both", true, true}} {
t.Run(string(capability)+"/"+ready.name, func(t *testing.T) {
req := Request{ActorRole: RoleAdmin, TargetRole: RoleUser, Capability: capability, RecentAuth: true,
RecoveryCodeConfigured: ready.code, ExternalEmailConfigured: ready.email}
err := Authorize(req)
if ready.code || ready.email {
if err != nil {
t.Fatalf("expected allow: %v", err)
}
} else if !IsDenied(err, CodeRecoveryCodeRequired) {
t.Fatalf("expected recovery requirement: %v", err)
}
req.RecentAuth = false
if err := Authorize(req); !IsDenied(err, CodeRecentAuthRequired) {
t.Fatalf("missing recent auth accepted: %v", err)
}
})
}
}
for _, target := range []Role{RoleAdmin, RoleSuperAdmin} {
req := Request{ActorRole: RoleAdmin, TargetRole: target, Capability: CapabilityPasswordReset, RecentAuth: true, ExternalEmailConfigured: true}
if err := Authorize(req); !IsDenied(err, CodeTargetRoleDenied) {
t.Fatalf("email bypassed target boundary: %v", err)
}
}
req := Request{ActorRole: RoleUser, TargetRole: RoleUser, Capability: CapabilityNotificationManage, RecentAuth: true, ExternalEmailConfigured: true, RecoveryCodeConfigured: true}
if err := Authorize(req); !IsDenied(err, CodeAccessDenied) {
t.Fatalf("user received management permission: %v", err)
}
}
func TestRoleChangeAcceptsTargetEmailWithoutBorrowingActorState(t *testing.T) {
req := Request{ActorRole: RoleSuperAdmin, Capability: CapabilityRoleManage, TargetRole: RoleUser, NewRole: RoleAdmin, RecentAuth: true, ExternalEmailConfigured: true}
if err := Authorize(req); !IsDenied(err, CodeRecoveryCodeRequired) {
t.Fatalf("actor email substituted for target: %v", err)
}
req.TargetExternalEmailConfigured = true
if err := Authorize(req); err != nil {
t.Fatalf("target email did not qualify: %v", err)
}
req.NewRole = RoleUser
req.TargetRole = RoleAdmin
if err := Authorize(req); err != nil {
t.Fatalf("email-only downgrade rejected: %v", err)
}
req.RecentAuth = false
if err := Authorize(req); !IsDenied(err, CodeRecentAuthRequired) {
t.Fatalf("role change bypassed recent auth: %v", err)
}
}
func TestDefaultDeny(t *testing.T) {
tests := []struct {
name string
+12 -12
View File
@@ -25,19 +25,19 @@ func IsDenied(err error, code DenialCode) bool {
}
type Request struct {
ActorRole Role
Capability Capability
TargetRole Role
NewRole Role
OwnResource bool
RecentAuth bool
RecoveryCodeConfigured bool
ExternalEmailConfigured bool
TargetRecoveryConfigured bool
ActorRole Role
Capability Capability
TargetRole Role
NewRole Role
OwnResource bool
RecentAuth bool
RecoveryCodeConfigured bool
ExternalEmailConfigured bool
TargetRecoveryConfigured bool
TargetExternalEmailConfigured bool
PluginInstanceAuthorized bool
AuditScopeAuthorized bool
DeploymentChannel bool
PluginInstanceAuthorized bool
AuditScopeAuthorized bool
DeploymentChannel bool
}
func Authorize(request Request) error {