| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "net/http" |
| 9 | "net/http/httptest" |
| 10 | "os" |
| 11 | "path/filepath" |
| 12 | "testing" |
| 13 | |
| 14 | "github.com/google/uuid" |
| 15 | ) |
| 16 | |
| 17 | func TestMacSharedAccountHTTPRegistration(t *testing.T) { |
| 18 | s := testServer(t) |
| 19 | if !s.macAccountHost() { |
| 20 | t.Skip("requires a Mac host") |
| 21 | } |
| 22 | a, err := loadLocalAccount(t.TempDir()) |
| 23 | if err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | defer a.lock() |
| 27 | s.account = a |
| 28 | w := request(s, "POST", "/api/account/create", s.origin, `{"username":"unsupported name","password":"fixture-password","confirm_password":"fixture-password"}`, true) |
| 29 | if w.Code != http.StatusBadRequest || a.view().Exists { |
| 30 | t.Fatalf("invalid registration: %d", w.Code) |
| 31 | } |
| 32 | w = request(s, "POST", "/api/account/create", s.origin, `{"username":"http_qa","password":"fixture-password","confirm_password":"fixture-password"}`, true) |
| 33 | var view accountView |
| 34 | if w.Code != http.StatusOK || json.Unmarshal(w.Body.Bytes(), &view) != nil || !view.MacOSCredentialsReady || view.Username != "http_qa" { |
| 35 | t.Fatalf("registration did not prepare shared credentials: %d %s", w.Code, w.Body.String()) |
| 36 | } |
| 37 | guest, err := a.macGuestCredentials(context.Background(), uuid.New()) |
| 38 | if err != nil || guest.Username != view.Username || guest.Password != "fixture-password" { |
| 39 | t.Fatal("installer did not receive registered account", err) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func newSharedMacAccount(t *testing.T) *localAccount { |
| 44 | t.Helper() |
| 45 | a, err := loadLocalAccount(t.TempDir()) |
| 46 | if err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | if err = a.createAccount("shihao", "initial-test-password", "", true); err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | t.Cleanup(a.lock) |
| 53 | return a |
| 54 | } |
| 55 | |
| 56 | func TestMacGuestCredentialEncryptionResumeAndIsolation(t *testing.T) { |
| 57 | a := newSharedMacAccount(t) |
| 58 | ctx := context.Background() |
| 59 | id := uuid.New() |
| 60 | guest, err := a.macGuestCredentials(ctx, id) |
| 61 | if err != nil || guest.Username != "shihao" || guest.Password != "initial-test-password" { |
| 62 | t.Fatalf("shared account: %#v %v", guest, err) |
| 63 | } |
| 64 | raw, _ := os.ReadFile(a.path) |
| 65 | if bytes.Contains(raw, []byte(guest.Password)) { |
| 66 | t.Fatal("plaintext password persisted") |
| 67 | } |
| 68 | a.lock() |
| 69 | if err = a.unlock(guest.Password); err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | resumed, err := a.macGuestCredentials(ctx, id) |
| 73 | if err != nil || resumed != guest { |
| 74 | t.Fatal("retry changed installation credentials", err) |
| 75 | } |
| 76 | other, err := a.macGuestCredentials(ctx, uuid.New()) |
| 77 | if err != nil || other != guest { |
| 78 | t.Fatal("new installation did not reuse Cage credentials", err) |
| 79 | } |
| 80 | s := &Server{account: a} |
| 81 | w := httptest.NewRecorder() |
| 82 | s.vaultList(w, httptest.NewRequest("GET", "/api/vault", nil)) |
| 83 | if bytes.Contains(w.Body.Bytes(), []byte(guest.Password)) || bytes.Contains(w.Body.Bytes(), []byte("MacLogin")) { |
| 84 | t.Fatal("website vault exposed shared credentials") |
| 85 | } |
| 86 | if err := a.updateProfile("another", ""); err == nil { |
| 87 | t.Fatal("bound username was renamed") |
| 88 | } |
| 89 | a.lock() |
| 90 | if _, err = a.savedMacGuestPassword(ctx, id); err == nil { |
| 91 | t.Fatal("locked account decrypted guest credentials") |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func TestMacGuestPasswordChangeOfflineAndInterruptedSync(t *testing.T) { |
| 96 | a := newSharedMacAccount(t) |
| 97 | ctx := context.Background() |
| 98 | id := uuid.New() |
| 99 | _, err := a.macGuestCredentials(ctx, id) |
| 100 | if err != nil { |
| 101 | t.Fatal(err) |
| 102 | } |
| 103 | if err = a.changePassword("initial-test-password", "replacement-test-password"); err != nil { |
| 104 | t.Fatal(err) |
| 105 | } |
| 106 | if !a.view().MacOSPasswordPending { |
| 107 | t.Fatal("offline guest update disappeared") |
| 108 | } |
| 109 | |
| 110 | if err = a.changePassword("replacement-test-password", "third-test-password"); err == nil { |
| 111 | t.Fatal("pending target was replaced") |
| 112 | } |
| 113 | path := a.path |
| 114 | a.lock() |
| 115 | a, err = loadLocalAccount(filepath.Dir(path)) |
| 116 | if err != nil { |
| 117 | t.Fatal(err) |
| 118 | } |
| 119 | defer a.lock() |
| 120 | if err = a.unlock("replacement-test-password"); err != nil { |
| 121 | t.Fatal(err) |
| 122 | } |
| 123 | old, _ := a.savedMacGuestPassword(ctx, id) |
| 124 | if old != "initial-test-password" { |
| 125 | t.Fatal("offline guest lost its confirmed password") |
| 126 | } |
| 127 | applied := "initial-test-password" |
| 128 | if err = a.syncMacPassword(ctx, id, "different-user", func(context.Context, string, string) error { t.Fatal("wrong guest account changed"); return nil }); err == nil { |
| 129 | t.Fatal("identity mismatch accepted") |
| 130 | } |
| 131 | |
| 132 | err = a.syncMacPassword(ctx, id, "shihao", func(_ context.Context, old, target string) error { |
| 133 | if old != applied { |
| 134 | t.Fatal("missing original credential") |
| 135 | } |
| 136 | applied = target |
| 137 | return errors.New("lost reply") |
| 138 | }) |
| 139 | if err == nil || !a.view().MacOSPasswordPending { |
| 140 | t.Fatal("partial update was reported complete") |
| 141 | } |
| 142 | err = a.syncMacPassword(ctx, id, "shihao", func(_ context.Context, old, target string) error { |
| 143 | if old != "initial-test-password" || target != applied { |
| 144 | t.Fatal("retry lost original or target credential") |
| 145 | } |
| 146 | return nil |
| 147 | }) |
| 148 | if err != nil || a.view().MacOSPasswordPending { |
| 149 | t.Fatal("retry did not finish", err) |
| 150 | } |
| 151 | saved, _ := a.savedMacGuestPassword(ctx, id) |
| 152 | if saved != applied { |
| 153 | t.Fatal("authorization helper received stale password") |
| 154 | } |
| 155 | if err = a.changePassword(applied, "third-test-password"); err != nil { |
| 156 | t.Fatal("completed sync blocked next change", err) |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | func TestMacGuestPasswordFailedSaveAndAcknowledgement(t *testing.T) { |
| 161 | a := newSharedMacAccount(t) |
| 162 | ctx := context.Background() |
| 163 | id := uuid.New() |
| 164 | if _, err := a.macGuestCredentials(ctx, id); err != nil { |
| 165 | t.Fatal(err) |
| 166 | } |
| 167 | path := a.path |
| 168 | a.path = t.TempDir() |
| 169 | if err := a.changePassword("initial-test-password", "replacement-test-password"); err == nil { |
| 170 | t.Fatal("failed account write accepted") |
| 171 | } |
| 172 | a.path = path |
| 173 | if a.view().MacOSPasswordPending { |
| 174 | t.Fatal("failed account write published new guest target") |
| 175 | } |
| 176 | if err := a.changePassword("initial-test-password", "replacement-test-password"); err != nil { |
| 177 | t.Fatal(err) |
| 178 | } |
| 179 | err := a.syncMacPassword(ctx, id, "shihao", func(context.Context, string, string) error { a.path = t.TempDir(); return nil }) |
| 180 | a.path = path |
| 181 | if err == nil || !a.view().MacOSPasswordPending { |
| 182 | t.Fatal("failed acknowledgement lost retry") |
| 183 | } |
| 184 | if err = a.syncMacPassword(ctx, id, "shihao", func(context.Context, string, string) error { return nil }); err != nil { |
| 185 | t.Fatal(err) |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | func TestMacGuestLegacyCredentialAndAccountAdoption(t *testing.T) { |
| 190 | a := newTestAccount(t) |
| 191 | ctx := context.Background() |
| 192 | id := uuid.New() |
| 193 | if err := a.withVault(ctx, true, func(db *vaultData) error { |
| 194 | db.GuestPasswords = map[string]string{id.String(): "old-guest-password"} |
| 195 | return nil |
| 196 | }); err != nil { |
| 197 | t.Fatal(err) |
| 198 | } |
| 199 | old, err := a.macGuestCredentials(ctx, id) |
| 200 | if err != nil || old.Username != "cage" || old.Password != "old-guest-password" { |
| 201 | t.Fatal("legacy installation credential changed", err) |
| 202 | } |
| 203 | before, _ := a.desktopKey() |
| 204 | if err = a.prepareMacAccount("cage", "initial-test-password", map[string]macGuestCredential{id.String(): old}); err != nil { |
| 205 | t.Fatal(err) |
| 206 | } |
| 207 | after, _ := a.desktopKey() |
| 208 | defer clear(before) |
| 209 | defer clear(after) |
| 210 | if !bytes.Equal(before, after) { |
| 211 | t.Fatal("adoption replaced personal-data encryption key") |
| 212 | } |
| 213 | if err = a.syncMacPassword(ctx, id, "cage", func(_ context.Context, old, target string) error { |
| 214 | if old != "old-guest-password" || target != "initial-test-password" { |
| 215 | t.Fatal("legacy migration lost either password") |
| 216 | } |
| 217 | return nil |
| 218 | }); err != nil { |
| 219 | t.Fatal(err) |
| 220 | } |
| 221 | resumed, err := a.macGuestCredentials(ctx, id) |
| 222 | if err != nil || resumed.Password != "initial-test-password" { |
| 223 | t.Fatal("legacy vault overrode synced password", err) |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | func TestMacAccountRegistrationAndTrustedAdoption(t *testing.T) { |
| 228 | for _, name := range []string{"root", "cagesetup", "cageprovision", "Guest", "测试", "name with spaces"} { |
| 229 | if validMacAccount(name, "fixture-password") == nil { |
| 230 | t.Fatalf("accepted unsupported username %q", name) |
| 231 | } |
| 232 | } |
| 233 | a, err := loadLocalAccount(t.TempDir()) |
| 234 | if err != nil { |
| 235 | t.Fatal(err) |
| 236 | } |
| 237 | defer a.lock() |
| 238 | devices := t.TempDir() |
| 239 | if err = a.trustedLogin("create", "", devices); err != nil { |
| 240 | t.Fatal(err) |
| 241 | } |
| 242 | before, _ := a.desktopKey() |
| 243 | defer clear(before) |
| 244 | if err = a.prepareMacAccount("shihao", "new-test-password", nil); err != nil { |
| 245 | t.Fatal(err) |
| 246 | } |
| 247 | after, _ := a.desktopKey() |
| 248 | defer clear(after) |
| 249 | if !bytes.Equal(before, after) || a.view().TrustedLocal { |
| 250 | t.Fatal("trusted adoption lost data key or retained passwordless account") |
| 251 | } |
| 252 | a.lock() |
| 253 | if err = a.unlock("new-test-password"); err != nil { |
| 254 | t.Fatal(err) |
| 255 | } |
| 256 | if validMacAccount("shihao", "密码不可用于macOS") == nil { |
| 257 | t.Fatal("accepted unenterable guest password") |
| 258 | } |
| 259 | } |