mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 19:11:53 +07:00
[PR #6201] [MERGED] feat(api): scoped, optionally expiring API tokens #6162
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#6162
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/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)
e98afa3security(api): add scoped expiring API tokensc261225security(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
scopeandexpires_aton the token row.scopedefaults toadmin,expires_at=0means never — so a legacy-shaped row keeps working exactly as today.admin, so a corrupt or future value cannot silently grant everything.monitorandnode-syncrather than a deny-list.hosts/listand the fullclients/del/:email, which the current runtime actually calls.server/updatePanelis deliberately not in the node-sync scope: remote self-update needs an admin token or a separate decision.Validation
go test ./...green; targeted-raceon the service and controller tests.admin/0and continues to authenticate.node-sync, notadmin.FullPathrather 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
monitorif 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:
provisioningis not a supported scope. An earlier draft of this description advertised it;NormalizeScoperejects it. The supported set isadmin,monitor,node-sync.admin, and the scope-checked revocation path is wired into the actual delete endpoint rather than existing only for its test.internal/database/db.go(both dialects, legacy-row defaults) instead of relying onAutoMigrate.frontend/public/openapi.json,docs/public/openapi.jsonand 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.