Skip to content

Commit 7351e4a

Browse files
committed
fix: enhance rate limiting by incorporating user ID and IP address in the key
1 parent df07d07 commit 7351e4a

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

cloudflare-worker/src/routes/router.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,24 @@ export class Router {
192192
const { pathname } = url;
193193

194194
// Apply rate limiting
195-
const { success } = await env.RATE_LIMITER.limit({ key: pathname });
195+
const ip = request.headers.get("CF-Connecting-IP") || "unknown";
196+
const userId = this.jwtPayload.sub || "anonymous";
197+
const endpoint = pathname;
198+
const rateLimitKey = `${ip}:${userId}:${endpoint}`;
199+
const { success } = await env.RATE_LIMITER.limit({ key: rateLimitKey });
196200

197201
if (!success) {
198202
return new Response(
199203
JSON.stringify(`429 Failure – rate limit exceeded for ${pathname}`),
200204
{
201205
status: 429,
202-
headers: { ...this.corsHeaders },
206+
headers: {
207+
...this.corsHeaders,
208+
"X-RateLimit-Limit": "10",
209+
"X-RateLimit-Remaining": "0",
210+
"X-RateLimit-Reset": "60",
211+
"Retry-After": "60",
212+
},
203213
},
204214
);
205215
}

0 commit comments

Comments
 (0)