mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[PR #6405] [MERGED] fix(cli): let -getApiToken name the token it regenerates #6257
Labels
No labels
backlog
bug
clarification needed
documentation
duplicate
enhancement
github_actions
good first issue
invalid
javascript
New Feature
PR welcomed
pull-request
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
MHSanaei/3x-ui#6257
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/MHSanaei/3x-ui/pull/6405
Author: @rokokol
Created: 9/3/2026
Status: ✅ Merged
Merged: 9/8/2026
Merged by: @MHSanaei
Base:
main← Head:fix/cli-token-name📝 Commits (4)
eaa4229fix(cli): let -getApiToken name the token it regenerates9a1fcf9fix(cli): keep the installer's token out of the rotated slota0f52c5fix(cli): stop the-getApiToken trueform from swallowing -tokenName351bbbbMerge branch 'main' into fix/cli-token-name📊 Changes
5 files changed (+208 additions, -9 deletions)
View changed files
➕
api_token_cli_test.go(+148 -0)📝
install.sh(+2 -2)📝
internal/web/service/panel/api_token.go(+4 -0)📝
internal/web/service/panel/api_token_test.go(+25 -0)📝
main.go(+29 -7)📄 Description
Summary
x-ui setting -getApiTokennow takes an optional-tokenName, so callers stop sharing one hardcoded credential slot, and its help text describes what it actually does instead of claiming to display a token.Why
The flag is registered as "Display current API token". It cannot display anything — tokens are stored as SHA-256 hashes, and the command's own first two output lines say exactly that. What it does is destroy and reissue a credential:
GetApiTokencallsRecreateByName(cliFallbackTokenName), andcliFallbackTokenNameis the hardcoded"cli-fallback".RecreateByNamedeletes every row with that name and creates a new one.Two consequences:
HTTP 404with an empty body, which is indistinguishable from a wrongwebBasePath, so the failure never says what happened.This is not hypothetical in this repository:
install.shis one of the colliding callers, at lines 1231 and 1325:A hand-run
-getApiTokeninvalidates the token the installer just wrote into the install-result file, and re-running the installer invalidates the operator's.What changed
-tokenName <name>, defaulting tocli-fallback. Every existing invocation,install.shincluded, behaves exactly as before.-getApiTokendeliberately stays a boolean.install.shcalled it as-getApiToken true; withBoolVarthat trailingtrueis a positional, but aStringVarwould swallow it as the flag's value and mint a token literally namedtrue. That rules out the-getApiToken=<name>shape.-getApiToken trueform no longer swallows later flags. Go'sflagpackage stops parsing at the first non-flag argument, so that trailingtruedoes not merely get ignored — it terminates parsing. An operator copying the shape the repository itself demonstrated and runningx-ui setting -getApiToken true -tokenName ci-botlefttokenNameempty and rotatedcli-fallback: exactly the collision this PR removes, reached through the documented invocation. Bothinstall.shcall sites drop the straytrue, andsettingnow warns whenever it is handed positional arguments, naming what it ignored. A warning rather than an error, because an olderinstall.shin the wild still passestrueand has to keep working.-hannouncedcli-fallbackwhile the command actually mintsinstall, and nothing is regenerated or invalidated there at all.GetApiToken. The function has two: on a database with no tokens it callsCreate, otherwise it rotates viaRecreateByName. Without-tokenNameeach keeps the name it already used —installandcli-fallbackrespectively — so unnamed calls are byte-for-byte unchanged. With-tokenName ci-botboth branches useci-bot, so the same command no longer yields a different name depending on whether the database happens to be empty.RecreateByNamenow caps the name at 64 characters.Createalready enforces that on the same column;RecreateByNamedid not, and it now receives operator input. It has exactly one production caller, so nothing else can regress.Why the two default names were left alone
Unifying them looks tempting — the
installtoken is never rotated by the CLI, so it persists indefinitely — but it would break the case this PR exists to fix.install.shrecords the token it gets on a fresh panel into its install-result file. If both branches rotatedcli-fallback, the operator's next bare-getApiTokenwould destroy that exact row and silently invalidate the recorded credential. The split default is what keeps an unnamed call harmless;TestGetApiTokenPreservesInstallTokenWhenRotatingpins it by comparing the row's id and hash across a rotation.Not included, deliberately:
RecreateByNamenever setsScope, so its rows silently take the column default ofadmin, whileCreaterunsNormalizeScope. Exposing a-tokenScopeflag means fixing that asymmetry first — a privilege question that deserves its own PR and its own reasoning rather than riding along here.Type of change
Areas affected
How was this tested?
New
api_token_cli_test.go(packagemain, throwaway DB viaXUI_DB_FOLDER) plus one case added tointernal/web/service/panel/api_token_test.go. Each was checked to fail without the fix:TestGetApiTokenRotatesOnlyTheNamedTokenci-botmust leaveweekly-reportauthenticatingTestGetApiTokenUsesGivenNameOnEmptyDatabaseCreatebranchTestGetApiTokenPreservesInstallTokenWhenRotatinginstallrow's id and hash survive a later rotation, so a deleted-and-recreated row cannot passTestGetApiTokenTrimsNameTrimSpacefallbackTestGetApiTokenWarnsOnIgnoredPositionalArgs-getApiToken true -tokenName ci-botleavestokenNameempty and leaves the positional visible, so the CLI can warn rather than silently rotatecli-fallbackTestRecreateByNameRejectsOverlongNameManual, against a scratch database:
The installer's token is still there and untouched; the fallback slot rotated; the named one is its own row.
Screenshots / recordings
N/A — CLI only.
Breaking changes
None. Without
-tokenNamethe command does exactly what it did before on both branches —installon a fresh panel,cli-fallbackafterwards — soinstall.shneeds no change and no recorded credential is invalidated. The only additions are a new optional flag and a corrected help string.Checklist
go build ./...and the test suite pass locally.npm run lint,npm run typecheck, andnpm run buildpass. — N/A, no frontend change-getApiTokenis not documented underdocs/; the corrected-houtput is the only reference🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.