server/browser_password_test.go (4635B)
1 package main 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "net/http" 7 "net/http/httptest" 8 "reflect" 9 "strings" 10 "testing" 11 ) 12 13 func TestBrowserPasswordChangePreservesSessionAndSurvivesRestart(t *testing.T) { 14 for _, scenario := range []string{"adopted", "created"} { 15 t.Run(scenario, func(t *testing.T) { 16 world := newTestServer(t) 17 visitor := accountVisitor(t, world) 18 var account *httptest.ResponseRecorder 19 id, oldPassword, newPassword := uint32(1), "old password", "valid" 20 if scenario == "adopted" { 21 account = accountRequest(world, visitor, "claim", "Alice", oldPassword) 22 } else { 23 account = creationRequest(world, visitor, creationForm()) 24 id, oldPassword, newPassword = 2, "test password", "new password" 25 } 26 if account.Code != http.StatusNoContent { 27 t.Fatal(account.Code, account.Body) 28 } 29 cookie := account.Result().Cookies()[0] 30 before := *world.characters[id] 31 conn := &combatInterestConn{} 32 client := &worldClient{id: id, ready: true, conn: conn} 33 world.clients[id] = client 34 var oldSalt []byte 35 if err := world.db.QueryRow("SELECT password_salt FROM characters WHERE id=?", id).Scan(&oldSalt); err != nil { 36 t.Fatal(err) 37 } 38 39 // A submitted name must never select a different account. 40 response := accountRequest(world, cookie, "password", world.characters[1].name, newPassword) 41 42 if response.Code != http.StatusNoContent || len(response.Result().Cookies()) != 0 || response.Header().Get("Cache-Control") != "no-store" || 43 !reflect.DeepEqual(before, *world.characters[id]) || world.clients[id] != client || !client.ready || len(conn.packets) != 0 { 44 t.Fatal("password change altered the character or live session", response.Code, response.Body) 45 } 46 var salt, hash []byte 47 if err := world.db.QueryRow("SELECT password_salt,password_hash FROM characters WHERE id=?", id).Scan(&salt, &hash); err != nil { 48 t.Fatal(err) 49 } 50 if len(salt) != 16 || bytes.Equal(salt, oldSalt) || !bytes.Equal(hash, world.browserPassword(newPassword, salt)) { 51 t.Fatal("new password did not receive a fresh salted hash") 52 } 53 request := httptest.NewRequest("GET", "/browser-account", nil) 54 request.AddCookie(cookie) 55 info := httptest.NewRecorder() 56 world.httpHandler().ServeHTTP(info, request) 57 var state struct{ Adopted, Visitor bool } 58 if err := json.Unmarshal(info.Body.Bytes(), &state); err != nil || !state.Adopted || state.Visitor { 59 t.Fatal("changed account lost password eligibility", info.Code, info.Body) 60 } 61 62 restored := newTestInventoryServer(t) 63 restored.baseURL = "/" 64 if restored.browserCharacter(request) == nil { 65 t.Fatal("existing browser credential did not survive restart") 66 } 67 other := accountVisitor(t, restored) 68 old := accountRequest(restored, other, "login", "Alice", oldPassword) 69 current := accountRequest(restored, other, "login", "Alice", newPassword) 70 if old.Code != http.StatusUnauthorized || current.Code != http.StatusNoContent { 71 t.Fatal("saved password did not govern subsequent logins", old.Code, current.Code) 72 } 73 }) 74 } 75 } 76 77 func TestBrowserPasswordChangeRejectsVisitorsAndUnauthenticatedRequests(t *testing.T) { 78 useNPCData(t, testNPCData()) 79 world := newTestServer(t) 80 visitor := accountVisitor(t, world) 81 before := *world.characters[1] 82 83 unclaimed := accountRequest(world, visitor, "password", "Alice", "secret") 84 anonymous := accountRequest(world, nil, "password", "Alice", "secret") 85 stale := accountRequest(world, &http.Cookie{Name: "ao_client", Value: "expired"}, "password", "Alice", "secret") 86 oversized := accountRequest(world, visitor, "password", "Alice", strings.Repeat("x", 1025)) 87 request := httptest.NewRequest("POST", "https://example.test/browser-account/password", strings.NewReader("password=secret")) 88 request.Header.Set("Content-Type", "application/x-www-form-urlencoded") 89 request.Header.Set("Origin", "https://unrelated.example") 90 request.AddCookie(visitor) 91 crossOrigin := httptest.NewRecorder() 92 world.httpHandler().ServeHTTP(crossOrigin, request) 93 94 if unclaimed.Code != http.StatusConflict || anonymous.Code != http.StatusUnauthorized || stale.Code != http.StatusUnauthorized || 95 oversized.Code != http.StatusBadRequest || crossOrigin.Code != http.StatusForbidden || !reflect.DeepEqual(before, *world.characters[1]) { 96 t.Fatal("invalid password request accepted", unclaimed.Code, anonymous.Code, stale.Code, oversized.Code, crossOrigin.Code) 97 } 98 var length int 99 if err := world.db.QueryRow("SELECT length(password_hash) FROM characters WHERE id=1").Scan(&length); err != nil || length != 0 { 100 t.Fatal("invalid password request adopted the visitor", err, length) 101 } 102 }