fix: add ratelimiting key - #57
Conversation
WalkthroughReplaces per-URL rate-limiting with a composite key (METHOD:IP:PATHNAME) via a new utility and updates analytics, claim, and auth callback routes plus the create-db worker to use it; limiter calls now use the composite key and return structured 429 JSON when throttled, with create-db worker failing open if limiter errors. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor Client
participant Route as API Route (analytics/claim/auth-callback/create)
participant Util as ratelimit.ts (buildRateLimitKey)
participant RL as RateLimiter
participant Service as Downstream Service
Client->>Route: HTTP request
Route->>Util: buildRateLimitKey(request)
Util-->>Route: "METHOD:IP:PATH"
Route->>RL: limit({ key })
alt Throttled
RL-->>Route: limited=true
Route-->>Client: 429 JSON { error:"rate_limited", message, path }
else Allowed or limiter error (fail-open)
RL-->>Route: limited=false / error -> treated as allowed
Route->>Service: proceed with existing flow (analytics/auth/claim/create)
Service-->>Route: result
Route-->>Client: route-specific response
end
Possibly related PRs
Suggested reviewers
Pre-merge checks and finishing touches and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
Comment |
|
✅ Preview CLIs & Workers are live! Test the CLIs locally under tag npx create-db@pr57
npx create-pg@pr57
npx create-postgres@$pr57Worker URLs
|
Deploying with
|
| Status | Name | Latest Commit | Preview URL | Updated (UTC) |
|---|---|---|---|---|
| ✅ Deployment successful! View logs |
claim-db-worker | 123669d | Commit Preview URL Branch Preview URL |
Sep 17 2025, 03:28 PM |
There was a problem hiding this comment.
Actionable comments posted: 9
📜 Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro
📒 Files selected for processing (3)
claim-db-worker/app/api/analytics/route.ts(1 hunks)claim-db-worker/app/api/claim/route.ts(1 hunks)claim-db-worker/lib/utils.ts(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
claim-db-worker/app/api/analytics/route.ts (2)
claim-db-worker/lib/env.ts (1)
getEnv(11-39)claim-db-worker/lib/utils.ts (1)
getClientIP(28-36)
claim-db-worker/app/api/claim/route.ts (3)
claim-db-worker/app/api/auth/callback/route.ts (1)
GET(46-174)claim-db-worker/lib/env.ts (1)
getEnv(11-39)claim-db-worker/lib/utils.ts (1)
getClientIP(28-36)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Workers Builds: create-db-worker
- GitHub Check: Workers Builds: claim-db-worker
|
✅ Preview CLIs & Workers are live! Test the CLIs locally under tag npx create-db@pr57
npx create-pg@pr57
npx create-postgres@$pr57Worker URLs
|
|
✅ Preview CLIs & Workers are live! Test the CLIs locally under tag npx create-db@pr57
npx create-pg@pr57
npx create-postgres@$pr57Worker URLs
|
There was a problem hiding this comment.
Actionable comments posted: 6
♻️ Duplicate comments (3)
claim-db-worker/app/api/analytics/route.ts (1)
11-21: ExposeRetry-After/rate-limit headers on 429If the limiter returns reset/ttl, surface it to clients for better backoff.
- const { success } = await env.CLAIM_DB_RATE_LIMITER.limit({ key }); + const { success, reset } = await env.CLAIM_DB_RATE_LIMITER.limit({ key }); if (!success) { - return NextResponse.json( + const headers = + typeof reset === "number" + ? { "Retry-After": String(Math.max(0, Math.ceil((reset - Date.now()) / 1000))) } + : undefined; + return NextResponse.json( { error: "rate_limited", message: "Rate limit exceeded. Please try again later.", path: url.pathname, }, - { status: 429 } + { status: 429, headers } ); }claim-db-worker/app/api/claim/route.ts (2)
12-22: AddRetry-Afterheader on 429Surface limiter reset/ttl to help clients respect backoff.
- const { success } = await env.CLAIM_DB_RATE_LIMITER.limit({ key }); + const { success, reset } = await env.CLAIM_DB_RATE_LIMITER.limit({ key }); if (!success) { - return NextResponse.json( + const headers = + typeof reset === "number" + ? { "Retry-After": String(Math.max(0, Math.ceil((reset - Date.now()) / 1000))) } + : undefined; + return NextResponse.json( { error: "rate_limited", message: "Rate limit exceeded. Please try again later.", path: url.pathname, }, - { status: 429 } + { status: 429, headers } ); }
25-25: NormalizeprojectIDand drop the"undefined"sentinel checkTrim whitespace and coalesce empty/“undefined” to null; then remove the literal comparison.
- const projectID = url.searchParams.get("projectID"); + const rawProjectID = url.searchParams.get("projectID"); + const projectID = + rawProjectID && rawProjectID.trim() && rawProjectID.trim().toLowerCase() !== "undefined" + ? rawProjectID.trim() + : null;Then:
- if (!projectID || projectID === "undefined") { + if (!projectID) { return NextResponse.json({ error: "Missing project ID" }, { status: 400 }); }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro
📒 Files selected for processing (4)
claim-db-worker/app/api/analytics/route.ts(1 hunks)claim-db-worker/app/api/auth/callback/route.ts(3 hunks)claim-db-worker/app/api/claim/route.ts(1 hunks)claim-db-worker/lib/server/ratelimit.ts(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (3)
claim-db-worker/app/api/analytics/route.ts (2)
claim-db-worker/lib/env.ts (1)
getEnv(11-39)claim-db-worker/lib/server/ratelimit.ts (1)
buildRateLimitKey(14-17)
claim-db-worker/app/api/claim/route.ts (3)
claim-db-worker/app/api/auth/callback/route.ts (1)
GET(47-179)claim-db-worker/lib/env.ts (1)
getEnv(11-39)claim-db-worker/lib/server/ratelimit.ts (1)
buildRateLimitKey(14-17)
claim-db-worker/app/api/auth/callback/route.ts (1)
claim-db-worker/lib/server/ratelimit.ts (1)
buildRateLimitKey(14-17)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Workers Builds: create-db-worker
- GitHub Check: Workers Builds: claim-db-worker
🔇 Additional comments (1)
claim-db-worker/lib/server/ratelimit.ts (1)
1-16: No drift detected: all limiter calls use buildRateLimitKeyVerified that the claim, auth callback, and analytics routes import and use buildRateLimitKey(request) for the CLAIM_DB_RATE_LIMITER.limit key.
There was a problem hiding this comment.
Actionable comments posted: 5
📜 Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro
📒 Files selected for processing (1)
create-db-worker/src/index.ts(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
create-db-worker/src/index.ts (3)
create-db/index.js (3)
url(326-326)res(124-124)res(327-327)claim-db-worker/worker-configuration.d.ts (1)
env(6913-6913)create-db-worker/worker-configuration.d.ts (1)
env(6794-6794)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Workers Builds: claim-db-worker
- GitHub Check: Workers Builds: create-db-worker
🔇 Additional comments (1)
create-db-worker/src/index.ts (1)
18-19: LGTM on moving to a composite rate-limit key.Keying by method:IP:pathname is a solid improvement and aligns with the PR objective.
|
✅ Preview CLIs & Workers are live! Test the CLIs locally under tag npx create-db@pr57
npx create-pg@pr57
npx create-postgres@$pr57Worker URLs
|
94d0a69 to
123669d
Compare
|
✅ Preview CLIs & Workers are live! Test the CLIs locally under tag npx create-db@pr57
npx create-pg@pr57
npx create-postgres@$pr57Worker URLs
|
b0058bb to
123669d
Compare
|
✅ Preview CLIs & Workers are live! Test the CLIs locally under tag npx create-db@pr57
npx create-pg@pr57
npx create-postgres@$pr57Worker URLs
|
|
✅ Preview CLIs & Workers are live! Test the CLIs locally under tag npx create-db@pr57
npx create-pg@pr57
npx create-postgres@$pr57Worker URLs
|
Summary by CodeRabbit
New Features
Bug Fixes
Tests
Chores