跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

macos_password_live_test.go

163 行 · 5.7 KB历史下载
1//go:build darwin && arm64
2
3package workbench
4
5import (
6 "bytes"
7 "context"
8 "crypto/sha256"
9 "encoding/hex"
10 "encoding/json"
11 "os"
12 "os/exec"
13 "path/filepath"
14 "strings"
15 "testing"
16 "time"
17
18 "github.com/google/uuid"
19 "github.com/sylvinus/agent-vm/internal/guestos"
20)
21
22// Opt-in only: use the isolated release QA VM, never a personal profile. The
23// source account and its key remain unchanged; guest passwords are restored.
24func TestMacSharedPasswordLive(t *testing.T) {
25 root := os.Getenv("CAGE_MACOS_SHARED_ACCOUNT_QA_ROOT")
26 if root == "" {
27 t.Skip("requires an owned, running macOS release QA VM")
28 }
29 raw, err := os.ReadFile(filepath.Join(root, "ownership.json"))
30 if err != nil {
31 t.Fatal(err)
32 }
33 var ownership struct {
34 Purpose string `json:"purpose"`
35 Existing bool `json:"existing_user_data"`
36 }
37 if json.Unmarshal(raw, &ownership) != nil || ownership.Existing || ownership.Purpose != "Seven release issue fixes and native guest acceptance" {
38 t.Fatal("not the isolated release QA environment")
39 }
40 profile := filepath.Join(root, "profile")
41 original, err := loadLocalAccount(profile)
42 if err != nil {
43 t.Fatal(err)
44 }
45 defer original.lock()
46 password, err := os.ReadFile(filepath.Join(root, "qa-account-password"))
47 if err != nil {
48 t.Fatal(err)
49 }
50 defer clear(password)
51 if err = original.unlock(string(password)); err != nil {
52 t.Fatal(err)
53 }
54 accountBefore, err := os.ReadFile(original.path)
55 if err != nil {
56 t.Fatal(err)
57 }
58 install, err := guestos.LoadInstallation(profile)
59 if err != nil || install == nil {
60 t.Fatal("missing QA installation")
61 }
62 bound, err := guestos.LoadMacOSProfile(profile)
63 if err != nil || bound == nil {
64 t.Fatal("missing QA guest binding")
65 }
66 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
67 defer cancel()
68 previous, err := original.savedMacGuestPassword(ctx, install.ID)
69 if err != nil || previous == "" {
70 t.Fatal("missing encrypted QA credential")
71 }
72 target := `Shared test-'"\$! 2026`
73 fixture, err := loadLocalAccount(t.TempDir())
74 if err != nil {
75 t.Fatal(err)
76 }
77 defer fixture.lock()
78 if err = fixture.create(bound.User, target, ""); err != nil {
79 t.Fatal(err)
80 }
81 if err = fixture.prepareMacAccount(bound.User, target, map[string]macGuestCredential{install.ID.String(): {Username: bound.User, Password: previous}}); err != nil {
82 t.Fatal(err)
83 }
84 kit := t.TempDir()
85 if err = os.Mkdir(filepath.Join(kit, "guest-os"), 0700); err != nil {
86 t.Fatal(err)
87 }
88 helper, err := os.ReadFile(os.Getenv("CAGE_MACOS_SHARED_ACCOUNT_HELPER"))
89 if err != nil {
90 t.Fatal(err)
91 }
92 if err = os.WriteFile(filepath.Join(kit, "guest-os", "cage-finish-initial-setup"), helper, 0700); err != nil {
93 t.Fatal(err)
94 }
95 s := &Server{profile: profile, account: fixture, macosGuest: bound, exe: filepath.Join(kit, "cage")}
96 sentinel := ".cage-shared-account-" + uuid.NewString()
97 cmd := s.macOSSSHCommand(ctx, "-T", "cage-macos", "--", `umask 077; printf preserved > "$HOME/`+sentinel+`"`)
98 if err = cmd.Run(); err != nil {
99 t.Fatal("cannot prepare isolated guest sentinel", err)
100 }
101 defer func() {
102 _ = s.macOSSSHCommand(context.Background(), "-T", "cage-macos", "--", `rm -f "$HOME/`+sentinel+`"`).Run()
103 }()
104 defer func() {
105 restore, cancel := context.WithTimeout(context.Background(), time.Minute)
106 defer cancel()
107 if err := s.applyMacGuestPassword(restore, install.ID, target, previous); err != nil {
108 t.Errorf("restore original QA guest credential: %v", err)
109 }
110 }()
111 if err = s.syncMacGuestPassword(ctx); err != nil {
112 t.Fatal(err)
113 }
114 // Both ordinary login and the temporary authorization user accept Cage's
115 // password. SSH still authenticates by key, so this checks password login too.
116 for _, user := range []string{bound.User, "cagesetup"} {
117 cmd = s.macOSSSHCommand(ctx, "-T", "cage-macos", "--", "/usr/bin/dscl /Local/Default -authonly "+user)
118 cmd.Stdin = strings.NewReader(target + "\n")
119 if err = cmd.Run(); err != nil {
120 t.Fatal("guest account did not accept shared password for", user, err)
121 }
122 }
123 cmd = s.macOSSSHCommand(ctx, "-T", "cage-macos", "--", "/usr/bin/dscl /Local/Default -authonly "+bound.User)
124 cmd.Stdin = strings.NewReader(previous + "\n")
125 if cmd.Run() == nil {
126 t.Fatal("ordinary account still accepted its previous password")
127 }
128 provisionArgs, err := guestos.MacOSSSHArgs(profile, "cageprovision")
129 if err != nil {
130 t.Fatal(err)
131 }
132 check := exec.CommandContext(ctx, sshExecutable(), append(provisionArgs, "-T", "cage-macos", "--", `sudo -S -p '' /usr/bin/shasum -a 256 /etc/kcpassword < /Users/cageprovision/password`)...)
133 actual, err := check.Output()
134 if err != nil {
135 t.Fatal("cannot verify system automatic login record", err)
136 }
137 encoded := append([]byte(target), 0)
138 for len(encoded)%12 != 0 {
139 encoded = append(encoded, 0)
140 }
141 key := []byte{0x7d, 0x89, 0x52, 0x23, 0xd2, 0xbc, 0xdd, 0xea, 0xa3, 0xb9, 0x1f}
142 for i := range encoded {
143 encoded[i] ^= key[i%len(key)]
144 }
145 expected := sha256.Sum256(encoded)
146 clear(encoded)
147 if !strings.HasPrefix(string(actual), hex.EncodeToString(expected[:])+" ") {
148 t.Fatal("system automatic login did not receive the new password")
149 }
150 // Replay the durable old/target pair as if the first reply was lost.
151 if err = s.applyMacGuestPassword(ctx, install.ID, previous, target); err != nil {
152 t.Fatal("idempotent replay:", err)
153 }
154 cmd = s.macOSSSHCommand(ctx, "-T", "cage-macos", "--", `test "$(cat "$HOME/`+sentinel+`")" = preserved && ! id -Gn | /usr/bin/grep -qw admin`)
155 if err = cmd.Run(); err != nil {
156 t.Fatal("guest data or ordinary account privilege changed", err)
157 }
158 accountAfter, err := os.ReadFile(original.path)
159 if err != nil || !bytes.Equal(accountBefore, accountAfter) {
160 t.Fatal("source QA account changed")
161 }
162 t.Log("Shared password accepted by ordinary and setup accounts; old password rejected; automatic login and replay passed; source account and guest sentinel retained")
163}