mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-09-04 09:27:15 +00:00
23511108bf
* fix(database): keep the SQLite store owner-only InitDB created the data directory 0755 and let SQLite create x-ui.db and its -wal/-shm side files under the default umask, so on a stock install they are world-readable. The store holds client UUIDs, Reality private keys and the admin password hash, so any local account could read them. Create the directory 0700 and chmod the database files to 0600 right after opening. SQLite gives -wal/-shm the mode of the main file, so files created later inherit it; existing installs are tightened on the next start. PostgreSQL deployments are untouched. Assisted-by: Claude Code:claude-fable-5-1 * fix(database): tolerate chmod failures, keep the dump and install dir owner-only Review follow-ups. A store the panel cannot chmod (root_squash NFS, a foreign uid in a container) refused to start, which is worse than the 0644 it had before; log and continue instead, as the backup-directory cleanup above already does. install.sh reset /etc/x-ui to 0755 right after the binary created it 0700, so the directory hunk was inert on real installs; create it 0700 there too. The migrate-db dump in the same directory is a plaintext copy of the same secrets and was written 0644. Assisted-by: Claude Code:claude-fable-5-1
80 lines
2.1 KiB
Go
80 lines
2.1 KiB
Go
package database
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"testing"
|
|
)
|
|
|
|
func TestInitDBRestrictsSQLiteFilePermissions(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("POSIX permission bits are not meaningful on Windows")
|
|
}
|
|
t.Setenv("XUI_DB_JOURNAL_MODE", "")
|
|
dbDir := filepath.Join(t.TempDir(), "x-ui")
|
|
dbPath := filepath.Join(dbDir, "x-ui.db")
|
|
|
|
if err := InitDB(dbPath); err != nil {
|
|
t.Fatalf("InitDB: %v", err)
|
|
}
|
|
t.Cleanup(func() { _ = CloseDB() })
|
|
|
|
if info, err := os.Stat(dbDir); err != nil {
|
|
t.Fatalf("stat db dir: %v", err)
|
|
} else if perm := info.Mode().Perm(); perm != 0o700 {
|
|
t.Fatalf("db dir perm = %o, want 700", perm)
|
|
}
|
|
for _, name := range []string{dbPath, dbPath + "-wal", dbPath + "-shm"} {
|
|
info, err := os.Stat(name)
|
|
if errors.Is(err, os.ErrNotExist) && name != dbPath {
|
|
continue
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("stat %s: %v", name, err)
|
|
}
|
|
if perm := info.Mode().Perm(); perm != 0o600 {
|
|
t.Fatalf("%s perm = %o, want 600", filepath.Base(name), perm)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestInitDBTightensExistingSQLiteFilePermissions(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("POSIX permission bits are not meaningful on Windows")
|
|
}
|
|
t.Setenv("XUI_DB_JOURNAL_MODE", "")
|
|
dbPath := filepath.Join(t.TempDir(), "x-ui.db")
|
|
if err := InitDB(dbPath); err != nil {
|
|
t.Fatalf("seed InitDB: %v", err)
|
|
}
|
|
if err := CloseDB(); err != nil {
|
|
t.Fatalf("seed CloseDB: %v", err)
|
|
}
|
|
// Simulate a store created by an older release under the default umask.
|
|
for _, name := range []string{dbPath, dbPath + "-wal", dbPath + "-shm"} {
|
|
if err := os.Chmod(name, 0o644); err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
t.Fatalf("chmod %s: %v", name, err)
|
|
}
|
|
}
|
|
|
|
if err := InitDB(dbPath); err != nil {
|
|
t.Fatalf("InitDB: %v", err)
|
|
}
|
|
t.Cleanup(func() { _ = CloseDB() })
|
|
|
|
for _, name := range []string{dbPath, dbPath + "-wal", dbPath + "-shm"} {
|
|
info, err := os.Stat(name)
|
|
if errors.Is(err, os.ErrNotExist) && name != dbPath {
|
|
continue
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("stat %s: %v", name, err)
|
|
}
|
|
if perm := info.Mode().Perm(); perm != 0o600 {
|
|
t.Fatalf("%s perm = %o, want 600", filepath.Base(name), perm)
|
|
}
|
|
}
|
|
}
|