mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[PR #6915] [MERGED] perf(auth): look up api tokens by their hash index #6517
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#6517
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/6915
Author: @muditagrawal2007
Created: 10/23/2026
Status: ✅ Merged
Merged: 10/24/2026
Merged by: @MHSanaei
Base:
main← Head:fix/api-token-hash-index📝 Commits (3)
1473603perf(auth): look up api tokens by their hash indexc010325Merge remote-tracking branch 'origin/main' into pr/69150283d4frefactor(database): let AutoMigrate create the api token hash index📊 Changes
4 files changed (+66 additions, -22 deletions)
View changed files
📝
internal/database/index_tags_test.go(+2 -1)📝
internal/database/model/model.go(+1 -1)📝
internal/web/service/panel/api_token.go(+9 -20)📝
internal/web/service/panel/api_token_test.go(+54 -0)📄 Description
Summary
Every authenticated API request looked its bearer token up by loading every enabled token row and comparing the hashes one by one. The cost of a request grew with the number of tokens a panel had ever issued.
With an index on the stored hash, the lookup is a single seek that reads one row.
SCAN api_tokensSEARCH api_tokens USING INDEX idx_api_tokens_token (token=?)Why
internal/web/service/panel/api_token.go—MatchTokenis on the hot path of every/panel/api/*request:Three costs, all on the request path:
crypto.HashTokenSHA256), and a SHA-256 of a 48-character random token is not invertible. The guard was protecting a comparison that no longer needs to happen in Go at all.The fix is the obvious one: index the stored hash and let the database find the row.
Expiry and scope are still checked on the matched row, so a corrupt value is not treated as admin — the same ordering as before.
Evidence — red before, green after
Type of change
Areas affected
How was this tested?
Two new tests:
internal/database/api_token_index_test.go—TestMigrateApiTokenTokenIndexIsIdempotentAndUsed: the index exists afterInitDB, the migration is idempotent (it runs on every start, so its guard has to actually match the index it created), and the query plan for the token-hash lookup usesidx_api_tokens_token.internal/web/service/panel/api_token_lookup_test.go—TestMatchTokenReadsOnlyTheMatchingTokenRow: seeds 25 enabled tokens and asserts that the lookup reads at most one row. The row count is observed through a GORM query callback (tx.RowsAffected), so the assertion is about what the database actually returned, not about the shape of the query.Commands run locally:
Notes for the reviewer
hashExistingApiTokensrewrites it to the hash insiderunSeeders, which runs afterinitModels(internal/database/db.go:3133→:3149). A UNIQUE index built over the pre-hash values could fail on a duplicate plaintext, and the seeder'sUPDATEcould then violate it. A non-unique index has neither problem, and the seek still returns exactly one row: the stored value is the SHA-256 of a 48-character random token.migrateApiTokenTokenIndexsits with the other index migrations ininitModels, guarded byHasIndex, and the model tagindex:idx_api_tokens_tokengives new installs the index directly. This is the same pattern asmigrateTgIDIndexandmigrateClientEmailLowerIndex.TestAPITokenScopeExpiryAndExpectedRevokestill covers the scope and expiry semantics, andinternal/web/controller(which drives the real auth path) is unchanged.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.