Skip to content

BE-16: Implement Redis-backed API rate limiting to prevent DoS attacks - #929

Merged
chinweobtagaz merged 1 commit into
OpenKnight-Foundation:mainfrom
emmyafolly123-coder:fix/be-16-rate-limiting-redis
Jul 30, 2026
Merged

BE-16: Implement Redis-backed API rate limiting to prevent DoS attacks#929
chinweobtagaz merged 1 commit into
OpenKnight-Foundation:mainfrom
emmyafolly123-coder:fix/be-16-rate-limiting-redis

Conversation

@emmyafolly123-coder

Copy link
Copy Markdown
Contributor

Overview

This PR adds a Redis-backed rate limiting middleware for actix-web that tracks request counts per client IP using Redis INCR + EXPIRE. Unlike the existing in-memory actix-governor (which provides per-second burst protection per worker), the Redis-backed limiter shares state across all server workers, making it suitable for sliding-window limits like "5 requests per minute" on auth endpoints.

Closes #865

Changes

Redis Rate Limiter Middleware

  • [ADD] backend/modules/api/src/rate_limiter.rsRedisRateLimiter actix-web Transform/Service middleware
    • Uses deadpool-redis connection pool for non-blocking Redis access
    • Sliding window via INCR + EXPIRE (auto-set on first request)
    • Falls open (allows request + logs warning) if Redis is unavailable
    • Returns HTTP 429 with X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset headers

Applied Limits

Endpoint Scope Limit Window
/v1/auth (login, register, refresh, logout) 5 requests 60 seconds
/v1/games (create, join, move, etc.) 30 requests 60 seconds
Existing actix-governor Per-second burst N/A

Environment Configuration

  • REDIS_AUTH_RATE_LIMIT (default: 5)
  • REDIS_AUTH_RATE_LIMIT_WINDOW (default: 60)
  • REDIS_GAME_RATE_LIMIT (default: 30)
  • REDIS_GAME_RATE_LIMIT_WINDOW (default: 60)

Dependencies

  • deadpool-redis = "0.14" added to backend/modules/api/Cargo.toml

Verification Results

cargo check -p api   -> 0 errors, 0 warnings
cargo test -p api    -> 12/12 passed
Test Status
Requests under limit allowed
Requests exceeding limit blocked with 429
Rate limit headers present (X-RateLimit-*)
Different IPs have independent counters
Existing actix-governor tests unaffected
Existing player/game/WS tests unaffected

Add a RedisRateLimiter middleware for actix-web that tracks request
counts per client IP using Redis INCR + EXPIRE, ensuring state is
shared across all server workers.

- Added rate_limiter.rs: generic Redis-backed middleware with
  configurable per-window limits and sliding window via INCR/EXPIRE
- Applied 5 req/min limit to /v1/auth (login, register, refresh, logout)
- Applied 30 req/min limit to /v1/games
- Added env config: REDIS_AUTH_RATE_LIMIT, REDIS_AUTH_RATE_LIMIT_WINDOW,
  REDIS_GAME_RATE_LIMIT, REDIS_GAME_RATE_LIMIT_WINDOW
- Kept existing actix-governor middleware for per-second burst protection
- Includes 4 unit tests: under-limit, exceeded, headers, independent IPs

Closes OpenKnight-Foundation#865
@drips-wave

drips-wave Bot commented Jul 30, 2026

Copy link
Copy Markdown

@emmyafolly123-coder Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@chinweobtagaz
chinweobtagaz merged commit 6091b3f into OpenKnight-Foundation:main Jul 30, 2026
3 checks passed
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.

BE-16: Implement API rate limiting using Redis to prevent DoS attacks

2 participants