Skip to content

Harden Cloudflare Worker with rate limiting, structured errors, and logging#15

Open
vikvang wants to merge 1 commit intofarzaa:mainfrom
vikvang:fix/worker-hardening
Open

Harden Cloudflare Worker with rate limiting, structured errors, and logging#15
vikvang wants to merge 1 commit intofarzaa:mainfrom
vikvang:fix/worker-hardening

Conversation

@vikvang
Copy link
Copy Markdown

@vikvang vikvang commented Apr 8, 2026

Summary

Add per-IP sliding-window rate limiting, structured JSON error responses, and request logging to the Cloudflare Worker proxy.

Changes

  • Rate limiting: Per-IP sliding-window limiter on /chat (20 req/min) and /tts (30 req/min) using an in-memory Map. Uses CF-Connecting-IP header for IP detection. No external storage needed — acceptable trade-off for abuse-prevention (not billing-grade) limiting.
  • Structured errors: All error paths now return { "error": "...", "code": "RATE_LIMITED" | "UPSTREAM_ERROR" | "BAD_REQUEST" | "INTERNAL_ERROR" } with appropriate HTTP status codes. The client can programmatically handle failures.
  • Request logging: Every request logs method, route, IP, and response status via console.log for observability in wrangler tail.

Files changed

  • worker/src/index.ts — sole file modified

Conversation

Co-Authored-By: Oz oz-agent@warp.dev

…ogging

Add per-IP sliding-window rate limiting on /chat (20 req/min) and /tts
(30 req/min) to prevent abuse without external storage. All error paths
now return consistent JSON with { error, code } shape so the client can
programmatically handle failures. Every request is logged with method,
route, IP, and status for observability via wrangler tail.

Co-Authored-By: Oz <oz-agent@warp.dev>
@Daniyaalbeg
Copy link
Copy Markdown

Daniyaalbeg commented Apr 8, 2026

@vikvang this wont work well since it relies on the same isolate being hit everytime. Best to use the built in RATE_LIMITER instead or a KV for the map.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants