feat(backend): replace in-memory challenge store with Redis-backed persistence#282
Merged
0xDeon merged 1 commit intoApr 23, 2026
Conversation
Fixes three problems in the wallet auth flow: - Server restarts wiped all pending challenges, forcing users to retry - Horizontal scaling caused cross-instance auth failures (instance A generated the challenge; instance B couldn't verify it) - Unbounded map growth allowed challenge-flood DoS by OOM Changes: - Introduces a ChallengeStore interface (Set / GetAndDelete) in internal/service - RedisChallengeStore uses GETDEL (atomic get-and-delete, prevents replay) and lets Redis enforce the TTL natively - InMemoryChallengeStore is the single-instance fallback when REDIS_ADDR is unset; also used in all unit tests - authService no longer owns a sync.RWMutex map; it delegates to the store - config: optional REDIS_ADDR env var (empty = in-memory) - main.go selects the store at startup and logs which backend is active - docker-compose.yml adds a Redis 7 service; API depends on it being healthy - .env.example documents REDIS_ADDR Closes Suncrest-Labs#253
|
@Salmatcre8 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
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.
Summary
ChallengeStoreinterface withSet/GetAndDeletethatauthServicedelegates to — removing thesync.RWMutex+mapit previously ownedRedisChallengeStoreusesGETDEL(atomic get-and-delete) so a challenge can only be verified once even under concurrent requests; TTL is enforced by Redis nativelyInMemoryChallengeStoreis retained as a single-instance fallback (and for tests) — active whenREDIS_ADDRis emptymain.gobuilds the store at startup and logs which backend is activedocker-compose.ymladds a Redis 7 Alpine service; the API service waits for it to be healthy before startingapps/api/.env.exampledocumentsREDIS_ADDRWhy each problem is now fixed
Test plan
InMemoryChallengeStore: 6 unit tests (set/get, one-time use, missing key, expiry, overwrite, wallet isolation)authServicetests updated to pass a store; all 6 existing tests still pass logic-unchangedGOOS=linux go build ./...cleanGOOS=linux go test -c ./internal/service/compiles with no errorsCloses #253