[PR #6196] [CLOSED] feat(database): opt-in uniqueness enforcement for non-empty sub_id #6157

Closed
opened 2026-11-02 04:06:45 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6196
Author: @n0ctal
Created: 8/10/2026
Status: ❌ Closed

Base: main ← Head: upstream-unique-subid-gate


📝 Commits (3)

  • 93fc85c database: gate unique non-empty sub_id enforcement
  • 47e2113 fix(database): enforce unique subids after seeders
  • 315cff3 Merge branch 'main' into upstream-unique-subid-gate

📊 Changes

2 files changed (+366 additions, -1 deletions)

View changed files

📝 internal/database/db.go (+163 -1)
➕ internal/database/unique_subid_test.go (+203 -0)

📄 Description

Summary

Add an opt-in XUI_ENFORCE_UNIQUE_SUBID=1 gate that enforces uniqueness of non-empty sub_id with a partial unique index. Default behaviour is unchanged and only audits.

Why

sub_id carries a plain index, so duplicates are accepted. Two clients sharing a subscription id are served the same subscription content, which is either an aggregation the operator intended or a mistake nobody is told about — and today there is no way to express the difference.

The empty value has to stay duplicable: it is the normal state for clients with no subscription. That rules out a plain unique index and is why this needs a partial one.

Scope

  • Without the variable: behaviour unchanged, a duplicate audit is logged and nothing is enforced.
  • With XUI_ENFORCE_UNIQUE_SUBID=1: a preflight scan reports existing duplicates before anything is created, then a partial unique index over non-empty values, then a structural check of the resulting index definition rather than only its name.
  • The structural check exists because a same-named index with a different predicate (for example WHERE sub_id IS NULL) would otherwise pass as if enforcement were in place. Enforcement fails closed if the definition does not match.

Validation

  • go test ./... green; go test -race ./internal/database -run TestUniqueSubID green.
  • Against a real PostgreSQL on a temporary local cluster: an index with the wrong predicate is rejected. The cluster was stopped after the run.

Not proven: no migration was run against a copy of a large production database containing real duplicates. The fail-closed path is covered by tests, not by that rehearsal.

Risk

Off by default. An operator enabling it is told about existing duplicates before the index is attempted, so enforcement cannot half-apply against dirty data.

Relation to #6065

#6065 establishes that sharing one sub_id across client records is legitimate and is how a subscription aggregates several entries. That is why this is opt-in and off by default: enabling it forbids exactly that aggregation.

The analysis in that issue also notes that the existing service-level checks are incomplete, which is the gap this closes for operators who do want one client per sub_id — but at the database level, where a check cannot be bypassed by a different write path, rather than by adding more service-level checks.

If you would rather this never becomes enforceable at the schema level because aggregation is the intended contract, say so and I will close it.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/6196 **Author:** [@n0ctal](https://github.com/n0ctal) **Created:** 8/10/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `upstream-unique-subid-gate` --- ### 📝 Commits (3) - [`93fc85c`](https://github.com/MHSanaei/3x-ui/commit/93fc85ca93146d855ee233adea157ddcef155756) database: gate unique non-empty sub_id enforcement - [`47e2113`](https://github.com/MHSanaei/3x-ui/commit/47e211316c35f6bd070e0ab5adc4d0cf1debefaf) fix(database): enforce unique subids after seeders - [`315cff3`](https://github.com/MHSanaei/3x-ui/commit/315cff36b4a6dc4c4911f4f5e95fb3924913cfa0) Merge branch 'main' into upstream-unique-subid-gate ### 📊 Changes **2 files changed** (+366 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `internal/database/db.go` (+163 -1) ➕ `internal/database/unique_subid_test.go` (+203 -0) </details> ### 📄 Description ## Summary Add an opt-in `XUI_ENFORCE_UNIQUE_SUBID=1` gate that enforces uniqueness of non-empty `sub_id` with a partial unique index. Default behaviour is unchanged and only audits. ## Why `sub_id` carries a plain index, so duplicates are accepted. Two clients sharing a subscription id are served the same subscription content, which is either an aggregation the operator intended or a mistake nobody is told about — and today there is no way to express the difference. The empty value has to stay duplicable: it is the normal state for clients with no subscription. That rules out a plain unique index and is why this needs a partial one. ## Scope - Without the variable: behaviour unchanged, a duplicate audit is logged and nothing is enforced. - With `XUI_ENFORCE_UNIQUE_SUBID=1`: a preflight scan reports existing duplicates before anything is created, then a partial unique index over non-empty values, then a structural check of the resulting index definition rather than only its name. - The structural check exists because a same-named index with a different predicate (for example `WHERE sub_id IS NULL`) would otherwise pass as if enforcement were in place. Enforcement fails closed if the definition does not match. ## Validation - `go test ./...` green; `go test -race ./internal/database -run TestUniqueSubID` green. - Against a real PostgreSQL on a temporary local cluster: an index with the wrong predicate is rejected. The cluster was stopped after the run. Not proven: no migration was run against a copy of a large production database containing real duplicates. The fail-closed path is covered by tests, not by that rehearsal. ## Risk Off by default. An operator enabling it is told about existing duplicates before the index is attempted, so enforcement cannot half-apply against dirty data. ## Relation to #6065 [#6065](https://github.com/MHSanaei/3x-ui/issues/6065) establishes that sharing one `sub_id` across client records is legitimate and is how a subscription aggregates several entries. That is why this is opt-in and off by default: **enabling it forbids exactly that aggregation.** The analysis in that issue also notes that the existing service-level checks are incomplete, which is the gap this closes for operators who do want one client per `sub_id` — but at the database level, where a check cannot be bypassed by a different write path, rather than by adding more service-level checks. If you would rather this never becomes enforceable at the schema level because aggregation is the intended contract, say so and I will close it. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:06:45 +07:00
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#6157
No description provided.