fix(sub): randomize fresh panel subscription paths (#6375)

* fix(sub): randomize fresh panel subscription paths

Seed distinct cryptographically random paths for base64, JSON, and Clash subscriptions when a panel database is first created. Persist them so restarts keep published URLs stable while upgrades preserve existing settings.

Generated-by: OpenCode:gpt-5.6-sol

* fix(sub): regenerate paths on settings reset

Keep subscription paths unpredictable after a factory reset, close the test database on failure, and update the builder, OpenAPI, and localized docs to describe panel-specific paths instead of obsolete fixed defaults.

Generated-by: OpenCode:gpt-5.6-sol
This commit is contained in:
ilyusha
2026-09-03 17:34:37 +03:00
committed by GitHub
parent ded2aa150c
commit f9898e0b24
20 changed files with 285 additions and 158 deletions
+21
View File
@@ -1169,6 +1169,22 @@ func initUser() error {
return nil
}
func seedRandomSubscriptionPaths() error {
settings := []model.Setting{
{Key: "subPath", Value: "/" + random.NumLower(16) + "/"},
{Key: "subJsonPath", Value: "/" + random.NumLower(16) + "/"},
{Key: "subClashPath", Value: "/" + random.NumLower(16) + "/"},
}
return db.Transaction(func(tx *gorm.DB) error {
for i := range settings {
if err := tx.Where("key = ?", settings[i].Key).FirstOrCreate(&settings[i]).Error; err != nil {
return err
}
}
return nil
})
}
func runSeeders(isUsersEmpty bool) error {
empty, err := isTableEmpty("history_of_seeders")
if err != nil {
@@ -2138,6 +2154,11 @@ func InitDB(dbPath string) error {
if err != nil {
return err
}
if isUsersEmpty {
if err := seedRandomSubscriptionPaths(); err != nil {
return err
}
}
if err := initUser(); err != nil {
return err
+51
View File
@@ -9,6 +9,54 @@ import (
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
)
func TestInitDB_GeneratesPerPanelSubscriptionPaths(t *testing.T) {
pathPattern := regexp.MustCompile(`^/[0-9a-z]{16}/$`)
loadPaths := func(dbPath string) map[string]string {
t.Helper()
if err := InitDB(dbPath); err != nil {
t.Fatalf("InitDB failed: %v", err)
}
defer func() {
if err := CloseDB(); err != nil {
t.Errorf("CloseDB failed: %v", err)
}
}()
keys := []string{"subPath", "subJsonPath", "subClashPath"}
paths := make(map[string]string, len(keys))
for _, key := range keys {
var setting model.Setting
if err := db.Where("key = ?", key).First(&setting).Error; err != nil {
t.Fatalf("read %s: %v", key, err)
}
if !pathPattern.MatchString(setting.Value) {
t.Fatalf("%s = %q, want /<16 lowercase alphanumeric characters>/", key, setting.Value)
}
paths[key] = setting.Value
}
if paths["subPath"] == paths["subJsonPath"] || paths["subPath"] == paths["subClashPath"] || paths["subJsonPath"] == paths["subClashPath"] {
t.Fatalf("subscription paths must be distinct: %v", paths)
}
return paths
}
firstDB := filepath.Join(t.TempDir(), "x-ui.db")
first := loadPaths(firstDB)
reloaded := loadPaths(firstDB)
for key, firstPath := range first {
if firstPath != reloaded[key] {
t.Fatalf("%s changed after restart: %q, then %q", key, firstPath, reloaded[key])
}
}
second := loadPaths(filepath.Join(t.TempDir(), "x-ui.db"))
for key, firstPath := range first {
if firstPath == second[key] {
t.Fatalf("%s reused across panels: %q", key, firstPath)
}
}
}
func TestSeedClientsFromInboundJSON_IsIdempotentAgainstExistingClients(t *testing.T) {
dbDir := t.TempDir()
t.Setenv("XUI_DB_FOLDER", dbDir)
@@ -168,6 +216,9 @@ func TestNormalizeSettingPaths_RepairsLegacyValues(t *testing.T) {
{Key: "subClashPath", Value: "clash/"},
{Key: "webBasePath", Value: "/panel/"},
}
if err := db.Where("key IN ?", []string{"subPath", "subJsonPath", "subClashPath"}).Delete(&model.Setting{}).Error; err != nil {
t.Fatalf("clear generated subscription paths: %v", err)
}
for i := range seed {
if err := db.Create(&seed[i]).Error; err != nil {
t.Fatalf("seed setting %s: %v", seed[i].Key, err)