Repository navigation
Conversation
TokenCache cached a token status read even when a revocation landed between the DB read and the cache write. The revoked token then authenticated for the whole 5-minute TTL. UserCache already guards this with a storeMu + epoch check. Apply the same guard to TokenCache: capture the epoch before the DB read, write through storeIfCurrent, and bump the epoch on every invalidation. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ADYQncLu3WWTRt5cfBbRau
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of the Change
Problem
TokenCacheis the per-request check that an API token is still active.GetStatusreads the token row from the database, then writes the result into a 5-minute cache. The write is unconditional, so a revocation that lands between the read and the write is lost: a request readsstatus = active; an admin revokes the token (UpdateTokenwritesrevoked, then callsInvalidate); the in-flight request then writesactiveback into the cache. For the next 5 minutes every request serves the cachedactive, so the revoked token keeps authenticating until the entry expires.DeleteTokenhas the same shape. The user-status/hash check on the same auth path is not affected, because it goes throughUserCache, which already guards this race.Solution
Apply the
storeMu+epochguard thatUserCachealready uses.GetStatuscapturesepochbefore the DB read and writes both cache entries (found and not-found) through a newstoreIfCurrent, which stores only ifepochis unchanged.Invalidate,InvalidateUser, andInvalidateAllbumpepochunder the lock. The window drops from the full 5-minute TTL to the single in-flight request.No related issue.
Type of Change
Areas Affected
Testing and Verification
Test Configuration
Backend Go unit tests only; no running stack, keys, or deployment needed.
Test Steps
cd backend && go test ./pkg/server/auth/cd backend && go test ./pkg/server/auth/ -raceapi_token_cache.goand re-run the new test to see it fail on the old code.Test Results
go test ./pkg/server/auth/ -racepasses (full package).TestAPITokenCache_GetStatus_DoesNotCacheAReadOverlappingAnInvalidationforces a revocation between the read and the store with a gorm query callback (same technique as the existingUserCachetest). It fails on the current code (servesactive) and passes with the fix (servesrevoked).gofmt,go vet, andgolangci-lint(repo config) clean.Security Considerations
Fixes a revocation-bypass window: a revoked or deleted API token could keep authenticating for up to the 5-minute cache TTL. No new dependencies, no permission or schema changes. The change improves the security model and adds no new sensitive data handling.
Performance Impact
Negligible. The cache-hit path is unchanged. On a cache miss the change adds one mutex-guarded read of a counter before the DB query and one mutex-guarded compare on write; no new allocations. Matches
UserCache.Documentation Updates
Deployment Notes
None. No new environment variables, configuration, or migrations.
Checklist
Code Quality
go fmtandgo vet(for Go code)pnpm run lint(no frontend change)Security
Compatibility
Documentation
Additional Notes
This mirrors the existing
storeMu/epochguard inbackend/pkg/server/auth/users_cache.go; reviewing the two side by side is the quickest check.