-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix(config): make agent-binding hints workspace-aware and surface user-identity risks #728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package auth | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/larksuite/cli/internal/cmdutil" | ||
| "github.com/larksuite/cli/internal/core" | ||
| ) | ||
|
|
||
| // TestAuthListRun_NotConfigured_ReturnsExitZero pins the contract that | ||
| // `lark-cli auth list` is a read-only probe and must not fail-hard when no | ||
| // config exists yet — scripts and AI agents use it as an idempotent "do I | ||
| // have any users?" check, so the exit code carries semantic weight. Pair | ||
| // that with the existing "configured but no logged-in users" branch (also | ||
| // exit 0) and both empty states are consistent. | ||
| func TestAuthListRun_NotConfigured_ReturnsExitZero(t *testing.T) { | ||
| t.Setenv("LARKSUITE_CLI_CONFIG_DIR", t.TempDir()) | ||
|
|
||
| f, _, stderr, _ := cmdutil.TestFactory(t, nil) | ||
| if err := authListRun(&ListOptions{Factory: f}); err != nil { | ||
| t.Fatalf("auth list should succeed when not configured (exit 0); got: %v", err) | ||
| } | ||
| // Local workspace → hint must mention init, not bind. | ||
| out := stderr.String() | ||
| if !strings.Contains(out, "config init") { | ||
| t.Errorf("local hint missing config init: %s", out) | ||
| } | ||
| if strings.Contains(out, "config bind") { | ||
| t.Errorf("local hint must not mention config bind: %s", out) | ||
| } | ||
| } | ||
|
liangshuo-1 marked this conversation as resolved.
|
||
|
|
||
| // TestAuthListRun_NotConfigured_AgentWorkspace_RoutesToBindHelp covers the | ||
| // reason this hint exists workspace-aware in the first place: an AI agent | ||
| // in OpenClaw / Hermes that probes auth list before binding gets routed to | ||
| // `config bind --help` instead of the local-only `config init`. | ||
| func TestAuthListRun_NotConfigured_AgentWorkspace_RoutesToBindHelp(t *testing.T) { | ||
| t.Setenv("LARKSUITE_CLI_CONFIG_DIR", t.TempDir()) | ||
|
|
||
| prev := core.CurrentWorkspace() | ||
| t.Cleanup(func() { core.SetCurrentWorkspace(prev) }) | ||
| core.SetCurrentWorkspace(core.WorkspaceOpenClaw) | ||
|
|
||
| f, _, stderr, _ := cmdutil.TestFactory(t, nil) | ||
| if err := authListRun(&ListOptions{Factory: f}); err != nil { | ||
| t.Fatalf("auth list should still succeed under agent workspace; got: %v", err) | ||
| } | ||
| out := stderr.String() | ||
| if !strings.Contains(out, "config bind --help") { | ||
| t.Errorf("agent hint must point at config bind --help: %s", out) | ||
| } | ||
| if strings.Contains(out, "config init") { | ||
| t.Errorf("agent hint must not mention config init: %s", out) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package config | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/larksuite/cli/internal/cmdutil" | ||
| ) | ||
|
|
||
| // runHermesBindWithIdentity boots a Hermes-shaped fake env, runs `config bind` | ||
| // with the given identity preset in flag (non-TUI) mode, and returns captured | ||
| // stderr. Hermes is the simplest source to fake (single .env file). | ||
| func runHermesBindWithIdentity(t *testing.T, identity string) string { | ||
| t.Helper() | ||
| saveWorkspace(t) | ||
| configDir := t.TempDir() | ||
| t.Setenv("LARKSUITE_CLI_CONFIG_DIR", configDir) | ||
|
|
||
| hermesHome := t.TempDir() | ||
| t.Setenv("HERMES_HOME", hermesHome) | ||
| envContent := "FEISHU_APP_ID=cli_hermes_abc\nFEISHU_APP_SECRET=hermes_secret_123\nFEISHU_DOMAIN=lark\n" | ||
| if err := os.WriteFile(filepath.Join(hermesHome, ".env"), []byte(envContent), 0600); err != nil { | ||
| t.Fatalf("write .env: %v", err) | ||
| } | ||
|
|
||
| f, _, stderr, _ := cmdutil.TestFactory(t, nil) | ||
| err := configBindRun(&BindOptions{ | ||
| Factory: f, | ||
| Source: "hermes", | ||
| Identity: identity, | ||
| Lang: "zh", | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("bind failed: %v", err) | ||
| } | ||
| return stderr.String() | ||
|
liangshuo-1 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // TestConfigBindRun_UserDefaultIdentity_WarnsAboutImpersonation covers the | ||
| // gap that previously slipped through: a fresh flag-mode bind landing on | ||
| // user-default. warnIdentityEscalation requires a previous bot lock to fire, | ||
| // and IdentityUserDefaultDesc only renders in TUI selection — so without | ||
| // noticeUserDefaultRisk the user/AI never see the impersonation risk on a | ||
| // first-time user-default bind. | ||
| func TestConfigBindRun_UserDefaultIdentity_WarnsAboutImpersonation(t *testing.T) { | ||
| out := runHermesBindWithIdentity(t, "user-default") | ||
| if !strings.Contains(out, bindMsgZh.IdentityEscalationMessage) { | ||
| t.Errorf("user-default bind must surface IdentityEscalationMessage; got: %s", out) | ||
| } | ||
| } | ||
|
|
||
| func TestConfigBindRun_BotOnlyIdentity_NoImpersonationWarning(t *testing.T) { | ||
| out := runHermesBindWithIdentity(t, "bot-only") | ||
| if strings.Contains(out, bindMsgZh.IdentityEscalationMessage) { | ||
| t.Errorf("bot-only bind must NOT warn about impersonation; got: %s", out) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.