From df26599cfe563a9cb3eaf291481cad251cfdc879 Mon Sep 17 00:00:00 2001 From: Tynwink2000 Date: Fri, 20 Nov 2026 00:55:02 +0800 Subject: [PATCH] feat(cli): link pending plugin permissions to WebUI review --- README.md | 2 + README_CN.md | 2 + internal/app/plugin_permissions.go | 8 +++ internal/command/operations.go | 5 ++ internal/command/operations_test.go | 103 ++++++++++++++++++++++++++++ internal/i18n/extra.go | 1 + 6 files changed, 121 insertions(+) diff --git a/README.md b/README.md index b8018d6..4d8d70d 100644 --- a/README.md +++ b/README.md @@ -746,6 +746,8 @@ lbctl plugin permissions confirm --yes lbctl plugin permissions confirm --capability storage.plugin --operation agent.list --yes ``` +When `plugin permissions get` reports pending confirmation, its human, JSON, and YAML output includes an optional WebUI extensions URL and the target Workspace. Review permissions there with an authorized account, or use `plugin permissions confirm`. Run the command again after approval to check the resulting state. + Confirmation grants all declared permissions unless individual IDs are supplied. Compatibility-mode plugins always receive the complete declared set. Core checks each grant against the API key's own permissions. `--confirm-permissions` implies waiting for successful installation, confirming, and verifying that the plugin is enabled. It requires `plugin.permissions.confirm_api_key` and `operation.invoke`; failure never proceeds to confirmation. Local/GitHub identity comes from the package manifest, not the filename/repository name. GitHub confirmation downloads the selected public release asset to inspect its manifest without sending the Workspace key to GitHub. ### Core access boundaries diff --git a/README_CN.md b/README_CN.md index 985854f..d11cea4 100644 --- a/README_CN.md +++ b/README_CN.md @@ -719,6 +719,8 @@ lbctl plugin permissions confirm --yes lbctl plugin permissions confirm --capability storage.plugin --operation agent.list --yes ``` +`plugin permissions get` 返回待确认状态时,默认、JSON 和 YAML 输出会包含可选的 WebUI 扩展页面地址和目标 Workspace。可使用有权管理插件的账户在该页面审阅权限,也可使用 `plugin permissions confirm`。审批后再次运行命令核对状态。 + 默认确认全部声明权限,也可逐项指定;compat 模式始终提交完整权限集合。Core 按 API Key 自身权限逐项校验,不能提权。`--confirm-permissions` 隐含等待安装成功、确认权限并核验启用状态,需要 `plugin.permissions.confirm_api_key` 和 `operation.invoke`;安装失败不继续确认。本地和 GitHub 安装从包内 manifest 确定插件身份,不把文件名或仓库名当作插件名;GitHub 确认会下载选定的公开 release asset 检查 manifest,不向 GitHub 发送 Workspace Key。 ### Core 权限边界 diff --git a/internal/app/plugin_permissions.go b/internal/app/plugin_permissions.go index a3ab5d9..f1869c5 100644 --- a/internal/app/plugin_permissions.go +++ b/internal/app/plugin_permissions.go @@ -18,6 +18,14 @@ import ( "go.yaml.in/yaml/v3" ) +func (session *OperationSession) PluginApprovalHint() map[string]any { + return map[string]any{ + "url": strings.TrimRight(session.Target.Endpoint, "/") + "/home/extensions", + "workspace_uuid": session.preflight.Identity.WorkspaceUUID, + "message": i18n.T("Review permissions in this Workspace's WebUI, or use lbctl plugin permissions confirm. Run this command again to check the resulting state."), + } +} + func (session *OperationSession) ConfirmPlugin(ctx context.Context, author, name string, capabilities, operations []string) (any, error) { if session.Target.UserToken == "" && !session.preflight.Capabilities.Operations["plugin.permissions.confirm_api_key"] { return nil, result.New("incompatible", i18n.T("Core does not yet support confirming plugin permissions with an API Key")) diff --git a/internal/command/operations.go b/internal/command/operations.go index 375e412..7a9b57d 100644 --- a/internal/command/operations.go +++ b/internal/command/operations.go @@ -434,6 +434,11 @@ func addOperationCommand(parent *cobra.Command, spec operationCommand, service * return session.Result(map[string]any{"completed": err == nil}), err } value, err := session.Client.OperationCall(cmd.Context(), session.Target, identifier, body) + if err == nil && identifier == "plugin.permissions.get" { + if permissions, ok := value.(map[string]any); ok && permissions["pending_confirmation"] == true { + permissions["approval"] = session.PluginApprovalHint() + } + } if err == nil && outputField != "" { object, _ := value.(map[string]any) encoded, ok := object[outputField].(string) diff --git a/internal/command/operations_test.go b/internal/command/operations_test.go index 41ecc5e..04a0ea0 100644 --- a/internal/command/operations_test.go +++ b/internal/command/operations_test.go @@ -1770,6 +1770,109 @@ func TestGenericOperationsModesAndConfirmation(t *testing.T) { } } +func TestPluginPermissionApprovalHint(t *testing.T) { + for _, test := range []struct { + name string + pending bool + format string + member bool + generic bool + }{ + {"pending JSON", true, "json", false, false}, + {"pending YAML", true, "yaml", false, false}, + {"pending human", true, "", false, false}, + {"approved JSON", false, "json", false, false}, + {"approved YAML", false, "yaml", false, false}, + {"approved human", false, "", false, false}, + {"member", true, "json", true, false}, + {"generic", true, "json", false, true}, + } { + t.Run(test.name, func(t *testing.T) { + calls := 0 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + switch r.URL.Path { + case "/api/v1/system/context": + fmt.Fprint(w, `{"code":0,"data":{"instance_uuid":"instance","workspace_uuid":"review-workspace","api_key_id":"key","permissions":["resource.view"]}}`) + return + case "/api/v1/workspaces/current": + fmt.Fprint(w, `{"code":0,"data":{"workspace":{"uuid":"review-workspace"},"permissions":["resource.view"]}}`) + return + } + if operationPreflight(w, r, true) { + return + } + if r.URL.Path != "/api/v1/operations/plugin.permissions.get" { + t.Errorf("unexpected request %s %s", r.Method, r.URL.Path) + w.WriteHeader(http.StatusNotFound) + return + } + if r.Method == http.MethodGet { + fmt.Fprint(w, `{"id":"plugin.permissions.get","permission":"resource.view","risk":"read","streaming":false}`) + return + } + if r.Method != http.MethodPost { + t.Errorf("unexpected method %s", r.Method) + } + calls++ + fmt.Fprintf(w, `{"code":0,"data":{"pending_confirmation":%t,"enabled":%t,"can_confirm":true,"permissions":[{"id":"bot.list","granted":false}]}}`, test.pending, !test.pending) + })) + defer server.Close() + args := []string{"plugin", "permissions", "get", "author", "name"} + if test.generic { + args = []string{"op", "call", "plugin.permissions.get", "--file", "-"} + } + if test.member { + args = append(args, "--as-member") + } + configPath := filepath.Join(t.TempDir(), "config.yaml") + configuration := fmt.Sprintf("version: 1\ncurrent_context: test\ncontexts:\n test:\n endpoint: %s/\n credential:\n type: env\n name: TEST_KEY\n", server.URL) + if err := os.WriteFile(configPath, []byte(configuration), 0600); err != nil { + t.Fatal(err) + } + prefix := []string{"--config", configPath} + if test.format != "" { + prefix = append(prefix, "-o", test.format) + } + var buffer bytes.Buffer + code := executeChinese(context.Background(), append(prefix, args...), Dependencies{ + In: strings.NewReader(`{"author":"author","plugin_name":"name"}`), Out: &buffer, Err: &buffer, + LookupEnv: func(name string) (string, bool) { + return "test-secret", name == "TEST_KEY" || name == "LANGBOT_USER_TOKEN" + }, + }) + output := buffer.String() + if code != 0 || calls != 1 || strings.Contains(output, "test-secret") { + t.Fatalf("code=%d calls=%d output=%s", code, calls, output) + } + if !test.pending { + if strings.Contains(output, "approval") || strings.Contains(output, "/home/extensions") { + t.Fatalf("approved plugin still has an approval hint: %s", output) + } + return + } + if test.format == "" { + if !strings.Contains(output, server.URL+"/home/extensions") || !strings.Contains(output, "review-workspace") || !strings.Contains(output, "plugin permissions confirm") { + t.Fatal(output) + } + return + } + var envelope struct { + Data struct { + Approval map[string]any `json:"approval" yaml:"approval"` + } `json:"data" yaml:"data"` + } + if err := yaml.Unmarshal([]byte(output), &envelope); err != nil { + t.Fatal(err) + } + approval := envelope.Data.Approval + if approval["url"] != server.URL+"/home/extensions" || approval["workspace_uuid"] != "review-workspace" || approval["required_principal"] != nil || !strings.Contains(fmt.Sprint(approval["message"]), "plugin permissions confirm") { + t.Fatalf("unexpected approval hint: %s", output) + } + }) + } +} + func TestPermissionConfirmation(t *testing.T) { for _, compat := range []bool{false, true} { for _, explicit := range []bool{false, true} { diff --git a/internal/i18n/extra.go b/internal/i18n/extra.go index eff2f19..8c8d92a 100644 --- a/internal/i18n/extra.go +++ b/internal/i18n/extra.go @@ -228,6 +228,7 @@ func init() { "Stopped waiting for plugin initialization": "已停止等待插件初始化", "Inspect the installed plugin": "查看已安装插件", "Review plugin permissions": "审阅插件权限", + "Review permissions in this Workspace's WebUI, or use lbctl plugin permissions confirm. Run this command again to check the resulting state.": "可在此 Workspace 的 WebUI 中审阅权限,也可使用 lbctl plugin permissions confirm。随后再次运行此命令核对状态。", "Watch Bot execution states and recorded deliveries": "值守 Bot 执行状态及已记录投递", "Polling interval and request budgets must be positive": "轮询间隔和请求预算必须大于零",