[GH-ISSUE #5951] [Bug]: Duplicate client created when changing email case (case-only edit) #4125

Closed
opened 2026-10-31 20:37:21 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @parsa-fathi on GitHub (Jul 14, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/5951

Describe the bug

Description

After the latest update, editing a client's email by changing only its letter casing (for example, test → Test) no longer works correctly.

Steps to Reproduce

  1. Create a client with the email:

    test
    
  2. Edit the same client.

  3. Change the email to:

    Test
    
  4. Save the changes.

Expected Behavior

The existing client should be updated successfully, preserving the same record while only changing the email's letter casing. This was the behavior before the latest update.

Actual Behavior

  • The panel first returns the following error:

    Something went wrong (UNIQUE constraint failed: clients.email)
    
  • Despite the error, a new client is created with the updated email (Test), while the original client (test) remains.

  • As a result, two separate clients end up existing with what should effectively be the same email, differing only by letter casing.

Additional Information

This issue did not exist before the latest update. Previously, changing only the capitalization of an existing client's email worked correctly without triggering a uniqueness error or creating a duplicate client.

How to reproduce the problem

  1. Create a client with the email:

    test
    
  2. Edit the same client.

  3. Change the email to:

    Test
    
  4. Save the changes.

Expected behavior

No response

Actual behavior

No response

Relevant logs


Screenshots

No response

3x-ui version

3.5.0

Xray-core version

No response

How did you install 3x-ui?

install.sh script

Operating system

Ubuntu 26

Which parts of the panel are affected?

No response

Browser (only if it is a UI bug)

No response

Is the panel behind a reverse proxy or CDN?

None

Before submitting

  • I searched existing issues and this bug has not been reported.
  • I am running the latest released version of 3x-ui (or have verified the bug still exists on it).
  • This bug report is written in English.
  • I have redacted any sensitive data (tokens, real domains, client UUIDs).
Originally created by @parsa-fathi on GitHub (Jul 14, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/5951 ### Describe the bug ### Description After the latest update, editing a client's email by changing only its letter casing (for example, `test` → `Test`) no longer works correctly. ### Steps to Reproduce 1. Create a client with the email: ``` test ``` 2. Edit the same client. 3. Change the email to: ``` Test ``` 4. Save the changes. ### Expected Behavior The existing client should be updated successfully, preserving the same record while only changing the email's letter casing. This was the behavior before the latest update. ### Actual Behavior * The panel first returns the following error: ``` Something went wrong (UNIQUE constraint failed: clients.email) ``` * Despite the error, a **new client** is created with the updated email (`Test`), while the original client (`test`) remains. * As a result, two separate clients end up existing with what should effectively be the same email, differing only by letter casing. ### Additional Information This issue did not exist before the latest update. Previously, changing only the capitalization of an existing client's email worked correctly without triggering a uniqueness error or creating a duplicate client. ### How to reproduce the problem 1. Create a client with the email: ``` test ``` 2. Edit the same client. 3. Change the email to: ``` Test ``` 4. Save the changes. ### Expected behavior _No response_ ### Actual behavior _No response_ ### Relevant logs ```shell ``` ### Screenshots _No response_ ### 3x-ui version 3.5.0 ### Xray-core version _No response_ ### How did you install 3x-ui? install.sh script ### Operating system Ubuntu 26 ### Which parts of the panel are affected? _No response_ ### Browser (only if it is a UI bug) _No response_ ### Is the panel behind a reverse proxy or CDN? None ### Before submitting - [x] I searched [existing issues](../issues?q=is%3Aissue) and this bug has not been reported. - [x] I am running the latest released version of 3x-ui (or have verified the bug still exists on it). - [x] This bug report is written in English. - [x] I have redacted any sensitive data (tokens, real domains, client UUIDs).
bsakaguchi 2026-10-31 20:37:21 +07:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@github-actions[bot] commented on GitHub (Jul 14, 2026):

Confirmed as a genuine bug; root cause traced end-to-end through the update path used by the panel edit-client action (POST /panel/api/clients/update/:email -> ClientController.update in internal/web/controller/client.go:159 -> ClientService.UpdateByEmail/Update in internal/web/service/client_crud.go).

Title: Case-only email edits are inconsistently treated as "unchanged" vs "changed" across three call sites, which desyncs the clients table row from the inbound settings JSON and produces an orphaned duplicate plus a raw UNIQUE constraint failed: clients.email error.

Severity: High

Category: Correctness

Why this matters:
Editing a client email is a common action, and a same-inbound, single-client edit is enough to trigger this - no multi-inbound setup needed. The panel ends up with two clients rows for what the admin intends as one identity: the original row (still holding the old-cased email) becomes an orphan detached from every inbound, while a new row is created under the new casing and keeps the live traffic/expiry/subscription state going forward. That silently splits client history and quota tracking across two records, and surfaces a raw SQL error to the admin instead of a clean validation message.

Trace of what happens for test to Test (client attached to one inbound):

  1. ClientService.Update (internal/web/service/client_crud.go:366) compares updated.Email != existing.Email with a case-sensitive Go !=, sees a change, and pre-checks for collisions excluding the record itself - passes, since no other row is named Test yet.
  2. It then calls UpdateInboundClient (internal/web/service/client_inbound_apply.go:525) with oldEmail="test" and the new settings JSON already containing Test. That function persists the inbound settings JSON with the new casing (tx.Save(oldInbound), line 805), then reaches the rename gate for the client record at line 810:
    // internal/web/service/client_inbound_apply.go:810
    if len(oldEmail) > 0 && !strings.EqualFold(oldEmail, clients[0].Email) {
        ...rename ClientRecord.email from oldEmail to the new email...
    }
    
    strings.EqualFold("test", "Test") is true, so !EqualFold(...) is false and this block is skipped - the existing clients row keeps email = "test".
  3. Still inside that same transaction, SyncInbound runs (internal/web/service/client_link.go:12) with the client list re-read from the just-saved settings (email now "Test"). Its existing-record lookup is case-sensitive SQL:
    // internal/web/service/client_link.go:40
    tx.Where("email IN ?", emails[start:end]).Find(&rows)
    
    This does not find the "test" row under the key "Test" (internal/web/service/client_link.go:59-66), so it falls into the create-new-record branch and inserts a new ClientRecord with email="Test". The link for the old "test" row in client_inbounds was already deleted at the top of SyncInbound (line 17) and is never recreated, so that row becomes an orphan. This transaction commits successfully - the duplicate now exists.
  4. Back in ClientService.Update, after the inbound loop, the fallback rename for records with no inbound left runs unconditionally on the same case-sensitive check:
    // internal/web/service/client_crud.go:428-431
    if updated.Email != existing.Email {
        database.GetDB().Model(&model.ClientRecord{}).
            Where("id = ? AND email = ?", id, existing.Email).
            Update("email", updated.Email) // sets the ORIGINAL row email to "Test"
    }
    
    This targets the original row (id from step 1, still email="test") and tries to rename it to "Test" too - but that value is now taken by the row created in step 3, so this raw UPDATE hits the uniqueIndex on ClientRecord.Email (internal/database/model/model.go:828) and returns the raw SQLite error UNIQUE constraint failed: clients.email, which the controller reports as "Something went wrong". By this point the duplicate from step 3 is already committed, matching the reported symptom exactly.

No COLLATE NOCASE/citext is used anywhere on this column (confirmed via repo-wide search), so both SQLite and Postgres compare email case-sensitively by default - consistent with the duplicate actually being created in step 3 rather than failing immediately.

The existing regression test TestUpdateInboundClientRenameDoesNotDuplicateRecord (internal/web/service/client_update_rename_test.go:19) only covers a full rename (old@x to new@x), so it does not exercise the case-only path and would not have caught this.

Recommendation: Pick one consistent case-sensitivity policy for "did the email change" and apply it at all three decision points, instead of mixing strings.EqualFold (steps 2/3) with a raw Go != (steps 1/4) and a case-sensitive SQL lookup (SyncInbound email IN ?). In particular, the existing-record lookup in internal/web/service/client_link.go needs to find the current row for a case-only rename (for example, match case-insensitively there too, or have the rename gate in client_inbound_apply.go:810 fire on any byte-level difference so the row is renamed before SyncInbound runs and its case-sensitive lookup then matches by construction). The fallback rename in client_crud.go:428 should use the same comparison as the gate it backstops. A regression test with a case-only email edit (mirroring TestUpdateInboundClientRenameDoesNotDuplicateRecord but old@x to Old@x) would catch this going forward.

Confidence: High - traced through the actual source for the exact request path the panel UI uses, with no assumptions about code not shown above.

@MHSanaei for a decision on the fix (per repo policy, changes are made only when requested via @claude).

This analysis was generated automatically; a maintainer may follow up.

<!-- gh-comment-id:4965926919 --> @github-actions[bot] commented on GitHub (Jul 14, 2026): Confirmed as a genuine bug; root cause traced end-to-end through the update path used by the panel edit-client action (`POST /panel/api/clients/update/:email` -> `ClientController.update` in `internal/web/controller/client.go:159` -> `ClientService.UpdateByEmail`/`Update` in `internal/web/service/client_crud.go`). **Title:** Case-only email edits are inconsistently treated as "unchanged" vs "changed" across three call sites, which desyncs the `clients` table row from the inbound settings JSON and produces an orphaned duplicate plus a raw `UNIQUE constraint failed: clients.email` error. **Severity:** High **Category:** Correctness **Why this matters:** Editing a client email is a common action, and a same-inbound, single-client edit is enough to trigger this - no multi-inbound setup needed. The panel ends up with two `clients` rows for what the admin intends as one identity: the original row (still holding the old-cased email) becomes an orphan detached from every inbound, while a new row is created under the new casing and keeps the live traffic/expiry/subscription state going forward. That silently splits client history and quota tracking across two records, and surfaces a raw SQL error to the admin instead of a clean validation message. Trace of what happens for `test` to `Test` (client attached to one inbound): 1. `ClientService.Update` (`internal/web/service/client_crud.go:366`) compares `updated.Email != existing.Email` with a case-sensitive Go `!=`, sees a change, and pre-checks for collisions excluding the record itself - passes, since no other row is named `Test` yet. 2. It then calls `UpdateInboundClient` (`internal/web/service/client_inbound_apply.go:525`) with `oldEmail="test"` and the new settings JSON already containing `Test`. That function persists the inbound settings JSON with the new casing (`tx.Save(oldInbound)`, line 805), then reaches the rename gate for the client record at **line 810**: ```go // internal/web/service/client_inbound_apply.go:810 if len(oldEmail) > 0 && !strings.EqualFold(oldEmail, clients[0].Email) { ...rename ClientRecord.email from oldEmail to the new email... } ``` `strings.EqualFold("test", "Test")` is `true`, so `!EqualFold(...)` is `false` and this block is **skipped** - the existing `clients` row keeps `email = "test"`. 3. Still inside that same transaction, `SyncInbound` runs (`internal/web/service/client_link.go:12`) with the client list re-read from the just-saved settings (email now `"Test"`). Its existing-record lookup is case-sensitive SQL: ```go // internal/web/service/client_link.go:40 tx.Where("email IN ?", emails[start:end]).Find(&rows) ``` This does not find the `"test"` row under the key `"Test"` (`internal/web/service/client_link.go:59-66`), so it falls into the create-new-record branch and inserts a **new** `ClientRecord` with `email="Test"`. The link for the old `"test"` row in `client_inbounds` was already deleted at the top of `SyncInbound` (line 17) and is never recreated, so that row becomes an orphan. This transaction commits successfully - the duplicate now exists. 4. Back in `ClientService.Update`, after the inbound loop, the fallback rename for records with no inbound left runs unconditionally on the same case-sensitive check: ```go // internal/web/service/client_crud.go:428-431 if updated.Email != existing.Email { database.GetDB().Model(&model.ClientRecord{}). Where("id = ? AND email = ?", id, existing.Email). Update("email", updated.Email) // sets the ORIGINAL row email to "Test" } ``` This targets the original row (id from step 1, still `email="test"`) and tries to rename it to `"Test"` too - but that value is now taken by the row created in step 3, so this raw `UPDATE` hits the `uniqueIndex` on `ClientRecord.Email` (`internal/database/model/model.go:828`) and returns the raw SQLite error `UNIQUE constraint failed: clients.email`, which the controller reports as "Something went wrong". By this point the duplicate from step 3 is already committed, matching the reported symptom exactly. No `COLLATE NOCASE`/citext is used anywhere on this column (confirmed via repo-wide search), so both SQLite and Postgres compare `email` case-sensitively by default - consistent with the duplicate actually being created in step 3 rather than failing immediately. The existing regression test `TestUpdateInboundClientRenameDoesNotDuplicateRecord` (`internal/web/service/client_update_rename_test.go:19`) only covers a full rename (`old@x` to `new@x`), so it does not exercise the case-only path and would not have caught this. **Recommendation:** Pick one consistent case-sensitivity policy for "did the email change" and apply it at all three decision points, instead of mixing `strings.EqualFold` (steps 2/3) with a raw Go `!=` (steps 1/4) and a case-sensitive SQL lookup (`SyncInbound` `email IN ?`). In particular, the existing-record lookup in `internal/web/service/client_link.go` needs to find the current row for a case-only rename (for example, match case-insensitively there too, or have the rename gate in `client_inbound_apply.go:810` fire on any byte-level difference so the row is renamed before `SyncInbound` runs and its case-sensitive lookup then matches by construction). The fallback rename in `client_crud.go:428` should use the same comparison as the gate it backstops. A regression test with a case-only email edit (mirroring `TestUpdateInboundClientRenameDoesNotDuplicateRecord` but `old@x` to `Old@x`) would catch this going forward. **Confidence:** High - traced through the actual source for the exact request path the panel UI uses, with no assumptions about code not shown above. @MHSanaei for a decision on the fix (per repo policy, changes are made only when requested via @claude). *This analysis was generated automatically; a maintainer may follow up.*
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#4125
No description provided.