-
-
Notifications
You must be signed in to change notification settings - Fork 30
Add rate-limit headers to the contact API #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
argyleink
wants to merge
2
commits into
shipshapecode:main
Choose a base branch
from
argyleink:fix/contact-rate-limit-headers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| /** | ||
| * Fixed-window rate limiting for API routes, scoped to a single deployed | ||
| * instance. Good enough to signal real limits on the write-side contact | ||
| * endpoint; not a substitute for a shared store (Upstash, Vercel KV, etc.) | ||
| * if this ever needs to hold across multiple instances/regions. | ||
| */ | ||
|
|
||
| interface RateLimitOptions { | ||
| limit: number; | ||
| windowMs: number; | ||
| } | ||
|
|
||
| interface RateLimitResult { | ||
| allowed: boolean; | ||
| limit: number; | ||
| remaining: number; | ||
| /** Epoch seconds the current window resets at. */ | ||
| resetAt: number; | ||
| } | ||
|
|
||
| const buckets = new Map<string, { count: number; resetAt: number }>(); | ||
|
|
||
| /** Drops any bucket whose window has already elapsed, so the map only ever | ||
| * holds entries for clients currently inside an active window - otherwise | ||
| * it grows forever, one entry per unique client key ever seen. */ | ||
| function pruneExpiredBuckets(now: number): void { | ||
| for (const [key, bucket] of buckets) { | ||
| if (bucket.resetAt <= now) { | ||
| buckets.delete(key); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export function checkRateLimit( | ||
| key: string, | ||
| { limit, windowMs }: RateLimitOptions | ||
| ): RateLimitResult { | ||
| const now = Date.now(); | ||
| pruneExpiredBuckets(now); | ||
| const bucket = buckets.get(key); | ||
|
|
||
| if (!bucket || bucket.resetAt <= now) { | ||
| const resetAt = now + windowMs; | ||
| buckets.set(key, { count: 1, resetAt }); | ||
| return { | ||
| allowed: true, | ||
| limit, | ||
| remaining: limit - 1, | ||
| resetAt: Math.ceil(resetAt / 1000) | ||
| }; | ||
| } | ||
|
|
||
| bucket.count += 1; | ||
|
|
||
| return { | ||
| allowed: bucket.count <= limit, | ||
| limit, | ||
| remaining: Math.max(0, limit - bucket.count), | ||
| resetAt: Math.ceil(bucket.resetAt / 1000) | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Derives a rate-limit key from the client's IP, falling back to a shared | ||
| * bucket if no forwarding header is present (e.g. local dev). | ||
| * | ||
| * Prefers `x-vercel-forwarded-for`: on Vercel (this project's target | ||
| * deployment), `x-forwarded-for` is already overwritten at the edge and | ||
| * client-supplied values are stripped, so both headers carry the same real | ||
| * IP there - but `x-vercel-forwarded-for` stays trustworthy even behind an | ||
| * extra reverse proxy in front of Vercel, where `x-forwarded-for` could be | ||
| * appended to instead of replaced. Deployed anywhere else (not Vercel), | ||
| * `x-forwarded-for` is only as trustworthy as whatever's terminating TLS in | ||
| * front of the app - fine for this project's actual target, worth keeping | ||
| * in mind for anyone self-hosting the OSS template differently. | ||
| */ | ||
| export function clientKey(request: Request): string { | ||
| const forwardedFor = | ||
| request.headers.get('x-vercel-forwarded-for') ?? | ||
| request.headers.get('x-forwarded-for'); | ||
| return forwardedFor?.split(',')[0]?.trim() || 'unknown'; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| export function rateLimitHeaders( | ||
| result: RateLimitResult | ||
| ): Record<string, string> { | ||
| const secondsUntilReset = Math.max( | ||
| 0, | ||
| result.resetAt - Math.floor(Date.now() / 1000) | ||
| ); | ||
| return { | ||
| 'RateLimit-Limit': String(result.limit), | ||
| 'RateLimit-Remaining': String(result.remaining), | ||
| // Per the IETF RateLimit header draft, Reset is seconds until the | ||
| // window resets (delta), not an absolute epoch timestamp. | ||
| 'RateLimit-Reset': String(secondsUntilReset) | ||
| }; | ||
| } | ||
|
|
||
| /** Test-only: clears all bucket state between test cases. */ | ||
| export function __resetRateLimitBucketsForTests(): void { | ||
| buckets.clear(); | ||
| } | ||
|
|
||
| /** Test-only: exposes the current bucket count to verify pruning. */ | ||
| export function __debugBucketCount(): number { | ||
| return buckets.size; | ||
| } | ||
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.