| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | "time" |
| 14 | ) |
| 15 | |
| 16 | func TestVaultEncryptionLockReopenPasswordChangeAndCorruption(t *testing.T) { |
| 17 | a := newTestAccount(t) |
| 18 | ctx := context.Background() |
| 19 | err := a.withVault(ctx, true, func(db *vaultData) error { |
| 20 | return db.save(vaultEntry{Origin: "https://mail.example.com/login", Username: "[email protected]", Password: "secret-mail-password"}) |
| 21 | }) |
| 22 | if err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | path := filepath.Join(filepath.Dir(a.path), "password-vault.json") |
| 26 | original, err := os.ReadFile(path) |
| 27 | if err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | for _, plain := range []string{"[email protected]", "secret-mail-password", "mail.example.com"} { |
| 31 | if bytes.Contains(original, []byte(plain)) { |
| 32 | t.Fatal("plaintext persisted") |
| 33 | } |
| 34 | } |
| 35 | if err = a.changePassword("initial-test-password", "changed-account-password"); err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | oldLease := a.vaultLease |
| 39 | a.lock() |
| 40 | if err = a.withVault(ctx, false, func(*vaultData) error { t.Fatal("locked callback executed"); return nil }); err == nil { |
| 41 | t.Fatal("locked read accepted") |
| 42 | } |
| 43 | reopened, err := loadLocalAccount(filepath.Dir(a.path)) |
| 44 | if err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | defer reopened.lock() |
| 48 | if err = reopened.unlock("changed-account-password"); err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | if reopened.vaultLease == oldLease { |
| 52 | t.Fatal("unlock reused browser lease") |
| 53 | } |
| 54 | err = reopened.withVault(ctx, false, func(db *vaultData) error { |
| 55 | if len(db.Entries) != 1 || db.Entries[0].Password != "secret-mail-password" || db.Entries[0].Origin != "https://mail.example.com" { |
| 56 | t.Fatalf("data lost after password change/reopen: count %d", len(db.Entries)) |
| 57 | } |
| 58 | return nil |
| 59 | }) |
| 60 | if err != nil { |
| 61 | t.Fatal(err) |
| 62 | } |
| 63 | var envelope vaultEnvelope |
| 64 | if json.Unmarshal(original, &envelope) != nil { |
| 65 | t.Fatal("invalid envelope") |
| 66 | } |
| 67 | envelope.Data[0] ^= 1 |
| 68 | corrupted, _ := json.Marshal(envelope) |
| 69 | if err = os.WriteFile(path, corrupted, 0600); err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | if err = reopened.withVault(ctx, true, func(db *vaultData) error { return db.save(vaultEntry{Origin: "https://new.example", Password: "new"}) }); err == nil { |
| 73 | t.Fatal("corrupt vault overwritten") |
| 74 | } |
| 75 | after, _ := os.ReadFile(path) |
| 76 | if !bytes.Equal(corrupted, after) { |
| 77 | t.Fatal("corrupt vault changed") |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func TestVaultAPIReauthenticationAndIsolation(t *testing.T) { |
| 82 | s := testServer(t) |
| 83 | s.account = newTestAccount(t) |
| 84 | save := `{"action":"save","entry":{"origin":"https://mail.example.com","username":"[email protected]","password":"mail-secret"}}` |
| 85 | w := request(s, "POST", "/api/vault", "https://evil.example", save, true) |
| 86 | if w.Code != 403 { |
| 87 | t.Fatalf("cross origin accepted: %d", w.Code) |
| 88 | } |
| 89 | w = request(s, "POST", "/api/vault", s.origin, save, true) |
| 90 | if w.Code != 200 { |
| 91 | t.Fatal(w.Code, w.Body.String()) |
| 92 | } |
| 93 | w = request(s, "GET", "/api/vault", s.origin, "", true) |
| 94 | if w.Code != 200 || strings.Contains(w.Body.String(), "mail-secret") { |
| 95 | t.Fatal("list leaks password", w.Code) |
| 96 | } |
| 97 | var db vaultData |
| 98 | if json.Unmarshal(w.Body.Bytes(), &db) != nil || len(db.Entries) != 1 { |
| 99 | t.Fatal("missing saved entry") |
| 100 | } |
| 101 | reveal := func(password string) *httptest.ResponseRecorder { |
| 102 | b, _ := json.Marshal(map[string]string{"action": "reveal", "id": db.Entries[0].ID, "account_password": password}) |
| 103 | return request(s, "POST", "/api/vault", s.origin, string(b), true) |
| 104 | } |
| 105 | if w = reveal("wrong"); w.Code == 200 || strings.Contains(w.Body.String(), "mail-secret") { |
| 106 | t.Fatal("wrong password revealed") |
| 107 | } |
| 108 | s.account.nextAttempt = time.Time{} |
| 109 | |
| 110 | if w = reveal("initial-test-password"); w.Code != 200 { |
| 111 | t.Fatal(w.Code, w.Body.String()) |
| 112 | } |
| 113 | if !strings.Contains(w.Body.String(), "mail-secret") { |
| 114 | t.Fatal("verified reveal missing") |
| 115 | } |
| 116 | s.account.lock() |
| 117 | for _, path := range []string{"/api/vault", "/api/vault/browser"} { |
| 118 | if w = request(s, "POST", path, s.origin, save, true); w.Code != http.StatusLocked { |
| 119 | t.Fatal("locked endpoint accepted", path, w.Code) |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | func TestVaultBrowserNeverExposesCandidatePasswordAndUsesStoredOrigin(t *testing.T) { |
| 125 | s := testServer(t) |
| 126 | s.ready, s.credentials.Token = true, "vault-guest-test" |
| 127 | var sent vaultBrowserInput |
| 128 | guest := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 129 | if r.URL.Path != "/vault" || r.Header.Get("Authorization") != "Bearer vault-guest-test" { |
| 130 | t.Error("wrong guest transport") |
| 131 | } |
| 132 | if json.NewDecoder(r.Body).Decode(&sent) != nil { |
| 133 | t.Error("invalid body") |
| 134 | } |
| 135 | switch sent.Action { |
| 136 | case "status": |
| 137 | jsonReply(w, vaultBrowserState{Available: true, Origin: "https://mail.example.com", Document: "doc", Candidates: []vaultCandidate{{ID: "candidate", Origin: "https://mail.example.com", Username: "alice", Password: "site-secret"}}}) |
| 138 | case "candidate": |
| 139 | jsonReply(w, vaultCandidate{ID: "candidate", Origin: "https://mail.example.com", Username: "alice", Password: "site-secret"}) |
| 140 | default: |
| 141 | jsonReply(w, map[string]bool{"ok": true}) |
| 142 | } |
| 143 | })) |
| 144 | defer guest.Close() |
| 145 | s.browserControl = localBrowserClient(t, s.lifetimeContext(), guest) |
| 146 | w := request(s, "POST", "/api/vault/browser", s.origin, `{"action":"status","tab":"tab"}`, true) |
| 147 | if w.Code != 200 || strings.Contains(w.Body.String(), "site-secret") { |
| 148 | t.Fatal("candidate leaked", w.Code) |
| 149 | } |
| 150 | w = request(s, "POST", "/api/vault/browser", s.origin, `{"action":"save","candidate":"candidate"}`, true) |
| 151 | if w.Code != 200 { |
| 152 | t.Fatal(w.Code, w.Body.String()) |
| 153 | } |
| 154 | w = request(s, "GET", "/api/vault", s.origin, "", true) |
| 155 | var db vaultData |
| 156 | if json.Unmarshal(w.Body.Bytes(), &db) != nil || len(db.Entries) != 1 { |
| 157 | t.Fatal("candidate not saved") |
| 158 | } |
| 159 | b, _ := json.Marshal(map[string]string{"action": "fill", "id": db.Entries[0].ID, "tab": "tab", "document": "doc", "origin": "https://evil.example", "password": "injected", "lease": "forged"}) |
| 160 | w = request(s, "POST", "/api/vault/browser", s.origin, string(b), true) |
| 161 | if w.Code != 200 || sent.Origin != "https://mail.example.com" || sent.Password != "site-secret" || sent.Lease != s.account.vaultLease { |
| 162 | t.Fatal("fill did not use stored credential") |
| 163 | } |
| 164 | w = request(s, "POST", "/api/vault/browser", s.origin, `{"action":"status","tab":"tab"}`, true) |
| 165 | var state vaultBrowserState |
| 166 | if json.Unmarshal(w.Body.Bytes(), &state) != nil || len(state.Candidates) != 0 { |
| 167 | t.Fatal("identical credential prompted again") |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | func TestVaultOriginAndDuplicateUpdate(t *testing.T) { |
| 172 | db := vaultData{} |
| 173 | for _, raw := range []string{"http://mail.example.com", "https://user:[email protected]", "file:///test", "https://"} { |
| 174 | if db.save(vaultEntry{Origin: raw, Password: "secret"}) == nil { |
| 175 | t.Fatal("invalid origin accepted", raw) |
| 176 | } |
| 177 | } |
| 178 | if err := db.save(vaultEntry{Origin: "https://MAIL.example.com:443/login", Username: "alice", Password: "old"}); err != nil { |
| 179 | t.Fatal(err) |
| 180 | } |
| 181 | if db.save(vaultEntry{Origin: "https://mail.example.com", Username: "alice", Password: "duplicate"}) == nil { |
| 182 | t.Fatal("duplicate accepted") |
| 183 | } |
| 184 | if err := db.save(vaultEntry{ID: db.Entries[0].ID, Origin: "https://mail.example.com", Username: "alice"}); err != nil { |
| 185 | t.Fatal(err) |
| 186 | } |
| 187 | if len(db.Entries) != 1 || db.Entries[0].Password != "old" { |
| 188 | t.Fatal("blank edit lost existing password") |
| 189 | } |
| 190 | if err := db.save(vaultEntry{Origin: "https://mail.example.com", Username: "bob", Password: "other"}); err != nil { |
| 191 | t.Fatal(err) |
| 192 | } |
| 193 | } |