[GH-ISSUE #6478] [Bug]: Portable client import forces enable=true, reactivating disabled clients #4372

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

Originally created by @mrchatam on GitHub (Sep 11, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6478

Describe the bug

ExportAll serializes each client's real enable flag via ClientRecord.ToClient(). On import, both paths that recreate clients unconditionally force enable to true:

  1. Attached clients → ImportClients → BulkCreate (internal/web/service/client_bulk.go):
if !client.Enable {
    client.Enable = true
}
  1. Orphan clients → ImportClients orphan loop (internal/web/service/client_portable.go): same pattern.

Because a Go bool cannot distinguish "omitted" from false after JSON unmarshal, this is equivalent to always writing Enable: true. A round-trip of a disabled client therefore reactivates it and (for attached imports) pushes it back into Xray as enabled.

This is adjacent to the portable traffic-preserve work (#5858 / #6469) but is a separate correctness hole: even after traffic counters round-trip, disabled clients still come back enabled.

How to reproduce the problem

  1. Create a client, disable it (enable: false), confirm it is disabled in the Clients list and absent/disabled in Xray.
  2. Export clients (portable export / ExportAll).
  3. On a clean panel (or after deleting that email), import the file.
  4. Observe the imported client is enabled.

Expected behavior

Portable import preserves enable from the export payload. Disabled clients stay disabled; enabled clients stay enabled. (API BulkCreate used as a plain "create many" may still choose to default omitted enable to true — but an explicit enable: false from an export must not be overwritten.)

Actual behavior

Every imported client is created with enable: true, including ones that were disabled at export time.

File pointers

  • internal/web/service/client_portable.go — orphan branch (if !client.Enable { client.Enable = true })
  • internal/web/service/client_bulk.go — BulkCreate (same force)
  • Export source: ClientRecord.ToClient() copies Enable correctly; the loss is on import only

3x-ui version

main (current default branch)

Suggested fix size

Small-to-medium: preserve enable on the ImportClients path (orphans + attached). If BulkCreate must keep "default to enabled" for the create API, use a pointer/*bool or a separate import entrypoint so export's explicit false survives.

Originally created by @mrchatam on GitHub (Sep 11, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6478 ### Describe the bug `ExportAll` serializes each client's real `enable` flag via `ClientRecord.ToClient()`. On import, both paths that recreate clients unconditionally force `enable` to true: 1. Attached clients → `ImportClients` → `BulkCreate` (`internal/web/service/client_bulk.go`): ```go if !client.Enable { client.Enable = true } ``` 2. Orphan clients → `ImportClients` orphan loop (`internal/web/service/client_portable.go`): same pattern. Because a Go `bool` cannot distinguish "omitted" from `false` after JSON unmarshal, this is equivalent to always writing `Enable: true`. A round-trip of a disabled client therefore reactivates it and (for attached imports) pushes it back into Xray as enabled. This is adjacent to the portable traffic-preserve work (#5858 / #6469) but is a separate correctness hole: even after traffic counters round-trip, disabled clients still come back enabled. ### How to reproduce the problem 1. Create a client, disable it (`enable: false`), confirm it is disabled in the Clients list and absent/disabled in Xray. 2. Export clients (portable export / `ExportAll`). 3. On a clean panel (or after deleting that email), import the file. 4. Observe the imported client is enabled. ### Expected behavior Portable import preserves `enable` from the export payload. Disabled clients stay disabled; enabled clients stay enabled. (API `BulkCreate` used as a plain "create many" may still choose to default omitted enable to true — but an explicit `enable: false` from an export must not be overwritten.) ### Actual behavior Every imported client is created with `enable: true`, including ones that were disabled at export time. ### File pointers - `internal/web/service/client_portable.go` — orphan branch (`if !client.Enable { client.Enable = true }`) - `internal/web/service/client_bulk.go` — `BulkCreate` (same force) - Export source: `ClientRecord.ToClient()` copies `Enable` correctly; the loss is on import only ### 3x-ui version main (current default branch) ### Suggested fix size Small-to-medium: preserve `enable` on the ImportClients path (orphans + attached). If `BulkCreate` must keep "default to enabled" for the create API, use a pointer/`*bool` or a separate import entrypoint so export's explicit `false` survives.
bsakaguchi 2026-10-31 20:39:12 +07:00
  • closed this issue
  • added the
    duplicate
    label
Author
Owner

@github-actions[bot] commented on GitHub (Sep 11, 2026):

Thanks — this is a real bug, but you already filed it as #6475 one minute earlier, and that issue has the full analysis. I'm closing this one as a duplicate so the discussion stays in one place; #6475 remains open and is the tracked report.

I did not take that on faith — I re-derived the root cause here before closing, and the two reports land on the same two lines.

The shared root cause

Export preserves the flag. ClientRecord.ToClient() copies it verbatim (internal/database/model/model.go:1207):

Enable:          r.Enable,

and model.Client.Enable carries no omitempty (internal/database/model/model.go:901), so a disabled client is serialized as an explicit "enable": false:

Enable              bool             `json:"enable" form:"enable"`         // Whether the client is enabled

Import then discards it, in both branches, exactly as you described:

  • attached items → BulkCreate, internal/web/service/client_bulk.go:1313-1315
  • orphan items → ImportClients orphan loop, internal/web/service/client_portable.go:158-160

both being the identical:

if !client.Enable {
    client.Enable = true
}

On the attached path the forced true also reaches the running Xray, because AddInboundClient has a guard that would have skipped a disabled client (internal/web/service/client_inbound_apply.go:572-574):

if !client.Enable {
    continue
}

That guard never fires, because BulkCreate has already flipped the flag before AddInboundClient sees the client.

Worth noting for context: the same force-enable also sits in the single-client create path (internal/web/service/client_crud.go:157-159), so "a create starts enabled" is a deliberate panel-wide default that ImportClients simply inherited — which is why the fix belongs on the import path rather than in the shared create code, as you suggested.

Workaround until it is fixed

Re-disable the affected clients in one call after importing. BulkSetEnable handles orphans too — with no inbound mappings they fall straight through the fan-out into successEmails and get their client_records / client_traffics rows updated (internal/web/service/client_bulk.go:1616-1640):

POST /panel/api/clients/bulkDisable
{"emails": ["client-a@example.com", "client-b@example.com"]}

Feed it the emails whose export entries carry "enable": false. The same action is available in the UI as the bulk Disable button on the Clients page.


Verdict: Duplicate
Severity: High
Confidence: High
Root cause: if !client.Enable { client.Enable = true } on the import path, in two places — ClientService.BulkCreate (internal/web/service/client_bulk.go:1313-1315) for items carrying inboundIds, and the orphan loop in ClientService.ImportClients (internal/web/service/client_portable.go:158-160) for items without. Triggers for every payload item whose enable is false, which ExportAll always emits explicitly for a disabled client. The orphan-path force arrived with 0b0b6250 feat(clients): orphan cleanup + export/import via CodeMirror modals (2026-06-21), the commit that added ImportClients; v3.4.0 (2026-06-23) is the first release carrying it.
Already fixed: Still present on the default branch at 0a2cd789. git log -S 'if !client.Enable {' over both files returns only 0b0b6250 and the 41645255 file-move refactor — nothing since changes either line.
Duplicate of: #6475 — same two force-enable sites discarding the exported enable flag; same author, filed one minute earlier, and already analysed there
Evidence: internal/database/model/model.go:901 (Enable bool with json tag enable, no omitempty); internal/database/model/model.go:1207 (Enable: r.Enable in ToClient); internal/web/service/client_bulk.go:1313-1315; internal/web/service/client_portable.go:158-160; internal/web/service/client_inbound_apply.go:572-574 (the if !client.Enable { continue } guard the forced flag defeats); internal/web/service/client_crud.go:157-159 (the same default on single create); internal/web/service/client_bulk.go:1616-1640 (BulkSetEnable covers orphans); frontend/src/pages/api-docs/endpoints.ts:1312 (the bulkDisable route used in the workaround); introducing commit 0b0b6250. No test in the repo pins the current force-enable behaviour, so nothing asserts it is intended.
Not determined: Whether POST /panel/api/clients/bulkCreate should also honour an explicit "enable": false from an ordinary API caller. That is a public-API contract decision the source does not settle, and it changes the shape of the fix — it is called out on #6475 and is a maintainer call.

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

<!-- gh-comment-id:5637886696 --> @github-actions[bot] commented on GitHub (Sep 11, 2026): Thanks — this is a real bug, but you already filed it as #6475 one minute earlier, and that issue has the full analysis. I'm closing this one as a duplicate so the discussion stays in one place; **#6475 remains open and is the tracked report.** I did not take that on faith — I re-derived the root cause here before closing, and the two reports land on the same two lines. ### The shared root cause Export preserves the flag. `ClientRecord.ToClient()` copies it verbatim (`internal/database/model/model.go:1207`): ```go Enable: r.Enable, ``` and `model.Client.Enable` carries no `omitempty` (`internal/database/model/model.go:901`), so a disabled client is serialized as an explicit `"enable": false`: ```go Enable bool `json:"enable" form:"enable"` // Whether the client is enabled ``` Import then discards it, in both branches, exactly as you described: - attached items → `BulkCreate`, `internal/web/service/client_bulk.go:1313-1315` - orphan items → `ImportClients` orphan loop, `internal/web/service/client_portable.go:158-160` both being the identical: ```go if !client.Enable { client.Enable = true } ``` On the attached path the forced `true` also reaches the running Xray, because `AddInboundClient` has a guard that *would* have skipped a disabled client (`internal/web/service/client_inbound_apply.go:572-574`): ```go if !client.Enable { continue } ``` That guard never fires, because `BulkCreate` has already flipped the flag before `AddInboundClient` sees the client. Worth noting for context: the same force-enable also sits in the single-client create path (`internal/web/service/client_crud.go:157-159`), so "a create starts enabled" is a deliberate panel-wide default that `ImportClients` simply inherited — which is why the fix belongs on the import path rather than in the shared create code, as you suggested. ### Workaround until it is fixed Re-disable the affected clients in one call after importing. `BulkSetEnable` handles orphans too — with no inbound mappings they fall straight through the fan-out into `successEmails` and get their `client_records` / `client_traffics` rows updated (`internal/web/service/client_bulk.go:1616-1640`): ``` POST /panel/api/clients/bulkDisable {"emails": ["client-a@example.com", "client-b@example.com"]} ``` Feed it the emails whose export entries carry `"enable": false`. The same action is available in the UI as the bulk **Disable** button on the Clients page. --- Verdict: Duplicate Severity: High Confidence: High Root cause: `if !client.Enable { client.Enable = true }` on the import path, in two places — `ClientService.BulkCreate` (`internal/web/service/client_bulk.go:1313-1315`) for items carrying `inboundIds`, and the orphan loop in `ClientService.ImportClients` (`internal/web/service/client_portable.go:158-160`) for items without. Triggers for every payload item whose `enable` is `false`, which `ExportAll` always emits explicitly for a disabled client. The orphan-path force arrived with `0b0b6250` *feat(clients): orphan cleanup + export/import via CodeMirror modals* (2026-06-21), the commit that added `ImportClients`; v3.4.0 (2026-06-23) is the first release carrying it. Already fixed: Still present on the default branch at `0a2cd789`. `git log -S 'if !client.Enable {'` over both files returns only `0b0b6250` and the `41645255` file-move refactor — nothing since changes either line. Duplicate of: #6475 — same two force-enable sites discarding the exported `enable` flag; same author, filed one minute earlier, and already analysed there Evidence: `internal/database/model/model.go:901` (`Enable bool` with json tag `enable`, no `omitempty`); `internal/database/model/model.go:1207` (`Enable: r.Enable` in `ToClient`); `internal/web/service/client_bulk.go:1313-1315`; `internal/web/service/client_portable.go:158-160`; `internal/web/service/client_inbound_apply.go:572-574` (the `if !client.Enable { continue }` guard the forced flag defeats); `internal/web/service/client_crud.go:157-159` (the same default on single create); `internal/web/service/client_bulk.go:1616-1640` (`BulkSetEnable` covers orphans); `frontend/src/pages/api-docs/endpoints.ts:1312` (the `bulkDisable` route used in the workaround); introducing commit `0b0b6250`. No test in the repo pins the current force-enable behaviour, so nothing asserts it is intended. Not determined: Whether `POST /panel/api/clients/bulkCreate` should also honour an explicit `"enable": false` from an ordinary API caller. That is a public-API contract decision the source does not settle, and it changes the shape of the fix — it is called out on #6475 and is a maintainer call. *This reply was generated automatically; a maintainer may follow up.* <!-- claude-issue:analyst -->
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#4372
No description provided.