个人扫码绑定实现前检查点;补充权限与账户隔离回归用例,业务实机验收尚未执行。
This commit is contained in:
1 parent
39ac7d2d2c
commit
2a532b2224
9 files changed
+196
-19
No files matched your search
@@ -43,6 +43,74 @@ func TestConfigCredentialsAreEncryptedAtRest(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotificationSystemConfigurationRequiresSuperAdmin(t *testing.T) {
|
||||
for _, id := range []string{"telegram_push", "wechat_push"} {
|
||||
t.Run(id, func(t *testing.T) {
|
||||
original := PluginInstance{Enabled: true, Settings: json.RawMessage(`{"recipient":"original","enabled":true}`)}
|
||||
current := RuntimeConfig{Plugins: PluginsSectionConfig{Plugins: map[string]PluginInstance{id: original}}}
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
modify func(*PluginInstance)
|
||||
}{
|
||||
{"enabled", func(p *PluginInstance) { p.Enabled = false }},
|
||||
{"settings", func(p *PluginInstance) { p.Settings = json.RawMessage(`{"recipient":"changed"}`) }},
|
||||
{"secret", func(p *PluginInstance) { p.Secret.Ciphertext = "changed" }},
|
||||
{"executable", func(p *PluginInstance) { p.Executable = "changed" }},
|
||||
{"capabilities", func(p *PluginInstance) { p.Permissions.Capabilities = []string{"network"} }},
|
||||
{"network", func(p *PluginInstance) {
|
||||
p.Permissions.Network = []PluginNetworkTarget{{Scheme: "https", Host: "example.com", Port: 443}}
|
||||
}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
next := original
|
||||
tc.modify(&next)
|
||||
candidate := RuntimeConfig{Plugins: PluginsSectionConfig{Plugins: map[string]PluginInstance{id: next}}}
|
||||
for _, role := range []string{"super_admin", "admin", "user"} {
|
||||
err := AuthorizePluginChanges(AuditActor{UserID: "1", Role: role}, current, candidate)
|
||||
if (err == nil) != (role == "super_admin") {
|
||||
t.Fatalf("角色=%s,修改=%s,授权结果=%v", role, tc.name, err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
for _, role := range []string{"super_admin", "admin", "user"} {
|
||||
actor := AuditActor{UserID: "1", Role: role}
|
||||
if err := AuthorizePluginChanges(actor, RuntimeConfig{}, current); (err == nil) != (role == "super_admin") {
|
||||
t.Fatalf("角色=%s,首次配置授权结果=%v", role, err)
|
||||
}
|
||||
if err := AuthorizePluginChanges(actor, current, RuntimeConfig{}); (err == nil) != (role == "super_admin") {
|
||||
t.Fatalf("角色=%s,移除配置授权结果=%v", role, err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminCanCarryUnchangedNotificationConfiguration(t *testing.T) {
|
||||
for _, id := range []string{"telegram_push", "wechat_push"} {
|
||||
current := RuntimeConfig{Plugins: PluginsSectionConfig{Plugins: map[string]PluginInstance{
|
||||
id: {Enabled: true, Settings: json.RawMessage(`{"recipient":"original","enabled":true}`)},
|
||||
"spam_block": {Settings: json.RawMessage(`{"score":1}`)},
|
||||
}}}
|
||||
candidate := RuntimeConfig{Plugins: PluginsSectionConfig{Plugins: map[string]PluginInstance{
|
||||
id: {Enabled: true, Settings: json.RawMessage(`{ "enabled": true, "recipient": "original" }`)},
|
||||
"spam_block": {Settings: json.RawMessage(`{"score":2}`)},
|
||||
}}}
|
||||
if err := AuthorizePluginChanges(AuditActor{UserID: "2", Role: "admin"}, current, candidate); err != nil {
|
||||
t.Fatalf("修改垃圾邮件参数不应误拦截未变化的渠道 %s:%v", id, err)
|
||||
}
|
||||
if err := AuthorizePluginChanges(AuditActor{UserID: "3", Role: "user"}, current, candidate); err == nil {
|
||||
t.Fatal("普通用户不应修改系统插件配置")
|
||||
}
|
||||
invalid := candidate.Plugins.Plugins[id]
|
||||
invalid.Settings = append(invalid.Settings, []byte(" {}")...)
|
||||
candidate.Plugins.Plugins[id] = invalid
|
||||
if err := AuthorizePluginChanges(AuditActor{UserID: "2", Role: "admin"}, current, candidate); err == nil {
|
||||
t.Fatal("尾随 JSON 不应视为未变化的系统配置")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigPrefixCredentialRoundTrip(t *testing.T) {
|
||||
oldRoot := ROOT_PATH
|
||||
root := t.TempDir()
|
||||
|
||||
@@ -182,7 +182,7 @@ func DefaultConfigAuthorization(_ context.Context, actor AuditActor, operation C
|
||||
return nil
|
||||
}
|
||||
|
||||
// AuthorizePluginChanges 限制管理员只能修改已有插件参数,不允许安装、替换程序或提升权限。
|
||||
// AuthorizePluginChanges 限制管理员只能修改普通插件参数;公众号和机器人始终由超级管理员配置。
|
||||
func AuthorizePluginChanges(actor AuditActor, current, candidate RuntimeConfig) error {
|
||||
if actor.Role == string(access.RoleSuperAdmin) {
|
||||
return nil
|
||||
@@ -195,10 +195,35 @@ func AuthorizePluginChanges(actor AuditActor, current, candidate RuntimeConfig)
|
||||
if !ok || next.Executable != previous.Executable || !reflect.DeepEqual(next.Permissions, previous.Permissions) {
|
||||
return ErrAuthorizationRequired
|
||||
}
|
||||
if id == "wechat_push" || id == "telegram_push" {
|
||||
// 允许普通插件保存时原样携带共享配置,但不允许借整个分区绕过权限。
|
||||
if !sameNotificationPlugin(previous, next) {
|
||||
return ErrAuthorizationRequired
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func sameNotificationPlugin(previous, next PluginInstance) bool {
|
||||
var settings [2]any
|
||||
for index, data := range []json.RawMessage{previous.Settings, next.Settings} {
|
||||
if len(data) == 0 {
|
||||
continue
|
||||
}
|
||||
if !json.Valid(data) {
|
||||
return false
|
||||
}
|
||||
decoder := json.NewDecoder(strings.NewReader(string(data)))
|
||||
decoder.UseNumber()
|
||||
if err := decoder.Decode(&settings[index]); err != nil {
|
||||
return false
|
||||
}
|
||||
}
|
||||
previous.Settings, next.Settings = nil, nil
|
||||
return reflect.DeepEqual(previous, next) && reflect.DeepEqual(settings[0], settings[1])
|
||||
}
|
||||
|
||||
func DefaultConfigConfirmation(_ context.Context, _ AuditActor, confirmation StepUpConfirmation, operation ConfigOperation, _ Section) error {
|
||||
if operation != ConfigOperationRollback && !strings.EqualFold(confirmation.Method, "webauthn") && !strings.EqualFold(confirmation.Method, "recent_auth") {
|
||||
return nil
|
||||
|
||||
Reference in new issue
Block a user