[PR #6700] [MERGED] fix(api-token): keep a token's scope when -getApiToken regenerates it, add -tokenScope #6418

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

📋 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)

  • 1f57abf fix(api-token): keep a token's scope when the CLI regenerates it
  • b40d23d feat(cli): let -getApiToken choose the scope of the token it issues
  • 84024d6 Merge branch 'main' into fix/token-scope-recreate
  • a800553 fix(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 -getApiToken no longer turns a regenerated token into an admin token. A new -tokenScope flag sets the scope of the token it issues.

Why

RecreateByName deletes the named row and creates a new one without a Scope. The insert then takes the column default, admin (model.ApiToken.Scope, default:admin). Create does not have this problem, because it runs NormalizeScope.

Since #6405, -tokenName lets the CLI regenerate any token, not only cli-fallback. So this sequence silently gives full access to a token that was meant to be read-only:

  1. In the panel, create a token grafana with scope monitor
  2. Run x-ui setting -getApiToken -tokenName grafana to rotate it
  3. The new grafana token has scope admin, 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

  • RecreateByName keeps the scope of the row it replaces. A new name still gets admin, 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 with token "NAME" has unknown scope "SCOPE" and leaves the row alone. It does not guess admin
  • -tokenScope admin|monitor|node-sync sets 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
  • Without -tokenScope, every existing invocation behaves as before, install.sh included: its fresh-panel token is still admin

The two commits are separate: the fix first, then the flag on top of it.

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Documentation
  • Tests only
  • Build / CI / tooling
  • Other

Areas affected

  • Frontend (UI / panel pages)
  • Backend (API endpoints, login, settings)
  • Xray config generation
  • Subscription (share links / Clash / JSON)
  • Statistics / traffic counters
  • Database / migrations
  • Install / upgrade script
  • Docker image
  • Multi-node (sub-nodes)
  • Telegram bot

How was this tested?

New tests, each seen failing before its code was written:

  • TestRecreateByNameKeepsReplacedTokenScope: a monitor token rotated without a scope stays monitor. It failed on main with stored scope = "admin", want "monitor"
  • TestRecreateByNameRefusesUnknownStoredScope: a stored node-admin row makes the rotation fail with the exact error, and the row keeps its id, hash and scope
  • TestRecreateByNameAppliesGivenScope: an explicit scope replaces the stored one, applies to a new name, and an invalid one is refused while the old token still authenticates
  • TestGetApiTokenAppliesGivenScope: -tokenScope reaches both the fresh-panel branch (Create) and the rotation branch (RecreateByName)

With the fix commit's api_token.go reverted and its tests kept, the RecreateByName tests go red.

go test . ./internal/web/service/panel/ -count=1 -shuffle=on
go test ./... -count=1 -shuffle=on
golangci-lint run . ./internal/web/service/panel/

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 -tokenScope a new token is still admin. A regenerated token now keeps its scope instead of becoming admin, which is the fix. RecreateByName takes a second scope argument; its only production caller is GetApiToken.

Checklist

  • I tested the change locally and confirmed the described behavior.
  • I added or updated tests for the new behavior (when applicable).
  • go build ./... and the test suite pass locally.
  • For frontend changes: npm run lint, npm run typecheck, and npm run build pass.
  • I updated the Wiki / README / API docs if user-facing behavior changed.
  • My commits follow the project's existing message style.
  • I have no unrelated changes mixed into this PR.

🔄 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/6700 **Author:** [@rokokol](https://github.com/rokokol) **Created:** 9/30/2026 **Status:** ✅ Merged **Merged:** 10/2/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/token-scope-recreate` --- ### 📝 Commits (4) - [`1f57abf`](https://github.com/MHSanaei/3x-ui/commit/1f57abf5546bb0646ff90772e78c94115d54bbdc) fix(api-token): keep a token's scope when the CLI regenerates it - [`b40d23d`](https://github.com/MHSanaei/3x-ui/commit/b40d23d04a33c11305dcb645e447002b30b22252) feat(cli): let -getApiToken choose the scope of the token it issues - [`84024d6`](https://github.com/MHSanaei/3x-ui/commit/84024d69224fc6b32f6e471de6d03d9f325db8c7) Merge branch 'main' into fix/token-scope-recreate - [`a800553`](https://github.com/MHSanaei/3x-ui/commit/a800553e21adfc2dc9e83ecd22e0a9bd56c03533) fix(api-token): keep a token's expiry when the CLI regenerates it ### 📊 Changes **4 files changed** (+229 additions, -19 deletions) <details> <summary>View changed files</summary> 📝 `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) </details> ### 📄 Description ## Summary `x-ui setting -getApiToken` no longer turns a regenerated token into an admin token. A new `-tokenScope` flag sets the scope of the token it issues. ## Why `RecreateByName` deletes the named row and creates a new one without a `Scope`. The insert then takes the column default, `admin` (`model.ApiToken.Scope`, `default:admin`). `Create` does not have this problem, because it runs `NormalizeScope`. Since #6405, `-tokenName` lets the CLI regenerate any token, not only `cli-fallback`. So this sequence silently gives full access to a token that was meant to be read-only: 1. In the panel, create a token `grafana` with scope `monitor` 2. Run `x-ui setting -getApiToken -tokenName grafana` to rotate it 3. The new `grafana` token has scope `admin`, 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 - **`RecreateByName` keeps the scope of the row it replaces.** A new name still gets `admin`, 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 with `token "NAME" has unknown scope "SCOPE"` and leaves the row alone. It does not guess `admin` - **`-tokenScope admin|monitor|node-sync`** sets 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 - Without `-tokenScope`, every existing invocation behaves as before, `install.sh` included: its fresh-panel token is still `admin` The two commits are separate: the fix first, then the flag on top of it. ## Type of change - [x] Bug fix - [x] New feature - [ ] Refactoring (no behavior change) - [ ] Documentation - [ ] Tests only - [ ] Build / CI / tooling - [ ] Other ## Areas affected - [ ] Frontend (UI / panel pages) - [x] Backend (API endpoints, login, settings) - [ ] Xray config generation - [ ] Subscription (share links / Clash / JSON) - [ ] Statistics / traffic counters - [ ] Database / migrations - [ ] Install / upgrade script - [ ] Docker image - [ ] Multi-node (sub-nodes) - [ ] Telegram bot ## How was this tested? New tests, each seen failing before its code was written: - `TestRecreateByNameKeepsReplacedTokenScope`: a `monitor` token rotated without a scope stays `monitor`. It failed on `main` with `stored scope = "admin", want "monitor"` - `TestRecreateByNameRefusesUnknownStoredScope`: a stored `node-admin` row makes the rotation fail with the exact error, and the row keeps its id, hash and scope - `TestRecreateByNameAppliesGivenScope`: an explicit scope replaces the stored one, applies to a new name, and an invalid one is refused while the old token still authenticates - `TestGetApiTokenAppliesGivenScope`: `-tokenScope` reaches both the fresh-panel branch (`Create`) and the rotation branch (`RecreateByName`) With the fix commit's `api_token.go` reverted and its tests kept, the `RecreateByName` tests go red. ```sh go test . ./internal/web/service/panel/ -count=1 -shuffle=on go test ./... -count=1 -shuffle=on golangci-lint run . ./internal/web/service/panel/ ``` 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 `-tokenScope` a new token is still `admin`. A regenerated token now keeps its scope instead of becoming `admin`, which is the fix. `RecreateByName` takes a second `scope` argument; its only production caller is `GetApiToken`. ## Checklist - [x] I tested the change locally and confirmed the described behavior. - [x] I added or updated tests for the new behavior (when applicable). - [x] `go build ./...` and the test suite pass locally. - [ ] For frontend changes: `npm run lint`, `npm run typecheck`, and `npm run build` pass. - [x] I updated the Wiki / README / API docs if user-facing behavior changed. - [x] My commits follow the project's existing message style. - [x] I have no unrelated changes mixed into this PR. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:08: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#6418
No description provided.