middleware

package
v1.13.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 25, 2026 License: Apache-2.0 Imports: 18 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CORS added in v1.13.0

func CORS(next http.Handler) http.Handler

CORS is fiber's cors middleware for the one configuration this service used: AllowOrigins, AllowHeaders and AllowMethods "*", MaxAge 86400, no credentials. Browser panels preflight their uploads and dos copies CDN images onto a canvas, so every header here is load-bearing.

func ClientIP added in v1.7.0

func ClientIP(r *http.Request) string

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 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

func DefaultAdvancedRateLimiter() func(http.Handler) http.Handler

DefaultAdvancedRateLimiter is the global limiter: RATE_LIMIT per minute. RATE_LIMIT_DURATION is not read here and never was.

func Favicon added in v1.13.0

func Favicon(file string) func(http.Handler) http.Handler

Favicon is fiber's favicon middleware: /favicon.ico only, matched on the raw path and therefore case-sensitive. The file is read at boot; a missing one stops boot, as it did under fiber.

func NewAdvancedRateLimiter

func NewAdvancedRateLimiter(max int, duration time.Duration) func(http.Handler) http.Handler

NewAdvancedRateLimiter limits by RateLimitKey with Redis storage. If Redis is unreachable at boot it retries briefly and then fails open instead of taking the service down; Cloudflare and nginx still throttle coarsely meanwhile.

func NoSniff added in v1.13.0

func NoSniff(next http.Handler) http.Handler

NoSniff stops user-uploaded content from being reinterpreted as HTML or script in the CDN origin's context.

func PanicLogger added in v1.13.0

func PanicLogger(next http.Handler) http.Handler

PanicLogger records a panic with its stack and re-panics for Recoverer.

func RateLimitKey

func RateLimitKey(r *http.Request) string

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:

  1. 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.
  2. 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 Recoverer added in v1.13.0

func Recoverer(next http.Handler) http.Handler

Recoverer answers a panic the way fiber's recover middleware and default error handler did: 500, text/plain, the panic value as the body. It has to be mounted OUTSIDE PanicLogger (registered first), per rules/security.md section 16.

func Transport added in v1.13.0

func Transport(maxBody int64, next http.Handler) http.Handler

Transport reproduces what fasthttp and fiber decided before the middleware chain: an unknown method is 400, a declared body over the limit is 413 with the connection closed. A body without a declared length is capped by MaxBytesReader instead.

Types

type RedisStorage

type RedisStorage struct {
	// contains filtered or unexported fields
}

RedisStorage is the rate limiter's store: keys sanitized, values opaque.

func NewRedisStorage

func NewRedisStorage() (*RedisStorage, error)

NewRedisStorage creates a new Redis storage adapter

func (*RedisStorage) Close

func (r *RedisStorage) Close() error

Close closes the Redis connection

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.

A key which does not exist returns (nil, nil), not an error. The limiter's first request from any client IP is a miss by definition, and reporting it as an error used to write a log line for every rate-limited request.

func (*RedisStorage) Reset

func (r *RedisStorage) Reset() error

Reset clears all values from Redis

func (*RedisStorage) Set

func (r *RedisStorage) Set(key string, val []byte, exp time.Duration) error

Set stores a value in Redis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL