Feature/api rate limiting - #56
Merged
Merged
Conversation
Adds multi-tier rate limiting with Flask-Limiter and Redis. Backend: Rate limits on auth, strokes, rooms Frontend: Auto-retry with exponential backoff Tests: 20+ comprehensive unit tests Docs: Complete documentation Security: Prevents DoS, brute force, spam
Adds multi-tier rate limiting with Flask-Limiter and Redis. Backend: Rate limits on auth, strokes, rooms Frontend: Auto-retry with exponential backoff Tests: 20+ comprehensive unit tests Docs: Complete documentation Security: Prevents DoS, brute force, spam
Contributor
Author
|
@bchou9 Please review the PR |
Contributor
Author
|
@bchou9 Please add label of 'hacktoberfest accepted' to this PR |
Contributor
Author
|
@bchou9 Please take a look at this |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

🛡️ Comprehensive API Rate Limiting
Fix #34
Summary
Implements multi-tier rate limiting to protect ResCanvas API from abuse, DoS attacks, and ensure fair resource allocation. Uses Flask-Limiter with Redis for distributed enforcement across backend instances.
Problem
ResCanvas API had zero rate limiting, exposing critical vulnerabilities:
Solution
🔐 Multi-Tier Rate Limits
5000/hr (auth)
POST /auth/loginPOST /auth/registerPOST /auth/refreshPOST /submitNewLineRoomPOST /rooms/{id}/undoPOST /roomsPOST /submitClearCanvasTimestampGET /users/suggest🏗️ Implementation
Backend (Flask-Limiter + Redis)
Frontend (Auto-retry with exponential backoff)
📊 Architecture
Backend Components:
Middleware:
middleware/rate_limit.py(245 lines)Configuration:
config.py(+28 lines)Route Protection: 11 endpoints protected
auth.py: Login (100/hr), Register (50/hr), Refresh (200/hr)rooms.py: Create (10/hr), Stroke submission (300/min), Undo/redo (60/min)clear_canvas.py: Clear (5/min/room)submit_room_line.py: Stroke submission (300/min)Frontend Components:
API Client:
api/apiClient.js(191 lines)Rate Limit Utilities:
utils/rateLimitHandler.js(310 lines)retryWithBackoff(): Smart retry logic with jitterparseRateLimitInfo(): Header parsing (X-RateLimit-*)RateLimitMonitor: Warns at 20% remainingRequestQueue: Queues requests during limitsUI Component:
components/RateLimitWarning.js(132 lines)📡 API Response (HTTP 429)
Error Body:
{ "status": "error", "error": "rate_limit_exceeded", "message": "Rate limit exceeded. Please try again in 45 seconds." }Headers:
🧪 Testing
Comprehensive test suite:
backend/tests/test_rate_limiting.py(334 lines, 16 tests)Test Coverage:
All 16 tests passing ✅
🔒 Security Benefits
📈 Performance Impact
swallow_errors=True)⚙️ Configuration
All limits configurable via environment variables:
Disable for development/testing:
📦 Files Changed
Backend (10 files)
backend/middleware/rate_limit.py(NEW - 245 lines) - Core rate limiting logicbackend/tests/test_rate_limiting.py(NEW - 334 lines) - Comprehensive test suitebackend/config.py(+28 lines) - Rate limit configurationbackend/app.py(+15 lines) - Limiter initialization & error handlersbackend/requirements.txt(+1 line) - Flask-Limiter==3.5.0backend/routes/auth.py(+3 decorators) - Login, register, refresh limitsbackend/routes/rooms.py(+4 decorators) - Room operation limitsbackend/routes/submit_room_line.py(+1 decorator) - Stroke submission limitbackend/routes/undo_redo.py(+2 decorators) - Undo/redo limitsbackend/routes/clear_canvas.py(+1 decorator) - Clear canvas limitFrontend (4 files)
frontend/src/api/apiClient.js(NEW - 191 lines) - Rate limit aware API clientfrontend/src/utils/rateLimitHandler.js(NEW - 310 lines) - Retry logic & utilitiesfrontend/src/components/RateLimitWarning.js(NEW - 132 lines) - User warning componentfrontend/src/components/RateLimitWarning.css(NEW - 114 lines) - Component stylingDocumentation (1 file)
RATE_LIMITING.md(NEW - 368 lines) - Complete guide with examples & troubleshootingTotal: 13 files, 1,600+ lines of code, 16 comprehensive tests
✅ Breaking Changes
NONE - Fully backward compatible:
RATE_LIMIT_ENABLED=False🚀 Deployment
Prerequisites:
Deployment steps:
Rollback: Set
RATE_LIMIT_ENABLED=Falseto disable instantly🔍 Testing Instructions for Reviewers
Run automated tests:
Manual rate limit trigger:
Verify Redis counters:
Test with rate limiting disabled:
📚 Documentation
Complete documentation available in:
This PR protects ResCanvas from abuse while maintaining smooth UX for legitimate users. All tests passing ✅
Ready for immediate production deployment.