mirror of
https://github.com/larksuite/cli.git
synced 2026-07-03 22:24:31 +08:00
* refactor: retire legacy error envelopes and enforce typed contract
Consolidate all command error reporting onto the typed errs.* contract, remove
the legacy error surface that predated it, and tighten the lint guards so the
contract holds across the whole repository going forward.
Every failure now reaches stderr as one envelope shape: a category, an
optional subtype, a human- and agent-readable message, and a recovery hint,
with invalid parameters listed under `params`. The legacy ExitError envelope,
its constructors, and the boundary bridge that promoted untyped config and
authorization errors are deleted, leaving a single path from error to wire.
Predicate commands keep their silent-exit behavior through a dedicated signal
that carries only an exit code.
Infrastructure paths that still emitted ad-hoc envelopes — flag parsing,
unknown commands and subcommands, plugin and policy guards, confirmation
prompts, and auth/config failures — now classify into the same taxonomy.
Business, API, auth, and config exit codes are preserved; the one behavioral
change is that Cobra usage failures (missing required flag, unknown command,
bad arguments) now emit the typed validation envelope and exit 2, matching the
explicit flag and subcommand guards, instead of Cobra's plain-text exit 1.
Enforcement is repo-wide rather than per-path:
- The errscontract guards run by default everywhere instead of through a
migration allowlist, so legacy envelopes cannot be reintroduced anywhere.
- errorlint runs across the whole repository: every error wrap must use %w and
every comparison must use errors.Is/errors.As, so interior wraps stay legal
but can no longer break the chain the typed boundary relies on.
- The errs-no-bare-wrap guard is keyed by structural prefix instead of an
explicit per-domain allowlist, so new shortcut domains are covered without
editing a list. It runs where forbidigo is enabled (the shortcut domains and
the auth/config/service command groups); repo-wide chain integrity for the
remaining command paths is carried by errorlint above.
* test: align cli_e2e success assertions to the ok envelope
The api and service success path now emits the {"ok":true} envelope, so the
cli_e2e workflow assertions that still expected the old {"code":0} shape via
AssertStdoutStatus(t, 0) fail once they run with live credentials. Switch those
workflow assertions to AssertStdoutStatus(t, true); the fake-payload helper test
in core_test.go keeps its code-shape assertion.
95 lines
3.4 KiB
Go
95 lines
3.4 KiB
Go
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package cmdpolicy_test
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/larksuite/cli/errs"
|
|
"github.com/larksuite/cli/extension/platform"
|
|
"github.com/larksuite/cli/internal/cmdpolicy"
|
|
)
|
|
|
|
// The envelope's policy_source must never leak the absolute home path.
|
|
// "yaml:/Users/alice/.lark-cli/policy.yml" would expose Alice's username
|
|
// to any agent or log consumer; the contract is to emit just "yaml" and
|
|
// rely on rule_name (from the yaml's "name:" field) for disambiguation.
|
|
func TestEnvelope_yamlPolicySourceDoesNotLeakHomePath(t *testing.T) {
|
|
root := &cobra.Command{Use: "lark-cli"}
|
|
docs := &cobra.Command{Use: "docs"}
|
|
root.AddCommand(docs)
|
|
leaf := &cobra.Command{Use: "+write", RunE: func(*cobra.Command, []string) error { return nil }}
|
|
docs.AddCommand(leaf)
|
|
|
|
e := cmdpolicy.New(&platform.Rule{
|
|
Name: "my-readonly-rule",
|
|
Allow: []string{"contact/**"}, // docs/* falls outside, denied
|
|
})
|
|
denied := cmdpolicy.BuildDeniedByPath(root, e.EvaluateAll(root),
|
|
cmdpolicy.ResolveSource{
|
|
Kind: cmdpolicy.SourceYAML,
|
|
Name: "/Users/alice/.lark-cli/policy.yml", // simulate an absolute path
|
|
}, "my-readonly-rule")
|
|
|
|
cmdpolicy.Apply(root, denied)
|
|
err := leaf.RunE(leaf, nil)
|
|
|
|
var ve *errs.ValidationError
|
|
if !errors.As(err, &ve) {
|
|
t.Fatalf("expected denial *errs.ValidationError, got %T %v", err, err)
|
|
}
|
|
// The policy source is folded into the Hint as "yaml" -- the bare
|
|
// kind, never the absolute path.
|
|
if !strings.Contains(ve.Hint, "source yaml") {
|
|
t.Errorf("hint must carry policy_source %q (no path leak), got %q", "yaml", ve.Hint)
|
|
}
|
|
// rule_name carries the disambiguating identifier.
|
|
if !strings.Contains(ve.Hint, "my-readonly-rule") {
|
|
t.Errorf("hint must carry rule_name my-readonly-rule, got %q", ve.Hint)
|
|
}
|
|
// Direct privacy probe: the absolute home path must not appear
|
|
// anywhere in the user-facing message OR hint text.
|
|
if strings.Contains(ve.Message, "/Users/alice") {
|
|
t.Errorf("error message must not leak '/Users/alice', got %q", ve.Message)
|
|
}
|
|
if strings.Contains(ve.Hint, "/Users/alice") {
|
|
t.Errorf("error hint must not leak '/Users/alice', got %q", ve.Hint)
|
|
}
|
|
}
|
|
|
|
// Plugin name IS allowed in policy_source because plugins are in-binary
|
|
// and their names are part of the contract (an integrator debugging a
|
|
// denial wants to know which plugin fired). This test pins that intent
|
|
// so a future change does not silently strip the plugin name too.
|
|
func TestEnvelope_pluginPolicySourceCarriesName(t *testing.T) {
|
|
root := &cobra.Command{Use: "lark-cli"}
|
|
leaf := &cobra.Command{Use: "+block", RunE: func(*cobra.Command, []string) error { return nil }}
|
|
root.AddCommand(leaf)
|
|
|
|
e := cmdpolicy.New(&platform.Rule{
|
|
Name: "secaudit-policy",
|
|
Deny: []string{"+block"},
|
|
})
|
|
denied := cmdpolicy.BuildDeniedByPath(root, e.EvaluateAll(root),
|
|
cmdpolicy.ResolveSource{Kind: cmdpolicy.SourcePlugin, Name: "secaudit"},
|
|
"secaudit-policy")
|
|
cmdpolicy.Apply(root, denied)
|
|
|
|
err := leaf.RunE(leaf, nil)
|
|
var ve *errs.ValidationError
|
|
if !errors.As(err, &ve) {
|
|
t.Fatalf("expected *errs.ValidationError, got %T", err)
|
|
}
|
|
// The plugin name IS surfaced (in-binary, part of the contract): it
|
|
// must appear in the Hint so an integrator debugging a denial knows
|
|
// which plugin fired.
|
|
if !strings.Contains(ve.Hint, "plugin:secaudit") {
|
|
t.Errorf("hint must carry policy_source plugin:secaudit, got %q", ve.Hint)
|
|
}
|
|
}
|