1 parent
7401d96a17
commit
7e007ea891
4 files changed
+51
-13
No files matched your search
@@ -13,7 +13,13 @@ import (
|
||||
|
||||
func TestPluginRolesValidateUpdateAndPending(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
writeRuntimeFixture(t, root, validRuntimeConfig())
|
||||
fixture := validRuntimeConfig()
|
||||
fixture.Plugins.Plugins["spam_block"] = PluginInstance{
|
||||
Executable: currentPluginExecutable("spam_block"),
|
||||
Permissions: PluginPermissions{Capabilities: []string{"network"}},
|
||||
Settings: json.RawMessage("{\"score\":1}"),
|
||||
}
|
||||
writeRuntimeFixture(t, root, fixture)
|
||||
manager, err := NewConfigManagerWithSecurity(root, ConfigLoaderOptions{Validation: RuntimeValidationOptions{HasSuperAdmin: true, Production: true}}, ConfigSecurity{Audit: &MemoryAuditWriter{}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -21,7 +27,7 @@ func TestPluginRolesValidateUpdateAndPending(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
admin := AuditActor{UserID: "2", Role: "admin", SessionID: "admin-session", RecentAuth: true, RecoveryCodeConfigured: true}
|
||||
current := manager.Snapshot()
|
||||
for _, change := range []string{"executable", "permissions", "install", "remove"} {
|
||||
for _, change := range []string{"executable", "permissions", "install", "remove", "settings"} {
|
||||
candidate := cloneRuntimeConfig(current)
|
||||
plugin := candidate.Plugins.Plugins["telegram_push"]
|
||||
switch change {
|
||||
@@ -31,6 +37,8 @@ func TestPluginRolesValidateUpdateAndPending(t *testing.T) {
|
||||
plugin.Permissions.Network[0].Host = "other.example.com"
|
||||
case "install":
|
||||
candidate.Plugins.Plugins["other"] = plugin
|
||||
case "settings":
|
||||
plugin.Settings = json.RawMessage("{\"timeout\":30}")
|
||||
}
|
||||
candidate.Plugins.Plugins["telegram_push"] = plugin
|
||||
if change == "remove" {
|
||||
@@ -44,10 +52,10 @@ func TestPluginRolesValidateUpdateAndPending(t *testing.T) {
|
||||
t.Fatalf("保存允许越权 %s:%v", change, err)
|
||||
}
|
||||
}
|
||||
plugins := current.Plugins
|
||||
plugin := plugins.Plugins["telegram_push"]
|
||||
plugins := cloneRuntimeConfig(current).Plugins
|
||||
plugin := plugins.Plugins["spam_block"]
|
||||
plugin.Settings = json.RawMessage(`{"displayName":"修改通知名称"}`)
|
||||
plugins.Plugins["telegram_push"] = plugin
|
||||
plugins.Plugins["spam_block"] = plugin
|
||||
payload, _ := json.Marshal(plugins)
|
||||
validation, err := manager.ValidateAuthorized(ctx, ValidateRequest{Section: SectionPlugins, ExpectedGeneration: current.Generation, Payload: payload}, admin)
|
||||
if err != nil || !validation.Valid {
|
||||
|
||||
@@ -102,12 +102,12 @@ func TestProtocolClientHandlersIsolateEveryRole(t *testing.T) {
|
||||
}
|
||||
}
|
||||
for _, protocol := range credentials.SupportedProtocols() {
|
||||
body := fmt.Sprintf(`{"protocol":%q}`, protocol)
|
||||
malicious := fmt.Sprintf(`{"protocol":%q,"enabled":false,"user_id":%d}`, protocol, victim.ID)
|
||||
body := fmt.Sprintf(`{"protocol":%q,"password":"WebOnly42"}`, protocol)
|
||||
malicious := fmt.Sprintf(`{"protocol":%q,"enabled":false,"user_id":%d,"password":"WebOnly42"}`, protocol, victim.ID)
|
||||
if result := call(SetProtocolCredentialEnabled, http.MethodPut, malicious); result.ErrorNo == 0 {
|
||||
t.Fatal("接口接受了未知目标用户字段")
|
||||
}
|
||||
mustSucceed(call(SetProtocolCredentialEnabled, http.MethodPut, fmt.Sprintf(`{"protocol":%q,"enabled":true}`, protocol)))
|
||||
mustSucceed(call(SetProtocolCredentialEnabled, http.MethodPut, fmt.Sprintf(`{"protocol":%q,"enabled":true,"password":"WebOnly42"}`, protocol)))
|
||||
generated := call(GenerateProtocolCredential, http.MethodPost, body)
|
||||
mustSucceed(generated)
|
||||
data, ok := generated.Data.(map[string]any)
|
||||
@@ -139,11 +139,11 @@ func TestProtocolClientHandlersIsolateEveryRole(t *testing.T) {
|
||||
if err != nil || strings.Contains(string(encoded), code) {
|
||||
t.Fatal("状态查询重新返回了授权码明文")
|
||||
}
|
||||
mustSucceed(call(SetProtocolCredentialEnabled, http.MethodPut, fmt.Sprintf(`{"protocol":%q,"enabled":false}`, protocol)))
|
||||
mustSucceed(call(SetProtocolCredentialEnabled, http.MethodPut, fmt.Sprintf(`{"protocol":%q,"enabled":false,"password":"WebOnly42"}`, protocol)))
|
||||
if login(code) {
|
||||
t.Fatal("关闭协议后实际认证入口仍放行")
|
||||
}
|
||||
mustSucceed(call(SetProtocolCredentialEnabled, http.MethodPut, fmt.Sprintf(`{"protocol":%q,"enabled":true}`, protocol)))
|
||||
mustSucceed(call(SetProtocolCredentialEnabled, http.MethodPut, fmt.Sprintf(`{"protocol":%q,"enabled":true,"password":"WebOnly42"}`, protocol)))
|
||||
if !login(code) {
|
||||
t.Fatal("重新开启协议后有效授权码未恢复")
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ func TestAccountRoleScopeAndOwnProfile(t *testing.T) {
|
||||
previous := db.Instance
|
||||
db.Instance = engine
|
||||
t.Cleanup(func() { db.Instance = previous; _ = engine.Close() })
|
||||
if err := engine.Sync2(&models.User{}, &models.RecoveryCode{}, &models.ExternalEmail{}); err != nil {
|
||||
if err := engine.Sync2(&models.User{}, &models.RecoveryCode{}, &models.ExternalEmail{}, &models.AccountGroup{}, &models.AccountGroupMember{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
users := []models.User{
|
||||
@@ -46,6 +46,9 @@ func TestAccountRoleScopeAndOwnProfile(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if _, err := engine.Insert(&models.AccountGroup{ID: 1, Name: "隔离分组"}, &models.AccountGroupMember{UserID: users[2].ID, GroupID: 1}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
admin := &appcontext.Context{UserID: users[1].ID, Role: access.RoleAdmin}
|
||||
ordinary := &appcontext.Context{UserID: users[3].ID, Role: access.RoleUser}
|
||||
call := func(ctx *appcontext.Context, handler func(*appcontext.Context, http.ResponseWriter, *http.Request), body string) response.Response {
|
||||
@@ -63,7 +66,7 @@ func TestAccountRoleScopeAndOwnProfile(t *testing.T) {
|
||||
t.Fatalf("低权限创建账号:%+v", got)
|
||||
}
|
||||
}
|
||||
for page, expected := range []string{"alice", "legacy"} {
|
||||
for page, expected := range []string{"manager", "alice", "legacy"} {
|
||||
body, _ := json.Marshal(map[string]int{"current_page": page + 1, "page_size": 1})
|
||||
got := call(admin, UserList, string(body))
|
||||
if got.ErrorNo != 0 {
|
||||
@@ -71,7 +74,7 @@ func TestAccountRoleScopeAndOwnProfile(t *testing.T) {
|
||||
}
|
||||
data := got.Data.(map[string]any)
|
||||
items := data["list"].([]any)
|
||||
if data["total_page"] != float64(2) || len(items) != 1 || items[0].(map[string]any)["Account"] != expected {
|
||||
if data["total_page"] != float64(3) || len(items) != 1 || items[0].(map[string]any)["Account"] != expected {
|
||||
t.Fatalf("分页前权限过滤错误:%+v", data)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user