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:
+13
-25
@@ -57,11 +57,7 @@ func Dialect() string {
|
||||
return db.Name()
|
||||
}
|
||||
|
||||
const (
|
||||
defaultUsername = "admin"
|
||||
defaultPassword = "admin"
|
||||
sqliteBackupDirPrefix = ".x-ui-backup-"
|
||||
)
|
||||
const sqliteBackupDirPrefix = ".x-ui-backup-"
|
||||
|
||||
func allModels() []any {
|
||||
return []any{
|
||||
@@ -1316,26 +1312,22 @@ func isIgnorableDuplicateColumnErr(gdb *gorm.DB, err error, mdl any) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func initUser() error {
|
||||
// EnsureFirstUser creates the panel's first user with random credentials and returns
|
||||
// them, or returns "" when one exists; only the starting server calls it, so it can print them.
|
||||
func EnsureFirstUser() (username, password string, err error) {
|
||||
empty, err := isTableEmpty("users")
|
||||
if err != nil || !empty {
|
||||
return "", "", err
|
||||
}
|
||||
username, password = random.Seq(10), random.Seq(16)
|
||||
hashedPassword, err := crypto.HashPasswordAsBcrypt(password)
|
||||
if err != nil {
|
||||
log.Printf("Error checking if users table is empty: %v", err)
|
||||
return err
|
||||
return "", "", err
|
||||
}
|
||||
if empty {
|
||||
hashedPassword, err := crypto.HashPasswordAsBcrypt(defaultPassword)
|
||||
if err != nil {
|
||||
log.Printf("Error hashing default password: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
user := &model.User{
|
||||
Username: defaultUsername,
|
||||
Password: hashedPassword,
|
||||
}
|
||||
return db.Create(user).Error
|
||||
if err := db.Create(&model.User{Username: username, Password: hashedPassword}).Error; err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
return nil
|
||||
return username, password, nil
|
||||
}
|
||||
|
||||
func seedRandomSubscriptionPaths() error {
|
||||
@@ -3145,10 +3137,6 @@ func InitDB(dbPath string) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := initUser(); err != nil {
|
||||
return err
|
||||
}
|
||||
return runSeeders(isUsersEmpty)
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -41,6 +41,10 @@ func InitDB(t testing.TB, path string) {
|
||||
t.Fatalf("InitDB: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = database.CloseDB() })
|
||||
// A serving panel always has its first user; a copied template already does.
|
||||
if _, _, err := database.EnsureFirstUser(); err != nil {
|
||||
t.Fatalf("EnsureFirstUser: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func migratedTemplate() ([]byte, error) {
|
||||
@@ -56,6 +60,10 @@ func migratedTemplate() ([]byte, error) {
|
||||
migrated.err = err
|
||||
return
|
||||
}
|
||||
if _, _, err := database.EnsureFirstUser(); err != nil {
|
||||
migrated.err = err
|
||||
return
|
||||
}
|
||||
// Closing the last connection checkpoints the WAL into the main file.
|
||||
if err := database.CloseDB(); err != nil {
|
||||
migrated.err = err
|
||||
|
||||
@@ -30,7 +30,7 @@ func TestInitDBGivesEachTestItsOwnDatabase(t *testing.T) {
|
||||
t.Fatalf("count users: %v", err)
|
||||
}
|
||||
if admins != 1 {
|
||||
t.Fatalf("users = %d, want the 1 seeded admin a fresh install has", admins)
|
||||
t.Fatalf("users = %d, want the 1 admin a started panel has", admins)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user