[kimi-task] [enhancement] Add rate limiting middleware for Matrix API endpoints #683

Closed
opened 2026-03-21 03:12:14 +00:00 by kimi · 1 comment
Collaborator

Epic: Matrix Unification

Task

Add simple rate limiting to the /api/matrix/* endpoints to prevent abuse when deployed publicly.

Files

  • src/dashboard/middleware/rate_limit.py — NEW: simple in-memory rate limiter
  • src/dashboard/app.py — apply rate limiter to /api/matrix/* routes
  • tests/unit/test_rate_limit.py — NEW: test rate limiting behavior

Acceptance Criteria

  • RateLimiter class with configurable requests_per_minute per IP
  • Default: 30 requests/minute for /api/matrix/* endpoints
  • Returns 429 Too Many Requests when exceeded
  • In-memory (dict), no external dependencies
  • Cleanup of stale entries every 60 seconds
  • tox -e unit passes
  • tox -e lint passes

Context

When the Matrix becomes the public frontend at alexanderwhitestone.com,
the API endpoints need basic DoS protection. This is a simple in-memory rate limiter;
Redis-backed can come later if needed.

## Epic: Matrix Unification ## Task Add simple rate limiting to the /api/matrix/* endpoints to prevent abuse when deployed publicly. ## Files - src/dashboard/middleware/rate_limit.py — NEW: simple in-memory rate limiter - src/dashboard/app.py — apply rate limiter to /api/matrix/* routes - tests/unit/test_rate_limit.py — NEW: test rate limiting behavior ## Acceptance Criteria - [ ] RateLimiter class with configurable requests_per_minute per IP - [ ] Default: 30 requests/minute for /api/matrix/* endpoints - [ ] Returns 429 Too Many Requests when exceeded - [ ] In-memory (dict), no external dependencies - [ ] Cleanup of stale entries every 60 seconds - [ ] tox -e unit passes - [ ] tox -e lint passes ## Context When the Matrix becomes the public frontend at alexanderwhitestone.com, the API endpoints need basic DoS protection. This is a simple in-memory rate limiter; Redis-backed can come later if needed.
kimi self-assigned this 2026-03-21 03:12:14 +00:00
Author
Collaborator

PR created: http://143.198.27.163:3000/rockachopa/Timmy-time-dashboard/pulls/746

Summary of Changes

New Files

  • src/dashboard/middleware/rate_limit.py - RateLimiter class and RateLimitMiddleware
  • tests/unit/test_rate_limit.py - 30 test cases covering all functionality

Modified Files

  • src/dashboard/middleware/__init__.py - Export new rate limiting classes
  • src/dashboard/app.py - Apply middleware to /api/matrix/* routes

Implementation Details

  • RateLimiter class: Tracks requests per IP using deque for O(1) popleft operations
  • Sliding window: 60-second window with automatic cleanup every 60 seconds
  • Proxy support: Respects X-Forwarded-For and X-Real-IP headers
  • 429 Response: Returns proper Retry-After header when rate limited
  • No external dependencies: Pure in-memory implementation

Verification

  • tox -e unit passes (2620 passed, 1 skipped)
  • tox -e lint passes (all checks clean)
  • tox -e pre-push passes (full CI gate)
PR created: http://143.198.27.163:3000/rockachopa/Timmy-time-dashboard/pulls/746 ## Summary of Changes ### New Files - `src/dashboard/middleware/rate_limit.py` - RateLimiter class and RateLimitMiddleware - `tests/unit/test_rate_limit.py` - 30 test cases covering all functionality ### Modified Files - `src/dashboard/middleware/__init__.py` - Export new rate limiting classes - `src/dashboard/app.py` - Apply middleware to `/api/matrix/*` routes ## Implementation Details - **RateLimiter class**: Tracks requests per IP using `deque` for O(1) popleft operations - **Sliding window**: 60-second window with automatic cleanup every 60 seconds - **Proxy support**: Respects `X-Forwarded-For` and `X-Real-IP` headers - **429 Response**: Returns proper `Retry-After` header when rate limited - **No external dependencies**: Pure in-memory implementation ## Verification - `tox -e unit` passes (2620 passed, 1 skipped) - `tox -e lint` passes (all checks clean) - `tox -e pre-push` passes (full CI gate)
kimi closed this issue 2026-03-21 16:23:17 +00:00
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Rockachopa/Timmy-time-dashboard#683