[PR #6175] [MERGED] fix(cli): stop -getApiToken accumulating admin tokens #6137

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6175
Author: @n0ctal
Created: 8/5/2026
Status: ✅ Merged
Merged: 8/14/2026
Merged by: @MHSanaei

Base: main ← Head: upstream-cli-fallback-token-expiry


📝 Commits (3)

  • 3346553 fix(cli): stop -getApiToken accumulating admin tokens
  • 0f401be fix(api-token): preserve token on failed replacement
  • 20b3bc6 Merge branch 'main' into upstream-cli-fallback-token-expiry

📊 Changes

3 files changed (+108 additions, -6 deletions)

View changed files

📝 internal/web/service/panel/api_token.go (+24 -0)
📝 internal/web/service/panel/api_token_test.go (+76 -1)
📝 main.go (+8 -5)

📄 Description

Summary

x-ui setting -getApiToken minted a new cli-fallback-<unix> token on every invocation when tokens already existed. Rotate a single cli-fallback token instead.

Why

The flag reads like a getter, and the surrounding output explains that existing tokens cannot be retrieved because only hashes are stored. It then creates one anyway — a fresh row, enabled, with no expiry, printed once.

An operator who runs the command a few times while debugging leaves several credentials behind. They all carry the same authority, none can be told apart later, and nothing indicates they were created rather than read. ApiToken has no expiry column, so they persist until someone notices and deletes them by hand.

I hit this while working on a downstream fork: four invocations left four enabled tokens on a production panel, and they were only spotted during an unrelated audit.

Scope

  • internal/web/service/panel/api_token.go — new RecreateByName, which deletes any row with that name before calling Create, so exactly one exists.
  • main.go — the fallback uses the fixed name cli-fallback via RecreateByName; the message states the previous token is now invalid. Drops the now-unused time import.
  • internal/web/service/panel/api_token_test.go — TestRecreateByNameKeepsOneToken asserts a second call rotates the plaintext and leaves one row.

Validation

  • go test ./internal/web/service/panel/ -count=1 green; go build ./... and go vet ./... clean.
  • Verified the test fails without the fix: the second call errors with a token with that name already exists.

Risk

Low. The fallback keeps working; the only behaviour change is that a previously issued CLI fallback token stops working when a new one is printed, which the message now says. Tokens created through the UI are untouched.


🔄 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/6175 **Author:** [@n0ctal](https://github.com/n0ctal) **Created:** 8/5/2026 **Status:** ✅ Merged **Merged:** 8/14/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `upstream-cli-fallback-token-expiry` --- ### 📝 Commits (3) - [`3346553`](https://github.com/MHSanaei/3x-ui/commit/33465532854aa37babe776a5d7c16fe6a4d839d2) fix(cli): stop -getApiToken accumulating admin tokens - [`0f401be`](https://github.com/MHSanaei/3x-ui/commit/0f401be899acb803a4bcd0e3d93dcde9d452ed80) fix(api-token): preserve token on failed replacement - [`20b3bc6`](https://github.com/MHSanaei/3x-ui/commit/20b3bc69c0d08fb32da5aacbe580f4b48fc54f95) Merge branch 'main' into upstream-cli-fallback-token-expiry ### 📊 Changes **3 files changed** (+108 additions, -6 deletions) <details> <summary>View changed files</summary> 📝 `internal/web/service/panel/api_token.go` (+24 -0) 📝 `internal/web/service/panel/api_token_test.go` (+76 -1) 📝 `main.go` (+8 -5) </details> ### 📄 Description ## Summary `x-ui setting -getApiToken` minted a new `cli-fallback-<unix>` token on every invocation when tokens already existed. Rotate a single `cli-fallback` token instead. ## Why The flag reads like a getter, and the surrounding output explains that existing tokens cannot be retrieved because only hashes are stored. It then creates one anyway — a fresh row, enabled, with no expiry, printed once. An operator who runs the command a few times while debugging leaves several credentials behind. They all carry the same authority, none can be told apart later, and nothing indicates they were created rather than read. `ApiToken` has no expiry column, so they persist until someone notices and deletes them by hand. I hit this while working on a downstream fork: four invocations left four enabled tokens on a production panel, and they were only spotted during an unrelated audit. ## Scope - `internal/web/service/panel/api_token.go` — new `RecreateByName`, which deletes any row with that name before calling `Create`, so exactly one exists. - `main.go` — the fallback uses the fixed name `cli-fallback` via `RecreateByName`; the message states the previous token is now invalid. Drops the now-unused `time` import. - `internal/web/service/panel/api_token_test.go` — `TestRecreateByNameKeepsOneToken` asserts a second call rotates the plaintext and leaves one row. ## Validation - `go test ./internal/web/service/panel/ -count=1` green; `go build ./...` and `go vet ./...` clean. - Verified the test fails without the fix: the second call errors with `a token with that name already exists`. ## Risk Low. The fallback keeps working; the only behaviour change is that a previously issued CLI fallback token stops working when a new one is printed, which the message now says. Tokens created through the UI are untouched. --- <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:38 +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#6137
No description provided.