返回 DeepSeek-Reasonix
bash_env_test.go
根目录 / internal / tool / builtin / bash_env_test.go
1 package builtin
2
3 import (
4 "context"
5 "encoding/json"
6 "os"
7 "path/filepath"
8 "runtime"
9 "strings"
10 "testing"
11
12 "reasonix/internal/sandbox"
13 "reasonix/internal/secrets"
14 )
15
16 func TestBashMergesLoginShellPath(t *testing.T) {
17 if runtime.GOOS == "windows" {
18 t.Skip("login shell PATH probing is POSIX-only")
19 }
20
21 dir := t.TempDir()
22 bin := filepath.Join(dir, "bin")
23 if err := os.Mkdir(bin, 0o755); err != nil {
24 t.Fatalf("mkdir bin: %v", err)
25 }
26 probe := filepath.Join(bin, "reasonix-path-probe")
27 if err := os.WriteFile(probe, []byte("#!/bin/sh\nprintf 'shell-path-ok\\n'\n"), 0o755); err != nil {
28 t.Fatalf("write probe: %v", err)
29 }
30
31 t.Setenv("PATH", bin+":/usr/bin:/bin:/usr/sbin:/sbin")
32
33 b := bash{shell: sandbox.Shell{Kind: sandbox.ShellBash, Path: "/bin/sh"}}
34 args, _ := json.Marshal(map[string]string{"command": "reasonix-path-probe"})
35
36 ctx := sandbox.WithPermissionPreset(context.Background(), "danger-full-access")
37 out, err := b.Execute(ctx, args)
38 if err != nil {
39 t.Fatalf("command should resolve through merged login-shell PATH: %v (out=%q)", err, out)
40 }
41 if !strings.Contains(out, "shell-path-ok") {
42 t.Fatalf("output = %q, want shell-path-ok", out)
43 }
44 }
45
46 func TestBashCommandEnvFiltersSensitiveKeysWhenEnabled(t *testing.T) {
47 secrets.SetFilterSubprocessEnv(true)
48 t.Cleanup(func() { secrets.SetFilterSubprocessEnv(false) })
49 t.Setenv("DEEPSEEK_API_KEY", "sk-real-secret-value-123456")
50 t.Setenv("GH_TOKEN", "ghp_abcdefghijklmnopqrstuvwxyz")
51 t.Setenv("REASONIX_TEST_VISIBLE", "ok")
52 // PWD is the POSIX working-directory variable, not a password: the name
53 // filter must never strip it or every subprocess loses its cwd context.
54 t.Setenv("PWD", "/tmp/somewhere")
55
56 env := strings.Join(bashCommandEnv(context.Background()), "\n")
57 if strings.Contains(env, "DEEPSEEK_API_KEY") || strings.Contains(env, "GH_TOKEN") {
58 t.Fatalf("bash env leaked sensitive keys:\n%s", env)
59 }
60 if !strings.Contains(env, "REASONIX_TEST_VISIBLE=ok") {
61 t.Fatalf("bash env dropped non-sensitive key:\n%s", env)
62 }
63 if !strings.Contains(env, "PWD=/tmp/somewhere") {
64 t.Fatalf("bash env dropped PWD:\n%s", env)
65 }
66 }
67
68 func TestBashCommandEnvKeepsTokensByDefault(t *testing.T) {
69 t.Setenv("GH_TOKEN", "ghp_abcdefghijklmnopqrstuvwxyz")
70
71 env := strings.Join(bashCommandEnv(context.Background()), "\n")
72 if !strings.Contains(env, "GH_TOKEN=ghp_abcdefghijklmnopqrstuvwxyz") {
73 t.Fatalf("bash env must inherit tokens while filter_subprocess_env is off (default):\n%s", env)
74 }
75 }
76
77 func TestMergePathLists(t *testing.T) {
78 sep := string(os.PathListSeparator)
79 cases := []struct {
80 name string
81 primary string
82 secondary string
83 want string
84 }{
85 {"dedupes, primary first", "/a" + sep + "/b", "/b" + sep + "/c", "/a" + sep + "/b" + sep + "/c"},
86 {"empty secondary", "/a" + sep + "/b", "", "/a" + sep + "/b"},
87 {"empty primary", "", "/x" + sep + "/y", "/x" + sep + "/y"},
88 {"skips blank entries", "/a" + sep + sep + "/b", "", "/a" + sep + "/b"},
89 }
90 for _, c := range cases {
91 t.Run(c.name, func(t *testing.T) {
92 if got := mergePathLists(c.primary, c.secondary); got != c.want {
93 t.Fatalf("mergePathLists(%q, %q) = %q, want %q", c.primary, c.secondary, got, c.want)
94 }
95 })
96 }
97 }
98
98 lines GO