| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "net" |
| 8 | "net/http" |
| 9 | "net/http/httptest" |
| 10 | "os" |
| 11 | "path/filepath" |
| 12 | "runtime" |
| 13 | "strconv" |
| 14 | "testing" |
| 15 | "time" |
| 16 | ) |
| 17 | |
| 18 | func TestTrustedLocalLifecycle(t *testing.T) { |
| 19 | profile, devices := t.TempDir(), t.TempDir() |
| 20 | a, err := loadLocalAccount(profile) |
| 21 | if err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | if err = a.trustedLogin("create", "", devices); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | original := bytes.Clone(a.key) |
| 28 | a.lock() |
| 29 | a, err = loadLocalAccount(profile) |
| 30 | if err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | if err = a.trustedLogin("resume", "", t.TempDir()); err == nil { |
| 34 | t.Fatal("untrusted OS profile opened workspace") |
| 35 | } |
| 36 | if err = a.trustedLogin("resume", "", devices); err != nil || !bytes.Equal(original, a.key) { |
| 37 | t.Fatal("restart lost workspace", err) |
| 38 | } |
| 39 | if err = a.trustedLogin("protect", "123", devices); err == nil { |
| 40 | t.Fatal("accepted short password") |
| 41 | } |
| 42 | if err = a.trustedLogin("protect", "first-password", devices); err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | if err = a.trustedLogin("create", "", devices); err == nil { |
| 46 | t.Fatal("overwrote password account") |
| 47 | } |
| 48 | a.lock() |
| 49 | if err = a.trustedLogin("resume", "", devices); err == nil { |
| 50 | t.Fatal("stale device grant") |
| 51 | } |
| 52 | if err = a.unlock("first-password"); err != nil || !bytes.Equal(original, a.key) { |
| 53 | t.Fatal("password conversion lost workspace", err) |
| 54 | } |
| 55 | if err = a.trustedLogin("enable", "first-password", devices); err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | a.lock() |
| 59 | if err = a.trustedLogin("resume", "", devices); err != nil || !bytes.Equal(original, a.key) { |
| 60 | t.Fatal("password account failed automatic unlock", err) |
| 61 | } |
| 62 | if err = a.lockAccess(false); err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | if err = a.trustedLogin("resume", "", devices); err == nil { |
| 66 | t.Fatal("ignored explicit lock") |
| 67 | } |
| 68 | if err = a.unlock("first-password"); err != nil { |
| 69 | t.Fatal(err) |
| 70 | } |
| 71 | if err = a.lockAccess(true); err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | a, err = loadLocalAccount(profile) |
| 75 | if err != nil { |
| 76 | t.Fatal(err) |
| 77 | } |
| 78 | if a.view().TrustedLogin || a.trustedLogin("resume", "", devices) == nil { |
| 79 | t.Fatal("logout retained trust") |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func TestTrustedLocalLauncherBoundary(t *testing.T) { |
| 84 | s := testServer(t) |
| 85 | if w := request(s, "POST", "/api/desktop-local/trusted-login", s.origin, `{"action":"resume"}`, true); w.Code != http.StatusUnauthorized { |
| 86 | t.Fatalf("ordinary session reached launcher action: %d", w.Code) |
| 87 | } |
| 88 | if w := request(s, "POST", "/api/account/trusted-login", s.origin, `{}`, true); w.Code != http.StatusNotFound { |
| 89 | t.Fatalf("ordinary account route accepted trusted action: %d", w.Code) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | func TestTrustedPasswordRememberMigrationAndRepair(t *testing.T) { |
| 94 | profile, devices := t.TempDir(), t.TempDir() |
| 95 | a, err := loadLocalAccount(profile) |
| 96 | if err != nil { |
| 97 | t.Fatal(err) |
| 98 | } |
| 99 | if err = a.create("Remember test", "first-password", ""); err != nil { |
| 100 | t.Fatal(err) |
| 101 | } |
| 102 | original := bytes.Clone(a.key) |
| 103 | automatic, err := a.enrollDevice("automatic") |
| 104 | if err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
| 107 | biometric, err := a.enrollDevice("biometric") |
| 108 | if err != nil { |
| 109 | t.Fatal(err) |
| 110 | } |
| 111 | if err = a.trustedLogin("enable", "first-password", devices); err != nil { |
| 112 | t.Fatal(err) |
| 113 | } |
| 114 | if a.record.Devices["automatic"].ID != "" || a.record.Devices["biometric"].ID != biometric.ID { |
| 115 | t.Fatal("migration did not replace only the automatic grant") |
| 116 | } |
| 117 | firstID := a.record.TrustedUnlock.ID |
| 118 | |
| 119 | a.lock() |
| 120 | a, err = loadLocalAccount(profile) |
| 121 | if err != nil { |
| 122 | t.Fatal(err) |
| 123 | } |
| 124 | otherDevice := t.TempDir() |
| 125 | if err = a.trustedLogin("resume", "", otherDevice); err == nil { |
| 126 | t.Fatal("copied account unlocked on a different device") |
| 127 | } |
| 128 | if err = a.unlock("first-password"); err != nil { |
| 129 | t.Fatal(err) |
| 130 | } |
| 131 | |
| 132 | if err = a.trustedLogin("enable", "first-password", otherDevice); err != nil { |
| 133 | t.Fatal(err) |
| 134 | } |
| 135 | if a.record.TrustedUnlock.ID == firstID { |
| 136 | t.Fatal("stale grant was not replaced") |
| 137 | } |
| 138 | previousID := a.record.TrustedUnlock.ID |
| 139 | if err = os.Remove(filepath.Join(otherDevice, previousID+".json")); err != nil { |
| 140 | t.Fatal(err) |
| 141 | } |
| 142 | if err = a.trustedLogin("enable", "first-password", otherDevice); err != nil { |
| 143 | t.Fatal(err) |
| 144 | } |
| 145 | a.lock() |
| 146 | a, err = loadLocalAccount(profile) |
| 147 | if err != nil { |
| 148 | t.Fatal(err) |
| 149 | } |
| 150 | if err = a.trustedLogin("resume", "", otherDevice); err != nil || !bytes.Equal(original, a.key) { |
| 151 | t.Fatal("repaired grant did not survive restart with the original workspace key", err) |
| 152 | } |
| 153 | |
| 154 | |
| 155 | for range 2 { |
| 156 | if _, err = a.enrollDevice("automatic"); err != nil { |
| 157 | t.Fatal(err) |
| 158 | } |
| 159 | if err = a.trustedLogin("disable", "", otherDevice); err != nil { |
| 160 | t.Fatal(err) |
| 161 | } |
| 162 | if a.view().TrustedLogin || a.record.Devices["automatic"].ID != "" { |
| 163 | t.Fatal("opt-out left an automatic login path enabled") |
| 164 | } |
| 165 | } |
| 166 | a.lock() |
| 167 | if err = a.trustedLogin("resume", "", otherDevice); err == nil { |
| 168 | t.Fatal("disabled trust reopened the account") |
| 169 | } |
| 170 | if err = a.unlockDevice("automatic", automatic); err == nil { |
| 171 | t.Fatal("revoked legacy grant reopened the account") |
| 172 | } |
| 173 | if err = a.unlockDevice("biometric", biometric); err != nil || !bytes.Equal(original, a.key) { |
| 174 | t.Fatal("remember opt-out broke biometric unlock or workspace data", err) |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | func TestTrustedLocalUnlockIsPrivateToDesktop(t *testing.T) { |
| 179 | s := testServer(t) |
| 180 | a, err := loadLocalAccount(s.profile) |
| 181 | if err != nil { |
| 182 | t.Fatal(err) |
| 183 | } |
| 184 | s.account = a |
| 185 | if err = a.trustedLogin("create", "", t.TempDir()); err != nil { |
| 186 | t.Fatal(err) |
| 187 | } |
| 188 | if !a.viewFor(true).Unlocked || a.viewFor(false).Unlocked { |
| 189 | t.Fatal("trusted login escaped desktop view") |
| 190 | } |
| 191 | if response := request(s, "GET", "/api/favorites", "", "", true); response.Code != http.StatusLocked { |
| 192 | t.Fatalf("session cookie alone opened favorites: %d", response.Code) |
| 193 | } |
| 194 | if response := request(s, "POST", "/api/account/profile", s.origin, `{"username":"Different"}`, true); response.Code != http.StatusLocked { |
| 195 | t.Fatalf("session cookie alone changed profile: %d", response.Code) |
| 196 | } |
| 197 | req, _ := http.NewRequest("GET", s.origin, nil) |
| 198 | req.AddCookie(&http.Cookie{Name: s.cookieName() + "-desktop", Value: trustedViewProof(s.token)}) |
| 199 | if !s.hasTrustedView(req) { |
| 200 | t.Fatal("local launcher proof was refused") |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | func TestTrustedLocalDesktopEntryAndWorkload(t *testing.T) { |
| 205 | for _, env := range []string{"LOCALAPPDATA", "XDG_CONFIG_HOME", "HOME"} { |
| 206 | t.Setenv(env, t.TempDir()) |
| 207 | } |
| 208 | workload := testServer(t) |
| 209 | var err error |
| 210 | workload.account, err = loadLocalAccount(workload.profile) |
| 211 | if err != nil { |
| 212 | t.Fatal(err) |
| 213 | } |
| 214 | workload.instanceID = randomToken() |
| 215 | server := httptest.NewUnstartedServer(workload.handler()) |
| 216 | port := server.Listener.Addr().(*net.TCPAddr).Port |
| 217 | workload.origin = "http://localhost:" + strconv.Itoa(port) |
| 218 | server.Start() |
| 219 | defer server.Close() |
| 220 | record := controlFile{Port: port, PID: os.Getpid(), Token: workload.token} |
| 221 | if err = writePrivate(filepath.Join(workload.profile, "control.json"), record); err != nil { |
| 222 | t.Fatal(err) |
| 223 | } |
| 224 | c, err := openController(workload.profile) |
| 225 | if err != nil { |
| 226 | t.Fatal(err) |
| 227 | } |
| 228 | defer c.close() |
| 229 | life := t.Context() |
| 230 | bootstrap, cancel := context.WithTimeout(life, 5*time.Second) |
| 231 | defer cancel() |
| 232 | local := &desktopLocal{profile: workload.profile} |
| 233 | session, err := local.localSession(life, bootstrap, c, desktopLocalReply{Identity: workload.identity, InstanceID: workload.instanceID}) |
| 234 | if err != nil { |
| 235 | t.Fatal(err) |
| 236 | } |
| 237 | defer session.Close() |
| 238 | gateway := testServer(t) |
| 239 | gateway.remote = newDesktopGateway(gateway.profile, local) |
| 240 | d := gateway.remote.desktop |
| 241 | d.active = &desktopTarget{id: d.generation, mode: "local", ctx: life, session: session} |
| 242 | body, _ := json.Marshal(trustedLoginRequest{Action: "create", ConnectionID: d.generation}) |
| 243 | w := request(gateway, "POST", "/api/client/trusted-login", gateway.origin, string(body), true) |
| 244 | if w.Code != http.StatusConflict || workload.account.view().Exists { |
| 245 | t.Fatalf("fresh profile bypassed registration: %d %s", w.Code, w.Body.String()) |
| 246 | } |
| 247 | |
| 248 | base, err := os.UserConfigDir() |
| 249 | if runtime.GOOS == "windows" { |
| 250 | base = os.Getenv("LOCALAPPDATA") |
| 251 | } |
| 252 | if err != nil { |
| 253 | t.Fatal(err) |
| 254 | } |
| 255 | if err = workload.account.trustedLogin("create", "", filepath.Join(base, "Cage", "trusted-local")); err != nil { |
| 256 | t.Fatal(err) |
| 257 | } |
| 258 | read := gatewayRequest(gateway, "/api/account", d.generation) |
| 259 | var view accountView |
| 260 | if read.Code != 200 || json.Unmarshal(read.Body.Bytes(), &view) != nil || !view.Unlocked || !view.TrustedLocal { |
| 261 | t.Fatalf("local gateway cannot see unlocked account: %s", read.Body.String()) |
| 262 | } |
| 263 | workload.account.lock() |
| 264 | body, _ = json.Marshal(trustedLoginRequest{Action: "resume", ConnectionID: d.generation}) |
| 265 | w = request(gateway, "POST", "/api/client/trusted-login", gateway.origin, string(body), true) |
| 266 | if w.Code != http.StatusOK || !workload.account.view().Unlocked { |
| 267 | t.Fatalf("local resume failed: %d %s", w.Code, w.Body.String()) |
| 268 | } |
| 269 | |
| 270 | for _, action := range []string{"protect", "enable"} { |
| 271 | body, _ = json.Marshal(trustedLoginRequest{Action: action, Password: "first-password", ConnectionID: d.generation}) |
| 272 | w = request(gateway, "POST", "/api/client/trusted-login", gateway.origin, string(body), true) |
| 273 | if w.Code != http.StatusOK { |
| 274 | t.Fatalf("password account %s: %d %s", action, w.Code, w.Body.String()) |
| 275 | } |
| 276 | } |
| 277 | workload.account.lock() |
| 278 | workload.account, err = loadLocalAccount(workload.profile) |
| 279 | if err != nil { |
| 280 | t.Fatal(err) |
| 281 | } |
| 282 | body, _ = json.Marshal(trustedLoginRequest{Action: "resume", ConnectionID: d.generation}) |
| 283 | w = request(gateway, "POST", "/api/client/trusted-login", gateway.origin, string(body), true) |
| 284 | if w.Code != http.StatusOK || !workload.account.viewFor(true).Unlocked || workload.account.viewFor(false).Unlocked { |
| 285 | t.Fatalf("password account did not resume privately after reload: %d %s", w.Code, w.Body.String()) |
| 286 | } |
| 287 | } |