fix: make rate limiter fail closed when Redis is unreachable - #11
Merged
Conversation
Upstash Ratelimit's default 5s timeout resolves { success: true,
reason: 'timeout' } when Redis is unreachable, silently bypassing rate
limits. Add an explicit 1s timeout and have checkRateLimit translate the
timeout response (and any thrown error) into a deny, so limits can't be
bypassed by inducing Redis latency. Also drop the unused globalLimit.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
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
The Upstash rate limiter failed open: on its default 5s timeout it resolves
{ success: true, reason: 'timeout' }, so a Redis outage or induced latency let requests bypasssaveLimit/shareLimitentirely. This change makes the limiter fail closed, the correct default for an abuse-prevention control.Changes
timeout: 1000on the limiters (was defaulting to 5s).checkRateLimitnow denies on Upstash'stimeoutresponse and on any thrownerror, so limits cannot be bypassed by inducing Redis latency.
globalLimitexport.Trade-off
During a full Redis outage, save/share return
429rather than passing through. Impact is bounded to the outage window.Testing
Added
__tests__/rate-limit.test.ts(timeout, error, allow, deny). Full suite passing: 12 suites, 65 tests. Route handlers unchanged — the{ success, retryAfter }contract is preserved.