Rate limiting has been successfully implemented for donation-related endpoints to prevent abuse and accidental overload while maintaining normal user functionality.
- Added
express-rate-limitpackage for rate limiting functionality
Rate limiting middleware with two configurations:
donationRateLimiter: 10 requests/minute for creation operationsverificationRateLimiter: 30 requests/minute for verification operations
Key features:
- IP-based rate limiting
- Standard RateLimit headers in responses
- HTTP 429 status code for exceeded limits
- Integration with idempotency middleware (cached responses don't count)
- Detailed error messages with retry information
Applied rate limiters to donation endpoints:
POST /donations- UsesdonationRateLimiterPOST /donations/send- UsesdonationRateLimiterPOST /donations/verify- UsesverificationRateLimiter
Read-only endpoints remain unaffected:
GET /donationsGET /donations/:idGET /donations/recentGET /donations/limitsPATCH /donations/:id/status
- Added rate limiting to features list
- Added idempotency to features list
- Updated API endpoints section with rate limit information
- Added rate limiting documentation reference
- Added
express-rate-limitdependency - Added
test:rate-limitscript
Comprehensive documentation covering:
- Implementation details
- Protected vs unaffected endpoints
- Rate limit response format and headers
- Integration with idempotency
- Configuration options
- Testing procedures
- Security considerations
- Monitoring recommendations
- Future enhancement suggestions
Test script to verify rate limiting functionality:
- Tests donation creation endpoint (10 req/min limit)
- Tests verification endpoint (30 req/min limit)
- Validates proper HTTP 429 responses
- Checks rate limit headers
- Run with:
npm run test:rate-limit
- Endpoints:
POST /donations,POST /donations/send - Limit: 10 requests per minute per IP
- Window: 60 seconds
- Rationale: Stricter limit for write operations that interact with blockchain
- Endpoint:
POST /donations/verify - Limit: 30 requests per minute per IP
- Window: 60 seconds
- Rationale: More lenient for read-heavy verification operations
{
"success": true,
"data": { ... }
}Headers include:
RateLimit-Limit: Maximum requests allowedRateLimit-Remaining: Requests remaining in windowRateLimit-Reset: Unix timestamp when limit resets
{
"success": false,
"error": {
"code": "RATE_LIMIT_EXCEEDED",
"message": "Too many donation requests from this IP. Please try again later.",
"retryAfter": "2024-01-15T10:30:00.000Z"
}
}HTTP Status: 429 (Too Many Requests)
Rate limiter is integrated with idempotency:
- Requests served from idempotency cache don't count toward rate limit
- Prevents legitimate retries from being blocked
- Maintains security while improving user experience
Correct execution order ensures security:
- Rate limiter (first check)
- API key validation
- Idempotency check
- Permission check
- Business logic
# Start the server
npm start
# In another terminal, run the test script
npm run test:rate-limit- First 10 donation requests: Success (201)
- 11th+ donation requests: Rate limited (429)
- After 60 seconds: Rate limit resets
- First 30 verification requests: Success/Error (200/500)
- 31st+ verification requests: Rate limited (429)
- Abuse Prevention: Limits malicious actors from overwhelming the system
- Resource Protection: Prevents accidental overload from misconfigured clients
- Fair Usage: Ensures all users get fair access to the API
- Cost Control: Reduces unnecessary blockchain transaction attempts
- DoS Mitigation: Provides basic protection against denial-of-service attacks
The implementation:
- Does not modify existing business logic
- Does not change response formats for successful requests
- Does not affect read-only endpoints
- Maintains backward compatibility
- Only adds rate limiting protection layer
The following remain unchanged:
- Wallet routes (
/wallets/*) - Stats routes (
/stats/*) - Stream routes (
/stream/*) - Transaction routes (
/transaction/*) - Database schema
- Service layer logic
- Stellar integration
- Authentication/authorization
- Error handling (except new 429 responses)
- User-based rate limiting: In addition to IP-based
- Dynamic limits: Based on user tier or reputation
- Redis backend: For distributed rate limiting across multiple servers
- Environment configuration: Make limits configurable via .env
- Monitoring dashboard: Track rate limit metrics
- Alerting: Notify admins of sustained violations
{
"express-rate-limit": "^7.4.1"
}