mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 19:11:53 +07:00
[PR #6700] [MERGED] fix(api-token): keep a token's scope when -getApiToken regenerates it, add -tokenScope #6418
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#6418
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/6700
Author: @rokokol
Created: 9/30/2026
Status: ✅ Merged
Merged: 10/2/2026
Merged by: @MHSanaei
Base:
main← Head:fix/token-scope-recreate📝 Commits (4)
1f57abffix(api-token): keep a token's scope when the CLI regenerates itb40d23dfeat(cli): let -getApiToken choose the scope of the token it issues84024d6Merge branch 'main' into fix/token-scope-recreatea800553fix(api-token): keep a token's expiry when the CLI regenerates it📊 Changes
4 files changed (+229 additions, -19 deletions)
View changed files
📝
api_token_cli_test.go(+23 -7)📝
internal/web/service/panel/api_token.go(+32 -2)📝
internal/web/service/panel/api_token_test.go(+168 -6)📝
main.go(+6 -4)📄 Description
Summary
x-ui setting -getApiTokenno longer turns a regenerated token into an admin token. A new-tokenScopeflag sets the scope of the token it issues.Why
RecreateByNamedeletes the named row and creates a new one without aScope. The insert then takes the column default,admin(model.ApiToken.Scope,default:admin).Createdoes not have this problem, because it runsNormalizeScope.Since #6405,
-tokenNamelets the CLI regenerate any token, not onlycli-fallback. So this sequence silently gives full access to a token that was meant to be read-only:grafanawith scopemonitorx-ui setting -getApiToken -tokenName grafanato rotate itgrafanatoken has scopeadmin, and the command says nothing about it#6405 left the scope out deliberately and named this asymmetry as a separate privilege question. This PR is that follow-up.
What changed
RecreateByNamekeeps the scope of the row it replaces. A new name still getsadmin, as before. If the stored scope is one this build does not know, for example after a downgrade from a build with more scopes, the rotation fails withtoken "NAME" has unknown scope "SCOPE"and leaves the row alone. It does not guessadmin-tokenScope admin|monitor|node-syncsets the scope on both branches of-getApiToken: the token minted on a fresh panel and the one regenerated on a populated panel. An invalid value is refused before anything is deleted, so a typo cannot revoke the token it meant to rotate-tokenScope, every existing invocation behaves as before,install.shincluded: its fresh-panel token is stilladminThe two commits are separate: the fix first, then the flag on top of it.
Type of change
Areas affected
How was this tested?
New tests, each seen failing before its code was written:
TestRecreateByNameKeepsReplacedTokenScope: amonitortoken rotated without a scope staysmonitor. It failed onmainwithstored scope = "admin", want "monitor"TestRecreateByNameRefusesUnknownStoredScope: a storednode-adminrow makes the rotation fail with the exact error, and the row keeps its id, hash and scopeTestRecreateByNameAppliesGivenScope: an explicit scope replaces the stored one, applies to a new name, and an invalid one is refused while the old token still authenticatesTestGetApiTokenAppliesGivenScope:-tokenScopereaches both the fresh-panel branch (Create) and the rotation branch (RecreateByName)With the fix commit's
api_token.goreverted and its tests kept, theRecreateByNametests go red.All pass, lint reports 0 issues. Not run locally:
make verify(no frontend change),make race, and the Postgres job.Breaking changes
None for the CLI: without
-tokenScopea new token is stilladmin. A regenerated token now keeps its scope instead of becomingadmin, which is the fix.RecreateByNametakes a secondscopeargument; its only production caller isGetApiToken.Checklist
go build ./...and the test suite pass locally.npm run lint,npm run typecheck, andnpm run buildpass.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.