Rate Limiting
Rate limiting is a crucial security feature that protects your application from brute force attacks, denial of service attacks, and abuse. Svelte Guardian provides flexible rate limiting options to secure your authentication endpoints.
Overview
Rate limiting works by tracking the number of requests from a specific source (IP address or user ID) within a time window. When the number of requests exceeds the defined limit, further requests are blocked for a specified duration.
Configuration
Rate limiting is configured in the security.rateLimiting
section of your Svelte Guardian configuration:
Rate Limiting Strategies
Svelte Guardian supports three rate limiting strategies:
In-Memory Rate Limiting
The simplest strategy, storing rate limit data in the application’s memory.
Pros:
- Easy to set up, no external dependencies
- Works out of the box
Cons:
- Not suitable for multi-server deployments
- Rate limit data is lost on server restart
- Memory usage increases with more clients
Redis Rate Limiting
Uses Redis to store rate limit data, suitable for production deployments.
Pros:
- Suitable for multi-server deployments
- Persists rate limit data across server restarts
- Efficient memory usage
Cons:
- Requires a Redis server
- Additional infrastructure to maintain
Upstash Redis Rate Limiting
Uses Upstash Redis, a serverless Redis service, ideal for serverless deployments.
Pros:
- Works well with serverless deployments
- No infrastructure to maintain
- Persists rate limit data across server restarts
Cons:
- External service dependency
- May have higher latency depending on region
Rate Limiting Options
Basic Options
- enabled: Boolean to enable or disable rate limiting
- strategy:
'memory'
,'redis'
, or'upstash-redis'
- requestsPerMinute: Shorthand for setting up per-minute rate limiting
Advanced Options
- maxRequests: Maximum number of requests allowed in the time window
- windowMs: Time window in milliseconds
- blockDuration: Duration to block requests after exceeding the limit
- redisConfig: Configuration for Redis-based strategies
Redis Configuration
For Redis-based strategies, you can configure:
Custom Key Generation
By default, rate limiting uses the user ID (if authenticated) or IP address as the key. You can customize this behavior:
Rate Limiting Headers
When rate limiting is enabled, Svelte Guardian automatically adds these headers to responses:
X-RateLimit-Limit
: Maximum number of requests allowed in the time windowX-RateLimit-Remaining
: Number of requests remaining in the current time windowX-RateLimit-Reset
: Timestamp when the rate limit window resets (in milliseconds)
When a request is blocked, the response will have:
- Status code
429 Too Many Requests
Retry-After
header indicating seconds to wait before retrying
Route-Specific Rate Limiting
While Svelte Guardian applies rate limiting to all authentication endpoints by default, you may want different limits for specific routes:
Handling Rate Limit Errors
When a client exceeds the rate limit, they receive a 429 response with a JSON payload:
Client-side example of handling rate limit errors:
Monitoring and Debugging
Rate limiting events are logged by Svelte Guardian’s logger:
Example log message when a request is blocked:
Best Practices
Start Conservative: Begin with lower limits and adjust based on legitimate usage patterns.
Use Redis in Production: The in-memory strategy is suitable for development but use Redis in production, especially with multiple servers.
Apply Stricter Limits to Sensitive Routes: Use stricter limits for authentication-related endpoints.
Monitor Rate Limiting Events: Regularly review logs to identify potential attacks and adjust limits if necessary.
Consider User Experience: Implement user-friendly error messages and UIs that explain rate limiting to legitimate users.
Environment-Specific Configuration: Use different rate limiting configurations for development, testing, and production environments.