Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

refactor(notification): extract wecomCLIEnv and wecomResultCode from CLIProvider.call (#335) (#336)

fx-kurumi577a43a1d0fa
diff --git a/internal/notification/provider.go b/internal/notification/provider.go
index b1f219ec..75448cb2 100644
--- a/internal/notification/provider.go
+++ b/internal/notification/provider.go
@@ -103,12 +103,7 @@ func (p *CLIProvider) call(ctx context.Context, botID, secret string, args ...st
return nil, err
}
cmd := exec.CommandContext(ctx, p.Binary, args...)
- for _, name := range []string{"PATH", "HOME", "TMPDIR", "LANG", "LC_ALL", "HTTPS_PROXY", "HTTP_PROXY", "NO_PROXY", "SSL_CERT_FILE", "SSL_CERT_DIR"} {
- if value, ok := os.LookupEnv(name); ok {
- cmd.Env = append(cmd.Env, name+"="+value)
- }
- }
- cmd.Env = append(cmd.Env, "WECOM_CLI_ACCESS_TOKEN="+token, "WECOM_CLI_CONFIG_DIR="+dir)
+ cmd.Env = wecomCLIEnv(token, dir)
var out boundedBuffer
cmd.Stdout = &out
cmd.Stderr = io.Discard
@@ -119,23 +114,15 @@ func (p *CLIProvider) call(ctx context.Context, botID, secret string, args ...st
if errors.Is(err, exec.ErrNotFound) {
return nil, errors.New("通知服务未安装 wecom-cli,请联系管理员")
}
- var result struct {
- Code int `json:"errcode"`
- Error *struct {
- Code int `json:"code"`
- } `json:"error"`
- }
- if json.Unmarshal(out.Bytes(), &result) != nil {
- return nil, errors.New("企业微信 CLI 响应格式异常")
+ code, parseErr := wecomResultCode(out.Bytes())
+ if parseErr != nil {
+ return nil, parseErr
}
- if result.Error != nil {
- result.Code = result.Error.Code
- }
- if result.Code == 853004 && attempt == 0 {
+ if code == 853004 && attempt == 0 {
continue
}
- if result.Code != 0 {
- return nil, failure(result.Code)
+ if code != 0 {
+ return nil, failure(code)
}
if err != nil {
return nil, errors.New("企业微信 CLI 调用失败")
@@ -145,6 +132,36 @@ func (p *CLIProvider) call(ctx context.Context, botID, secret string, args ...st
return nil, failure(853004)
}
+// wecomCLIEnv builds the isolated wecom-cli environment: only a whitelist of
+// host variables is inherited, plus the per-call access token and config dir.
+func wecomCLIEnv(token, dir string) []string {
+ var env []string
+ for _, name := range []string{"PATH", "HOME", "TMPDIR", "LANG", "LC_ALL", "HTTPS_PROXY", "HTTP_PROXY", "NO_PROXY", "SSL_CERT_FILE", "SSL_CERT_DIR"} {
+ if value, ok := os.LookupEnv(name); ok {
+ env = append(env, name+"="+value)
+ }
+ }
+ return append(env, "WECOM_CLI_ACCESS_TOKEN="+token, "WECOM_CLI_CONFIG_DIR="+dir)
+}
+
+// wecomResultCode extracts the WeCom error code from wecom-cli output; a
+// nested error.code takes precedence over the top-level errcode.
+func wecomResultCode(raw []byte) (int, error) {
+ var result struct {
+ Code int `json:"errcode"`
+ Error *struct {
+ Code int `json:"code"`
+ } `json:"error"`
+ }
+ if json.Unmarshal(raw, &result) != nil {
+ return 0, errors.New("企业微信 CLI 响应格式异常")
+ }
+ if result.Error != nil {
+ return result.Error.Code, nil
+ }
+ return result.Code, nil
+}
+
type boundedBuffer struct{ bytes.Buffer }
func (b *boundedBuffer) Write(p []byte) (int, error) {
diff --git a/internal/notification/provider_helpers_test.go b/internal/notification/provider_helpers_test.go
new file mode 100644
index 00000000..c6c5b883
--- /dev/null
+++ b/internal/notification/provider_helpers_test.go
@@ -0,0 +1,57 @@
+package notification
+
+import (
+ "slices"
+ "strings"
+ "testing"
+)
+
+func TestWecomCLIEnvWhitelistsHostVariables(t *testing.T) {
+ t.Setenv("PATH", "/usr/bin")
+ t.Setenv("HTTPS_PROXY", "http://proxy:8080")
+ t.Setenv("AGENTFLOW_SECRET_SHOULD_NOT_LEAK", "secret")
+ env := wecomCLIEnv("tok", "/tmp/cfg")
+ for _, want := range []string{"PATH=/usr/bin", "HTTPS_PROXY=http://proxy:8080", "WECOM_CLI_ACCESS_TOKEN=tok", "WECOM_CLI_CONFIG_DIR=/tmp/cfg"} {
+ if !slices.Contains(env, want) {
+ t.Fatalf("env missing %q: %v", want, env)
+ }
+ }
+ for _, item := range env {
+ if strings.HasPrefix(item, "AGENTFLOW_SECRET_SHOULD_NOT_LEAK=") {
+ t.Fatalf("non-whitelisted variable leaked: %v", env)
+ }
+ }
+ if n := len(env); env[n-2] != "WECOM_CLI_ACCESS_TOKEN=tok" || env[n-1] != "WECOM_CLI_CONFIG_DIR=/tmp/cfg" {
+ t.Fatalf("token/config dir must be appended last: %v", env)
+ }
+}
+
+func TestWecomResultCode(t *testing.T) {
+ cases := []struct {
+ name string
+ raw string
+ code int
+ wantErr bool
+ }{
+ {name: "success", raw: `{"errcode":0,"success":true}`, code: 0},
+ {name: "top level errcode", raw: `{"errcode":850003}`, code: 850003},
+ {name: "nested error wins", raw: `{"errcode":1,"error":{"code":853004}}`, code: 853004},
+ {name: "nested zero overrides top level", raw: `{"errcode":7,"error":{"code":0}}`, code: 0},
+ {name: "malformed", raw: `not json`, wantErr: true},
+ {name: "empty", raw: ``, wantErr: true},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ code, err := wecomResultCode([]byte(tc.raw))
+ if tc.wantErr {
+ if err == nil || err.Error() != "企业微信 CLI 响应格式异常" {
+ t.Fatalf("want format error, got code=%d err=%v", code, err)
+ }
+ return
+ }
+ if err != nil || code != tc.code {
+ t.Fatalf("code=%d err=%v, want %d", code, err, tc.code)
+ }
+ })
+ }
+}