隔离系统通知配置权限并保留各角色个人设置
Security Checks / security (push) Waiting to run

个人扫码绑定实现前检查点;补充权限与账户隔离回归用例,业务实机验收尚未执行。
This commit is contained in:
xuans committed 2026-09-28 20:57:56 +08:00
1 parent 39ac7d2d2c
commit 2a532b2224
9 files changed
+196 -19

No files matched your search

+2 -1
View File
@@ -25,7 +25,8 @@ func PluginSecret(ctx *appcontext.Context, w http.ResponseWriter, req *http.Requ
if req.Method == http.MethodPut {
capability = access.CapabilityPluginWriteGlobal
}
if ctx == nil || ctx.UserID <= 0 {
// 共享公众号和机器人密钥不属于分组管理员权限,首次配置和后续维护使用同一边界。
if ctx == nil || ctx.UserID <= 0 || ctx.EffectiveRole() != access.RoleSuperAdmin {
writeConfigAccessError(w, &access.Denial{Code: access.CodeAccessDenied})
return
}
+21 -10
View File
@@ -32,13 +32,16 @@ func TestPluginSecretAuthorization(t *testing.T) {
{"anonymous", nil, "ACCESS_DENIED"},
{"no session user", &appcontext.Context{Role: access.RoleSuperAdmin, RecentAuth: true, RecoveryCodeConfigured: true}, "ACCESS_DENIED"},
{"ordinary user", &appcontext.Context{UserID: 3, Role: access.RoleUser, RecentAuth: true, RecoveryCodeConfigured: true}, "ACCESS_DENIED"},
{"missing recent authentication", &appcontext.Context{UserID: 2, Role: access.RoleAdmin, RecoveryCodeConfigured: true}, "RECENT_AUTH_REQUIRED"},
{"missing recovery code", &appcontext.Context{UserID: 2, Role: access.RoleAdmin, RecentAuth: true}, "RECOVERY_CODE_REQUIRED"},
{"authenticated administrator", &appcontext.Context{UserID: 2, Role: access.RoleAdmin, RecentAuth: true, RecoveryCodeConfigured: true}, "ACCESS_DENIED"},
{"missing recent authentication", &appcontext.Context{UserID: 1, Role: access.RoleSuperAdmin, RecoveryCodeConfigured: true}, "RECENT_AUTH_REQUIRED"},
{"administrator without recovery code", &appcontext.Context{UserID: 2, Role: access.RoleAdmin, RecentAuth: true}, "ACCESS_DENIED"},
} {
t.Run(tc.name, func(t *testing.T) {
recorder := callPluginSecret(tc.ctx, http.MethodPut, "telegram_push", `{"version":0,"change":{"operation":"replace","value":{"plaintext":"test-only-secret"}}}`)
if recorder.Code != http.StatusForbidden || !strings.Contains(recorder.Body.String(), tc.code) {
t.Fatalf("status=%d response=%s", recorder.Code, recorder.Body.String())
for _, id := range []string{"telegram_push", "wechat_push"} {
recorder := callPluginSecret(tc.ctx, http.MethodPut, id, `{"version":0,"change":{"operation":"replace","value":{"plaintext":"test-only-secret"}}}`)
if recorder.Code != http.StatusForbidden || !strings.Contains(recorder.Body.String(), tc.code) {
t.Fatalf("渠道=%s,状态=%d,响应=%s", id, recorder.Code, recorder.Body.String())
}
}
})
}
@@ -46,10 +49,18 @@ func TestPluginSecretAuthorization(t *testing.T) {
if err != nil || count != 0 {
t.Fatalf("denied writes changed secrets: count=%d err=%v", count, err)
}
for _, role := range []access.Role{access.RoleAdmin, access.RoleSuperAdmin} {
recorder := callPluginSecret(&appcontext.Context{UserID: 2, Role: role}, http.MethodGet, "telegram_push", "")
if recorder.Code != http.StatusOK || !strings.Contains(recorder.Body.String(), `"configured":false`) {
t.Fatalf("read role=%s status=%d body=%s", role, recorder.Code, recorder.Body.String())
for _, id := range []string{"telegram_push", "wechat_push"} {
for _, role := range []access.Role{access.RoleUser, access.RoleAdmin, access.RoleSuperAdmin} {
recorder := callPluginSecret(&appcontext.Context{UserID: 2, Role: role}, http.MethodGet, id, "")
if role != access.RoleSuperAdmin {
if recorder.Code != http.StatusForbidden || !strings.Contains(recorder.Body.String(), "ACCESS_DENIED") {
t.Fatalf("角色=%s 不应读取系统渠道 %s 的密钥状态", role, id)
}
continue
}
if recorder.Code != http.StatusOK || !strings.Contains(recorder.Body.String(), `"configured":false`) {
t.Fatalf("超级管理员读取渠道=%s,状态=%d", id, recorder.Code)
}
}
}
recorder := callPluginSecret(&appcontext.Context{UserID: 3, Role: access.RoleUser}, http.MethodGet, "telegram_push", "")
@@ -63,7 +74,7 @@ func TestPluginSecretEncryptedLifecycleAndValidation(t *testing.T) {
previous := hooks.PluginConfigService()
hooks.SetPluginConfigService(service)
t.Cleanup(func() { hooks.SetPluginConfigService(previous) })
ctx := &appcontext.Context{UserID: 2, Role: access.RoleAdmin, RecentAuth: true, RecoveryCodeConfigured: true}
ctx := &appcontext.Context{UserID: 1, Role: access.RoleSuperAdmin, RecentAuth: true, RecoveryCodeConfigured: true}
for _, id := range []string{"telegram_push", "wechat_push"} {
t.Run(id, func(t *testing.T) {
for _, body := range []string{