Documentation
¶
Index ¶
- Variables
- func ClientIP(c *fiber.Ctx) string
- func DefaultAdvancedRateLimiter() fiber.Handler
- func DefaultRateLimiter() fiber.Handler
- func DeleteLimiter(cfg RateLimitConfig) fiber.Handler
- func NewAdvancedRateLimiter(max int, duration time.Duration) fiber.Handler
- func NewRateLimiter(max int, duration time.Duration) fiber.Handler
- func RateLimitKey(c *fiber.Ctx) string
- func SecurityMiddleware(cfg SecurityConfig) []fiber.Handler
- func UploadLimiter(cfg RateLimitConfig) fiber.Handler
- type CORSConfig
- type RateLimitConfig
- type RedisStorage
- type SecurityConfig
Constants ¶
This section is empty.
Variables ¶
var RateLimiterConfig = limiter.Config{ Max: config.GetEnvAsIntOrDefault("RATE_LIMIT", 100), Expiration: time.Duration(config.GetEnvAsIntOrDefault("RATE_LIMIT_DURATION", 1)) * time.Minute, KeyGenerator: func(c *fiber.Ctx) string { return ClientIP(c) }, LimitReached: func(c *fiber.Ctx) error { return c.Status(fiber.StatusTooManyRequests).JSON(fiber.Map{ "success": false, "message": "Rate limit exceeded. Please try again later.", }) }, SkipFailedRequests: false, SkipSuccessfulRequests: false, }
RateLimiterConfig default rate limiter configuration
Functions ¶
func ClientIP ¶ added in v1.7.0
ClientIP returns the real originating client IP for rate limiting.
Cloudflare sits in front of this service and sets CF-Connecting-IP to the originating client; a browser cannot forge it through Cloudflare. We do NOT trust X-Real-IP (the docker nginx hop overwrites it with the upstream hop's address) nor the leftmost X-Forwarded-For entry (attacker-controllable — Cloudflare appends the real client after any client-supplied value). The TCP peer (c.IP()) is only a degenerate fallback for non-Cloudflare/internal access; without this helper every request collapses onto the proxy IP and shares one rate-limit bucket.
Trust note: CF-Connecting-IP is only authoritative while the origin is reachable ONLY via Cloudflare. Lock the origin firewall to Cloudflare IP ranges so an attacker cannot hit the origin directly and spoof this header.
func DefaultAdvancedRateLimiter ¶
DefaultAdvancedRateLimiter returns a default rate limiter middleware (100 requests per minute)
func DefaultRateLimiter ¶
DefaultRateLimiter creates a rate limiter with default configuration
func DeleteLimiter ¶
func DeleteLimiter(cfg RateLimitConfig) fiber.Handler
DeleteLimiter returns specific rate limiter for delete endpoints
func NewAdvancedRateLimiter ¶
NewAdvancedRateLimiter creates a new rate limiter middleware with Redis storage. If Redis is unreachable at boot it retries briefly and then fails open (serves without rate limiting) instead of crashing the process, so a transient Redis outage cannot take down the whole service. Cloudflare/nginx still provide a coarse protection layer in that window.
func NewRateLimiter ¶
NewRateLimiter creates a new rate limiter middleware with custom config
func RateLimitKey ¶
RateLimitKey derives the rate-limit bucket for a request from the real client IP and the *verified* identity of its credential.
Two properties matter here, and the previous "<ip>:<raw bearer>" key had neither:
- No secret in the keyspace. The key is built from the identity a credential resolves to (the general token, or a bucket name), never from the credential itself, so token material never reaches Redis.
- An unverified credential cannot mint a fresh counter. Because the old key trusted the raw header, a client sending a different random token on every request got a brand-new counter each time and was effectively never rate limited. Anything that does not authenticate now shares the plain per-IP bucket, which is the limit it was always meant to be under.
Bucket names are validated at load time to lowercase letters, digits and '-', so a bucket name can never inject a separator into the key.
The rate limiter is mounted before the auth middleware, so resolving here is deliberately read-only: rejecting a bad credential stays the auth middleware's job, this only decides which counter the request belongs to.
func SecurityMiddleware ¶
func SecurityMiddleware(cfg SecurityConfig) []fiber.Handler
SecurityMiddleware returns security middleware chain
func UploadLimiter ¶
func UploadLimiter(cfg RateLimitConfig) fiber.Handler
UploadLimiter returns specific rate limiter for upload endpoints
Types ¶
type CORSConfig ¶
type RateLimitConfig ¶
type RedisStorage ¶
type RedisStorage struct {
// contains filtered or unexported fields
}
RedisStorage implements fiber.Storage interface for Redis
func NewRedisStorage ¶
func NewRedisStorage() (*RedisStorage, error)
NewRedisStorage creates a new Redis storage adapter
func (*RedisStorage) Delete ¶
func (r *RedisStorage) Delete(key string) error
Delete removes a value from Redis
func (*RedisStorage) Get ¶
func (r *RedisStorage) Get(key string) ([]byte, error)
Get retrieves a value from Redis.
fiber's Storage contract is that a key which does not exist returns (nil, nil), not an error. That distinction matters here because this adapter backs the rate limiter, where the first request from any client IP is a miss by definition: passing the miss up as an error both violated the contract and meant every rate-limited request produced a log line.
func (*RedisStorage) Reset ¶
func (r *RedisStorage) Reset() error
Reset clears all values from Redis
type SecurityConfig ¶
type SecurityConfig struct {
Token string
RateLimit RateLimitConfig
CORS CORSConfig
TrustedProxies []string
MaxRequestBodySize int
RequestTimeout time.Duration
}
SecurityConfig represents security middleware configuration
func DefaultSecurityConfig ¶
func DefaultSecurityConfig() SecurityConfig
DefaultSecurityConfig returns default security configuration