mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-10-24 14:55:02 +03:00
fix(database): create the first panel user with random credentials
The first user is now created when the server starts, with a random username and password printed once to its log (docker logs, the journal), instead of a fixed pair seeded by InitDB. CLI calls that initialize the database are often piped away, as the Docker entrypoint's port lookup is, so only the server's own log reliably reaches the operator. `x-ui setting -show` keeps reporting hasDefaultCredential: true until a user exists, so install.sh still sets its own credentials on a fresh install. Existing databases are not changed. Verified on a built binary: fresh setting -show, install.sh-style credential set, first start, login and restart. The PostgreSQL lane was not run locally; the change adds no query.
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/util/crypto"
|
||||
)
|
||||
|
||||
func TestInitDB_GeneratesPerPanelSubscriptionPaths(t *testing.T) {
|
||||
@@ -57,6 +58,55 @@ func TestInitDB_GeneratesPerPanelSubscriptionPaths(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Every launcher but install.sh serves the panel on the database as InitDB left
|
||||
// it, so a credential seeded here is one anyone on the network can log in with.
|
||||
func TestInitDBLeavesNoGuessableCredential(t *testing.T) {
|
||||
if err := InitDB(filepath.Join(t.TempDir(), "x-ui.db")); err != nil {
|
||||
t.Fatalf("InitDB: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = CloseDB() })
|
||||
|
||||
var users []model.User
|
||||
if err := db.Find(&users).Error; err != nil {
|
||||
t.Fatalf("read users: %v", err)
|
||||
}
|
||||
for _, u := range users {
|
||||
if crypto.CheckPasswordHash(u.Password, "admin") {
|
||||
t.Fatalf("fresh database holds user %q with the password admin", u.Username)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The server prints what EnsureFirstUser returns as the only copy of the first
|
||||
// login, so it must be the stored credential, and a restart must not mint another.
|
||||
func TestEnsureFirstUserCreatesOneWorkingCredential(t *testing.T) {
|
||||
if err := InitDB(filepath.Join(t.TempDir(), "x-ui.db")); err != nil {
|
||||
t.Fatalf("InitDB: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = CloseDB() })
|
||||
|
||||
username, password, err := EnsureFirstUser()
|
||||
if err != nil {
|
||||
t.Fatalf("EnsureFirstUser: %v", err)
|
||||
}
|
||||
var stored model.User
|
||||
if err := db.First(&stored).Error; err != nil {
|
||||
t.Fatalf("read user: %v", err)
|
||||
}
|
||||
if stored.Username != username || password == "" || !crypto.CheckPasswordHash(stored.Password, password) {
|
||||
t.Fatalf("returned %q/%q, stored user %q does not log in with it", username, password, stored.Username)
|
||||
}
|
||||
|
||||
againUser, againPass, err := EnsureFirstUser()
|
||||
if err != nil || againUser != "" || againPass != "" {
|
||||
t.Fatalf("second EnsureFirstUser = %q, %q, %v; want nothing created", againUser, againPass, err)
|
||||
}
|
||||
var count int64
|
||||
if err := db.Model(&model.User{}).Count(&count).Error; err != nil || count != 1 {
|
||||
t.Fatalf("users = %d (%v), want 1", count, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSeedClientsFromInboundJSON_IsIdempotentAgainstExistingClients(t *testing.T) {
|
||||
dbDir := t.TempDir()
|
||||
t.Setenv("XUI_DB_FOLDER", dbDir)
|
||||
|
||||
Reference in New Issue
Block a user