[PR #6201] [MERGED] feat(api): scoped, optionally expiring API tokens #6162

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

📋 Pull Request Information

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

Base: main ← Head: upstream-api-token-scopes


📝 Commits (2)

  • e98afa3 security(api): add scoped expiring API tokens
  • c261225 security(api): make scoped token lifecycle enforceable

📊 Changes

19 files changed (+785 additions, -94 deletions)

View changed files

📝 docs/content/docs/en/reference/api/api-tokens.mdx (+27 -46)
📝 docs/public/openapi.json (+70 -6)
📝 frontend/public/openapi.json (+69 -7)
📝 frontend/src/generated/examples.ts (+4 -0)
📝 frontend/src/generated/schemas.ts (+21 -1)
📝 frontend/src/generated/types.ts (+4 -0)
📝 frontend/src/generated/zod.ts (+4 -0)
📝 frontend/src/pages/api-docs/endpoints.ts (+9 -4)
📝 frontend/src/pages/settings/SecurityTab.tsx (+4 -2)
📝 internal/database/api_token_timestamp_test.go (+31 -0)
📝 internal/database/db.go (+19 -0)
📝 internal/database/model/model.go (+12 -0)
📝 internal/web/controller/api.go (+96 -1)
📝 internal/web/controller/api_auth_test.go (+95 -7)
📝 internal/web/controller/setting.go (+17 -5)
📝 internal/web/controller/setting_test.go (+42 -0)
📝 internal/web/service/panel/api_token.go (+116 -14)
➕ internal/web/service/panel/api_token_scope_test.go (+144 -0)
📝 main.go (+1 -1)

📄 Description

Summary

Add scopes and expiry to API tokens: admin (legacy default), monitor, node-sync. Additive schema, existing tokens keep full access and never expire.

This is a proposal rather than a drop-in fix — it changes the token model, so I would rather agree the shape than land it unannounced.

Why

An API token is currently all-or-nothing and never expires. Anything that needs to read status, or a node that only needs to synchronise, has to be handed a credential that can do everything the panel can do, for as long as the row exists.

That leaves no way to hand out a read-only token for monitoring, to scope a node's credential to what synchronisation actually calls, or to issue anything time-bounded.

Scope

  • scope and expires_at on the token row. scope defaults to admin, expires_at=0 means never — so a legacy-shaped row keeps working exactly as today.
  • An unknown scope fails closed rather than falling back to admin, so a corrupt or future value cannot silently grant everything.
  • Explicit allowlists for monitor and node-sync rather than a deny-list.
  • Revocation takes an expected scope, so revoking cannot hit a token of a different class by accident.
  • The node-sync matrix includes hosts/list and the full clients/del/:email, which the current runtime actually calls.
  • server/updatePanel is deliberately not in the node-sync scope: remote self-update needs an admin token or a separate decision.

Validation

  • go test ./... green; targeted -race on the service and controller tests.
  • A raw legacy-shaped INSERT with neither new column resolves to admin/0 and continues to authenticate.
  • An unknown scope and an expired token both fail closed.
  • A verified client certificate resolves to node-sync, not admin.
  • The route matrix is compared in full, with dynamic parameters matched through FullPath rather than by string.

Not proven: the frontend has no controls for scope or expiry — the API accepts the fields and empty values preserve today's behaviour. A full master↔worker exchange using a node-sync token was not stood up.

Risk

None for existing installations: without the new fields, every token behaves as it does now. The parts worth arguing about are the scope boundaries themselves, and I would rather have that argument before the code than after — happy to cut it down to just monitor if the full matrix is more than you want.

Relation to #5769

This follows the design you asked for in #5769, specifically the preference for an audited allowlist over ad-hoc per-route checks. The scope names and the node allowlist here are meant to be that allowlist; if the naming or the granularity does not match what you had in mind, the boundaries are the cheapest part of this to change.

Update after review

Corrections from the review round, so the body matches the code:

  • provisioning is not a supported scope. An earlier draft of this description advertised it; NormalizeScope rejects it. The supported set is admin, monitor, node-sync.
  • Expiry is stored and returned as Unix milliseconds.
  • Revocation now requires a non-empty expected scope — an omitted value is no longer normalised to admin, and the scope-checked revocation path is wired into the actual delete endpoint rather than existing only for its test.
  • The new model fields ship with an explicit migration in internal/database/db.go (both dialects, legacy-row defaults) instead of relying on AutoMigrate.
  • frontend/public/openapi.json, docs/public/openapi.json and the generated API reference are regenerated and in sync.

🔄 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/6201 **Author:** [@n0ctal](https://github.com/n0ctal) **Created:** 8/10/2026 **Status:** ✅ Merged **Merged:** 8/15/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `upstream-api-token-scopes` --- ### 📝 Commits (2) - [`e98afa3`](https://github.com/MHSanaei/3x-ui/commit/e98afa3dfd7bcbd62b1f4de67fe5971f8fe98223) security(api): add scoped expiring API tokens - [`c261225`](https://github.com/MHSanaei/3x-ui/commit/c261225d2787d19138eba4a9a56ebed7803a0936) security(api): make scoped token lifecycle enforceable ### 📊 Changes **19 files changed** (+785 additions, -94 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/en/reference/api/api-tokens.mdx` (+27 -46) 📝 `docs/public/openapi.json` (+70 -6) 📝 `frontend/public/openapi.json` (+69 -7) 📝 `frontend/src/generated/examples.ts` (+4 -0) 📝 `frontend/src/generated/schemas.ts` (+21 -1) 📝 `frontend/src/generated/types.ts` (+4 -0) 📝 `frontend/src/generated/zod.ts` (+4 -0) 📝 `frontend/src/pages/api-docs/endpoints.ts` (+9 -4) 📝 `frontend/src/pages/settings/SecurityTab.tsx` (+4 -2) 📝 `internal/database/api_token_timestamp_test.go` (+31 -0) 📝 `internal/database/db.go` (+19 -0) 📝 `internal/database/model/model.go` (+12 -0) 📝 `internal/web/controller/api.go` (+96 -1) 📝 `internal/web/controller/api_auth_test.go` (+95 -7) 📝 `internal/web/controller/setting.go` (+17 -5) 📝 `internal/web/controller/setting_test.go` (+42 -0) 📝 `internal/web/service/panel/api_token.go` (+116 -14) ➕ `internal/web/service/panel/api_token_scope_test.go` (+144 -0) 📝 `main.go` (+1 -1) </details> ### 📄 Description ## Summary Add scopes and expiry to API tokens: `admin` (legacy default), `monitor`, `node-sync`. Additive schema, existing tokens keep full access and never expire. This is a proposal rather than a drop-in fix — it changes the token model, so I would rather agree the shape than land it unannounced. ## Why An API token is currently all-or-nothing and never expires. Anything that needs to read status, or a node that only needs to synchronise, has to be handed a credential that can do everything the panel can do, for as long as the row exists. That leaves no way to hand out a read-only token for monitoring, to scope a node's credential to what synchronisation actually calls, or to issue anything time-bounded. ## Scope - `scope` and `expires_at` on the token row. `scope` defaults to `admin`, `expires_at=0` means never — so a legacy-shaped row keeps working exactly as today. - An unknown scope fails closed rather than falling back to `admin`, so a corrupt or future value cannot silently grant everything. - Explicit allowlists for `monitor` and `node-sync` rather than a deny-list. - Revocation takes an expected scope, so revoking cannot hit a token of a different class by accident. - The node-sync matrix includes `hosts/list` and the full `clients/del/:email`, which the current runtime actually calls. - `server/updatePanel` is deliberately **not** in the node-sync scope: remote self-update needs an admin token or a separate decision. ## Validation - `go test ./...` green; targeted `-race` on the service and controller tests. - A raw legacy-shaped INSERT with neither new column resolves to `admin/0` and continues to authenticate. - An unknown scope and an expired token both fail closed. - A verified client certificate resolves to `node-sync`, not `admin`. - The route matrix is compared in full, with dynamic parameters matched through `FullPath` rather than by string. Not proven: the frontend has no controls for scope or expiry — the API accepts the fields and empty values preserve today's behaviour. A full master↔worker exchange using a node-sync token was not stood up. ## Risk None for existing installations: without the new fields, every token behaves as it does now. The parts worth arguing about are the scope boundaries themselves, and I would rather have that argument before the code than after — happy to cut it down to just `monitor` if the full matrix is more than you want. ## Relation to #5769 This follows the design you asked for in [#5769](https://github.com/MHSanaei/3x-ui/issues/5769), specifically the preference for an audited allowlist over ad-hoc per-route checks. The scope names and the node allowlist here are meant to be that allowlist; if the naming or the granularity does not match what you had in mind, the boundaries are the cheapest part of this to change. ## Update after review Corrections from the review round, so the body matches the code: - **`provisioning` is not a supported scope.** An earlier draft of this description advertised it; `NormalizeScope` rejects it. The supported set is `admin`, `monitor`, `node-sync`. - Expiry is stored and returned as **Unix milliseconds**. - Revocation now **requires a non-empty expected scope** — an omitted value is no longer normalised to `admin`, and the scope-checked revocation path is wired into the actual delete endpoint rather than existing only for its test. - The new model fields ship with an explicit migration in `internal/database/db.go` (both dialects, legacy-row defaults) instead of relying on `AutoMigrate`. - `frontend/public/openapi.json`, `docs/public/openapi.json` and the generated API reference are regenerated and in sync. --- <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:48 +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#6162
No description provided.