aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


commit de1cc738f68dfe6ca150a96fd491feb7484ffc0c
parent ffb8126e6fdfbfc32fe9f74858b76041dd36adf1
Author: anon <anon@example.com>
Date:   Thu,  5 Nov 2026 07:04:42 +0000

Restrict admin password changes to adopted players

Diffstat:
Mserver/admin_player_password.go | 23+++++++++++++++++++----
Mserver/admin_player_password_test.go | 41+++++++++++++++++++++++++++++++++++++++++
Mserver/control.go | 24++++++++++++++++++++++--
3 files changed, 82 insertions(+), 6 deletions(-)

diff --git a/server/admin_player_password.go b/server/admin_player_password.go @@ -24,6 +24,23 @@ func (server *server) changePlayerPassword(w http.ResponseWriter, r *http.Reques name := player.name server.mutex.Unlock() + db, err := server.database() + var adopted bool + if err == nil { + err = db.QueryRow("SELECT length(password_hash)>0 FROM characters WHERE id=?", id).Scan(&adopted) + } + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + if !adopted { + w.Header().Set("Content-Type", "text/html; charset=utf-8") + w.WriteHeader(http.StatusConflict) + server.adminPageStart(w, "Change player password", "Players") + fmt.Fprintf(w, "<section><h1>Change password</h1><p>This player has not been adopted.</p><a href='admin/players/%d'>Back to player</a></section></main></body></html>", id) + return + } + message := "" if r.Method == http.MethodPost { r.Body = http.MaxBytesReader(w, r.Body, 8192) @@ -37,10 +54,8 @@ func (server *server) changePlayerPassword(w http.ResponseWriter, r *http.Reques return } hash := server.browserPassword(r.PostForm.Get("password"), salt) - db, err := server.database() - if err == nil { - _, err = db.Exec("UPDATE characters SET password_salt=?, password_hash=? WHERE id=?", salt, hash, id) - } + var savedID uint32 + err = db.QueryRow("UPDATE characters SET password_salt=?, password_hash=? WHERE id=? AND length(password_hash)>0 RETURNING id", salt, hash, id).Scan(&savedID) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return diff --git a/server/admin_player_password_test.go b/server/admin_player_password_test.go @@ -24,6 +24,7 @@ func TestAdminChangesPlayerPasswordWithoutDisconnecting(t *testing.T) { page := adminRequest(handler, "GET", path, "", cookie) unauthorized := adminRequest(handler, "POST", path, "password=wrong", nil) changed := adminRequest(handler, "POST", path, "password=", cookie) + profile := adminRequest(handler, "GET", "/admin/players/1", "", cookie) var salt, hash []byte if err := world.db.QueryRow("SELECT password_salt,password_hash FROM characters WHERE id=1").Scan(&salt, &hash); err != nil { t.Fatal(err) @@ -35,6 +36,10 @@ func TestAdminChangesPlayerPasswordWithoutDisconnecting(t *testing.T) { if unauthorized.Code != http.StatusUnauthorized || changed.Code != http.StatusSeeOther || changed.Header().Get("Location") != "/admin/players/1?password=saved" { t.Fatal(unauthorized.Code, changed.Code, changed.Body) } + if profile.Code != 200 || !strings.Contains(profile.Body.String(), "<p>Player ID: 1</p><p>Connected: Yes</p><p>Account: Adopted</p>") || + !strings.Contains(profile.Body.String(), "<p><a href='admin/players/1/teleport'>Teleport</a></p><p><a href='admin/players/1/password'>Change password</a></p>") { + t.Fatal("adopted profile did not separate its details and actions", profile.Code, profile.Body) + } if len(salt) != 16 || !bytes.Equal(hash, world.browserPassword("", salt)) || world.clients[1] != client || !client.ready || len(connection.packets) != 0 { t.Fatal("password change did not preserve the live session or store a salted hash") } @@ -49,3 +54,39 @@ func TestAdminChangesPlayerPasswordWithoutDisconnecting(t *testing.T) { t.Fatal("new credential did not survive restart", old.Code, current.Code, current.Body) } } + +func TestAdminCannotSetVisitorPassword(t *testing.T) { + useNPCData(t, testNPCData()) + world := newTestServer(t) + visitor := accountVisitor(t, world) + handler := world.httpHandler() + setup := adminRequest(handler, "POST", "/admin/login", "username=Operator&password=", nil) + cookie := setup.Result().Cookies()[0] + + profile := adminRequest(handler, "GET", "/admin/players/1", "", cookie) + page := adminRequest(handler, "GET", "/admin/players/1/password", "", cookie) + changed := adminRequest(handler, "POST", "/admin/players/1/password", "password=secret", cookie) + blank := adminRequest(handler, "POST", "/admin/players/1/password", "password=", cookie) + + if profile.Code != 200 || !strings.Contains(profile.Body.String(), "<p>Player ID: 1</p><p>Connected: No</p><p>Account: Unadopted visitor</p>") || + strings.Contains(profile.Body.String(), "Change password") { + t.Fatal("visitor profile offered account changes or hid its status", profile.Code, profile.Body) + } + if page.Code != http.StatusConflict || changed.Code != http.StatusConflict || blank.Code != http.StatusConflict || + !strings.Contains(page.Body.String(), "AO Admin") || !strings.Contains(page.Body.String(), "This player has not been adopted.") { + t.Fatal("visitor password route was not rejected with an admin page", page.Code, changed.Code, blank.Code, page.Body) + } + var salt, hash []byte + if err := world.db.QueryRow("SELECT password_salt,password_hash FROM characters WHERE id=1").Scan(&salt, &hash); err != nil { + t.Fatal(err) + } + if len(salt) != 0 || len(hash) != 0 { + t.Fatal("rejected password change adopted the visitor") + } + + // The visitor can still adopt the same character through the normal flow. + claimed := accountRequest(world, visitor, "claim", "Alice", "secret") + if claimed.Code != http.StatusNoContent || world.characters[1].name != "Alice" { + t.Fatal("rejected password change prevented adoption", claimed.Code, claimed.Body) + } +} diff --git a/server/control.go b/server/control.go @@ -29,6 +29,16 @@ func (server *server) userPage(w http.ResponseWriter, r *http.Request) { http.NotFound(w, r) return } + db, err := server.database() + var adopted bool + if err == nil { + err = db.QueryRow("SELECT length(password_hash)>0 FROM characters WHERE id=?", player.id).Scan(&adopted) + } + if err != nil { + server.mutex.Unlock() + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } data := player.userData() connected := server.clients[data.ID] != nil && server.clients[data.ID].ready @@ -47,8 +57,18 @@ func (server *server) userPage(w http.ResponseWriter, r *http.Request) { if r.URL.Query().Get("password") == "saved" { fmt.Fprint(w, "<p role='status'>Password changed.</p>") } - fmt.Fprintf(w, "<h1>%s</h1><p>Player %d. Connected: %t.</p>", html.EscapeString(data.Name), data.ID, connected) - fmt.Fprintf(w, "<p><a href='admin/players/%d/teleport'>Teleport</a> | <a href='admin/players/%d/password'>Change password</a></p>", data.ID, data.ID) + account, connection := "Unadopted visitor", "No" + if adopted { + account = "Adopted" + } + if connected { + connection = "Yes" + } + fmt.Fprintf(w, "<h1>%s</h1><p>Player ID: %d</p><p>Connected: %s</p><p>Account: %s</p>", html.EscapeString(data.Name), data.ID, connection, account) + fmt.Fprintf(w, "<p><a href='admin/players/%d/teleport'>Teleport</a></p>", data.ID) + if adopted { + fmt.Fprintf(w, "<p><a href='admin/players/%d/password'>Change password</a></p>", data.ID) + } fmt.Fprint(w, "<h2>Character data</h2>") fmt.Fprintf(w, "<form method='post' action='admin/players/%d'><p><label>Character:<br><textarea name='data' rows='40' cols='100' required>%s</textarea></label></p><button>Save</button></form>", data.ID, html.EscapeString(string(contents))) fmt.Fprint(w, itemTables.String())