middleware

package
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: May 17, 2026 License: AGPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package middleware provides HTTP middleware, including per-IP rate limiting.

Package middleware provides HTTP middleware, including per-IP rate limiting.

Package middleware provides HTTP middleware, including per-IP rate limiting.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CORS

func CORS(config CORSConfig) func(http.Handler) http.Handler

CORS middleware handles Cross-Origin Resource Sharing

func CSRFProtection added in v0.2.0

func CSRFProtection(disabled bool) func(http.Handler) http.Handler

CSRFProtection implements the double-submit cookie pattern for CSRF protection. It sets a non-HttpOnly cookie with a random token, and requires state-changing requests to include that token in the X-CSRF-Token header. Safe methods (GET, HEAD, OPTIONS) are exempt. Requests with non-browser content types or API auth headers are exempt. When disabled is true, all requests pass through without CSRF validation.

func IsOriginAllowed

func IsOriginAllowed(origin string, allowedOrigins []string) bool

IsOriginAllowed checks if an origin is in the allowed list

func MaxStorageCheck added in v0.2.0

func MaxStorageCheck(sharedDir string, maxBytes int64) func(http.Handler) http.Handler

MaxStorageCheck rejects write requests when usage exceeds maxBytes. If maxBytes is 0, the check is disabled (unlimited storage).

func ParseTrustedProxies added in v0.2.0

func ParseTrustedProxies(raw string) []*net.IPNet

ParseTrustedProxies parses a comma-separated list of IPs/CIDRs into net.IPNet entries.

func SecurityHeaders

func SecurityHeaders(enableHSTS bool, disableCSP bool) func(http.Handler) http.Handler

SecurityHeaders adds security-related HTTP headers to all responses

Types

type CORSConfig

type CORSConfig struct {
	// AllowedOrigins is a list of origins allowed for CORS
	// If empty, CORS is disabled (most secure for local file sharing)
	AllowedOrigins []string
	// AllowCredentials indicates whether credentials are allowed
	AllowCredentials bool
}

CORSConfig holds CORS configuration

func DefaultCORSConfig

func DefaultCORSConfig() CORSConfig

DefaultCORSConfig returns a secure default CORS configuration By default, CORS is disabled for security

type RateLimiter

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

RateLimiter enforces per-IP, per-tier rate limits.

func NewRateLimiter

func NewRateLimiter(cfg RateLimiterConfig) *RateLimiter

NewRateLimiter creates a RateLimiter and starts a background goroutine that evicts stale entries every 5 minutes.

func (*RateLimiter) Close

func (rl *RateLimiter) Close()

Close stops the background cleanup goroutine.

func (*RateLimiter) Middleware

func (rl *RateLimiter) Middleware(next http.Handler) http.Handler

Middleware returns an http.Handler middleware that enforces rate limits.

type RateLimiterConfig

type RateLimiterConfig struct {
	// General rate: requests per minute for normal endpoints.
	GeneralRate int
	// AuthRate: requests per minute for auth endpoints (e.g. /auth/login).
	AuthRate int
	// UploadRate: requests per minute for upload endpoints.
	UploadRate int
	// Enabled can be set to false to skip rate limiting entirely.
	Enabled bool
	// TrustedProxies is a list of trusted proxy IPs/CIDRs.
	// Only trust X-Forwarded-For/X-Real-IP from these sources.
	TrustedProxies []*net.IPNet
}

RateLimiterConfig holds the rates for different endpoint tiers.

func DefaultRateLimiterConfig

func DefaultRateLimiterConfig() RateLimiterConfig

DefaultRateLimiterConfig returns sensible defaults.

Jump to

Keyboard shortcuts

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