Skip to content

Add rate limiting to the analytics events endpoint #200

@3m1n3nc3

Description

@3m1n3nc3

Description

POST /api/analytics/events accepts event tracking requests with no authentication and no rate limiting. Any client (or bot) can flood this endpoint with thousands of events per second, filling up the AnalyticsEvent table, degrading database performance, and skewing all metrics.

The userId field is taken from the x-user-id request header which is entirely client-controlled — any user can claim any other user's ID in analytics events.

More info

  • Add IP-based rate limiting using a middleware solution (e.g. @upstash/ratelimit with Upstash Redis, or a simple in-memory token bucket for development).
  • For the x-user-id header, replace the client-controlled header with a server-side session lookup: call auth() and use session?.user?.id instead of trusting the header.
  • Consider batching analytics events on the client side (collect for 5s, then send a batch) to reduce request volume.

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions