Files
3x-ui/internal/web/service/client_create_reuse_test.go
T
MHSanaei 3b731cd657 fix(clients): reuse stored credentials when re-adding an existing identity
Create permits a repeat add for an email that already exists when the
payload subId matches the stored one (the documented way to attach an
identity to more inbounds), but it never seeded the payload from the
existing record, so an omitted id minted a fresh UUID via
fillProtocolDefaults. SyncInbound then overwrote the shared clients.uuid
row by email while previously-attached inbounds kept the original UUID
in their settings JSON, silently desyncing panel credentials from
subscription links. BulkCreate had the identical gap.

Seed ID/Password/Auth/Secret from the existing record in both paths
(mirroring what Update, Attach and BulkAttach already do), and preserve
Secret in Update too so partial edits of MTProto clients cannot rotate
the stored secret.

Closes #5903
2026-07-11 22:48:51 +02:00

55 lines
1.7 KiB
Go

package service
import (
"strings"
"testing"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
)
func settingsHoldUUID(t *testing.T, inboundSvc *InboundService, inboundId int, uuid string) bool {
t.Helper()
ib, err := inboundSvc.GetInbound(inboundId)
if err != nil {
t.Fatalf("GetInbound %d: %v", inboundId, err)
}
return strings.Contains(ib.Settings, uuid)
}
func TestCreateRepeatKeepsExistingUUID(t *testing.T) {
setupBulkDB(t)
svc := &ClientService{}
inboundSvc := &InboundService{}
ibA := mkInbound(t, 21001, model.VLESS, `{"clients":[]}`)
ibB := mkInbound(t, 21002, model.VLESS, `{"clients":[]}`)
const originalUUID = "aaaaaaaa-1111-2222-3333-444444444444"
if _, err := svc.Create(inboundSvc, &ClientCreatePayload{
Client: model.Client{Email: "repeat@x", ID: originalUUID, SubID: "sub-repeat", Enable: true},
InboundIds: []int{ibA.Id},
}); err != nil {
t.Fatalf("first Create: %v", err)
}
if rec := lookupClientRecord(t, "repeat@x"); rec.UUID != originalUUID {
t.Fatalf("record UUID after first Create = %q, want %q", rec.UUID, originalUUID)
}
if _, err := svc.Create(inboundSvc, &ClientCreatePayload{
Client: model.Client{Email: "repeat@x", SubID: "sub-repeat", Enable: true},
InboundIds: []int{ibB.Id},
}); err != nil {
t.Fatalf("repeat Create: %v", err)
}
if rec := lookupClientRecord(t, "repeat@x"); rec.UUID != originalUUID {
t.Fatalf("record UUID after repeat Create = %q, want %q", rec.UUID, originalUUID)
}
if !settingsHoldUUID(t, inboundSvc, ibA.Id, originalUUID) {
t.Fatalf("inbound A settings lost the original UUID")
}
if !settingsHoldUUID(t, inboundSvc, ibB.Id, originalUUID) {
t.Fatalf("inbound B settings did not reuse the original UUID")
}
}